def test_embedded_transformer(self): class T(Transformer): def a(self, children): return "<a>" def b(self, children): return "<b>" def c(self, children): return "<c>" # Test regular g = Lark("""start: a a : "x" """, parser='lalr') r = T().transform(g.parse("x")) self.assertEqual(r.children, ["<a>"]) g = Lark("""start: a a : "x" """, parser='lalr', transformer=T()) r = g.parse("x") self.assertEqual(r.children, ["<a>"]) # Test Expand1 g = Lark("""start: a ?a : b b : "x" """, parser='lalr') r = T().transform(g.parse("x")) self.assertEqual(r.children, ["<b>"]) g = Lark("""start: a ?a : b b : "x" """, parser='lalr', transformer=T()) r = g.parse("x") self.assertEqual(r.children, ["<b>"]) # Test Expand1 -> Alias g = Lark("""start: a ?a : b b -> c b : "x" """, parser='lalr') r = T().transform(g.parse("xx")) self.assertEqual(r.children, ["<c>"]) g = Lark("""start: a ?a : b b -> c b : "x" """, parser='lalr', transformer=T()) r = g.parse("xx") self.assertEqual(r.children, ["<c>"])
_NL: /(\r?\n[\t ]*)+/ _INDENT: "<INDENT>" _DEDENT: "<DEDENT>" """ class TreeIndenter(Indenter): NL_type = '_NL' OPEN_PAREN_types = [] CLOSE_PAREN_types = [] INDENT_type = '_INDENT' DEDENT_type = '_DEDENT' tab_len = 8 parser = Lark(tree_grammar, parser='lalr', postlex=TreeIndenter()) test_tree = """ a b c d e f g """ def test(): print(parser.parse(test_tree).pretty())