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 Lepl is a parser library written in Python.
36
37 This is the API documentation; the module index is at the bottom of this page.
38 There is also a `manual <../index.html>`_ which gives a higher level
39 overview.
40
41 The home page for this package is the
42 `Lepl website <http://www.acooke.org/lepl>`_.
43
44
45 Example
46 -------
47
48 A simple example of how to use Lepl::
49
50 from lepl import *
51
52 # For a simpler result these could be replaced with 'list', giving
53 # an AST as a set of nested lists
54 # (ie replace '> Term' etc with '> list' below).
55
56 class Term(List): pass
57 class Factor(List): pass
58 class Expression(List): pass
59
60 def build():
61
62 # Here we define the grammar
63
64 # A delayed value is defined later (see penultimate line in block)
65 expr = Delayed()
66 number = Digit()[1:,...] >> int
67
68 # Allow spaces between items
69 with DroppedSpace():
70 term = number | '(' & expr & ')' > Term
71 muldiv = Any('*/')
72 factor = term & (muldiv & term)[:] > Factor
73 addsub = Any('+-')
74 expr += factor & (addsub & factor)[:] > Expression
75 line = Trace(expr) & Eos()
76
77 return line.get_parse()
78
79 if __name__ == '__main__':
80 parser = build()
81 # parser returns a list of tokens, but line
82 # returns a single value, so take the first entry
83 print(parser('1 + 2 * (3 + 4 - 5)')[0])
84
85 Running this gives the result::
86
87 Expression
88 +- Factor
89 | `- Term
90 | `- 1
91 +- '+'
92 `- Factor
93 +- Term
94 | `- 2
95 +- '*'
96 `- Term
97 +- '('
98 +- Expression
99 | +- Factor
100 | | `- Term
101 | | `- 3
102 | +- '+'
103 | +- Factor
104 | | `- Term
105 | | `- 4
106 | +- '-'
107 | `- Factor
108 | `- Term
109 | `- 5
110 `- ')'
111 '''
112
113 from lepl.contrib.matchers import SmartSeparator2
114 from lepl.core.config import Configuration, ConfigBuilder
115 from lepl.core.manager import GeneratorManager
116 from lepl.core.trace import RecordDeepest, TraceStack
117 from lepl.matchers.combine import And, Or, First, Difference, Limit
118 from lepl.matchers.core import Empty, Any, Delayed, Literal, Empty, \
119 Lookahead, Regexp
120 from lepl.matchers.complex import PostMatch, Columns, Iterate
121 from lepl.matchers.monitor import Trace
122 from lepl.matchers.derived import Apply, args, KApply, Join, \
123 AnyBut, Optional, Star, ZeroOrMore, Map, Add, Drop, Repeat, Plus, \
124 OneOrMore, Substitute, Name, Eof, Eos, Identity, Newline, Space, \
125 Whitespace, Digit, Letter, Upper, Lower, Printable, Punctuation, \
126 UnsignedInteger, SignedInteger, Integer, UnsignedFloat, SignedFloat, \
127 UnsignedEFloat, SignedEFloat, Float, UnsignedReal, SignedReal, \
128 UnsignedEReal, SignedEReal, Real, Word, DropEmpty, Literals, \
129 String, SingleLineString, SkipString, SkipTo
130 from lepl.matchers.error import Error, make_error, raise_error
131 from lepl.matchers.memo import RMemo, LMemo, MemoException
132 from lepl.matchers.operators import Override, Separator, SmartSeparator1, \
133 GREEDY, NON_GREEDY, DEPTH_FIRST, BREADTH_FIRST, DroppedSpace, REDUCE
134 from lepl.matchers.support import function_matcher, function_matcher_factory, \
135 sequence_matcher, sequence_matcher_factory, \
136 trampoline_matcher, trampoline_matcher_factory
137 from lepl.matchers.transform import PostCondition, Transform, Assert
138 from lepl.matchers.variables import TraceVariables
139 from lepl.lexer.matchers import Token
140 from lepl.lexer.support import LexerError, RuntimeLexerError
141 from lepl.lexer.lines.matchers import Block, Line, LineStart, LineEnd, \
142 constant_indent, explicit, to_right, ContinuedLineFactory, Extend, \
143 NO_BLOCKS, DEFAULT_POLICY
144 from lepl.regexp.core import RegexpError
145 from lepl.regexp.matchers import NfaRegexp, DfaRegexp
146 from lepl.regexp.unicode import UnicodeAlphabet
147 from lepl.stream.core import s_debug, s_deepest, s_delta, s_empty, s_eq, \
148 s_factory, s_fmt, s_global_kargs, s_id, s_join, s_kargs, s_key, \
149 s_len, s_line, s_max, s_next, s_stream
150 from lepl.stream.maxdepth import FullFirstMatchException
151 from lepl.stream.factory import DEFAULT_STREAM_FACTORY
152 from lepl.support.list import List, sexpr_fold, sexpr_throw, sexpr_flatten, \
153 sexpr_to_tree
154 from lepl.support.node import Node, make_dict, join_with, node_throw
155 from lepl.support.timer import print_timing
156
157 __all__ = [
158
159
160 'Configuration',
161 'ConfigBuilder',
162
163
164 'SmartSeparator2',
165
166
167 'make_error',
168 'raise_error',
169 'Error',
170
171
172 'Empty',
173 'Repeat',
174 'Join',
175 'Any',
176 'Literal',
177 'Empty',
178 'Lookahead',
179 'Regexp',
180
181
182 'And',
183 'Or',
184 'First',
185 'Difference',
186 'Limit',
187
188
189 'Apply',
190 'args',
191 'KApply',
192 'Delayed',
193 'Trace',
194 'AnyBut',
195 'Optional',
196 'Star',
197 'ZeroOrMore',
198 'Plus',
199 'OneOrMore',
200 'Map',
201 'Add',
202 'Drop',
203 'Substitute',
204 'Name',
205 'Eof',
206 'Eos',
207 'Identity',
208 'Newline',
209 'Space',
210 'Whitespace',
211 'Digit',
212 'Letter',
213 'Upper',
214 'Lower',
215 'Printable',
216 'Punctuation',
217
218 'UnsignedInteger',
219 'SignedInteger',
220 'Integer',
221
222
223 'UnsignedFloat',
224 'SignedFloat',
225 'UnsignedEFloat',
226 'SignedEFloat',
227 'Float',
228
229
230 'UnsignedReal',
231 'SignedReal',
232 'UnsignedEReal',
233 'SignedEReal',
234 'Real',
235
236 'Word',
237 'DropEmpty',
238 'Literals',
239 'String',
240 'SingleLineString',
241 'SkipString',
242 'SkipTo',
243 'GREEDY',
244 'NON_GREEDY',
245 'DEPTH_FIRST',
246 'BREADTH_FIRST',
247 'REDUCE',
248
249
250 'PostMatch',
251 'Columns',
252 'Iterate',
253
254
255 'function_matcher',
256 'function_matcher_factory',
257 'sequence_matcher',
258 'sequence_matcher_factory',
259 'trampoline_matcher',
260 'trampoline_matcher_factory',
261
262
263 'PostCondition',
264 'Transform',
265 'Assert',
266
267
268 'TraceVariables',
269
270
271 'DEFAULT_STREAM_FACTORY',
272
273
274 'Override',
275 'Separator',
276 'SmartSeparator1',
277 'DroppedSpace',
278
279
280 'Node',
281 'make_dict',
282 'join_with',
283 'node_throw',
284
285
286 'List',
287 'sexpr_fold',
288 'sexpr_throw',
289 'sexpr_flatten',
290 'sexpr_to_tree',
291
292
293 'Token',
294 'LexerError',
295 'RuntimeLexerError',
296
297
298 'GeneratorManager',
299
300
301 'RecordDeepest',
302 'TraceStack',
303
304
305 'RMemo',
306 'LMemo',
307 'MemoException',
308
309
310 'RegexpError',
311
312
313 'NfaRegexp',
314 'DfaRegexp',
315
316
317 'UnicodeAlphabet',
318
319
320 's_debug',
321 's_deepest',
322 's_delta',
323 's_empty',
324 's_eq',
325 's_factory',
326 's_fmt',
327 's_global_kargs',
328 's_id',
329 's_join',
330 's_kargs',
331 's_key',
332 's_len',
333 's_line',
334 's_max',
335 's_next',
336 's_stream',
337
338
339 'FullFirstMatchException',
340
341
342 'LineStart',
343 'LineEnd',
344 'Line',
345 'ContinuedLineFactory',
346 'Extend',
347 'Block',
348 'NO_BLOCKS',
349 'DEFAULT_POLICY',
350 'explicit',
351 'constant_indent',
352 'to_right',
353
354
355 'print_timing'
356 ]
357
358 __version__ = '5.1.3'
359
360 if __version__.find('b') > -1:
361 from logging import getLogger, basicConfig, WARN
362
363 getLogger('lepl').warn('You are using a BETA version of LEPL.')
364