1 from lepl.matchers.variables import TraceVariables
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 Test a Python-like grammar.
33 '''
34
35
36
37
38 from logging import basicConfig, DEBUG
39 from unittest import TestCase
40
41 from lepl import *
45
46 @property
71
73
74 program1 = '''
75 kopo fjire ifejfr
76 ogptkr jgitr gtr
77 ffjireofr(kfkorp, freopk):
78 jifr fireo
79 frefre jifoji
80 jio frefre:
81 jiforejifre fiorej
82 jfore fire
83 jioj
84 jioj
85 jiojio
86 '''
87 result = self.parser(program1)
88 assert result == [[],
89 ['kopo', 'fjire', 'ifejfr'],
90 ['ogptkr', 'jgitr', 'gtr'],
91 ['ffjireofr', ('kfkorp', 'freopk'),
92 ['jifr', 'fireo'],
93 ['frefre', 'jifoji'],
94 ['jio', 'frefre',
95 ['jiforejifre', 'fiorej'],
96 ['jfore', 'fire'],
97 ['jioj']],
98 ['jioj']],
99 ['jiojio']], result
100
102
103 try:
104 self.parser('123')
105 assert False, 'expected exception'
106 except LexerError as error:
107 assert str(error) == 'No token for \'123\' at ' \
108 'line 1, character 1 of \'123\'.', str(error)
109
111
112 result = self.parser('''
113 def foo(abc, def,
114 ghi):
115 jgiog
116 ''')
117 assert result == [[],
118 ['def', 'foo', ('abc', 'def', 'ghi'),
119 ['jgiog']]], result
120
122
123 result = self.parser('''
124 this is a single \
125 line spread over \
126 many actual \
127 lines
128 and this is another
129 ''')
130 assert result == [[],
131 ['this', 'is', 'a', 'single', 'line', 'spread',
132 'over', 'many', 'actual', 'lines'],
133 ['and', 'this', 'is', 'another']], result
134
136
137 result = self.parser('''
138 def foo():
139 a blank line can be
140
141 inside a block
142
143 or can separate blocks
144 ''')
145 assert result == [[],
146 ['def', 'foo', (),
147 ['a', 'blank', 'line', 'can', 'be'],
148 [],
149 ['inside', 'a', 'block'],
150 []
151 ],
152 ['or', 'can', 'separate', 'blocks']
153 ], result
154
170
172
173 result = self.parser('''
174 this is a grammar with a similar
175 line structure to python
176
177 # it supports comments
178 if something:
179 then we indent
180 else:
181 something else
182
183 def function(a, b, c):
184 we can nest blocks:
185 like this
186 and we can also \
187 have explicit continuations \
188 with \
189 any \
190 indentation
191
192 same for (argument,
193 lists):
194 which do not need the
195 continuation marker
196 # and we can have blank lines inside a block:
197
198 like this
199 ''')
200 assert result == \
201 [ [],
202 ['this', 'is', 'a', 'grammar', 'with', 'a', 'similar'],
203 ['line', 'structure', 'to', 'python'],
204 [],
205 [],
206 ['if', 'something',
207 ['then', 'we', 'indent']],
208 ['else',
209 ['something', 'else'],
210 []],
211 ['def', 'function', ('a', 'b', 'c'),
212 ['we', 'can', 'nest', 'blocks',
213 ['like', 'this']],
214 ['and', 'we', 'can', 'also', 'have', 'explicit',
215 'continuations', 'with', 'any', 'indentation'],
216 []],
217 ['same', 'for', ('argument', 'lists'),
218 ['which', 'do', 'not', 'need', 'the'],
219 ['continuation', 'marker'],
220 [],
221 [],
222 ['like', 'this']]], result
223