|
@@ -1,19 +1,22 @@ |
|
|
# coding=utf-8 |
|
|
# coding=utf-8 |
|
|
|
|
|
|
|
|
import json |
|
|
import json |
|
|
|
|
|
import sys |
|
|
import unittest |
|
|
import unittest |
|
|
from unittest import TestCase |
|
|
from unittest import TestCase |
|
|
|
|
|
|
|
|
from lark import Lark |
|
|
from lark import Lark |
|
|
from lark.reconstruct import Reconstructor |
|
|
from lark.reconstruct import Reconstructor |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
common = """ |
|
|
common = """ |
|
|
%import common (WS_INLINE, NUMBER, WORD) |
|
|
%import common (WS_INLINE, NUMBER, WORD) |
|
|
%ignore WS_INLINE |
|
|
%ignore WS_INLINE |
|
|
""" |
|
|
""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _remove_ws(s): |
|
|
def _remove_ws(s): |
|
|
return s.replace(' ', '').replace('\n','') |
|
|
|
|
|
|
|
|
return s.replace(' ', '').replace('\n', '') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestReconstructor(TestCase): |
|
|
class TestReconstructor(TestCase): |
|
|
|
|
|
|
|
@@ -24,7 +27,6 @@ class TestReconstructor(TestCase): |
|
|
self.assertEqual(_remove_ws(code), _remove_ws(new)) |
|
|
self.assertEqual(_remove_ws(code), _remove_ws(new)) |
|
|
|
|
|
|
|
|
def test_starred_rule(self): |
|
|
def test_starred_rule(self): |
|
|
|
|
|
|
|
|
g = """ |
|
|
g = """ |
|
|
start: item* |
|
|
start: item* |
|
|
item: NL |
|
|
item: NL |
|
@@ -40,7 +42,6 @@ class TestReconstructor(TestCase): |
|
|
self.assert_reconstruct(g, code) |
|
|
self.assert_reconstruct(g, code) |
|
|
|
|
|
|
|
|
def test_starred_group(self): |
|
|
def test_starred_group(self): |
|
|
|
|
|
|
|
|
g = """ |
|
|
g = """ |
|
|
start: (rule | NL)* |
|
|
start: (rule | NL)* |
|
|
rule: WORD ":" NUMBER |
|
|
rule: WORD ":" NUMBER |
|
@@ -54,7 +55,6 @@ class TestReconstructor(TestCase): |
|
|
self.assert_reconstruct(g, code) |
|
|
self.assert_reconstruct(g, code) |
|
|
|
|
|
|
|
|
def test_alias(self): |
|
|
def test_alias(self): |
|
|
|
|
|
|
|
|
g = """ |
|
|
g = """ |
|
|
start: line* |
|
|
start: line* |
|
|
line: NL |
|
|
line: NL |
|
@@ -142,6 +142,7 @@ class TestReconstructor(TestCase): |
|
|
new_json = Reconstructor(json_parser).reconstruct(tree) |
|
|
new_json = Reconstructor(json_parser).reconstruct(tree) |
|
|
self.assertEqual(json.loads(new_json), json.loads(test_json)) |
|
|
self.assertEqual(json.loads(new_json), json.loads(test_json)) |
|
|
|
|
|
|
|
|
|
|
|
@unittest.skipIf(sys.version_info < (3, 0), "Python 2 does not play well with Unicode.") |
|
|
def test_switch_grammar_unicode_terminal(self): |
|
|
def test_switch_grammar_unicode_terminal(self): |
|
|
""" |
|
|
""" |
|
|
This test checks that a parse tree built with a grammar containing only ascii characters can be reconstructed |
|
|
This test checks that a parse tree built with a grammar containing only ascii characters can be reconstructed |
|
@@ -179,6 +180,5 @@ class TestReconstructor(TestCase): |
|
|
assert l2.parse(code2) == tree |
|
|
assert l2.parse(code2) == tree |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
if __name__ == '__main__': |
|
|
unittest.main() |
|
|
unittest.main() |