My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones  
Changes to /src/lepl/__init__.py
4e7ed779a9fe vs. 13a59b816b7c Compare: vs.  Format:
Revision 13a59b816b7c
Go to: 
Project members, sign in to write a code review
/src/lepl/__init__.py   4e7ed779a9fe /src/lepl/__init__.py   13a59b816b7c
1 1
2 # The contents of this file are subject to the Mozilla Public License 2 # The contents of this file are subject to the Mozilla Public License
3 # (MPL) Version 1.1 (the "License"); you may not use this file except 3 # (MPL) Version 1.1 (the "License"); you may not use this file except
4 # in compliance with the License. You may obtain a copy of the License 4 # in compliance with the License. You may obtain a copy of the License
5 # at http://www.mozilla.org/MPL/ 5 # at http://www.mozilla.org/MPL/
6 # 6 #
7 # Software distributed under the License is distributed on an "AS IS" 7 # Software distributed under the License is distributed on an "AS IS"
8 # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 8 # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
9 # the License for the specific language governing rights and 9 # the License for the specific language governing rights and
10 # limitations under the License. 10 # limitations under the License.
11 # 11 #
12 # The Original Code is LEPL (http://www.acooke.org/lepl) 12 # The Original Code is LEPL (http://www.acooke.org/lepl)
13 # The Initial Developer of the Original Code is Andrew Cooke. 13 # The Initial Developer of the Original Code is Andrew Cooke.
14 # Portions created by the Initial Developer are Copyright (C) 2009-2010 14 # Portions created by the Initial Developer are Copyright (C) 2009-2010
15 # Andrew Cooke (andrew@acooke.org). All Rights Reserved. 15 # Andrew Cooke (andrew@acooke.org). All Rights Reserved.
16 # 16 #
17 # Alternatively, the contents of this file may be used under the terms 17 # Alternatively, the contents of this file may be used under the terms
18 # of the LGPL license (the GNU Lesser General Public License, 18 # of the LGPL license (the GNU Lesser General Public License,
19 # http://www.gnu.org/licenses/lgpl.html), in which case the provisions 19 # http://www.gnu.org/licenses/lgpl.html), in which case the provisions
20 # of the LGPL License are applicable instead of those above. 20 # of the LGPL License are applicable instead of those above.
21 # 21 #
22 # If you wish to allow use of your version of this file only under the 22 # If you wish to allow use of your version of this file only under the
23 # terms of the LGPL License and not to allow others to use your version 23 # terms of the LGPL License and not to allow others to use your version
24 # of this file under the MPL, indicate your decision by deleting the 24 # of this file under the MPL, indicate your decision by deleting the
25 # provisions above and replace them with the notice and other provisions 25 # provisions above and replace them with the notice and other provisions
26 # required by the LGPL License. If you do not delete the provisions 26 # required by the LGPL License. If you do not delete the provisions
27 # above, a recipient may use your version of this file under either the 27 # above, a recipient may use your version of this file under either the
28 # MPL or the LGPL License. 28 # MPL or the LGPL License.
29 29
30 #@PydevCodeAnalysisIgnore 30 #@PydevCodeAnalysisIgnore
31 # pylint: disable-msg=C0301, E0611, W0401 31 # pylint: disable-msg=C0301, E0611, W0401
32 # confused by __init__? 32 # confused by __init__?
33 33
34 ''' 34 '''
35 Lepl is a parser library written in Python. 35 Lepl is a parser library written in Python.
36 36
37 This is the API documentation; the module index is at the bottom of this page. 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 38 There is also a `manual <../index.html>`_ which gives a higher level
39 overview. 39 overview.
40 40
41 The home page for this package is the 41 The home page for this package is the
42 `Lepl website <http://www.acooke.org/lepl>`_. 42 `Lepl website <http://www.acooke.org/lepl>`_.
43 43
44 44
45 Example 45 Example
46 ------- 46 -------
47 47
48 A simple example of how to use Lepl:: 48 A simple example of how to use Lepl::
49 49
50 from lepl import * 50 from lepl import *
51 51
52 # For a simpler result these could be replaced with 'list', giving 52 # For a simpler result these could be replaced with 'list', giving
53 # an AST as a set of nested lists 53 # an AST as a set of nested lists
54 # (ie replace '> Term' etc with '> list' below). 54 # (ie replace '> Term' etc with '> list' below).
55 55
56 class Term(List): pass 56 class Term(List): pass
57 class Factor(List): pass 57 class Factor(List): pass
58 class Expression(List): pass 58 class Expression(List): pass
59 59
60 def build(): 60 def build():
61 61
62 # Here we define the grammar 62 # Here we define the grammar
63 63
64 # A delayed value is defined later (see penultimate line in block) 64 # A delayed value is defined later (see penultimate line in block)
65 expr = Delayed() 65 expr = Delayed()
66 number = Digit()[1:,...] >> int 66 number = Digit()[1:,...] >> int
67 67
68 # Allow spaces between items 68 # Allow spaces between items
69 with DroppedSpace(): 69 with DroppedSpace():
70 term = number | '(' & expr & ')' > Term 70 term = number | '(' & expr & ')' > Term
71 muldiv = Any('*/') 71 muldiv = Any('*/')
72 factor = term & (muldiv & term)[:] > Factor 72 factor = term & (muldiv & term)[:] > Factor
73 addsub = Any('+-') 73 addsub = Any('+-')
74 expr += factor & (addsub & factor)[:] > Expression 74 expr += factor & (addsub & factor)[:] > Expression
75 line = Trace(expr) & Eos() 75 line = Trace(expr) & Eos()
76 76
77 return line.get_parse() 77 return line.get_parse()
78 78
79 if __name__ == '__main__': 79 if __name__ == '__main__':
80 parser = build() 80 parser = build()
81 # parser returns a list of tokens, but line 81 # parser returns a list of tokens, but line
82 # returns a single value, so take the first entry 82 # returns a single value, so take the first entry
83 print(parser('1 + 2 * (3 + 4 - 5)')[0]) 83 print(parser('1 + 2 * (3 + 4 - 5)')[0])
84 84
85 Running this gives the result:: 85 Running this gives the result::
86 86
87 Expression 87 Expression
88 +- Factor 88 +- Factor
89 | `- Term 89 | `- Term
90 | `- 1 90 | `- 1
91 +- '+' 91 +- '+'
92 `- Factor 92 `- Factor
93 +- Term 93 +- Term
94 | `- 2 94 | `- 2
95 +- '*' 95 +- '*'
96 `- Term 96 `- Term
97 +- '(' 97 +- '('
98 +- Expression 98 +- Expression
99 | +- Factor 99 | +- Factor
100 | | `- Term 100 | | `- Term
101 | | `- 3 101 | | `- 3
102 | +- '+' 102 | +- '+'
103 | +- Factor 103 | +- Factor
104 | | `- Term 104 | | `- Term
105 | | `- 4 105 | | `- 4
106 | +- '-' 106 | +- '-'
107 | `- Factor 107 | `- Factor
108 | `- Term 108 | `- Term
109 | `- 5 109 | `- 5
110 `- ')' 110 `- ')'
111 ''' 111 '''
112 112
113 from lepl.contrib.matchers import SmartSeparator2 113 from lepl.contrib.matchers import SmartSeparator2
114 from lepl.core.config import Configuration, ConfigBuilder 114 from lepl.core.config import Configuration, ConfigBuilder
115 from lepl.core.manager import GeneratorManager 115 from lepl.core.manager import GeneratorManager
116 from lepl.core.trace import RecordDeepest, TraceStack 116 from lepl.core.trace import RecordDeepest, TraceStack
117 from lepl.matchers.combine import And, Or, First, Difference, Limit 117 from lepl.matchers.combine import And, Or, First, Difference, Limit
118 from lepl.matchers.core import Empty, Any, Delayed, Literal, Empty, \ 118 from lepl.matchers.core import Empty, Any, Delayed, Literal, Empty, \
119 Lookahead, Regexp 119 Lookahead, Regexp
120 from lepl.matchers.complex import PostMatch, Columns 120 from lepl.matchers.complex import PostMatch, Columns
121 from lepl.matchers.monitor import Trace 121 from lepl.matchers.monitor import Trace
122 from lepl.matchers.derived import Apply, args, KApply, Join, \ 122 from lepl.matchers.derived import Apply, args, KApply, Join, \
123 AnyBut, Optional, Star, ZeroOrMore, Map, Add, Drop, Repeat, Plus, \ 123 AnyBut, Optional, Star, ZeroOrMore, Map, Add, Drop, Repeat, Plus, \
124 OneOrMore, Substitute, Name, Eof, Eos, Identity, Newline, Space, \ 124 OneOrMore, Substitute, Name, Eof, Eos, Identity, Newline, Space, \
125 Whitespace, Digit, Letter, Upper, Lower, Printable, Punctuation, \ 125 Whitespace, Digit, Letter, Upper, Lower, Printable, Punctuation, \
126 UnsignedInteger, SignedInteger, Integer, UnsignedFloat, SignedFloat, \ 126 UnsignedInteger, SignedInteger, Integer, UnsignedFloat, SignedFloat, \
127 UnsignedEFloat, SignedEFloat, Float, UnsignedReal, SignedReal, \ 127 UnsignedEFloat, SignedEFloat, Float, UnsignedReal, SignedReal, \
128 UnsignedEReal, SignedEReal, Real, Word, DropEmpty, Literals, \ 128 UnsignedEReal, SignedEReal, Real, Word, DropEmpty, Literals, \
129 String, SingleLineString, SkipString, SkipTo 129 String, SingleLineString, SkipString, SkipTo
130 from lepl.matchers.error import Error, make_error, raise_error 130 from lepl.matchers.error import Error, make_error, raise_error
131 from lepl.matchers.memo import RMemo, LMemo, MemoException 131 from lepl.matchers.memo import RMemo, LMemo, MemoException
132 from lepl.matchers.operators import Override, Separator, SmartSeparator1, \ 132 from lepl.matchers.operators import Override, Separator, SmartSeparator1, \
133 GREEDY, NON_GREEDY, DEPTH_FIRST, BREADTH_FIRST, DroppedSpace, REDUCE 133 GREEDY, NON_GREEDY, DEPTH_FIRST, BREADTH_FIRST, DroppedSpace, REDUCE
134 from lepl.matchers.support import function_matcher, function_matcher_factory, \ 134 from lepl.matchers.support import function_matcher, function_matcher_factory, \
135 sequence_matcher, sequence_matcher_factory, \ 135 sequence_matcher, sequence_matcher_factory, \
136 trampoline_matcher, trampoline_matcher_factory 136 trampoline_matcher, trampoline_matcher_factory
137 from lepl.matchers.transform import PostCondition 137 from lepl.matchers.transform import PostCondition
138 from lepl.matchers.variables import TraceVariables 138 from lepl.matchers.variables import TraceVariables
139 from lepl.lexer.matchers import Token 139 from lepl.lexer.matchers import Token
140 from lepl.lexer.support import LexerError, RuntimeLexerError 140 from lepl.lexer.support import LexerError, RuntimeLexerError
141 from lepl.lexer.lines.matchers import LineStart, LineEnd, Line, \ 141 #from lepl.lexer.lines.matchers import LineStart, LineEnd, Line, \
142 ContinuedLineFactory, Extend 142 # ContinuedLineFactory, Extend
143 from lepl.lexer.blocks.matchers import Indent, Block, BLine, NO_BLOCKS, \ 143 #from lepl.lexer.blocks.matchers import Indent, Block, BLine, NO_BLOCKS, \
144 constant_indent, explicit, to_right, ContinuedBLineFactory, BExtend 144 # constant_indent, explicit, to_right, ContinuedBLineFactory, BExtend
145 from lepl.lexer.offside.matchers import Block, Line, LineStart, LineEnd, \
146 constant_indent, explicit, to_right, ContinuedLineFactory, Extend, \
147 NO_BLOCKS, DEFAULT_POLICY
145 from lepl.regexp.core import RegexpError 148 from lepl.regexp.core import RegexpError
146 from lepl.regexp.matchers import NfaRegexp, DfaRegexp 149 from lepl.regexp.matchers import NfaRegexp, DfaRegexp
147 from lepl.regexp.unicode import UnicodeAlphabet 150 from lepl.regexp.unicode import UnicodeAlphabet
148 from lepl.stream.core import s_debug, s_deepest, s_delta, s_empty, s_eq, \ 151 from lepl.stream.core import s_debug, s_deepest, s_delta, s_empty, s_eq, \
149 s_factory, s_fmt, s_global_kargs, s_id, s_join, s_kargs, s_key, \ 152 s_factory, s_fmt, s_global_kargs, s_id, s_join, s_kargs, s_key, \
150 s_len, s_line, s_max, s_next, s_stream 153 s_len, s_line, s_max, s_next, s_stream
151 from lepl.stream.maxdepth import FullFirstMatchException 154 from lepl.stream.maxdepth import FullFirstMatchException
152 from lepl.stream.factory import DEFAULT_STREAM_FACTORY 155 from lepl.stream.factory import DEFAULT_STREAM_FACTORY
153 from lepl.support.list import List, sexpr_fold, sexpr_throw 156 from lepl.support.list import List, sexpr_fold, sexpr_throw
154 from lepl.support.node import Node, make_dict, join_with, node_throw 157 from lepl.support.node import Node, make_dict, join_with, node_throw
155 from lepl.support.timer import print_timing 158 from lepl.support.timer import print_timing
156 159
157 __all__ = [ 160 __all__ = [
158 161
159 # lepl.core.config 162 # lepl.core.config
160 'Configuration', 163 'Configuration',
161 'ConfigBuilder', 164 'ConfigBuilder',
162 165
163 # lepl.contrib.matchers 166 # lepl.contrib.matchers
164 'SmartSeparator2', 167 'SmartSeparator2',
165 168
166 # lepl.matchers.error 169 # lepl.matchers.error
167 'make_error', 170 'make_error',
168 'raise_error', 171 'raise_error',
169 'Error', 172 'Error',
170 173
171 # lepl.matchers.core 174 # lepl.matchers.core
172 'Empty', 175 'Empty',
173 'Repeat', 176 'Repeat',
174 'Join', 177 'Join',
175 'Any', 178 'Any',
176 'Literal', 179 'Literal',
177 'Empty', 180 'Empty',
178 'Lookahead', 181 'Lookahead',
179 'Regexp', 182 'Regexp',
180 183
181 # lepl.matchers.combine 184 # lepl.matchers.combine
182 'And', 185 'And',
183 'Or', 186 'Or',
184 'First', 187 'First',
185 'Difference', 188 'Difference',
186 'Limit', 189 'Limit',
187 190
188 # lepl.matchers.derived 191 # lepl.matchers.derived
189 'Apply', 192 'Apply',
190 'args', 193 'args',
191 'KApply', 194 'KApply',
192 'Delayed', 195 'Delayed',
193 'Trace', 196 'Trace',
194 'AnyBut', 197 'AnyBut',
195 'Optional', 198 'Optional',
196 'Star', 199 'Star',
197 'ZeroOrMore', 200 'ZeroOrMore',
198 'Plus', 201 'Plus',
199 'OneOrMore', 202 'OneOrMore',
200 'Map', 203 'Map',
201 'Add', 204 'Add',
202 'Drop', 205 'Drop',
203 'Substitute', 206 'Substitute',
204 'Name', 207 'Name',
205 'Eof', 208 'Eof',
206 'Eos', 209 'Eos',
207 'Identity', 210 'Identity',
208 'Newline', 211 'Newline',
209 'Space', 212 'Space',
210 'Whitespace', 213 'Whitespace',
211 'Digit', 214 'Digit',
212 'Letter', 215 'Letter',
213 'Upper', 216 'Upper',
214 'Lower', 217 'Lower',
215 'Printable', 218 'Printable',
216 'Punctuation', 219 'Punctuation',
217 220
218 'UnsignedInteger', 221 'UnsignedInteger',
219 'SignedInteger', 222 'SignedInteger',
220 'Integer', 223 'Integer',
221 224
222 # float matchers exclude integers 225 # float matchers exclude integers
223 'UnsignedFloat', 226 'UnsignedFloat',
224 'SignedFloat', 227 'SignedFloat',
225 'UnsignedEFloat', 228 'UnsignedEFloat',
226 'SignedEFloat', 229 'SignedEFloat',
227 'Float', 230 'Float',
228 231
229 # real matchers match both floats and integers 232 # real matchers match both floats and integers
230 'UnsignedReal', 233 'UnsignedReal',
231 'SignedReal', 234 'SignedReal',
232 'UnsignedEReal', 235 'UnsignedEReal',
233 'SignedEReal', 236 'SignedEReal',
234 'Real', 237 'Real',
235 238
236 'Word', 239 'Word',
237 'DropEmpty', 240 'DropEmpty',
238 'Literals', 241 'Literals',
239 'String', 242 'String',
240 'SingleLineString', 243 'SingleLineString',
241 'SkipString', 244 'SkipString',
242 'SkipTo', 245 'SkipTo',
243 'GREEDY', 246 'GREEDY',
244 'NON_GREEDY', 247 'NON_GREEDY',
245 'DEPTH_FIRST', 248 'DEPTH_FIRST',
246 'BREADTH_FIRST', 249 'BREADTH_FIRST',
247 'REDUCE', 250 'REDUCE',
248 251
249 # lepl.matchers.complex 252 # lepl.matchers.complex
250 'PostMatch', 253 'PostMatch',
251 'Columns', 254 'Columns',
252 255
253 # lepl.matchers.support 256 # lepl.matchers.support
254 'function_matcher', 257 'function_matcher',
255 'function_matcher_factory', 258 'function_matcher_factory',
256 'sequence_matcher', 259 'sequence_matcher',
257 'sequence_matcher_factory', 260 'sequence_matcher_factory',
258 'trampoline_matcher', 261 'trampoline_matcher',
259 'trampoline_matcher_factory', 262 'trampoline_matcher_factory',
260 263
261 # lepl.matchers.transform 264 # lepl.matchers.transform
262 'PostCondition', 265 'PostCondition',
263 266
264 # lepl.matchers.variables 267 # lepl.matchers.variables
265 'TraceVariables', 268 'TraceVariables',
266 269
267 # lepl.stream.stream 270 # lepl.stream.stream
268 'DEFAULT_STREAM_FACTORY', 271 'DEFAULT_STREAM_FACTORY',
269 272
270 # lepl.matchers.operators 273 # lepl.matchers.operators
271 'Override', 274 'Override',
272 'Separator', 275 'Separator',
273 'SmartSeparator1', 276 'SmartSeparator1',
274 'DroppedSpace', 277 'DroppedSpace',
275 278
276 # lepl.support.node 279 # lepl.support.node
277 'Node', 280 'Node',
278 'make_dict', 281 'make_dict',
279 'join_with', 282 'join_with',
280 'node_throw', 283 'node_throw',
281 284
282 # lepl.support.list 285 # lepl.support.list
283 'List', 286 'List',
284 'sexpr_fold', 287 'sexpr_fold',
285 'sexpr_throw', 288 'sexpr_throw',
286 289
287 # lepl.lexer.matchers 290 # lepl.lexer.matchers
288 'Token', 291 'Token',
289 'LexerError', 292 'LexerError',
290 'RuntimeLexerError', 293 'RuntimeLexerError',
291 294
292 # lepl.core.manager 295 # lepl.core.manager
293 'GeneratorManager', 296 'GeneratorManager',
294 297
295 # lepl.core.trace 298 # lepl.core.trace
296 'RecordDeepest', 299 'RecordDeepest',
297 'TraceStack', 300 'TraceStack',
298 301
299 # lepl.core.memo, 302 # lepl.core.memo,
300 'RMemo', 303 'RMemo',
301 'LMemo', 304 'LMemo',
302 'MemoException', 305 'MemoException',
303 306
304 # lepl.regexp.core 307 # lepl.regexp.core
305 'RegexpError', 308 'RegexpError',
306 309
307 # lepl.regexp.matchers 310 # lepl.regexp.matchers
308 'NfaRegexp', 311 'NfaRegexp',
309 'DfaRegexp', 312 'DfaRegexp',
310 313
311 # lepl.regexp.unicode 314 # lepl.regexp.unicode
312 'UnicodeAlphabet', 315 'UnicodeAlphabet',
313 316
314 # lepl.stream.core 317 # lepl.stream.core
315 's_debug', 318 's_debug',
316 's_deepest', 319 's_deepest',
317 's_delta', 320 's_delta',
318 's_empty', 321 's_empty',
319 's_eq', 322 's_eq',
320 's_factory', 323 's_factory',
321 's_fmt', 324 's_fmt',
322 's_global_kargs', 325 's_global_kargs',
323 's_id', 326 's_id',
324 's_join', 327 's_join',
325 's_kargs', 328 's_kargs',
326 's_key', 329 's_key',
327 's_len', 330 's_len',
328 's_line', 331 's_line',
329 's_max', 332 's_max',
330 's_next', 333 's_next',
331 's_stream', 334 's_stream',
332 335
333 # lepl.stream.maxdepth 336 # lepl.stream.maxdepth
334 'FullFirstMatchException', 337 'FullFirstMatchException',
335 338
336 # lepl.lexer.lines.matchers 339 # # lepl.lexer.lines.matchers
340 # 'LineStart',
341 # 'LineEnd',
342 # 'Line',
343 # 'ContinuedLineFactory',
344 # 'Extend',
345 #
346 # # lepl.lexer.blocks.matchers
347 # 'Block',
348 # 'Indent',
349 # 'BLine',
350 # 'NO_BLOCKS',
351 # 'ContinuedBLineFactory',
352 # 'BExtend',
353 # 'explicit',
354 # 'constant_indent',
355 # 'to_right',
356
357 # lepl.lexer.offside.matchers
337 'LineStart', 358 'LineStart',
338 'LineEnd', 359 'LineEnd',
339 'Line', 360 'Line',
340 'ContinuedLineFactory', 361 'ContinuedLineFactory',
341 'Extend', 362 'Extend',
342
343 # lepl.lexer.blocks.matchers
344 'Block', 363 'Block',
345 'Indent',
346 'BLine',
347 'NO_BLOCKS', 364 'NO_BLOCKS',
348 'ContinuedBLineFactory', 365 'DEFAULT_POLICY',
349 'BExtend',
350 'explicit', 366 'explicit',
351 'constant_indent', 367 'constant_indent',
352 'to_right', 368 'to_right',
353 369
354 # lepl.support.timer 370 # lepl.support.timer
355 'print_timing' 371 'print_timing'
356 ] 372 ]
357 373
358 __version__ = '5.0.0' 374 __version__ = '5.0.0'
359 375
360 if __version__.find('b') > -1: 376 if __version__.find('b') > -1:
361 from logging import getLogger, basicConfig, WARN 377 from logging import getLogger, basicConfig, WARN
362 #basicConfig(level=WARN) 378 #basicConfig(level=WARN)
363 getLogger('lepl').warn('You are using a BETA version of LEPL.') 379 getLogger('lepl').warn('You are using a BETA version of LEPL.')
Powered by Google Project Hosting