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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363

# 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.

#@PydevCodeAnalysisIgnore
# pylint: disable-msg=C0301, E0611, W0401
# confused by __init__?

'''
Lepl is a parser library written in Python.

This is the API documentation; the module index is at the bottom of this page.
There is also a `manual <../index.html>`_ which gives a higher level
overview.

The home page for this package is the
`Lepl website <http://www.acooke.org/lepl>`_.


Example
-------

A simple example of how to use Lepl::

from lepl import *

# For a simpler result these could be replaced with 'list', giving
# an AST as a set of nested lists
# (ie replace '> Term' etc with '> list' below).

class Term(List): pass
class Factor(List): pass
class Expression(List): pass

def build():

# Here we define the grammar

# A delayed value is defined later (see penultimate line in block)
expr = Delayed()
number = Digit()[1:,...] >> int

# Allow spaces between items
with DroppedSpace():
term = number | '(' & expr & ')' > Term
muldiv = Any('*/')
factor = term & (muldiv & term)[:] > Factor
addsub = Any('+-')
expr += factor & (addsub & factor)[:] > Expression
line = Trace(expr) & Eos()

return line.get_parse()

if __name__ == '__main__':
parser = build()
# parser returns a list of tokens, but line
# returns a single value, so take the first entry
print(parser('1 + 2 * (3 + 4 - 5)')[0])

Running this gives the result::

Expression
+- Factor
| `- Term
| `- 1
+- '+'
`- Factor
+- Term
| `- 2
+- '*'
`- Term
+- '('
+- Expression
| +- Factor
| | `- Term
| | `- 3
| +- '+'
| +- Factor
| | `- Term
| | `- 4
| +- '-'
| `- Factor
| `- Term
| `- 5
`- ')'
'''

from lepl.contrib.matchers import SmartSeparator2
from lepl.core.config import Configuration, ConfigBuilder
from lepl.core.manager import GeneratorManager
from lepl.core.trace import RecordDeepest, TraceStack
from lepl.matchers.combine import And, Or, First, Difference, Limit
from lepl.matchers.core import Empty, Any, Delayed, Literal, Empty, \
Lookahead, Regexp
from lepl.matchers.complex import PostMatch, Columns, Iterate
from lepl.matchers.monitor import Trace
from lepl.matchers.derived import Apply, args, KApply, Join, \
AnyBut, Optional, Star, ZeroOrMore, Map, Add, Drop, Repeat, Plus, \
OneOrMore, Substitute, Name, Eof, Eos, Identity, Newline, Space, \
Whitespace, Digit, Letter, Upper, Lower, Printable, Punctuation, \
UnsignedInteger, SignedInteger, Integer, UnsignedFloat, SignedFloat, \
UnsignedEFloat, SignedEFloat, Float, UnsignedReal, SignedReal, \
UnsignedEReal, SignedEReal, Real, Word, DropEmpty, Literals, \
String, SingleLineString, SkipString, SkipTo
from lepl.matchers.error import Error, make_error, raise_error
from lepl.matchers.memo import RMemo, LMemo, MemoException
from lepl.matchers.operators import Override, Separator, SmartSeparator1, \
GREEDY, NON_GREEDY, DEPTH_FIRST, BREADTH_FIRST, DroppedSpace, REDUCE
from lepl.matchers.support import function_matcher, function_matcher_factory, \
sequence_matcher, sequence_matcher_factory, \
trampoline_matcher, trampoline_matcher_factory
from lepl.matchers.transform import PostCondition, Transform, Assert
from lepl.matchers.variables import TraceVariables
from lepl.lexer.matchers import Token
from lepl.lexer.support import LexerError, RuntimeLexerError
from lepl.lexer.lines.matchers import Block, Line, LineStart, LineEnd, \
constant_indent, explicit, to_right, ContinuedLineFactory, Extend, \
NO_BLOCKS, DEFAULT_POLICY
from lepl.regexp.core import RegexpError
from lepl.regexp.matchers import NfaRegexp, DfaRegexp
from lepl.regexp.unicode import UnicodeAlphabet
from lepl.stream.core import s_debug, s_deepest, s_delta, s_empty, s_eq, \
s_factory, s_fmt, s_global_kargs, s_id, s_join, s_kargs, s_key, \
s_len, s_line, s_max, s_next, s_stream
from lepl.stream.maxdepth import FullFirstMatchException
from lepl.stream.factory import DEFAULT_STREAM_FACTORY
from lepl.support.list import List, sexpr_fold, sexpr_throw, sexpr_flatten, \
sexpr_to_tree
from lepl.support.node import Node, make_dict, join_with, node_throw
from lepl.support.timer import print_timing

__all__ = [

# lepl.core.config
'Configuration',
'ConfigBuilder',

# lepl.contrib.matchers
'SmartSeparator2',

# lepl.matchers.error
'make_error',
'raise_error',
'Error',

# lepl.matchers.core
'Empty',
'Repeat',
'Join',
'Any',
'Literal',
'Empty',
'Lookahead',
'Regexp',

# lepl.matchers.combine
'And',
'Or',
'First',
'Difference',
'Limit',

# lepl.matchers.derived
'Apply',
'args',
'KApply',
'Delayed',
'Trace',
'AnyBut',
'Optional',
'Star',
'ZeroOrMore',
'Plus',
'OneOrMore',
'Map',
'Add',
'Drop',
'Substitute',
'Name',
'Eof',
'Eos',
'Identity',
'Newline',
'Space',
'Whitespace',
'Digit',
'Letter',
'Upper',
'Lower',
'Printable',
'Punctuation',

'UnsignedInteger',
'SignedInteger',
'Integer',

# float matchers exclude integers
'UnsignedFloat',
'SignedFloat',
'UnsignedEFloat',
'SignedEFloat',
'Float',

# real matchers match both floats and integers
'UnsignedReal',
'SignedReal',
'UnsignedEReal',
'SignedEReal',
'Real',

'Word',
'DropEmpty',
'Literals',
'String',
'SingleLineString',
'SkipString',
'SkipTo',
'GREEDY',
'NON_GREEDY',
'DEPTH_FIRST',
'BREADTH_FIRST',
'REDUCE',

# lepl.matchers.complex
'PostMatch',
'Columns',
'Iterate',

# lepl.matchers.support
'function_matcher',
'function_matcher_factory',
'sequence_matcher',
'sequence_matcher_factory',
'trampoline_matcher',
'trampoline_matcher_factory',

# lepl.matchers.transform
'PostCondition',
'Transform',
'Assert',

# lepl.matchers.variables
'TraceVariables',

# lepl.stream.stream
'DEFAULT_STREAM_FACTORY',

# lepl.matchers.operators
'Override',
'Separator',
'SmartSeparator1',
'DroppedSpace',

# lepl.support.node
'Node',
'make_dict',
'join_with',
'node_throw',

# lepl.support.list
'List',
'sexpr_fold',
'sexpr_throw',
'sexpr_flatten',
'sexpr_to_tree',

# lepl.lexer.matchers
'Token',
'LexerError',
'RuntimeLexerError',

# lepl.core.manager
'GeneratorManager',

# lepl.core.trace
'RecordDeepest',
'TraceStack',

# lepl.core.memo,
'RMemo',
'LMemo',
'MemoException',

# lepl.regexp.core
'RegexpError',

# lepl.regexp.matchers
'NfaRegexp',
'DfaRegexp',

# lepl.regexp.unicode
'UnicodeAlphabet',

# lepl.stream.core
's_debug',
's_deepest',
's_delta',
's_empty',
's_eq',
's_factory',
's_fmt',
's_global_kargs',
's_id',
's_join',
's_kargs',
's_key',
's_len',
's_line',
's_max',
's_next',
's_stream',

# lepl.stream.maxdepth
'FullFirstMatchException',

# lepl.lexer.lines.matchers
'LineStart',
'LineEnd',
'Line',
'ContinuedLineFactory',
'Extend',
'Block',
'NO_BLOCKS',
'DEFAULT_POLICY',
'explicit',
'constant_indent',
'to_right',

# lepl.support.timer
'print_timing'
]

__version__ = '6.0.0'

if __version__.find('b') > -1:
from logging import getLogger, basicConfig, WARN
#basicConfig(level=WARN)
getLogger('lepl').warn('You are using a BETA version of LEPL.')

Change log

7ae6957ff515 by and...@acooke.org on Jan 22, 2012   Diff
looking at pypy speed; cleaning out code.
Go to: 
Project members, sign in to write a code review

Older revisions

431aadb3c826 by and...@acooke.org on Jan 8, 2012   Diff
releasing 5.0.1
ea2d4bea011b by andrew on Mar 19, 2011   Diff
before adding links
1082e82fed90 by andrew on Mar 19, 2011   Diff
still cleaning out docs
All revisions of this file

File info

Size: 10500 bytes, 363 lines
Powered by Google Project Hosting