My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones  
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311

# The contents of this file are subject to the Mozilla Public License
# (MPL) Version 1.1 (the "License"); you may not use this file except
# in compliance with the License. You may obtain a copy of the License
# at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
# the License for the specific language governing rights and
# limitations under the License.
#
# The Original Code is LEPL (http://www.acooke.org/lepl)
# The Initial Developer of the Original Code is Andrew Cooke.
# Portions created by the Initial Developer are Copyright (C) 2009-2010
# Andrew Cooke (andrew@acooke.org). All Rights Reserved.
#
# Alternatively, the contents of this file may be used under the terms
# of the LGPL license (the GNU Lesser General Public License,
# http://www.gnu.org/licenses/lgpl.html), in which case the provisions
# of the LGPL License are applicable instead of those above.
#
# If you wish to allow use of your version of this file only under the
# terms of the LGPL License and not to allow others to use your version
# of this file under the MPL, indicate your decision by deleting the
# provisions above and replace them with the notice and other provisions
# required by the LGPL License. If you do not delete the provisions
# above, a recipient may use your version of this file under either the
# MPL or the LGPL License.

# pylint: disable-msg=W0401,C0111,W0614,W0622,C0301,C0321,C0324,C0103,R0201,R0903
#@PydevCodeAnalysisIgnore
# (the code style is for documentation, not "real")

'''
Examples from the documentation.
'''

#from logging import basicConfig, DEBUG
from operator import add, sub, mul, truediv
from timeit import timeit

from lepl import *
from lepl._example.support import Example


def add_sub_node():
value = Token(UnsignedReal())
symbol = Token('[^0-9a-zA-Z \t\r\n]')
number = Optional(symbol('-')) + value >> float
group2, group3 = Delayed(), Delayed()

# first layer, most tightly grouped, is parens and numbers
parens = symbol('(') & group3 & symbol(')')
group1 = parens | number

# second layer, next most tightly grouped, is multiplication
mul = group1 & symbol('*') & group2 > List
div = group1 & symbol('/') & group2 > List
group2 += mul | div | group1

# third layer, least tightly grouped, is addition
add = group2 & symbol('+') & group3 > List
sub = group2 & symbol('-') & group3 > List
group3 += add | sub | group2
return group3

def error_1():
value = Token(UnsignedReal())
symbol = Token('[^0-9a-zA-Z \t\r\n]')
number = Optional(symbol('-')) + value >> float
group2, group3 = Delayed(), Delayed()

# first layer, most tightly grouped, is parens and numbers
parens = symbol('(') & group3 & symbol(')')
group1 = parens | number

# second layer, next most tightly grouped, is multiplication
mul = group1 & symbol('*') & group2 > List
div = group1 & symbol('/') & group2 > List
group2 += group1 | mul | div

# third layer, least tightly grouped, is addition
add = group2 & symbol('+') & group3 > List
sub = group2 & symbol('-') & group3 > List
group3 += group2 | add | sub
return group3

def error_2():
value = Token(UnsignedReal())
symbol = Token('[^0-9a-zA-Z \t\r\n]')
number = Optional(symbol('-')) + value >> float
group2, group3 = Delayed(), Delayed()

# first layer, most tightly grouped, is parens and numbers
parens = symbol('(') & group3 & symbol(')')
group1 = parens | number

# second layer, next most tightly grouped, is multiplication
mul = group2 & symbol('*') & group2 > List
div = group2 & symbol('/') & group2 > List
group2 += group1 | mul | div

# third layer, least tightly grouped, is addition
add = group3 & symbol('+') & group3 > List
sub = group3 & symbol('-') & group3 > List
group3 += group2 | add | sub
return group3

def node_1():
class Add(List): pass
class Sub(List): pass
class Mul(List): pass
class Div(List): pass
value = Token(UnsignedReal())
symbol = Token('[^0-9a-zA-Z \t\r\n]')
number = Optional(symbol('-')) + value >> float
group2, group3 = Delayed(), Delayed()

# first layer, most tightly grouped, is parens and numbers
parens = ~symbol('(') & group3 & ~symbol(')')
group1 = parens | number

# second layer, next most tightly grouped, is multiplication
mul = group1 & ~symbol('*') & group2 > Mul
div = group1 & ~symbol('/') & group2 > Div
group2 += mul | div | group1

# third layer, least tightly grouped, is addition
add = group2 & ~symbol('+') & group3 > Add
sub = group2 & ~symbol('-') & group3 > Sub
group3 += add | sub | group2
return group3

def node_2():
class Op(List):
def __float__(self):
return self._op(float(self[0]), float(self[1]))
class Add(Op): _op = add
class Sub(Op): _op = sub
class Mul(Op): _op = mul
class Div(Op): _op = truediv
value = Token(UnsignedReal())
symbol = Token('[^0-9a-zA-Z \t\r\n]')
number = Optional(symbol('-')) + value >> float
group2, group3 = Delayed(), Delayed()
# first layer, most tightly grouped, is parens and numbers
parens = ~symbol('(') & group3 & ~symbol(')')
group1 = parens | number
# second layer, next most tightly grouped, is multiplication
mul_ = group1 & ~symbol('*') & group2 > Mul
div_ = group1 & ~symbol('/') & group2 > Div
group2 += mul_ | div_ | group1
# third layer, least tightly grouped, is addition
add_ = group2 & ~symbol('+') & group3 > Add
sub_ = group2 & ~symbol('-') & group3 > Sub
group3 += add_ | sub_ | group2
return group3


class Tutorial4Example(Example):

def run_add_sub_node(self):
return add_sub_node()

def run_error_1(self):
return error_1()

def unlimited_run_error_1(self):
matcher = self.run_error_1()
matcher.config.no_full_first_match()
return matcher

def run_error_2(self):
return error_2()

def unlimited_run_error_2(self):
matcher = self.run_error_2()
matcher.config.no_full_first_match()
return matcher

def test_all(self):

self.examples([
(lambda: self.run_add_sub_node().parse('1+2*(3-4)+5/6+7')[0],
"""List
+- 1.0
+- '+'
`- List
+- List
| +- 2.0
| +- '*'
| +- '('
| +- List
| | +- 3.0
| | +- '-'
| | `- 4.0
| `- ')'
+- '+'
`- List
+- List
| +- 5.0
| +- '/'
| `- 6.0
+- '+'
`- 7.0"""),
(lambda: self.run_error_1().parse('1+2*(3-4)+5/6+7')[0],
"""FullFirstMatchException: The match failed in <string> at '+' (line 1, character 2).
"""),
(lambda: len(list(self.unlimited_run_error_1().parse_all('1+2*(3-4)+5/6+7'))),
"""6"""),
(lambda: (self.run_error_1() & Eos()).parse('1+2*(3-4)+5/6+7')[0],
"""List
+- 1.0
+- '+'
`- List
+- List
| +- 2.0
| +- '*'
| +- '('
| +- List
| | +- 3.0
| | +- '-'
| | `- 4.0
| `- ')'
+- '+'
`- List
+- List
| +- 5.0
| +- '/'
| `- 6.0
+- '+'
`- 7.0"""),
(lambda: len(list((self.unlimited_run_error_1() & Eos()).parse_all('1+2*(3-4)+5/6+7'))),
"""1"""),
# this is the left-recursive issue
#(lambda: self.run_error_2().parse('1+2*(3-4)+5/6+7')[0],
#"""FullFirstMatchException: The match failed in <string> at '+' (line 1, character 2).
#"""),
#(lambda: len(list(self.unlimited_run_error_2().parse_all('1+2*(3-4)+5/6+7'))),
#"""12"""),
#(lambda: (self.run_error_2() & Eos()).parse('1+2*(3-4)+5/6+7')[0],
#"""List
# +- 1.0
# +- '+'
# `- List
# +- List
# | +- 2.0
# | +- '*'
# | +- '('
# | +- List
# | | +- 3.0
# | | +- '-'
# | | `- 4.0
# | `- ')'
# +- '+'
# `- List
# +- List
# | +- 5.0
# | +- '/'
# | `- 6.0
# +- '+'
# `- 7.0"""),
#(lambda: len(list((self.unlimited_run_error_2() & Eos()).parse_all('1+2*(3-4)+5/6+7'))),
#"""5"""),
(lambda: node_1().parse('1+2*(3-4)+5/6+7')[0],
"""Add
+- 1.0
`- Add
+- Mul
| +- 2.0
| `- Sub
| +- 3.0
| `- 4.0
`- Add
+- Div
| +- 5.0
| `- 6.0
`- 7.0"""),
(lambda: node_2().parse('1+2*(3-4)+5/6+7')[0],
"""Add
+- 1.0
`- Add
+- Mul
| +- 2.0
| `- Sub
| +- 3.0
| `- 4.0
`- Add
+- Div
| +- 5.0
| `- 6.0
`- 7.0"""),
(lambda: str(float(node_2().parse('1+2*(3-4)+5/6+7')[0]))[:5],
"""6.833"""),
])


if __name__ == '__main__':

parser = add_sub_node().get_parse()
print(timeit('parser("1+2*(3-4)+5/6+7")',
'from __main__ import parser', number=100))

parser = (error_1() & Eos()).get_parse()
print(timeit('parser("1+2*(3-4)+5/6+7")',
'from __main__ import parser', number=100))

parser = (error_2() & Eos()).get_parse()
print(timeit('parser("1+2*(3-4)+5/6+7")',
'from __main__ import parser', number=100))

Change log

1bc735ea4745 by andrew on Mar 11, 2011   Diff
a complete mess, but finally left
recursion appears to work again, so
committing now
Go to: 
Project members, sign in to write a code review

Older revisions

154dccddf5f0 by andrew on Mar 5, 2011   Diff
fixed max depth issue with tokens
5ff087d77f0b by andrew on Feb 27, 2011   Diff
fixes for 3.2, some profiling
312cbff931aa by andrew on Feb 27, 2011   Diff
all tests ok!
All revisions of this file

File info

Size: 9328 bytes, 311 lines
Powered by Google Project Hosting