示例#1
0
def extract_bin_ops(rules, current_expr_rule, ops_to_extrat, new_rule_name,
                    handle_conditional_fn, handle_inside_fn):
    # find option with binary op rule
    # expr = rule_by_name(rules, "expression")
    ops_no_special = [
        o for o in ops_to_extrat if o not in [
            "KW_INSIDE",
            "KW_DIST",
            "QUESTIONMARK",
        ]
    ]

    bin_op_choices = []
    if len(ops_no_special) > 0:
        if len(ops_no_special) == 1:
            op = Antlr4Symbol(ops_no_special[0], False)
        else:
            op = Antlr4Selection(
                [Antlr4Symbol(o, False) for o in ops_no_special])

        # expression (binary_operator ( attribute_instance )* expression)*
        bin_op_choice = Antlr4Sequence([
            op,
            Antlr4Iteration(Antlr4Symbol("attribute_instance", False)),
            Antlr4Symbol(current_expr_rule.name, False)
        ])
        bin_op_choices.append(bin_op_choice)

    if "KW_INSIDE" in ops_to_extrat:
        handle_inside_fn(bin_op_choices, current_expr_rule)

    if "KW_DIST" in ops_to_extrat:
        # handled differently, only allowed on specified places
        pass

    if "QUESTIONMARK" in ops_to_extrat:
        handle_conditional_fn(bin_op_choices, current_expr_rule)

    for c in bin_op_choices:
        assert isinstance(c, iAntlr4GramElem), c
    # create a new rule which contains rule for extracted binary operators
    if len(bin_op_choices) > 1:
        new_body = Antlr4Selection(bin_op_choices)
    else:
        new_body = bin_op_choices[0]
    new_body = Antlr4Sequence([
        Antlr4Symbol(current_expr_rule.name, False),
        Antlr4Iteration(new_body)
    ])
    new_r = Antlr4Rule(new_rule_name, new_body)
    rules.insert(rules.index(current_expr_rule), new_r)

    return new_r
示例#2
0
 def handle_conditional_fn(bin_op_choices, current_expr_rule):
     bin_op_choices.extend([
         Antlr4Symbol(current_expr_rule.name, False),
         Antlr4Iteration(
             Antlr4Sequence([
                 Antlr4Symbol("QUESTIONMARK", False),
                 Antlr4Iteration(Antlr4Symbol("attribute_instance", False)),
                 Antlr4Symbol("constant_expression", False),
                 Antlr4Symbol("COLON", False),
                 Antlr4Symbol("constant_expression", False),
             ]))
     ])
 def _group(self):
     item = self._selection()
     group_type = self.next()
     if group_type == ")":
         return item
     elif group_type == ")?":
         return Antlr4Option(item)
     elif group_type == ")*":
         return Antlr4Iteration(item, positive=False)
     elif group_type == ")+":
         return Antlr4Iteration(item, positive=True)
     else:
         raise ValueError(group_type)
示例#4
0
def fix_implicit_data_type(rules):
    r = rule_by_name(rules, "implicit_data_type")
    # : (signing)? (packed_dimension)*
    # ->
    # : signing (packed_dimension)*
    # | (packed_dimension)+
    # ;
    r.body = Antlr4Selection([
        Antlr4Sequence([
            Antlr4Symbol("signing", False),
            Antlr4Iteration(Antlr4Symbol("packed_dimension", False))
        ]),
        Antlr4Iteration(Antlr4Symbol("packed_dimension", False), positive=True)
    ])
示例#5
0
 def parse_element_iteration(
         self, ctx: Element_iterationContext) -> Antlr4Iteration:
     """
     element_iteration: '{' element '}';
     """
     body = self.parse_element(ctx.element())
     return Antlr4Iteration(body)
示例#6
0
def _iterate_everything_except_first_and_replace_first(seq, repl):
    rest = list(iter_non_visuals(seq))[1:]
    if len(rest) == 1:
        rest = rest[0]
    else:
        rest = Antlr4Sequence(rest)
    rest_iterated = Antlr4Iteration(rest)
    seq.clear()
    seq.append(repl)
    seq.append(rest_iterated)
    seq.append(Antlr4Newline())
    seq.append(Antlr4Indent(1))
示例#7
0
 def handle_inside_fn(bin_op_choices, current_expr_rule):
     bin_op_choices[-1].extend([Antlr4Newline(), Antlr4Indent(1)])
     # expression (KW_INSIDE LBRACE open_range_list RBRACE)*;
     bin_op_choice = Antlr4Sequence([
         Antlr4Symbol(current_expr_rule.name, False),
         Antlr4Iteration(
             Antlr4Sequence([
                 Antlr4Symbol("KW_INSIDE", False),
                 Antlr4Symbol("LBRACE", False),
                 Antlr4Symbol("open_range_list", False),
                 Antlr4Symbol("RBRACE", False),
             ]))
     ])
     bin_op_choices.append(bin_op_choice)
示例#8
0
def add_file_path_literal_rules(p):
    FILE_PATH_SPEC_CHAR = Antlr4Rule(
        "FILE_PATH_SPEC_CHAR",
        Antlr4Symbol("[^ !$`&()+] | ( '\\\\' [ !$`&*()+] )", True, True),
        is_fragment=True)
    p.rules.append(FILE_PATH_SPEC_CHAR)

    file_spec_path = Antlr4Rule(
        "FILE_PATH_SPEC",
        Antlr4Iteration(Antlr4Sequence([
            Antlr4Symbol("FILE_PATH_SPEC_CHAR", False),
            Antlr4Option(
                Antlr4Sequence([
                    Antlr4Symbol('SEMI', False),
                    Antlr4Symbol("FILE_PATH_SPEC_CHAR", False),
                ])),
        ]),
                        positive=True))
    p.rules.append(file_spec_path)
示例#9
0
def simplify_select_rule(rules, rule_name):
    """
    ( ( KW0 a0 ( a1 )* )* KW0 a0 )? ( a1 )* ...
    ->
    ( KW0 a0 | a1 )* ...
    """
    r = rule_by_name(rules, rule_name)
    g0 = r.body[0]
    g1 = r.body[1]
    first_part = Antlr4Iteration(Antlr4Selection([Antlr4Sequence(g0.body[-2:]), g1.body]), positive=False)
    if len(r.body) > 2:
        if len(r.body) > 3:
            rest = r.body[2:]
        else:
            rest = [r.body[2], ]

        new_body = Antlr4Sequence([
            first_part,
            *rest
        ])
    else:
        new_body = first_part

    r.body = new_body
示例#10
0
 def match_replace_fn(o):
     if o == r_symb:
         return Antlr4Iteration(o, positive=False)