def test_common_prefix_empty(self): r_str = "a | a b" expec = "a ( | b )" r = Antlr4parser().from_str(r_str) _selection_options_to_sequnces(r) res, _ = _selection_share_prefix(r) self.assertTextEq(expec, res.toAntlr4())
def test_empty_option_to_optional_selection(self): r_str = "a | | c" expec = "( a | c )?" r = Antlr4parser().from_str(r_str) _selection_options_to_sequnces(r) res, _ = _selection_empty_option_to_optional(r) self.assertTextEq(expec, res.toAntlr4())
def test_selection_propagate_optionality3(self): r_str = "b | ( a )? ( a1 )* ( a2 )* | ( c )?" expec = "( b | a ( a1 )* ( a2 )* | ( a1 )+ ( a2 )* | ( a2 )+ | c)?" r = Antlr4parser().from_str(r_str) _selection_options_to_sequnces(r) res, _ = _selection_propagate_optionality(r) self.assertTextEq(expec, res.toAntlr4())
def test_selection_propagate_optionality1(self): r_str = "( a )? | b | ( c )?" expec = "( a | b | c)?" r = Antlr4parser().from_str(r_str) _selection_options_to_sequnces(r) res, _ = _selection_propagate_optionality(r) self.assertTextEq(expec, res.toAntlr4())
def test_common_suffix(self): r_str = """ x0 a b c | d | x1 ( x2 )? a b c """ expected = """ ( x0 a b | x1 ( x2 )? a b ) c | d """ r = Antlr4parser().from_str(r_str) _selection_options_to_sequnces(r) res, _ = _selection_share_suffix(r) self.assertTextEq(expected, res.toAntlr4())
def match_replace_fn(self, o): if o.optimalizer_keep_out: return None if isinstance(o, Antlr4Selection): _selection_options_to_sequnces(o) optimizations = [ _selection_only_unique, _selection_reduce_optional, _selection_empty_option_to_optional, _selection_share_prefix, _selection_share_suffix, _selection_propagate_optionality, _selection_flatten, ] for opt in optimizations: if o.optimalizer_keep_out: return None o, changed = opt(o) self.modified |= changed if changed or not isinstance(o, Antlr4Selection): return o return None elif isinstance(o, Antlr4Option): o, changed = _option_reduce_option(o) self.modified |= changed if changed: return o else: return None elif isinstance(o, Antlr4Sequence): optimizations = [ _sequence_flatten, _sequence_extract_positive_iterations, ] for opt in optimizations: if o.optimalizer_keep_out: return None o, changed = opt(o) self.modified |= changed if changed or not isinstance(o, Antlr4Sequence): return o return None
def test_extra_option(self): rules = rules_from_str(""" a: b c | c1; d: b c; g: b0 c0; h: a d b c; """) rule_map = { "a": "d", "b": "b", "c": "c", "c1": "c1", } _selection_options_to_sequnces(rules[0].body) extract_common_from_rule_clusters(rules, rule_map, "new_") self.assertEqual(4, len(rules)) self.assertTextEq(rules[0], "new_a: b c;") self.assertTextEq(rules[1], "a: new_a | c1;") self.assertTextEq(rules[2], "g: b0 c0;") self.assertTextEq(rules[3], "h: a new_a b c;")
def test_common_prefix(self): r_str = """ a ( b c d )? | a b e | a b f """ expected0 = """ a ( ( b c d )? | b e | b f ) """ expected1 = """ ( b c d )? | b ( e | f ) """ r = Antlr4parser().from_str(r_str) _selection_options_to_sequnces(r) res, _ = _selection_share_prefix(r) self.assertTextEq(expected0, res.toAntlr4()) res, _ = _selection_share_prefix(res[1]) self.assertTextEq(expected1, res.toAntlr4())
def test_optimize0(self): # data_type rule r_str = """ a ( b )? ( c )* | d ( b )? | e | f ( kw0 ( b )? )? kw1 f0 ( f0 )* kw2 ( c )* | kw3 ( a0 )? kw1 a1 ( kw4 a1 )* kw2 ( c )* | kw5 | kw6 | kw7 ( kw8 )? a2 ( a3 )? ( kw8 a2 )? | ( a4 | a5 )? a2 ( c )* | a6 | kw9 | a7 | a8 """ exp0 = """ ( a ( b )? | f ( kw0 ( b )? )? kw1 f0 ( f0 )* kw2 | kw3 ( a0 )? kw1 a1 ( kw4 a1 )* kw2 | ( a4 | a5 )? a2 ) ( c )* | d ( b )? | e | kw5 | kw6 | kw7 ( kw8 )? a2 ( a3 )? ( kw8 a2 )? | a6 | kw9 | a7 | a8 """ r = Antlr4parser().from_str(r_str) _selection_options_to_sequnces(r) res, _ = _selection_share_suffix(r) self.assertTextEq(exp0, res.toAntlr4()) exp1 = """ a ( b )? | ( f ( kw0 ( b )? )? kw1 f0 ( f0 )* | kw3 ( a0 )? kw1 a1 ( kw4 a1 )* ) kw2 | ( a4 | a5 )? a2 """ _selection_options_to_sequnces(r[0][0]) res, _ = _selection_share_suffix(r[0][0]) self.assertTextEq(exp1, res.toAntlr4()) exp2 = """ ( a ( b )? | ( f ( kw0 ( b )? )? kw1 f0 ( f0 )* | kw3 ( a0 )? kw1 a1 ( kw4 a1 )* ) kw2 | ( a4 | a5 )? a2 ) ( c )* | d ( b )? | e | kw5 | kw6 | kw7 ( kw8 )? a2 ( a3 )? ( kw8 a2 )? | a6 | kw9 | a7 | a8 """ r = Antlr4parser().from_str(r_str) Antlr4GenericOptimizer().optimize([ Antlr4Rule("tmp", r), ]) self.assertTextEq(exp2, r.toAntlr4())