forked from lark-parser/lark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_reconstructor.py
196 lines (155 loc) · 5.06 KB
/
test_reconstructor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# coding=utf-8
import json
import sys
import unittest
from itertools import product
from unittest import TestCase
from lark import Lark
from lark.reconstruct import Reconstructor
common = """
%import common (WS_INLINE, NUMBER, WORD)
%ignore WS_INLINE
"""
def _remove_ws(s):
return s.replace(' ', '').replace('\n', '')
class TestReconstructor(TestCase):
def assert_reconstruct(self, grammar, code, **options):
parser = Lark(grammar, parser='lalr', maybe_placeholders=False, **options)
tree = parser.parse(code)
new = Reconstructor(parser).reconstruct(tree)
self.assertEqual(_remove_ws(code), _remove_ws(new))
def test_starred_rule(self):
g = """
start: item*
item: NL
| rule
rule: WORD ":" NUMBER
NL: /(\\r?\\n)+\\s*/
""" + common
code = """
Elephants: 12
"""
self.assert_reconstruct(g, code)
def test_starred_group(self):
g = """
start: (rule | NL)*
rule: WORD ":" NUMBER
NL: /(\\r?\\n)+\\s*/
""" + common
code = """
Elephants: 12
"""
self.assert_reconstruct(g, code)
def test_alias(self):
g = """
start: line*
line: NL
| rule
| "hello" -> hi
rule: WORD ":" NUMBER
NL: /(\\r?\\n)+\\s*/
""" + common
code = """
Elephants: 12
hello
"""
self.assert_reconstruct(g, code)
def test_keep_tokens(self):
g = """
start: (NL | stmt)*
stmt: var op var
!op: ("+" | "-" | "*" | "/")
var: WORD
NL: /(\\r?\\n)+\\s*/
""" + common
code = """
a+b
"""
self.assert_reconstruct(g, code)
def test_expand_rule(self):
g = """
?start: (NL | mult_stmt)*
?mult_stmt: sum_stmt ["*" sum_stmt]
?sum_stmt: var ["+" var]
var: WORD
NL: /(\\r?\\n)+\\s*/
""" + common
code = ['a', 'a*b', 'a+b', 'a*b+c', 'a+b*c', 'a+b*c+d']
for c in code:
self.assert_reconstruct(g, c)
def test_json_example(self):
test_json = '''
{
"empty_object" : {},
"empty_array" : [],
"booleans" : { "YES" : true, "NO" : false },
"numbers" : [ 0, 1, -2, 3.3, 4.4e5, 6.6e-7 ],
"strings" : [ "This", [ "And" , "That", "And a \\"b" ] ],
"nothing" : null
}
'''
json_grammar = r"""
?start: value
?value: object
| array
| string
| SIGNED_NUMBER -> number
| "true" -> true
| "false" -> false
| "null" -> null
array : "[" [value ("," value)*] "]"
object : "{" [pair ("," pair)*] "}"
pair : string ":" value
string : ESCAPED_STRING
%import common.ESCAPED_STRING
%import common.SIGNED_NUMBER
%import common.WS
%ignore WS
"""
json_parser = Lark(json_grammar, parser='lalr', maybe_placeholders=False)
tree = json_parser.parse(test_json)
new_json = Reconstructor(json_parser).reconstruct(tree)
self.assertEqual(json.loads(new_json), json.loads(test_json))
def test_keep_all_tokens(self):
g = """
start: "a"? _B? c? _d?
_B: "b"
c: "c"
_d: "d"
"""
examples = list(map(''.join, product(('', 'a'), ('', 'b'), ('', 'c'), ('', 'd'), )))
for code in examples:
self.assert_reconstruct(g, code, keep_all_tokens=True)
@unittest.skipIf(sys.version_info < (3, 0), "Python 2 does not play well with Unicode.")
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
with a grammar that has unicode rules (or vice versa). The original bug assigned ANON terminals to unicode
keywords, which offsets the ANON terminal count in the unicode grammar and causes subsequent identical ANON
tokens (e.g., `+=`) to mis-match between the two grammars.
"""
g1 = """
start: (NL | stmt)*
stmt: "keyword" var op var
!op: ("+=" | "-=" | "*=" | "/=")
var: WORD
NL: /(\\r?\\n)+\\s*/
""" + common
g2 = """
start: (NL | stmt)*
stmt: "குறிப்பு" var op var
!op: ("+=" | "-=" | "*=" | "/=")
var: WORD
NL: /(\\r?\\n)+\\s*/
""" + common
code = """
keyword x += y
"""
l1 = Lark(g1, parser='lalr', maybe_placeholders=False)
l2 = Lark(g2, parser='lalr', maybe_placeholders=False)
r = Reconstructor(l2)
tree = l1.parse(code)
code2 = r.reconstruct(tree)
assert l2.parse(code2) == tree
if __name__ == '__main__':
unittest.main()