|
@@ -55,6 +55,18 @@ class CustomLexer(Lexer): |
|
|
yield Token('A', obj) |
|
|
yield Token('A', obj) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestT(Transformer): |
|
|
|
|
|
def add(self, children): |
|
|
|
|
|
return sum(children if isinstance(children, list) else children.children) |
|
|
|
|
|
|
|
|
|
|
|
def NUM(self, token): |
|
|
|
|
|
return int(token) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def append_zero(t): |
|
|
|
|
|
return t.update(value=t.value + '0') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestCache(TestCase): |
|
|
class TestCache(TestCase): |
|
|
def setUp(self): |
|
|
def setUp(self): |
|
|
pass |
|
|
pass |
|
@@ -80,7 +92,7 @@ class TestCache(TestCase): |
|
|
parser = Lark(g, parser='lalr', cache=True) |
|
|
parser = Lark(g, parser='lalr', cache=True) |
|
|
assert parser.parse('a') == Tree('start', []) |
|
|
assert parser.parse('a') == Tree('start', []) |
|
|
|
|
|
|
|
|
parser = Lark(g+' "b"', parser='lalr', cache=True) |
|
|
|
|
|
|
|
|
parser = Lark(g + ' "b"', parser='lalr', cache=True) |
|
|
assert len(mock_fs.files) == 2 |
|
|
assert len(mock_fs.files) == 2 |
|
|
assert parser.parse('ab') == Tree('start', []) |
|
|
assert parser.parse('ab') == Tree('start', []) |
|
|
|
|
|
|
|
@@ -100,7 +112,7 @@ class TestCache(TestCase): |
|
|
parser = Lark(g, parser="lalr", debug=True, cache=True) |
|
|
parser = Lark(g, parser="lalr", debug=True, cache=True) |
|
|
assert parser.options.options['debug'] |
|
|
assert parser.options.options['debug'] |
|
|
|
|
|
|
|
|
# Test inline transformer (tree-less) |
|
|
|
|
|
|
|
|
# Test inline transformer (tree-less) & lexer_callbacks |
|
|
mock_fs.files = {} |
|
|
mock_fs.files = {} |
|
|
g = """ |
|
|
g = """ |
|
|
start: add+ |
|
|
start: add+ |
|
@@ -109,19 +121,19 @@ class TestCache(TestCase): |
|
|
%ignore " " |
|
|
%ignore " " |
|
|
""" |
|
|
""" |
|
|
text = "1+2 3+4" |
|
|
text = "1+2 3+4" |
|
|
expected = Tree('start', [3, 7]) |
|
|
|
|
|
|
|
|
expected = Tree('start', [30, 70]) |
|
|
|
|
|
|
|
|
parser = Lark(g, parser='lalr', transformer=TestT(), cache=True) |
|
|
|
|
|
parser = Lark(g, parser='lalr', transformer=TestT(), cache=True) |
|
|
|
|
|
|
|
|
parser = Lark(g, parser='lalr', transformer=TestT(), cache=True, lexer_callbacks={'NUM': append_zero}) |
|
|
|
|
|
res0 = parser.parse(text) |
|
|
|
|
|
parser = Lark(g, parser='lalr', transformer=TestT(), cache=True, lexer_callbacks={'NUM': append_zero}) |
|
|
assert len(mock_fs.files) == 1 |
|
|
assert len(mock_fs.files) == 1 |
|
|
res1 = parser.parse(text) |
|
|
res1 = parser.parse(text) |
|
|
res2 = TestT().transform( Lark(g, parser="lalr", cache=True).parse(text) ) |
|
|
|
|
|
assert res1 == res2 |
|
|
|
|
|
|
|
|
res2 = TestT().transform(Lark(g, parser="lalr", cache=True, lexer_callbacks={'NUM': append_zero}).parse(text)) |
|
|
|
|
|
assert res0 == res1 == res2 == expected |
|
|
|
|
|
|
|
|
finally: |
|
|
finally: |
|
|
lark_module.FS = fs |
|
|
lark_module.FS = fs |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
if __name__ == '__main__': |
|
|
main() |
|
|
main() |