My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones  
Changes to /src/lepl/__init__.py
431aadb3c826 vs. 7078985aebe3 Compare: vs.  Format:
Revision 7078985aebe3
Go to: 
Project members, sign in to write a code review
/src/lepl/__init__.py   431aadb3c826 /src/lepl/__init__.py   7078985aebe3
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, Iterate 120 from lepl.matchers.complex import PostMatch, Columns, Iterate
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, Transform, Assert 137 from lepl.matchers.transform import PostCondition, Transform, Assert
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 Block, Line, LineStart, LineEnd, \ 141 from lepl.lexer.lines.matchers import Block, Line, LineStart, LineEnd, \
142 constant_indent, explicit, to_right, ContinuedLineFactory, Extend, \ 142 constant_indent, explicit, to_right, ContinuedLineFactory, Extend, \
143 NO_BLOCKS, DEFAULT_POLICY 143 NO_BLOCKS, DEFAULT_POLICY
144 from lepl.regexp.core import RegexpError 144 from lepl.regexp.core import RegexpError
145 from lepl.regexp.matchers import NfaRegexp, DfaRegexp 145 from lepl.regexp.matchers import NfaRegexp, DfaRegexp
146 from lepl.regexp.unicode import UnicodeAlphabet 146 from lepl.regexp.unicode import UnicodeAlphabet
147 from lepl.stream.core import s_debug, s_deepest, s_delta, s_empty, s_eq, \ 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, \ 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 149 s_len, s_line, s_max, s_next, s_stream
150 from lepl.stream.maxdepth import FullFirstMatchException 150 from lepl.stream.maxdepth import FullFirstMatchException
151 from lepl.stream.factory import DEFAULT_STREAM_FACTORY 151 from lepl.stream.factory import DEFAULT_STREAM_FACTORY
152 from lepl.support.list import List, sexpr_fold, sexpr_throw, sexpr_flatten, \ 152 from lepl.support.list import List, sexpr_fold, sexpr_throw, sexpr_flatten, \
153 sexpr_to_tree 153 sexpr_to_tree
154 from lepl.support.node import Node, make_dict, join_with, node_throw 154 from lepl.support.node import Node, make_dict, join_with, node_throw
155 from lepl.support.timer import print_timing 155 from lepl.support.timer import print_timing
156 156
157 __all__ = [ 157 __all__ = [
158 158
159 # lepl.core.config 159 # lepl.core.config
160 'Configuration', 160 'Configuration',
161 'ConfigBuilder', 161 'ConfigBuilder',
162 162
163 # lepl.contrib.matchers 163 # lepl.contrib.matchers
164 'SmartSeparator2', 164 'SmartSeparator2',
165 165
166 # lepl.matchers.error 166 # lepl.matchers.error
167 'make_error', 167 'make_error',
168 'raise_error', 168 'raise_error',
169 'Error', 169 'Error',
170 170
171 # lepl.matchers.core 171 # lepl.matchers.core
172 'Empty', 172 'Empty',
173 'Repeat', 173 'Repeat',
174 'Join', 174 'Join',
175 'Any', 175 'Any',
176 'Literal', 176 'Literal',
177 'Empty', 177 'Empty',
178 'Lookahead', 178 'Lookahead',
179 'Regexp', 179 'Regexp',
180 180
181 # lepl.matchers.combine 181 # lepl.matchers.combine
182 'And', 182 'And',
183 'Or', 183 'Or',
184 'First', 184 'First',
185 'Difference', 185 'Difference',
186 'Limit', 186 'Limit',
187 187
188 # lepl.matchers.derived 188 # lepl.matchers.derived
189 'Apply', 189 'Apply',
190 'args', 190 'args',
191 'KApply', 191 'KApply',
192 'Delayed', 192 'Delayed',
193 'Trace', 193 'Trace',
194 'AnyBut', 194 'AnyBut',
195 'Optional', 195 'Optional',
196 'Star', 196 'Star',
197 'ZeroOrMore', 197 'ZeroOrMore',
198 'Plus', 198 'Plus',
199 'OneOrMore', 199 'OneOrMore',
200 'Map', 200 'Map',
201 'Add', 201 'Add',
202 'Drop', 202 'Drop',
203 'Substitute', 203 'Substitute',
204 'Name', 204 'Name',
205 'Eof', 205 'Eof',
206 'Eos', 206 'Eos',
207 'Identity', 207 'Identity',
208 'Newline', 208 'Newline',
209 'Space', 209 'Space',
210 'Whitespace', 210 'Whitespace',
211 'Digit', 211 'Digit',
212 'Letter', 212 'Letter',
213 'Upper', 213 'Upper',
214 'Lower', 214 'Lower',
215 'Printable', 215 'Printable',
216 'Punctuation', 216 'Punctuation',
217 217
218 'UnsignedInteger', 218 'UnsignedInteger',
219 'SignedInteger', 219 'SignedInteger',
220 'Integer', 220 'Integer',
221 221
222 # float matchers exclude integers 222 # float matchers exclude integers
223 'UnsignedFloat', 223 'UnsignedFloat',
224 'SignedFloat', 224 'SignedFloat',
225 'UnsignedEFloat', 225 'UnsignedEFloat',
226 'SignedEFloat', 226 'SignedEFloat',
227 'Float', 227 'Float',
228 228
229 # real matchers match both floats and integers 229 # real matchers match both floats and integers
230 'UnsignedReal', 230 'UnsignedReal',
231 'SignedReal', 231 'SignedReal',
232 'UnsignedEReal', 232 'UnsignedEReal',
233 'SignedEReal', 233 'SignedEReal',
234 'Real', 234 'Real',
235 235
236 'Word', 236 'Word',
237 'DropEmpty', 237 'DropEmpty',
238 'Literals', 238 'Literals',
239 'String', 239 'String',
240 'SingleLineString', 240 'SingleLineString',
241 'SkipString', 241 'SkipString',
242 'SkipTo', 242 'SkipTo',
243 'GREEDY', 243 'GREEDY',
244 'NON_GREEDY', 244 'NON_GREEDY',
245 'DEPTH_FIRST', 245 'DEPTH_FIRST',
246 'BREADTH_FIRST', 246 'BREADTH_FIRST',
247 'REDUCE', 247 'REDUCE',
248 248
249 # lepl.matchers.complex 249 # lepl.matchers.complex
250 'PostMatch', 250 'PostMatch',
251 'Columns', 251 'Columns',
252 'Iterate', 252 'Iterate',
253 253
254 # lepl.matchers.support 254 # lepl.matchers.support
255 'function_matcher', 255 'function_matcher',
256 'function_matcher_factory', 256 'function_matcher_factory',
257 'sequence_matcher', 257 'sequence_matcher',
258 'sequence_matcher_factory', 258 'sequence_matcher_factory',
259 'trampoline_matcher', 259 'trampoline_matcher',
260 'trampoline_matcher_factory', 260 'trampoline_matcher_factory',
261 261
262 # lepl.matchers.transform 262 # lepl.matchers.transform
263 'PostCondition', 263 'PostCondition',
264 'Transform', 264 'Transform',
265 'Assert', 265 'Assert',
266 266
267 # lepl.matchers.variables 267 # lepl.matchers.variables
268 'TraceVariables', 268 'TraceVariables',
269 269
270 # lepl.stream.stream 270 # lepl.stream.stream
271 'DEFAULT_STREAM_FACTORY', 271 'DEFAULT_STREAM_FACTORY',
272 272
273 # lepl.matchers.operators 273 # lepl.matchers.operators
274 'Override', 274 'Override',
275 'Separator', 275 'Separator',
276 'SmartSeparator1', 276 'SmartSeparator1',
277 'DroppedSpace', 277 'DroppedSpace',
278 278
279 # lepl.support.node 279 # lepl.support.node
280 'Node', 280 'Node',
281 'make_dict', 281 'make_dict',
282 'join_with', 282 'join_with',
283 'node_throw', 283 'node_throw',
284 284
285 # lepl.support.list 285 # lepl.support.list
286 'List', 286 'List',
287 'sexpr_fold', 287 'sexpr_fold',
288 'sexpr_throw', 288 'sexpr_throw',
289 'sexpr_flatten', 289 'sexpr_flatten',
290 'sexpr_to_tree', 290 'sexpr_to_tree',
291 291
292 # lepl.lexer.matchers 292 # lepl.lexer.matchers
293 'Token', 293 'Token',
294 'LexerError', 294 'LexerError',
295 'RuntimeLexerError', 295 'RuntimeLexerError',
296 296
297 # lepl.core.manager 297 # lepl.core.manager
298 'GeneratorManager', 298 'GeneratorManager',
299 299
300 # lepl.core.trace 300 # lepl.core.trace
301 'RecordDeepest', 301 'RecordDeepest',
302 'TraceStack', 302 'TraceStack',
303 303
304 # lepl.core.memo, 304 # lepl.core.memo,
305 'RMemo', 305 'RMemo',
306 'LMemo', 306 'LMemo',
307 'MemoException', 307 'MemoException',
308 308
309 # lepl.regexp.core 309 # lepl.regexp.core
310 'RegexpError', 310 'RegexpError',
311 311
312 # lepl.regexp.matchers 312 # lepl.regexp.matchers
313 'NfaRegexp', 313 'NfaRegexp',
314 'DfaRegexp', 314 'DfaRegexp',
315 315
316 # lepl.regexp.unicode 316 # lepl.regexp.unicode
317 'UnicodeAlphabet', 317 'UnicodeAlphabet',
318 318
319 # lepl.stream.core 319 # lepl.stream.core
320 's_debug', 320 's_debug',
321 's_deepest', 321 's_deepest',
322 's_delta', 322 's_delta',
323 's_empty', 323 's_empty',
324 's_eq', 324 's_eq',
325 's_factory', 325 's_factory',
326 's_fmt', 326 's_fmt',
327 's_global_kargs', 327 's_global_kargs',
328 's_id', 328 's_id',
329 's_join', 329 's_join',
330 's_kargs', 330 's_kargs',
331 's_key', 331 's_key',
332 's_len', 332 's_len',
333 's_line', 333 's_line',
334 's_max', 334 's_max',
335 's_next', 335 's_next',
336 's_stream', 336 's_stream',
337 337
338 # lepl.stream.maxdepth 338 # lepl.stream.maxdepth
339 'FullFirstMatchException', 339 'FullFirstMatchException',
340 340
341 # lepl.lexer.lines.matchers 341 # lepl.lexer.lines.matchers
342 'LineStart', 342 'LineStart',
343 'LineEnd', 343 'LineEnd',
344 'Line', 344 'Line',
345 'ContinuedLineFactory', 345 'ContinuedLineFactory',
346 'Extend', 346 'Extend',
347 'Block', 347 'Block',
348 'NO_BLOCKS', 348 'NO_BLOCKS',
349 'DEFAULT_POLICY', 349 'DEFAULT_POLICY',
350 'explicit', 350 'explicit',
351 'constant_indent', 351 'constant_indent',
352 'to_right', 352 'to_right',
353 353
354 # lepl.support.timer 354 # lepl.support.timer
355 'print_timing' 355 'print_timing'
356 ] 356 ]
357 357
358 __version__ = '5.0.1' 358 __version__ = '5.0.2'
359 359
360 if __version__.find('b') > -1: 360 if __version__.find('b') > -1:
361 from logging import getLogger, basicConfig, WARN 361 from logging import getLogger, basicConfig, WARN
362 #basicConfig(level=WARN) 362 #basicConfig(level=WARN)
363 getLogger('lepl').warn('You are using a BETA version of LEPL.') 363 getLogger('lepl').warn('You are using a BETA version of LEPL.')
Powered by Google Project Hosting