Package lepl
[hide private]
[frames] | no frames]

Source Code for Package lepl

  1   
  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 
  4  # in compliance with the License. You may obtain a copy of the License 
  5  # at http://www.mozilla.org/MPL/ 
  6  # 
  7  # Software distributed under the License is distributed on an "AS IS" 
  8  # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 
  9  # the License for the specific language governing rights and 
 10  # limitations under the License. 
 11  # 
 12  # The Original Code is LEPL (http://www.acooke.org/lepl) 
 13  # The Initial Developer of the Original Code is Andrew Cooke. 
 14  # Portions created by the Initial Developer are Copyright (C) 2009-2010 
 15  # Andrew Cooke (andrew@acooke.org). All Rights Reserved. 
 16  # 
 17  # Alternatively, the contents of this file may be used under the terms 
 18  # of the LGPL license (the GNU Lesser General Public License, 
 19  # http://www.gnu.org/licenses/lgpl.html), in which case the provisions 
 20  # of the LGPL License are applicable instead of those above. 
 21  # 
 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 
 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 
 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 
 28  # MPL or the LGPL License. 
 29   
 30  #@PydevCodeAnalysisIgnore 
 31  # pylint: disable-msg=C0301, E0611, W0401 
 32  # confused by __init__? 
 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          # lepl.core.config 
160          'Configuration', 
161          'ConfigBuilder', 
162           
163          # lepl.contrib.matchers 
164          'SmartSeparator2', 
165           
166          # lepl.matchers.error 
167          'make_error', 
168          'raise_error', 
169          'Error', 
170           
171          # lepl.matchers.core 
172          'Empty', 
173          'Repeat', 
174          'Join', 
175          'Any', 
176          'Literal', 
177          'Empty', 
178          'Lookahead', 
179          'Regexp',  
180           
181          # lepl.matchers.combine 
182          'And', 
183          'Or', 
184          'First', 
185          'Difference', 
186          'Limit', 
187           
188          # lepl.matchers.derived 
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          # float matchers exclude integers 
223          'UnsignedFloat',  
224          'SignedFloat',  
225          'UnsignedEFloat', 
226          'SignedEFloat',  
227          'Float', 
228            
229          # real matchers match both floats and integers 
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          # lepl.matchers.complex 
250          'PostMatch', 
251          'Columns', 
252          'Iterate', 
253   
254          # lepl.matchers.support 
255          'function_matcher',  
256          'function_matcher_factory', 
257          'sequence_matcher',  
258          'sequence_matcher_factory', 
259          'trampoline_matcher',  
260          'trampoline_matcher_factory', 
261           
262          # lepl.matchers.transform 
263          'PostCondition', 
264          'Transform', 
265          'Assert', 
266           
267          # lepl.matchers.variables 
268          'TraceVariables', 
269           
270          # lepl.stream.stream 
271          'DEFAULT_STREAM_FACTORY', 
272           
273          # lepl.matchers.operators 
274          'Override', 
275          'Separator', 
276          'SmartSeparator1', 
277          'DroppedSpace', 
278           
279          # lepl.support.node 
280          'Node', 
281          'make_dict', 
282          'join_with', 
283          'node_throw', 
284           
285          # lepl.support.list 
286          'List', 
287          'sexpr_fold', 
288          'sexpr_throw', 
289          'sexpr_flatten', 
290          'sexpr_to_tree', 
291           
292          # lepl.lexer.matchers 
293          'Token', 
294          'LexerError', 
295          'RuntimeLexerError', 
296           
297          # lepl.core.manager 
298          'GeneratorManager', 
299           
300          # lepl.core.trace 
301          'RecordDeepest', 
302          'TraceStack', 
303           
304          # lepl.core.memo, 
305          'RMemo', 
306          'LMemo', 
307          'MemoException', 
308           
309          # lepl.regexp.core 
310          'RegexpError', 
311           
312          # lepl.regexp.matchers 
313          'NfaRegexp', 
314          'DfaRegexp', 
315           
316          # lepl.regexp.unicode 
317          'UnicodeAlphabet', 
318           
319          # lepl.stream.core 
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          # lepl.stream.maxdepth 
339          'FullFirstMatchException', 
340           
341          # lepl.lexer.lines.matchers 
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          # lepl.support.timer 
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      #basicConfig(level=WARN) 
363      getLogger('lepl').warn('You are using a BETA version of LEPL.') 
364