示例#1
0
    def removeRedundantParenExpr():
        tbl = _pte.ExprDict()
        someAssignOpExpr = comp(
            "(op_assign | op_add_assign | op_sub_assign | op_mul_assign | op_div_assign | op_mod_assign | op_and_assign | op_xor_assign | op_or_assign | op_lshift_assign | op_rshift_assign | op_signed_rshift_assign);"
        )
        tbl["someAssignOpExpr"] = someAssignOpExpr

        eRemoveParenExpr = comp("""
        (<>param :: (null <- LP), req(param, RP), @0, (null <- RP)) 
        | (<>param :: (null <- LP), *(req^(RP), @er), (null <- RP));
        """)
        tbl["eRemoveParenExpr"] = eRemoveParenExpr

        er = comp("""
        (r_return | @someAssignOpExpr), req(param, semicolon), @eRemoveParenExpr , semicolon, ?(null <- +semicolon)
        | semicolon, ?(null <- +semicolon)
        | (param :: LP, req(param, RP), @eRemoveParenExpr, RP) | (param :: ~@0) 
        | (index :: LP, req(param, RP), @eRemoveParenExpr, RP) | (index :: ~@0)
        | (block :: ~@0)
        | any;
        """,
                  replaces=tbl)
        tbl["er"] = er

        return [
            0,
        ] * er
 def removeRedundantParenExpr():
     tbl = _pte.ExprDict()
     someAssignOperatorExpr = tbl['someAssignOperatorExpr'] = comp(
         "(op_lshift_assign | op_rshift_assign | op_power_assign  | op_intdev_assign | op_lshift | op_rshift | op_le | op_ge | op_eq | op_ne | op_ne | op_add_assign | op_sub_assign | op_mul_assign | op_div_assign | op_mod_assign | op_and_assign | op_xor_assign | op_or_assign | op_assign);"
     )
     singleValueParamExpr = tbl['singleValueParamExpr'] = comp("""
     req^((param :: LP, RP)), (param :: LP, *any^(comma));
     """)
     mutipleValueParamExpr = tbl['mutipleValueParamExpr'] = comp("""
     (param :: LP, *any^(comma), comma, *any);
     """)
     removeParenExpr = tbl['removeParenExpr'] = comp("""
     (<>param :: (null <- LP), req(@singleValueParamExpr, RP), @0, (null <- RP)) 
     | (<>param :: (null <- LP), *(req^(RP), @er), (null <- RP));
     """,
                                                     replaces=tbl)
     er = tbl['er'] = comp("""
     (r_return | r_yield | @someAssignOperatorExpr), req(@singleValueParamExpr, newline), @removeParenExpr, newline
     | (block :: LB, (null <- +(r_pass, newline)), LK)
     | (block :: ~@0)
     | req(@mutipleValueParamExpr), (param :: LP, *(req(@singleValueParamExpr), @removeParenExpr | req^(RP), @0), RP)
     | (param :: LP, req(@singleValueParamExpr, RP), @removeParenExpr, RP) 
     | (param :: ~@0)
     | (r_pass, newline), ?(null <- +(r_pass, newline))
     | any;
     """,
                           replaces=tbl)
     return [
         0,
     ] * er
 def identifyImplicitTupleExpr():
     tbl = _pte.ExprDict()
     someAssignOperatorExpr = tbl['someAssignOperatorExpr'] = comp(
         "(op_lshift_assign | op_rshift_assign | op_power_assign  | op_intdev_assign | op_lshift | op_rshift | op_le | op_ge | op_eq | op_ne | op_ne | op_add_assign | op_sub_assign | op_mul_assign | op_div_assign | op_mod_assign | op_and_assign | op_xor_assign | op_or_assign | op_assign);"
     )
     indexContentWoCommaExpr = tbl['indexContentWoCommaExpr'] = comp("""
     (param <- (LP<-), (req^(comma | RK | index_colon), @er), comma, *(req^(RK | index_colon), @er), (RP<-));
     """)
     er = tbl['er'] = comp("""
     (r_return | r_yield | @someAssignOperatorExpr), 
        (param <- (LP<-), (req^(comma | newline), @0), comma, *(req^(newline), @0), (RP<-), req(newline))
     | (index :: LK, 
         ?@indexContentWoCommaExpr, *(index_colon, @indexContentWoCommaExpr),
         RK)
     | (id, *(dot, id), (param :: LP, *(comma | ?(id, op_assign), *(req^(comma | RP), @0))))
     | (block :: ~@0)
     | ((r_except | r_with), *any^(colon), colon)
     | ((r_from | r_import), *any^(newline), newline)
     | (param <- (LP<-), backquote, +(req^(backquote | newline), @0), backquote, (RP<-))
     | any
     ;
     """,
                           replaces=tbl)
     return [
         0,
     ] * er
示例#4
0
        def braceInsertionExpr():
            tbl = _pte.ExprDict()

            someCompoundStmtKeywordExpr = comp(
                "(r_if | r_while | r_for | r_do | r_try | r_catch | r_switch);"
            )
            tbl['someCompoundStmtKeywordExpr'] = someCompoundStmtKeywordExpr

            shouldBeBlockExpr = comp(
                "((block :: ~@er) | (block <- (LB<-), @er, (RB<-)));")
            tbl['shouldBeBlockExpr'] = shouldBeBlockExpr

            er = comp("""
            r_if, param, @shouldBeBlockExpr, *(r_else, r_if, param, @shouldBeBlockExpr), ?(r_else, @shouldBeBlockExpr) 
            | r_else, @shouldBeBlockExpr
            | (r_for | r_while), param, @shouldBeBlockExpr
            | r_do, @shouldBeBlockExpr, r_while, param, semicolon
            | r_try, (block :: ~@0), *(r_catch, param, (block :: ~@0))
            | r_catch, (block :: ~@0)
            | +((r_case, (id | l_bool | l_char | l_int) | r_default), colon), 
                ((block :: ~@0), ?(null <- r_break, semicolon) | (block <- ((LB<-), *(req^(r_break | r_case | r_default), @0), (RB<-))), ?(null <- r_break, semicolon)) # enclose each case clause by block
            | r_switch, (block :: ~@0) 
            | (r_return | r_break | r_continue | op_assign), *any^(block | LB | semicolon), semicolon
            | *any^(block | LB | semicolon | @someCompoundStmtKeywordExpr), semicolon
            | (block :: ~@0) # recurse into block
            ;""",
                      replaces=tbl)
            tbl['er'] = er

            return [
                0,
            ] * (er | _pte.Any())
 def identifyParamIndexDictLamda():
     tbl = _pte.ExprDict()
     argumentExpr = tbl["argumentExpr"] = comp("""
     ?((param_power <- <>op_power) | (param_star <- <>op_star)), 
     (id <- <>word), 
     ?((param_assign <- <>op_assign), +(req^(comma | RP | colon), @er))
     ;""")
     er = tbl["er"] = comp("""
     (id <- <>word)
     | (l_string <- <>l_string, +(<>op_plus, <>l_string))
     | (param <- LP, @argumentExpr, *(comma, @argumentExpr), RP) # param or tuple expr
     | (param <- LP, *(req^(RP), @0), RP) # paren in expression, param or tuple expr
     | (index <- LK, *(req^(RK | colon), @0 | (index_colon <- <>colon)), RK) # getitem, or list expr
     | (dict <- LB, *(req^(RB | colon), @0 | (dict_colon <- <>colon)), RB)
     | r_lambda, (param <- (LP<-), ?(@argumentExpr, *(comma, @argumentExpr)), (RP<-)), (lamda_colon <- <>colon)
     | r_for, (param <- (LP<-), (id <- <>word), *(comma, (id <- <>word)), (RP<-)), r_in
     | (block :: ~@0)
     | any
     ;
     """,
                           replaces=tbl)
     return [
         0,
     ] * er