annotate pyparsing_py3.py @ 358:70fead908abe

add info re: assembla page
author catherine@Drou
date Thu, 18 Feb 2010 00:10:25 -0500
parents 12010fcc4e38
children
rev   line source
342
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1 # module pyparsing.py
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2 #
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3 # Copyright (c) 2003-2009 Paul T. McGuire
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
4 #
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
5 # Permission is hereby granted, free of charge, to any person obtaining
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
6 # a copy of this software and associated documentation files (the
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
7 # "Software"), to deal in the Software without restriction, including
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
8 # without limitation the rights to use, copy, modify, merge, publish,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
9 # distribute, sublicense, and/or sell copies of the Software, and to
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
10 # permit persons to whom the Software is furnished to do so, subject to
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
11 # the following conditions:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
12 #
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
13 # The above copyright notice and this permission notice shall be
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
14 # included in all copies or substantial portions of the Software.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
15 #
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
19 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
20 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
21 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
22 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
23 #
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
24 #from __future__ import generators
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
25
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
26 __doc__ = \
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
27 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
28 pyparsing module - Classes and methods to define and execute parsing grammars
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
29
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
30 The pyparsing module is an alternative approach to creating and executing simple grammars,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
31 vs. the traditional lex/yacc approach, or the use of regular expressions. With pyparsing, you
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
32 don't need to learn a new syntax for defining grammars or matching expressions - the parsing module
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
33 provides a library of classes that you use to construct the grammar directly in Python.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
34
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
35 Here is a program to parse "Hello, World!" (or any greeting of the form "<salutation>, <addressee>!")::
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
36
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
37 from pyparsing_py3 import Word, alphas
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
38
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
39 # define grammar of a greeting
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
40 greet = Word( alphas ) + "," + Word( alphas ) + "!"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
41
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
42 hello = "Hello, World!"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
43 print hello, "->", greet.parseString( hello )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
44
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
45 The program outputs the following::
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
46
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
47 Hello, World! -> ['Hello', ',', 'World', '!']
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
48
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
49 The Python representation of the grammar is quite readable, owing to the self-explanatory
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
50 class names, and the use of '+', '|' and '^' operators.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
51
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
52 The parsed results returned from parseString() can be accessed as a nested list, a dictionary, or an
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
53 object with named attributes.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
54
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
55 The pyparsing module handles some of the problems that are typically vexing when writing text parsers:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
56 - extra or missing whitespace (the above program will also handle "Hello,World!", "Hello , World !", etc.)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
57 - quoted strings
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
58 - embedded comments
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
59 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
60
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
61 __version__ = "1.5.2.Py3"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
62 __versionTime__ = "9 April 2009 12:21"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
63 __author__ = "Paul McGuire <ptmcg@users.sourceforge.net>"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
64
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
65 import string
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
66 from weakref import ref as wkref
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
67 import copy
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
68 import sys
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
69 import warnings
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
70 import re
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
71 import sre_constants
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
72 #~ sys.stderr.write( "testing pyparsing module, version %s, %s\n" % (__version__,__versionTime__ ) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
73
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
74 __all__ = [
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
75 'And', 'CaselessKeyword', 'CaselessLiteral', 'CharsNotIn', 'Combine', 'Dict', 'Each', 'Empty',
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
76 'FollowedBy', 'Forward', 'GoToColumn', 'Group', 'Keyword', 'LineEnd', 'LineStart', 'Literal',
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
77 'MatchFirst', 'NoMatch', 'NotAny', 'OneOrMore', 'OnlyOnce', 'Optional', 'Or',
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
78 'ParseBaseException', 'ParseElementEnhance', 'ParseException', 'ParseExpression', 'ParseFatalException',
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
79 'ParseResults', 'ParseSyntaxException', 'ParserElement', 'QuotedString', 'RecursiveGrammarException',
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
80 'Regex', 'SkipTo', 'StringEnd', 'StringStart', 'Suppress', 'Token', 'TokenConverter', 'Upcase',
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
81 'White', 'Word', 'WordEnd', 'WordStart', 'ZeroOrMore',
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
82 'alphanums', 'alphas', 'alphas8bit', 'anyCloseTag', 'anyOpenTag', 'cStyleComment', 'col',
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
83 'commaSeparatedList', 'commonHTMLEntity', 'countedArray', 'cppStyleComment', 'dblQuotedString',
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
84 'dblSlashComment', 'delimitedList', 'dictOf', 'downcaseTokens', 'empty', 'getTokensEndLoc', 'hexnums',
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
85 'htmlComment', 'javaStyleComment', 'keepOriginalText', 'line', 'lineEnd', 'lineStart', 'lineno',
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
86 'makeHTMLTags', 'makeXMLTags', 'matchOnlyAtCol', 'matchPreviousExpr', 'matchPreviousLiteral',
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
87 'nestedExpr', 'nullDebugAction', 'nums', 'oneOf', 'opAssoc', 'operatorPrecedence', 'printables',
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
88 'punc8bit', 'pythonStyleComment', 'quotedString', 'removeQuotes', 'replaceHTMLEntity',
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
89 'replaceWith', 'restOfLine', 'sglQuotedString', 'srange', 'stringEnd',
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
90 'stringStart', 'traceParseAction', 'unicodeString', 'upcaseTokens', 'withAttribute',
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
91 'indentedBlock', 'originalTextFor',
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
92 ]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
93
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
94 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
95 Detect if we are running version 3.X and make appropriate changes
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
96 Robert A. Clark
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
97 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
98 _PY3K = sys.version_info[0] > 2
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
99 if _PY3K:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
100 _MAX_INT = sys.maxsize
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
101 basestring = str
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
102 unichr = chr
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
103 _ustr = str
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
104 _str2dict = set
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
105 alphas = string.ascii_lowercase + string.ascii_uppercase
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
106 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
107 _MAX_INT = sys.maxint
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
108
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
109 def _ustr(obj):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
110 """Drop-in replacement for str(obj) that tries to be Unicode friendly. It first tries
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
111 str(obj). If that fails with a UnicodeEncodeError, then it tries unicode(obj). It
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
112 then < returns the unicode object | encodes it with the default encoding | ... >.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
113 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
114 if isinstance(obj,unicode):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
115 return obj
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
116
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
117 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
118 # If this works, then _ustr(obj) has the same behaviour as str(obj), so
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
119 # it won't break any existing code.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
120 return str(obj)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
121
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
122 except UnicodeEncodeError:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
123 # The Python docs (http://docs.python.org/ref/customization.html#l2h-182)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
124 # state that "The return value must be a string object". However, does a
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
125 # unicode object (being a subclass of basestring) count as a "string
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
126 # object"?
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
127 # If so, then return a unicode object:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
128 return unicode(obj)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
129 # Else encode it... but how? There are many choices... :)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
130 # Replace unprintables with escape codes?
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
131 #return unicode(obj).encode(sys.getdefaultencoding(), 'backslashreplace_errors')
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
132 # Replace unprintables with question marks?
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
133 #return unicode(obj).encode(sys.getdefaultencoding(), 'replace')
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
134 # ...
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
135
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
136 def _str2dict(strg):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
137 return dict( [(c,0) for c in strg] )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
138
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
139 alphas = string.lowercase + string.uppercase
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
140
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
141
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
142 def _xml_escape(data):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
143 """Escape &, <, >, ", ', etc. in a string of data."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
144
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
145 # ampersand must be replaced first
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
146 from_symbols = '&><"\''
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
147 to_symbols = ['&'+s+';' for s in "amp gt lt quot apos".split()]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
148 for from_,to_ in zip(from_symbols, to_symbols):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
149 data = data.replace(from_, to_)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
150 return data
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
151
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
152 class _Constants(object):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
153 pass
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
154
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
155 nums = string.digits
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
156 hexnums = nums + "ABCDEFabcdef"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
157 alphanums = alphas + nums
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
158 _bslash = chr(92)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
159 printables = "".join( [ c for c in string.printable if c not in string.whitespace ] )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
160
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
161 class ParseBaseException(Exception):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
162 """base exception class for all parsing runtime exceptions"""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
163 # Performance tuning: we construct a *lot* of these, so keep this
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
164 # constructor as small and fast as possible
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
165 def __init__( self, pstr, loc=0, msg=None, elem=None ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
166 self.loc = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
167 if msg is None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
168 self.msg = pstr
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
169 self.pstr = ""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
170 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
171 self.msg = msg
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
172 self.pstr = pstr
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
173 self.parserElement = elem
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
174
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
175 def __getattr__( self, aname ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
176 """supported attributes by name are:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
177 - lineno - returns the line number of the exception text
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
178 - col - returns the column number of the exception text
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
179 - line - returns the line containing the exception text
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
180 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
181 if( aname == "lineno" ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
182 return lineno( self.loc, self.pstr )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
183 elif( aname in ("col", "column") ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
184 return col( self.loc, self.pstr )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
185 elif( aname == "line" ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
186 return line( self.loc, self.pstr )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
187 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
188 raise AttributeError(aname)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
189
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
190 def __str__( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
191 return "%s (at char %d), (line:%d, col:%d)" % \
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
192 ( self.msg, self.loc, self.lineno, self.column )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
193 def __repr__( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
194 return _ustr(self)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
195 def markInputline( self, markerString = ">!<" ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
196 """Extracts the exception line from the input string, and marks
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
197 the location of the exception with a special symbol.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
198 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
199 line_str = self.line
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
200 line_column = self.column - 1
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
201 if markerString:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
202 line_str = "".join( [line_str[:line_column],
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
203 markerString, line_str[line_column:]])
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
204 return line_str.strip()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
205 def __dir__(self):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
206 return "loc msg pstr parserElement lineno col line " \
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
207 "markInputLine __str__ __repr__".split()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
208
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
209 class ParseException(ParseBaseException):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
210 """exception thrown when parse expressions don't match class;
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
211 supported attributes by name are:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
212 - lineno - returns the line number of the exception text
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
213 - col - returns the column number of the exception text
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
214 - line - returns the line containing the exception text
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
215 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
216 pass
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
217
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
218 class ParseFatalException(ParseBaseException):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
219 """user-throwable exception thrown when inconsistent parse content
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
220 is found; stops all parsing immediately"""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
221 pass
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
222
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
223 class ParseSyntaxException(ParseFatalException):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
224 """just like ParseFatalException, but thrown internally when an
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
225 ErrorStop indicates that parsing is to stop immediately because
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
226 an unbacktrackable syntax error has been found"""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
227 def __init__(self, pe):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
228 super(ParseSyntaxException, self).__init__(
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
229 pe.pstr, pe.loc, pe.msg, pe.parserElement)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
230
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
231 #~ class ReparseException(ParseBaseException):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
232 #~ """Experimental class - parse actions can raise this exception to cause
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
233 #~ pyparsing to reparse the input string:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
234 #~ - with a modified input string, and/or
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
235 #~ - with a modified start location
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
236 #~ Set the values of the ReparseException in the constructor, and raise the
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
237 #~ exception in a parse action to cause pyparsing to use the new string/location.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
238 #~ Setting the values as None causes no change to be made.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
239 #~ """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
240 #~ def __init_( self, newstring, restartLoc ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
241 #~ self.newParseText = newstring
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
242 #~ self.reparseLoc = restartLoc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
243
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
244 class RecursiveGrammarException(Exception):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
245 """exception thrown by validate() if the grammar could be improperly recursive"""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
246 def __init__( self, parseElementList ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
247 self.parseElementTrace = parseElementList
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
248
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
249 def __str__( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
250 return "RecursiveGrammarException: %s" % self.parseElementTrace
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
251
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
252 class _ParseResultsWithOffset(object):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
253 def __init__(self,p1,p2):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
254 self.tup = (p1,p2)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
255 def __getitem__(self,i):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
256 return self.tup[i]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
257 def __repr__(self):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
258 return repr(self.tup)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
259 def setOffset(self,i):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
260 self.tup = (self.tup[0],i)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
261
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
262 class ParseResults(object):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
263 """Structured parse results, to provide multiple means of access to the parsed data:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
264 - as a list (len(results))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
265 - by list index (results[0], results[1], etc.)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
266 - by attribute (results.<resultsName>)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
267 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
268 __slots__ = ( "__toklist", "__tokdict", "__doinit", "__name", "__parent", "__accumNames", "__weakref__" )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
269 def __new__(cls, toklist, name=None, asList=True, modal=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
270 if isinstance(toklist, cls):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
271 return toklist
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
272 retobj = object.__new__(cls)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
273 retobj.__doinit = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
274 return retobj
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
275
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
276 # Performance tuning: we construct a *lot* of these, so keep this
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
277 # constructor as small and fast as possible
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
278 def __init__( self, toklist, name=None, asList=True, modal=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
279 if self.__doinit:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
280 self.__doinit = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
281 self.__name = None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
282 self.__parent = None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
283 self.__accumNames = {}
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
284 if isinstance(toklist, list):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
285 self.__toklist = toklist[:]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
286 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
287 self.__toklist = [toklist]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
288 self.__tokdict = dict()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
289
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
290 if name:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
291 if not modal:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
292 self.__accumNames[name] = 0
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
293 if isinstance(name,int):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
294 name = _ustr(name) # will always return a str, but use _ustr for consistency
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
295 self.__name = name
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
296 if not toklist in (None,'',[]):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
297 if isinstance(toklist,basestring):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
298 toklist = [ toklist ]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
299 if asList:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
300 if isinstance(toklist,ParseResults):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
301 self[name] = _ParseResultsWithOffset(toklist.copy(),0)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
302 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
303 self[name] = _ParseResultsWithOffset(ParseResults(toklist[0]),0)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
304 self[name].__name = name
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
305 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
306 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
307 self[name] = toklist[0]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
308 except (KeyError,TypeError,IndexError):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
309 self[name] = toklist
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
310
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
311 def __getitem__( self, i ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
312 if isinstance( i, (int,slice) ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
313 return self.__toklist[i]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
314 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
315 if i not in self.__accumNames:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
316 return self.__tokdict[i][-1][0]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
317 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
318 return ParseResults([ v[0] for v in self.__tokdict[i] ])
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
319
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
320 def __setitem__( self, k, v ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
321 if isinstance(v,_ParseResultsWithOffset):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
322 self.__tokdict[k] = self.__tokdict.get(k,list()) + [v]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
323 sub = v[0]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
324 elif isinstance(k,int):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
325 self.__toklist[k] = v
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
326 sub = v
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
327 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
328 self.__tokdict[k] = self.__tokdict.get(k,list()) + [_ParseResultsWithOffset(v,0)]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
329 sub = v
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
330 if isinstance(sub,ParseResults):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
331 sub.__parent = wkref(self)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
332
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
333 def __delitem__( self, i ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
334 if isinstance(i,(int,slice)):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
335 mylen = len( self.__toklist )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
336 del self.__toklist[i]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
337
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
338 # convert int to slice
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
339 if isinstance(i, int):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
340 if i < 0:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
341 i += mylen
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
342 i = slice(i, i+1)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
343 # get removed indices
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
344 removed = list(range(*i.indices(mylen)))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
345 removed.reverse()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
346 # fixup indices in token dictionary
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
347 for name in self.__tokdict:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
348 occurrences = self.__tokdict[name]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
349 for j in removed:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
350 for k, (value, position) in enumerate(occurrences):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
351 occurrences[k] = _ParseResultsWithOffset(value, position - (position > j))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
352 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
353 del self.__tokdict[i]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
354
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
355 def __contains__( self, k ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
356 return k in self.__tokdict
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
357
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
358 def __len__( self ): return len( self.__toklist )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
359 def __bool__(self): return len( self.__toklist ) > 0
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
360 __nonzero__ = __bool__
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
361 def __iter__( self ): return iter( self.__toklist )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
362 def __reversed__( self ): return iter( reversed(self.__toklist) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
363 def keys( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
364 """Returns all named result keys."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
365 return self.__tokdict.keys()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
366
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
367 def pop( self, index=-1 ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
368 """Removes and returns item at specified index (default=last).
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
369 Will work with either numeric indices or dict-key indicies."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
370 ret = self[index]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
371 del self[index]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
372 return ret
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
373
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
374 def get(self, key, defaultValue=None):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
375 """Returns named result matching the given key, or if there is no
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
376 such name, then returns the given defaultValue or None if no
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
377 defaultValue is specified."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
378 if key in self:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
379 return self[key]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
380 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
381 return defaultValue
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
382
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
383 def insert( self, index, insStr ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
384 self.__toklist.insert(index, insStr)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
385 # fixup indices in token dictionary
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
386 for name in self.__tokdict:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
387 occurrences = self.__tokdict[name]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
388 for k, (value, position) in enumerate(occurrences):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
389 occurrences[k] = _ParseResultsWithOffset(value, position + (position > index))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
390
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
391 def items( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
392 """Returns all named result keys and values as a list of tuples."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
393 return [(k,self[k]) for k in self.__tokdict]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
394
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
395 def values( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
396 """Returns all named result values."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
397 return [ v[-1][0] for v in self.__tokdict.values() ]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
398
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
399 def __getattr__( self, name ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
400 if name not in self.__slots__:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
401 if name in self.__tokdict:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
402 if name not in self.__accumNames:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
403 return self.__tokdict[name][-1][0]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
404 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
405 return ParseResults([ v[0] for v in self.__tokdict[name] ])
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
406 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
407 return ""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
408 return None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
409
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
410 def __add__( self, other ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
411 ret = self.copy()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
412 ret += other
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
413 return ret
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
414
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
415 def __iadd__( self, other ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
416 if other.__tokdict:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
417 offset = len(self.__toklist)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
418 addoffset = ( lambda a: (a<0 and offset) or (a+offset) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
419 otheritems = other.__tokdict.items()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
420 otherdictitems = [(k, _ParseResultsWithOffset(v[0],addoffset(v[1])) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
421 for (k,vlist) in otheritems for v in vlist]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
422 for k,v in otherdictitems:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
423 self[k] = v
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
424 if isinstance(v[0],ParseResults):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
425 v[0].__parent = wkref(self)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
426
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
427 self.__toklist += other.__toklist
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
428 self.__accumNames.update( other.__accumNames )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
429 del other
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
430 return self
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
431
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
432 def __repr__( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
433 return "(%s, %s)" % ( repr( self.__toklist ), repr( self.__tokdict ) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
434
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
435 def __str__( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
436 out = "["
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
437 sep = ""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
438 for i in self.__toklist:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
439 if isinstance(i, ParseResults):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
440 out += sep + _ustr(i)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
441 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
442 out += sep + repr(i)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
443 sep = ", "
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
444 out += "]"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
445 return out
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
446
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
447 def _asStringList( self, sep='' ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
448 out = []
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
449 for item in self.__toklist:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
450 if out and sep:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
451 out.append(sep)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
452 if isinstance( item, ParseResults ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
453 out += item._asStringList()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
454 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
455 out.append( _ustr(item) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
456 return out
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
457
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
458 def asList( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
459 """Returns the parse results as a nested list of matching tokens, all converted to strings."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
460 out = []
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
461 for res in self.__toklist:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
462 if isinstance(res,ParseResults):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
463 out.append( res.asList() )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
464 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
465 out.append( res )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
466 return out
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
467
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
468 def asDict( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
469 """Returns the named parse results as dictionary."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
470 return dict( self.items() )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
471
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
472 def copy( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
473 """Returns a new copy of a ParseResults object."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
474 ret = ParseResults( self.__toklist )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
475 ret.__tokdict = self.__tokdict.copy()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
476 ret.__parent = self.__parent
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
477 ret.__accumNames.update( self.__accumNames )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
478 ret.__name = self.__name
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
479 return ret
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
480
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
481 def asXML( self, doctag=None, namedItemsOnly=False, indent="", formatted=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
482 """Returns the parse results as XML. Tags are created for tokens and lists that have defined results names."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
483 nl = "\n"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
484 out = []
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
485 namedItems = dict( [ (v[1],k) for (k,vlist) in self.__tokdict.items()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
486 for v in vlist ] )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
487 nextLevelIndent = indent + " "
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
488
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
489 # collapse out indents if formatting is not desired
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
490 if not formatted:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
491 indent = ""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
492 nextLevelIndent = ""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
493 nl = ""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
494
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
495 selfTag = None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
496 if doctag is not None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
497 selfTag = doctag
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
498 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
499 if self.__name:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
500 selfTag = self.__name
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
501
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
502 if not selfTag:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
503 if namedItemsOnly:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
504 return ""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
505 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
506 selfTag = "ITEM"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
507
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
508 out += [ nl, indent, "<", selfTag, ">" ]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
509
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
510 worklist = self.__toklist
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
511 for i,res in enumerate(worklist):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
512 if isinstance(res,ParseResults):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
513 if i in namedItems:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
514 out += [ res.asXML(namedItems[i],
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
515 namedItemsOnly and doctag is None,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
516 nextLevelIndent,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
517 formatted)]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
518 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
519 out += [ res.asXML(None,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
520 namedItemsOnly and doctag is None,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
521 nextLevelIndent,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
522 formatted)]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
523 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
524 # individual token, see if there is a name for it
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
525 resTag = None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
526 if i in namedItems:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
527 resTag = namedItems[i]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
528 if not resTag:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
529 if namedItemsOnly:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
530 continue
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
531 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
532 resTag = "ITEM"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
533 xmlBodyText = _xml_escape(_ustr(res))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
534 out += [ nl, nextLevelIndent, "<", resTag, ">",
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
535 xmlBodyText,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
536 "</", resTag, ">" ]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
537
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
538 out += [ nl, indent, "</", selfTag, ">" ]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
539 return "".join(out)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
540
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
541 def __lookup(self,sub):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
542 for k,vlist in self.__tokdict.items():
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
543 for v,loc in vlist:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
544 if sub is v:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
545 return k
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
546 return None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
547
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
548 def getName(self):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
549 """Returns the results name for this token expression."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
550 if self.__name:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
551 return self.__name
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
552 elif self.__parent:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
553 par = self.__parent()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
554 if par:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
555 return par.__lookup(self)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
556 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
557 return None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
558 elif (len(self) == 1 and
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
559 len(self.__tokdict) == 1 and
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
560 self.__tokdict.values()[0][0][1] in (0,-1)):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
561 return self.__tokdict.keys()[0]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
562 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
563 return None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
564
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
565 def dump(self,indent='',depth=0):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
566 """Diagnostic method for listing out the contents of a ParseResults.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
567 Accepts an optional indent argument so that this string can be embedded
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
568 in a nested display of other data."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
569 out = []
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
570 out.append( indent+_ustr(self.asList()) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
571 keys = self.items()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
572 keys.sort()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
573 for k,v in keys:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
574 if out:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
575 out.append('\n')
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
576 out.append( "%s%s- %s: " % (indent,(' '*depth), k) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
577 if isinstance(v,ParseResults):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
578 if v.keys():
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
579 out.append( v.dump(indent,depth+1) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
580 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
581 out.append(_ustr(v))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
582 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
583 out.append(_ustr(v))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
584 return "".join(out)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
585
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
586 # add support for pickle protocol
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
587 def __getstate__(self):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
588 return ( self.__toklist,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
589 ( self.__tokdict.copy(),
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
590 self.__parent is not None and self.__parent() or None,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
591 self.__accumNames,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
592 self.__name ) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
593
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
594 def __setstate__(self,state):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
595 self.__toklist = state[0]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
596 self.__tokdict, \
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
597 par, \
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
598 inAccumNames, \
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
599 self.__name = state[1]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
600 self.__accumNames = {}
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
601 self.__accumNames.update(inAccumNames)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
602 if par is not None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
603 self.__parent = wkref(par)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
604 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
605 self.__parent = None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
606
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
607 def __dir__(self):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
608 return dir(super(ParseResults,self)) + self.keys()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
609
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
610 def col (loc,strg):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
611 """Returns current column within a string, counting newlines as line separators.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
612 The first column is number 1.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
613
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
614 Note: the default parsing behavior is to expand tabs in the input string
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
615 before starting the parsing process. See L{I{ParserElement.parseString}<ParserElement.parseString>} for more information
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
616 on parsing strings containing <TAB>s, and suggested methods to maintain a
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
617 consistent view of the parsed string, the parse location, and line and column
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
618 positions within the parsed string.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
619 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
620 return (loc<len(strg) and strg[loc] == '\n') and 1 or loc - strg.rfind("\n", 0, loc)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
621
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
622 def lineno(loc,strg):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
623 """Returns current line number within a string, counting newlines as line separators.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
624 The first line is number 1.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
625
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
626 Note: the default parsing behavior is to expand tabs in the input string
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
627 before starting the parsing process. See L{I{ParserElement.parseString}<ParserElement.parseString>} for more information
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
628 on parsing strings containing <TAB>s, and suggested methods to maintain a
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
629 consistent view of the parsed string, the parse location, and line and column
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
630 positions within the parsed string.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
631 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
632 return strg.count("\n",0,loc) + 1
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
633
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
634 def line( loc, strg ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
635 """Returns the line of text containing loc within a string, counting newlines as line separators.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
636 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
637 lastCR = strg.rfind("\n", 0, loc)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
638 nextCR = strg.find("\n", loc)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
639 if nextCR > 0:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
640 return strg[lastCR+1:nextCR]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
641 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
642 return strg[lastCR+1:]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
643
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
644 def _defaultStartDebugAction( instring, loc, expr ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
645 print ("Match " + _ustr(expr) + " at loc " + _ustr(loc) + "(%d,%d)" % ( lineno(loc,instring), col(loc,instring) ))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
646
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
647 def _defaultSuccessDebugAction( instring, startloc, endloc, expr, toks ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
648 print ("Matched " + _ustr(expr) + " -> " + str(toks.asList()))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
649
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
650 def _defaultExceptionDebugAction( instring, loc, expr, exc ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
651 print ("Exception raised:" + _ustr(exc))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
652
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
653 def nullDebugAction(*args):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
654 """'Do-nothing' debug action, to suppress debugging output during parsing."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
655 pass
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
656
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
657 class ParserElement(object):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
658 """Abstract base level parser element class."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
659 DEFAULT_WHITE_CHARS = " \n\t\r"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
660
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
661 def setDefaultWhitespaceChars( chars ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
662 """Overrides the default whitespace chars
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
663 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
664 ParserElement.DEFAULT_WHITE_CHARS = chars
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
665 setDefaultWhitespaceChars = staticmethod(setDefaultWhitespaceChars)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
666
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
667 def __init__( self, savelist=False ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
668 self.parseAction = list()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
669 self.failAction = None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
670 #~ self.name = "<unknown>" # don't define self.name, let subclasses try/except upcall
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
671 self.strRepr = None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
672 self.resultsName = None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
673 self.saveAsList = savelist
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
674 self.skipWhitespace = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
675 self.whiteChars = ParserElement.DEFAULT_WHITE_CHARS
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
676 self.copyDefaultWhiteChars = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
677 self.mayReturnEmpty = False # used when checking for left-recursion
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
678 self.keepTabs = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
679 self.ignoreExprs = list()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
680 self.debug = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
681 self.streamlined = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
682 self.mayIndexError = True # used to optimize exception handling for subclasses that don't advance parse index
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
683 self.errmsg = ""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
684 self.modalResults = True # used to mark results names as modal (report only last) or cumulative (list all)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
685 self.debugActions = ( None, None, None ) #custom debug actions
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
686 self.re = None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
687 self.callPreparse = True # used to avoid redundant calls to preParse
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
688 self.callDuringTry = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
689
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
690 def copy( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
691 """Make a copy of this ParserElement. Useful for defining different parse actions
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
692 for the same parsing pattern, using copies of the original parse element."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
693 cpy = copy.copy( self )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
694 cpy.parseAction = self.parseAction[:]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
695 cpy.ignoreExprs = self.ignoreExprs[:]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
696 if self.copyDefaultWhiteChars:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
697 cpy.whiteChars = ParserElement.DEFAULT_WHITE_CHARS
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
698 return cpy
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
699
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
700 def setName( self, name ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
701 """Define name for this expression, for use in debugging."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
702 self.name = name
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
703 self.errmsg = "Expected " + self.name
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
704 if hasattr(self,"exception"):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
705 self.exception.msg = self.errmsg
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
706 return self
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
707
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
708 def setResultsName( self, name, listAllMatches=False ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
709 """Define name for referencing matching tokens as a nested attribute
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
710 of the returned parse results.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
711 NOTE: this returns a *copy* of the original ParserElement object;
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
712 this is so that the client can define a basic element, such as an
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
713 integer, and reference it in multiple places with different names.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
714 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
715 newself = self.copy()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
716 newself.resultsName = name
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
717 newself.modalResults = not listAllMatches
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
718 return newself
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
719
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
720 def setBreak(self,breakFlag = True):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
721 """Method to invoke the Python pdb debugger when this element is
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
722 about to be parsed. Set breakFlag to True to enable, False to
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
723 disable.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
724 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
725 if breakFlag:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
726 _parseMethod = self._parse
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
727 def breaker(instring, loc, doActions=True, callPreParse=True):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
728 import pdb
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
729 pdb.set_trace()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
730 return _parseMethod( instring, loc, doActions, callPreParse )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
731 breaker._originalParseMethod = _parseMethod
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
732 self._parse = breaker
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
733 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
734 if hasattr(self._parse,"_originalParseMethod"):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
735 self._parse = self._parse._originalParseMethod
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
736 return self
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
737
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
738 def _normalizeParseActionArgs( f ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
739 """Internal method used to decorate parse actions that take fewer than 3 arguments,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
740 so that all parse actions can be called as f(s,l,t)."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
741 STAR_ARGS = 4
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
742
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
743 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
744 restore = None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
745 if isinstance(f,type):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
746 restore = f
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
747 f = f.__init__
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
748 if not _PY3K:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
749 codeObj = f.func_code
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
750 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
751 codeObj = f.code
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
752 if codeObj.co_flags & STAR_ARGS:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
753 return f
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
754 numargs = codeObj.co_argcount
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
755 if not _PY3K:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
756 if hasattr(f,"im_self"):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
757 numargs -= 1
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
758 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
759 if hasattr(f,"__self__"):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
760 numargs -= 1
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
761 if restore:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
762 f = restore
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
763 except AttributeError:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
764 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
765 if not _PY3K:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
766 call_im_func_code = f.__call__.im_func.func_code
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
767 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
768 call_im_func_code = f.__code__
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
769
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
770 # not a function, must be a callable object, get info from the
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
771 # im_func binding of its bound __call__ method
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
772 if call_im_func_code.co_flags & STAR_ARGS:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
773 return f
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
774 numargs = call_im_func_code.co_argcount
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
775 if not _PY3K:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
776 if hasattr(f.__call__,"im_self"):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
777 numargs -= 1
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
778 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
779 if hasattr(f.__call__,"__self__"):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
780 numargs -= 0
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
781 except AttributeError:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
782 if not _PY3K:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
783 call_func_code = f.__call__.func_code
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
784 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
785 call_func_code = f.__call__.__code__
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
786 # not a bound method, get info directly from __call__ method
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
787 if call_func_code.co_flags & STAR_ARGS:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
788 return f
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
789 numargs = call_func_code.co_argcount
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
790 if not _PY3K:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
791 if hasattr(f.__call__,"im_self"):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
792 numargs -= 1
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
793 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
794 if hasattr(f.__call__,"__self__"):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
795 numargs -= 1
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
796
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
797
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
798 #~ print ("adding function %s with %d args" % (f.func_name,numargs))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
799 if numargs == 3:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
800 return f
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
801 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
802 if numargs > 3:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
803 def tmp(s,l,t):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
804 return f(f.__call__.__self__, s,l,t)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
805 if numargs == 2:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
806 def tmp(s,l,t):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
807 return f(l,t)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
808 elif numargs == 1:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
809 def tmp(s,l,t):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
810 return f(t)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
811 else: #~ numargs == 0:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
812 def tmp(s,l,t):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
813 return f()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
814 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
815 tmp.__name__ = f.__name__
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
816 except (AttributeError,TypeError):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
817 # no need for special handling if attribute doesnt exist
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
818 pass
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
819 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
820 tmp.__doc__ = f.__doc__
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
821 except (AttributeError,TypeError):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
822 # no need for special handling if attribute doesnt exist
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
823 pass
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
824 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
825 tmp.__dict__.update(f.__dict__)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
826 except (AttributeError,TypeError):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
827 # no need for special handling if attribute doesnt exist
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
828 pass
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
829 return tmp
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
830 _normalizeParseActionArgs = staticmethod(_normalizeParseActionArgs)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
831
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
832 def setParseAction( self, *fns, **kwargs ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
833 """Define action to perform when successfully matching parse element definition.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
834 Parse action fn is a callable method with 0-3 arguments, called as fn(s,loc,toks),
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
835 fn(loc,toks), fn(toks), or just fn(), where:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
836 - s = the original string being parsed (see note below)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
837 - loc = the location of the matching substring
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
838 - toks = a list of the matched tokens, packaged as a ParseResults object
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
839 If the functions in fns modify the tokens, they can return them as the return
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
840 value from fn, and the modified list of tokens will replace the original.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
841 Otherwise, fn does not need to return any value.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
842
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
843 Note: the default parsing behavior is to expand tabs in the input string
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
844 before starting the parsing process. See L{I{parseString}<parseString>} for more information
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
845 on parsing strings containing <TAB>s, and suggested methods to maintain a
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
846 consistent view of the parsed string, the parse location, and line and column
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
847 positions within the parsed string.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
848 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
849 self.parseAction = list(map(self._normalizeParseActionArgs, list(fns)))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
850 self.callDuringTry = ("callDuringTry" in kwargs and kwargs["callDuringTry"])
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
851 return self
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
852
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
853 def addParseAction( self, *fns, **kwargs ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
854 """Add parse action to expression's list of parse actions. See L{I{setParseAction}<setParseAction>}."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
855 self.parseAction += list(map(self._normalizeParseActionArgs, list(fns)))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
856 self.callDuringTry = self.callDuringTry or ("callDuringTry" in kwargs and kwargs["callDuringTry"])
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
857 return self
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
858
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
859 def setFailAction( self, fn ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
860 """Define action to perform if parsing fails at this expression.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
861 Fail acton fn is a callable function that takes the arguments
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
862 fn(s,loc,expr,err) where:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
863 - s = string being parsed
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
864 - loc = location where expression match was attempted and failed
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
865 - expr = the parse expression that failed
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
866 - err = the exception thrown
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
867 The function returns no value. It may throw ParseFatalException
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
868 if it is desired to stop parsing immediately."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
869 self.failAction = fn
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
870 return self
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
871
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
872 def _skipIgnorables( self, instring, loc ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
873 exprsFound = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
874 while exprsFound:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
875 exprsFound = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
876 for e in self.ignoreExprs:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
877 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
878 while 1:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
879 loc,dummy = e._parse( instring, loc )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
880 exprsFound = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
881 except ParseException:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
882 pass
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
883 return loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
884
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
885 def preParse( self, instring, loc ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
886 if self.ignoreExprs:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
887 loc = self._skipIgnorables( instring, loc )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
888
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
889 if self.skipWhitespace:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
890 wt = self.whiteChars
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
891 instrlen = len(instring)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
892 while loc < instrlen and instring[loc] in wt:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
893 loc += 1
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
894
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
895 return loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
896
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
897 def parseImpl( self, instring, loc, doActions=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
898 return loc, []
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
899
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
900 def postParse( self, instring, loc, tokenlist ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
901 return tokenlist
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
902
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
903 #~ @profile
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
904 def _parseNoCache( self, instring, loc, doActions=True, callPreParse=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
905 debugging = ( self.debug ) #and doActions )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
906
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
907 if debugging or self.failAction:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
908 #~ print ("Match",self,"at loc",loc,"(%d,%d)" % ( lineno(loc,instring), col(loc,instring) ))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
909 if (self.debugActions[0] ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
910 self.debugActions[0]( instring, loc, self )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
911 if callPreParse and self.callPreparse:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
912 preloc = self.preParse( instring, loc )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
913 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
914 preloc = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
915 tokensStart = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
916 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
917 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
918 loc,tokens = self.parseImpl( instring, preloc, doActions )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
919 except IndexError:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
920 raise ParseException( instring, len(instring), self.errmsg, self )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
921 except ParseBaseException:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
922 #~ print ("Exception raised:", err)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
923 err = None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
924 if self.debugActions[2]:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
925 err = sys.exc_info()[1]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
926 self.debugActions[2]( instring, tokensStart, self, err )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
927 if self.failAction:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
928 if err is None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
929 err = sys.exc_info()[1]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
930 self.failAction( instring, tokensStart, self, err )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
931 raise
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
932 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
933 if callPreParse and self.callPreparse:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
934 preloc = self.preParse( instring, loc )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
935 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
936 preloc = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
937 tokensStart = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
938 if self.mayIndexError or loc >= len(instring):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
939 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
940 loc,tokens = self.parseImpl( instring, preloc, doActions )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
941 except IndexError:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
942 raise ParseException( instring, len(instring), self.errmsg, self )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
943 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
944 loc,tokens = self.parseImpl( instring, preloc, doActions )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
945
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
946 tokens = self.postParse( instring, loc, tokens )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
947
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
948 retTokens = ParseResults( tokens, self.resultsName, asList=self.saveAsList, modal=self.modalResults )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
949 if self.parseAction and (doActions or self.callDuringTry):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
950 if debugging:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
951 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
952 for fn in self.parseAction:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
953 tokens = fn( instring, tokensStart, retTokens )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
954 if tokens is not None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
955 retTokens = ParseResults( tokens,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
956 self.resultsName,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
957 asList=self.saveAsList and isinstance(tokens,(ParseResults,list)),
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
958 modal=self.modalResults )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
959 except ParseBaseException:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
960 #~ print "Exception raised in user parse action:", err
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
961 if (self.debugActions[2] ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
962 err = sys.exc_info()[1]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
963 self.debugActions[2]( instring, tokensStart, self, err )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
964 raise
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
965 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
966 for fn in self.parseAction:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
967 tokens = fn( instring, tokensStart, retTokens )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
968 if tokens is not None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
969 retTokens = ParseResults( tokens,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
970 self.resultsName,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
971 asList=self.saveAsList and isinstance(tokens,(ParseResults,list)),
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
972 modal=self.modalResults )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
973
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
974 if debugging:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
975 #~ print ("Matched",self,"->",retTokens.asList())
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
976 if (self.debugActions[1] ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
977 self.debugActions[1]( instring, tokensStart, loc, self, retTokens )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
978
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
979 return loc, retTokens
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
980
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
981 def tryParse( self, instring, loc ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
982 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
983 return self._parse( instring, loc, doActions=False )[0]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
984 except ParseFatalException:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
985 raise ParseException( instring, loc, self.errmsg, self)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
986
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
987 # this method gets repeatedly called during backtracking with the same arguments -
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
988 # we can cache these arguments and save ourselves the trouble of re-parsing the contained expression
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
989 def _parseCache( self, instring, loc, doActions=True, callPreParse=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
990 lookup = (self,instring,loc,callPreParse,doActions)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
991 if lookup in ParserElement._exprArgCache:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
992 value = ParserElement._exprArgCache[ lookup ]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
993 if isinstance(value,Exception):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
994 raise value
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
995 return value
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
996 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
997 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
998 value = self._parseNoCache( instring, loc, doActions, callPreParse )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
999 ParserElement._exprArgCache[ lookup ] = (value[0],value[1].copy())
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1000 return value
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1001 except ParseBaseException:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1002 pe = sys.exc_info()[1]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1003 ParserElement._exprArgCache[ lookup ] = pe
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1004 raise
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1005
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1006 _parse = _parseNoCache
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1007
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1008 # argument cache for optimizing repeated calls when backtracking through recursive expressions
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1009 _exprArgCache = {}
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1010 def resetCache():
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1011 ParserElement._exprArgCache.clear()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1012 resetCache = staticmethod(resetCache)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1013
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1014 _packratEnabled = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1015 def enablePackrat():
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1016 """Enables "packrat" parsing, which adds memoizing to the parsing logic.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1017 Repeated parse attempts at the same string location (which happens
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1018 often in many complex grammars) can immediately return a cached value,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1019 instead of re-executing parsing/validating code. Memoizing is done of
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1020 both valid results and parsing exceptions.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1021
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1022 This speedup may break existing programs that use parse actions that
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1023 have side-effects. For this reason, packrat parsing is disabled when
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1024 you first import pyparsing_py3 as pyparsing. To activate the packrat feature, your
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1025 program must call the class method ParserElement.enablePackrat(). If
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1026 your program uses psyco to "compile as you go", you must call
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1027 enablePackrat before calling psyco.full(). If you do not do this,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1028 Python will crash. For best results, call enablePackrat() immediately
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1029 after importing pyparsing.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1030 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1031 if not ParserElement._packratEnabled:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1032 ParserElement._packratEnabled = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1033 ParserElement._parse = ParserElement._parseCache
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1034 enablePackrat = staticmethod(enablePackrat)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1035
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1036 def parseString( self, instring, parseAll=False ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1037 """Execute the parse expression with the given string.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1038 This is the main interface to the client code, once the complete
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1039 expression has been built.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1040
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1041 If you want the grammar to require that the entire input string be
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1042 successfully parsed, then set parseAll to True (equivalent to ending
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1043 the grammar with StringEnd()).
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1044
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1045 Note: parseString implicitly calls expandtabs() on the input string,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1046 in order to report proper column numbers in parse actions.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1047 If the input string contains tabs and
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1048 the grammar uses parse actions that use the loc argument to index into the
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1049 string being parsed, you can ensure you have a consistent view of the input
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1050 string by:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1051 - calling parseWithTabs on your grammar before calling parseString
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1052 (see L{I{parseWithTabs}<parseWithTabs>})
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1053 - define your parse action using the full (s,loc,toks) signature, and
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1054 reference the input string using the parse action's s argument
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1055 - explictly expand the tabs in your input string before calling
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1056 parseString
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1057 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1058 ParserElement.resetCache()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1059 if not self.streamlined:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1060 self.streamline()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1061 #~ self.saveAsList = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1062 for e in self.ignoreExprs:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1063 e.streamline()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1064 if not self.keepTabs:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1065 instring = instring.expandtabs()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1066 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1067 loc, tokens = self._parse( instring, 0 )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1068 if parseAll:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1069 loc = self.preParse( instring, loc )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1070 StringEnd()._parse( instring, loc )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1071 except ParseBaseException:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1072 exc = sys.exc_info()[1]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1073 # catch and re-raise exception from here, clears out pyparsing internal stack trace
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1074 raise exc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1075 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1076 return tokens
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1077
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1078 def scanString( self, instring, maxMatches=_MAX_INT ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1079 """Scan the input string for expression matches. Each match will return the
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1080 matching tokens, start location, and end location. May be called with optional
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1081 maxMatches argument, to clip scanning after 'n' matches are found.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1082
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1083 Note that the start and end locations are reported relative to the string
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1084 being parsed. See L{I{parseString}<parseString>} for more information on parsing
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1085 strings with embedded tabs."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1086 if not self.streamlined:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1087 self.streamline()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1088 for e in self.ignoreExprs:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1089 e.streamline()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1090
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1091 if not self.keepTabs:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1092 instring = _ustr(instring).expandtabs()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1093 instrlen = len(instring)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1094 loc = 0
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1095 preparseFn = self.preParse
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1096 parseFn = self._parse
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1097 ParserElement.resetCache()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1098 matches = 0
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1099 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1100 while loc <= instrlen and matches < maxMatches:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1101 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1102 preloc = preparseFn( instring, loc )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1103 nextLoc,tokens = parseFn( instring, preloc, callPreParse=False )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1104 except ParseException:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1105 loc = preloc+1
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1106 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1107 if nextLoc > loc:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1108 matches += 1
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1109 yield tokens, preloc, nextLoc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1110 loc = nextLoc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1111 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1112 loc = preloc+1
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1113 except ParseBaseException:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1114 pe = sys.exc_info()[1]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1115 raise pe
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1116
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1117 def transformString( self, instring ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1118 """Extension to scanString, to modify matching text with modified tokens that may
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1119 be returned from a parse action. To use transformString, define a grammar and
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1120 attach a parse action to it that modifies the returned token list.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1121 Invoking transformString() on a target string will then scan for matches,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1122 and replace the matched text patterns according to the logic in the parse
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1123 action. transformString() returns the resulting transformed string."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1124 out = []
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1125 lastE = 0
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1126 # force preservation of <TAB>s, to minimize unwanted transformation of string, and to
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1127 # keep string locs straight between transformString and scanString
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1128 self.keepTabs = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1129 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1130 for t,s,e in self.scanString( instring ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1131 out.append( instring[lastE:s] )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1132 if t:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1133 if isinstance(t,ParseResults):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1134 out += t.asList()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1135 elif isinstance(t,list):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1136 out += t
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1137 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1138 out.append(t)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1139 lastE = e
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1140 out.append(instring[lastE:])
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1141 return "".join(map(_ustr,out))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1142 except ParseBaseException:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1143 pe = sys.exc_info()[1]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1144 raise pe
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1145
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1146 def searchString( self, instring, maxMatches=_MAX_INT ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1147 """Another extension to scanString, simplifying the access to the tokens found
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1148 to match the given parse expression. May be called with optional
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1149 maxMatches argument, to clip searching after 'n' matches are found.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1150 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1151 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1152 return ParseResults([ t for t,s,e in self.scanString( instring, maxMatches ) ])
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1153 except ParseBaseException:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1154 pe = sys.exc_info()[1]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1155 raise pe
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1156
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1157 def __add__(self, other ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1158 """Implementation of + operator - returns And"""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1159 if isinstance( other, basestring ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1160 other = Literal( other )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1161 if not isinstance( other, ParserElement ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1162 warnings.warn("Cannot combine element of type %s with ParserElement" % type(other),
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1163 SyntaxWarning, stacklevel=2)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1164 return None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1165 return And( [ self, other ] )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1166
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1167 def __radd__(self, other ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1168 """Implementation of + operator when left operand is not a ParserElement"""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1169 if isinstance( other, basestring ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1170 other = Literal( other )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1171 if not isinstance( other, ParserElement ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1172 warnings.warn("Cannot combine element of type %s with ParserElement" % type(other),
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1173 SyntaxWarning, stacklevel=2)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1174 return None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1175 return other + self
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1176
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1177 def __sub__(self, other):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1178 """Implementation of - operator, returns And with error stop"""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1179 if isinstance( other, basestring ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1180 other = Literal( other )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1181 if not isinstance( other, ParserElement ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1182 warnings.warn("Cannot combine element of type %s with ParserElement" % type(other),
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1183 SyntaxWarning, stacklevel=2)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1184 return None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1185 return And( [ self, And._ErrorStop(), other ] )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1186
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1187 def __rsub__(self, other ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1188 """Implementation of - operator when left operand is not a ParserElement"""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1189 if isinstance( other, basestring ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1190 other = Literal( other )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1191 if not isinstance( other, ParserElement ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1192 warnings.warn("Cannot combine element of type %s with ParserElement" % type(other),
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1193 SyntaxWarning, stacklevel=2)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1194 return None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1195 return other - self
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1196
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1197 def __mul__(self,other):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1198 if isinstance(other,int):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1199 minElements, optElements = other,0
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1200 elif isinstance(other,tuple):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1201 other = (other + (None, None))[:2]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1202 if other[0] is None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1203 other = (0, other[1])
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1204 if isinstance(other[0],int) and other[1] is None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1205 if other[0] == 0:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1206 return ZeroOrMore(self)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1207 if other[0] == 1:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1208 return OneOrMore(self)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1209 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1210 return self*other[0] + ZeroOrMore(self)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1211 elif isinstance(other[0],int) and isinstance(other[1],int):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1212 minElements, optElements = other
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1213 optElements -= minElements
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1214 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1215 raise TypeError("cannot multiply 'ParserElement' and ('%s','%s') objects", type(other[0]),type(other[1]))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1216 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1217 raise TypeError("cannot multiply 'ParserElement' and '%s' objects", type(other))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1218
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1219 if minElements < 0:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1220 raise ValueError("cannot multiply ParserElement by negative value")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1221 if optElements < 0:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1222 raise ValueError("second tuple value must be greater or equal to first tuple value")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1223 if minElements == optElements == 0:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1224 raise ValueError("cannot multiply ParserElement by 0 or (0,0)")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1225
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1226 if (optElements):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1227 def makeOptionalList(n):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1228 if n>1:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1229 return Optional(self + makeOptionalList(n-1))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1230 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1231 return Optional(self)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1232 if minElements:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1233 if minElements == 1:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1234 ret = self + makeOptionalList(optElements)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1235 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1236 ret = And([self]*minElements) + makeOptionalList(optElements)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1237 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1238 ret = makeOptionalList(optElements)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1239 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1240 if minElements == 1:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1241 ret = self
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1242 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1243 ret = And([self]*minElements)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1244 return ret
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1245
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1246 def __rmul__(self, other):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1247 return self.__mul__(other)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1248
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1249 def __or__(self, other ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1250 """Implementation of | operator - returns MatchFirst"""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1251 if isinstance( other, basestring ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1252 other = Literal( other )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1253 if not isinstance( other, ParserElement ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1254 warnings.warn("Cannot combine element of type %s with ParserElement" % type(other),
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1255 SyntaxWarning, stacklevel=2)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1256 return None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1257 return MatchFirst( [ self, other ] )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1258
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1259 def __ror__(self, other ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1260 """Implementation of | operator when left operand is not a ParserElement"""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1261 if isinstance( other, basestring ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1262 other = Literal( other )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1263 if not isinstance( other, ParserElement ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1264 warnings.warn("Cannot combine element of type %s with ParserElement" % type(other),
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1265 SyntaxWarning, stacklevel=2)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1266 return None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1267 return other | self
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1268
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1269 def __xor__(self, other ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1270 """Implementation of ^ operator - returns Or"""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1271 if isinstance( other, basestring ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1272 other = Literal( other )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1273 if not isinstance( other, ParserElement ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1274 warnings.warn("Cannot combine element of type %s with ParserElement" % type(other),
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1275 SyntaxWarning, stacklevel=2)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1276 return None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1277 return Or( [ self, other ] )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1278
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1279 def __rxor__(self, other ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1280 """Implementation of ^ operator when left operand is not a ParserElement"""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1281 if isinstance( other, basestring ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1282 other = Literal( other )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1283 if not isinstance( other, ParserElement ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1284 warnings.warn("Cannot combine element of type %s with ParserElement" % type(other),
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1285 SyntaxWarning, stacklevel=2)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1286 return None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1287 return other ^ self
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1288
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1289 def __and__(self, other ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1290 """Implementation of & operator - returns Each"""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1291 if isinstance( other, basestring ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1292 other = Literal( other )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1293 if not isinstance( other, ParserElement ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1294 warnings.warn("Cannot combine element of type %s with ParserElement" % type(other),
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1295 SyntaxWarning, stacklevel=2)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1296 return None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1297 return Each( [ self, other ] )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1298
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1299 def __rand__(self, other ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1300 """Implementation of & operator when left operand is not a ParserElement"""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1301 if isinstance( other, basestring ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1302 other = Literal( other )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1303 if not isinstance( other, ParserElement ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1304 warnings.warn("Cannot combine element of type %s with ParserElement" % type(other),
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1305 SyntaxWarning, stacklevel=2)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1306 return None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1307 return other & self
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1308
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1309 def __invert__( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1310 """Implementation of ~ operator - returns NotAny"""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1311 return NotAny( self )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1312
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1313 def __call__(self, name):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1314 """Shortcut for setResultsName, with listAllMatches=default::
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1315 userdata = Word(alphas).setResultsName("name") + Word(nums+"-").setResultsName("socsecno")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1316 could be written as::
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1317 userdata = Word(alphas)("name") + Word(nums+"-")("socsecno")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1318 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1319 return self.setResultsName(name)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1320
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1321 def suppress( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1322 """Suppresses the output of this ParserElement; useful to keep punctuation from
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1323 cluttering up returned output.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1324 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1325 return Suppress( self )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1326
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1327 def leaveWhitespace( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1328 """Disables the skipping of whitespace before matching the characters in the
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1329 ParserElement's defined pattern. This is normally only used internally by
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1330 the pyparsing module, but may be needed in some whitespace-sensitive grammars.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1331 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1332 self.skipWhitespace = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1333 return self
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1334
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1335 def setWhitespaceChars( self, chars ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1336 """Overrides the default whitespace chars
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1337 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1338 self.skipWhitespace = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1339 self.whiteChars = chars
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1340 self.copyDefaultWhiteChars = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1341 return self
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1342
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1343 def parseWithTabs( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1344 """Overrides default behavior to expand <TAB>s to spaces before parsing the input string.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1345 Must be called before parseString when the input grammar contains elements that
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1346 match <TAB> characters."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1347 self.keepTabs = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1348 return self
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1349
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1350 def ignore( self, other ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1351 """Define expression to be ignored (e.g., comments) while doing pattern
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1352 matching; may be called repeatedly, to define multiple comment or other
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1353 ignorable patterns.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1354 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1355 if isinstance( other, Suppress ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1356 if other not in self.ignoreExprs:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1357 self.ignoreExprs.append( other )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1358 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1359 self.ignoreExprs.append( Suppress( other ) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1360 return self
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1361
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1362 def setDebugActions( self, startAction, successAction, exceptionAction ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1363 """Enable display of debugging messages while doing pattern matching."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1364 self.debugActions = (startAction or _defaultStartDebugAction,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1365 successAction or _defaultSuccessDebugAction,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1366 exceptionAction or _defaultExceptionDebugAction)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1367 self.debug = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1368 return self
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1369
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1370 def setDebug( self, flag=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1371 """Enable display of debugging messages while doing pattern matching.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1372 Set flag to True to enable, False to disable."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1373 if flag:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1374 self.setDebugActions( _defaultStartDebugAction, _defaultSuccessDebugAction, _defaultExceptionDebugAction )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1375 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1376 self.debug = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1377 return self
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1378
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1379 def __str__( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1380 return self.name
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1381
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1382 def __repr__( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1383 return _ustr(self)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1384
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1385 def streamline( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1386 self.streamlined = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1387 self.strRepr = None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1388 return self
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1389
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1390 def checkRecursion( self, parseElementList ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1391 pass
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1392
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1393 def validate( self, validateTrace=[] ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1394 """Check defined expressions for valid structure, check for infinite recursive definitions."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1395 self.checkRecursion( [] )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1396
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1397 def parseFile( self, file_or_filename, parseAll=False ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1398 """Execute the parse expression on the given file or filename.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1399 If a filename is specified (instead of a file object),
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1400 the entire file is opened, read, and closed before parsing.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1401 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1402 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1403 file_contents = file_or_filename.read()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1404 except AttributeError:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1405 f = open(file_or_filename, "rb")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1406 file_contents = f.read()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1407 f.close()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1408 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1409 return self.parseString(file_contents, parseAll)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1410 except ParseBaseException:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1411 # catch and re-raise exception from here, clears out pyparsing internal stack trace
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1412 exc = sys.exc_info()[1]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1413 raise exc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1414
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1415 def getException(self):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1416 return ParseException("",0,self.errmsg,self)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1417
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1418 def __getattr__(self,aname):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1419 if aname == "myException":
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1420 self.myException = ret = self.getException();
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1421 return ret;
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1422 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1423 raise AttributeError("no such attribute " + aname)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1424
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1425 def __eq__(self,other):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1426 if isinstance(other, ParserElement):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1427 return self is other or self.__dict__ == other.__dict__
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1428 elif isinstance(other, basestring):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1429 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1430 self.parseString(_ustr(other), parseAll=True)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1431 return True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1432 except ParseBaseException:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1433 return False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1434 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1435 return super(ParserElement,self)==other
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1436
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1437 def __ne__(self,other):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1438 return not (self == other)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1439
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1440 def __hash__(self):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1441 return hash(id(self))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1442
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1443 def __req__(self,other):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1444 return self == other
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1445
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1446 def __rne__(self,other):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1447 return not (self == other)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1448
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1449
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1450 class Token(ParserElement):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1451 """Abstract ParserElement subclass, for defining atomic matching patterns."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1452 def __init__( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1453 super(Token,self).__init__( savelist=False )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1454 #self.myException = ParseException("",0,"",self)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1455
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1456 def setName(self, name):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1457 s = super(Token,self).setName(name)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1458 self.errmsg = "Expected " + self.name
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1459 #s.myException.msg = self.errmsg
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1460 return s
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1461
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1462
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1463 class Empty(Token):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1464 """An empty token, will always match."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1465 def __init__( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1466 super(Empty,self).__init__()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1467 self.name = "Empty"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1468 self.mayReturnEmpty = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1469 self.mayIndexError = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1470
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1471
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1472 class NoMatch(Token):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1473 """A token that will never match."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1474 def __init__( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1475 super(NoMatch,self).__init__()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1476 self.name = "NoMatch"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1477 self.mayReturnEmpty = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1478 self.mayIndexError = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1479 self.errmsg = "Unmatchable token"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1480 #self.myException.msg = self.errmsg
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1481
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1482 def parseImpl( self, instring, loc, doActions=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1483 exc = self.myException
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1484 exc.loc = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1485 exc.pstr = instring
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1486 raise exc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1487
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1488
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1489 class Literal(Token):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1490 """Token to exactly match a specified string."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1491 def __init__( self, matchString ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1492 super(Literal,self).__init__()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1493 self.match = matchString
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1494 self.matchLen = len(matchString)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1495 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1496 self.firstMatchChar = matchString[0]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1497 except IndexError:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1498 warnings.warn("null string passed to Literal; use Empty() instead",
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1499 SyntaxWarning, stacklevel=2)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1500 self.__class__ = Empty
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1501 self.name = '"%s"' % _ustr(self.match)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1502 self.errmsg = "Expected " + self.name
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1503 self.mayReturnEmpty = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1504 #self.myException.msg = self.errmsg
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1505 self.mayIndexError = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1506
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1507 # Performance tuning: this routine gets called a *lot*
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1508 # if this is a single character match string and the first character matches,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1509 # short-circuit as quickly as possible, and avoid calling startswith
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1510 #~ @profile
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1511 def parseImpl( self, instring, loc, doActions=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1512 if (instring[loc] == self.firstMatchChar and
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1513 (self.matchLen==1 or instring.startswith(self.match,loc)) ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1514 return loc+self.matchLen, self.match
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1515 #~ raise ParseException( instring, loc, self.errmsg )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1516 exc = self.myException
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1517 exc.loc = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1518 exc.pstr = instring
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1519 raise exc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1520 _L = Literal
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1521
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1522 class Keyword(Token):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1523 """Token to exactly match a specified string as a keyword, that is, it must be
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1524 immediately followed by a non-keyword character. Compare with Literal::
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1525 Literal("if") will match the leading 'if' in 'ifAndOnlyIf'.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1526 Keyword("if") will not; it will only match the leading 'if in 'if x=1', or 'if(y==2)'
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1527 Accepts two optional constructor arguments in addition to the keyword string:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1528 identChars is a string of characters that would be valid identifier characters,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1529 defaulting to all alphanumerics + "_" and "$"; caseless allows case-insensitive
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1530 matching, default is False.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1531 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1532 DEFAULT_KEYWORD_CHARS = alphanums+"_$"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1533
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1534 def __init__( self, matchString, identChars=DEFAULT_KEYWORD_CHARS, caseless=False ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1535 super(Keyword,self).__init__()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1536 self.match = matchString
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1537 self.matchLen = len(matchString)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1538 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1539 self.firstMatchChar = matchString[0]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1540 except IndexError:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1541 warnings.warn("null string passed to Keyword; use Empty() instead",
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1542 SyntaxWarning, stacklevel=2)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1543 self.name = '"%s"' % self.match
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1544 self.errmsg = "Expected " + self.name
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1545 self.mayReturnEmpty = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1546 #self.myException.msg = self.errmsg
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1547 self.mayIndexError = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1548 self.caseless = caseless
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1549 if caseless:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1550 self.caselessmatch = matchString.upper()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1551 identChars = identChars.upper()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1552 self.identChars = _str2dict(identChars)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1553
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1554 def parseImpl( self, instring, loc, doActions=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1555 if self.caseless:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1556 if ( (instring[ loc:loc+self.matchLen ].upper() == self.caselessmatch) and
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1557 (loc >= len(instring)-self.matchLen or instring[loc+self.matchLen].upper() not in self.identChars) and
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1558 (loc == 0 or instring[loc-1].upper() not in self.identChars) ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1559 return loc+self.matchLen, self.match
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1560 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1561 if (instring[loc] == self.firstMatchChar and
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1562 (self.matchLen==1 or instring.startswith(self.match,loc)) and
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1563 (loc >= len(instring)-self.matchLen or instring[loc+self.matchLen] not in self.identChars) and
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1564 (loc == 0 or instring[loc-1] not in self.identChars) ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1565 return loc+self.matchLen, self.match
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1566 #~ raise ParseException( instring, loc, self.errmsg )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1567 exc = self.myException
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1568 exc.loc = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1569 exc.pstr = instring
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1570 raise exc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1571
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1572 def copy(self):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1573 c = super(Keyword,self).copy()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1574 c.identChars = Keyword.DEFAULT_KEYWORD_CHARS
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1575 return c
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1576
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1577 def setDefaultKeywordChars( chars ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1578 """Overrides the default Keyword chars
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1579 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1580 Keyword.DEFAULT_KEYWORD_CHARS = chars
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1581 setDefaultKeywordChars = staticmethod(setDefaultKeywordChars)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1582
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1583 class CaselessLiteral(Literal):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1584 """Token to match a specified string, ignoring case of letters.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1585 Note: the matched results will always be in the case of the given
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1586 match string, NOT the case of the input text.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1587 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1588 def __init__( self, matchString ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1589 super(CaselessLiteral,self).__init__( matchString.upper() )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1590 # Preserve the defining literal.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1591 self.returnString = matchString
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1592 self.name = "'%s'" % self.returnString
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1593 self.errmsg = "Expected " + self.name
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1594 #self.myException.msg = self.errmsg
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1595
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1596 def parseImpl( self, instring, loc, doActions=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1597 if instring[ loc:loc+self.matchLen ].upper() == self.match:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1598 return loc+self.matchLen, self.returnString
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1599 #~ raise ParseException( instring, loc, self.errmsg )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1600 exc = self.myException
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1601 exc.loc = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1602 exc.pstr = instring
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1603 raise exc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1604
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1605 class CaselessKeyword(Keyword):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1606 def __init__( self, matchString, identChars=Keyword.DEFAULT_KEYWORD_CHARS ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1607 super(CaselessKeyword,self).__init__( matchString, identChars, caseless=True )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1608
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1609 def parseImpl( self, instring, loc, doActions=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1610 if ( (instring[ loc:loc+self.matchLen ].upper() == self.caselessmatch) and
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1611 (loc >= len(instring)-self.matchLen or instring[loc+self.matchLen].upper() not in self.identChars) ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1612 return loc+self.matchLen, self.match
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1613 #~ raise ParseException( instring, loc, self.errmsg )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1614 exc = self.myException
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1615 exc.loc = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1616 exc.pstr = instring
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1617 raise exc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1618
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1619 class Word(Token):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1620 """Token for matching words composed of allowed character sets.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1621 Defined with string containing all allowed initial characters,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1622 an optional string containing allowed body characters (if omitted,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1623 defaults to the initial character set), and an optional minimum,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1624 maximum, and/or exact length. The default value for min is 1 (a
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1625 minimum value < 1 is not valid); the default values for max and exact
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1626 are 0, meaning no maximum or exact length restriction.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1627 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1628 def __init__( self, initChars, bodyChars=None, min=1, max=0, exact=0, asKeyword=False ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1629 super(Word,self).__init__()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1630 self.initCharsOrig = initChars
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1631 self.initChars = _str2dict(initChars)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1632 if bodyChars :
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1633 self.bodyCharsOrig = bodyChars
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1634 self.bodyChars = _str2dict(bodyChars)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1635 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1636 self.bodyCharsOrig = initChars
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1637 self.bodyChars = _str2dict(initChars)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1638
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1639 self.maxSpecified = max > 0
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1640
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1641 if min < 1:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1642 raise ValueError("cannot specify a minimum length < 1; use Optional(Word()) if zero-length word is permitted")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1643
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1644 self.minLen = min
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1645
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1646 if max > 0:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1647 self.maxLen = max
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1648 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1649 self.maxLen = _MAX_INT
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1650
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1651 if exact > 0:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1652 self.maxLen = exact
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1653 self.minLen = exact
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1654
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1655 self.name = _ustr(self)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1656 self.errmsg = "Expected " + self.name
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1657 #self.myException.msg = self.errmsg
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1658 self.mayIndexError = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1659 self.asKeyword = asKeyword
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1660
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1661 if ' ' not in self.initCharsOrig+self.bodyCharsOrig and (min==1 and max==0 and exact==0):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1662 if self.bodyCharsOrig == self.initCharsOrig:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1663 self.reString = "[%s]+" % _escapeRegexRangeChars(self.initCharsOrig)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1664 elif len(self.bodyCharsOrig) == 1:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1665 self.reString = "%s[%s]*" % \
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1666 (re.escape(self.initCharsOrig),
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1667 _escapeRegexRangeChars(self.bodyCharsOrig),)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1668 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1669 self.reString = "[%s][%s]*" % \
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1670 (_escapeRegexRangeChars(self.initCharsOrig),
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1671 _escapeRegexRangeChars(self.bodyCharsOrig),)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1672 if self.asKeyword:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1673 self.reString = r"\b"+self.reString+r"\b"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1674 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1675 self.re = re.compile( self.reString )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1676 except:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1677 self.re = None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1678
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1679 def parseImpl( self, instring, loc, doActions=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1680 if self.re:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1681 result = self.re.match(instring,loc)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1682 if not result:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1683 exc = self.myException
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1684 exc.loc = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1685 exc.pstr = instring
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1686 raise exc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1687
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1688 loc = result.end()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1689 return loc,result.group()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1690
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1691 if not(instring[ loc ] in self.initChars):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1692 #~ raise ParseException( instring, loc, self.errmsg )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1693 exc = self.myException
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1694 exc.loc = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1695 exc.pstr = instring
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1696 raise exc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1697 start = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1698 loc += 1
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1699 instrlen = len(instring)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1700 bodychars = self.bodyChars
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1701 maxloc = start + self.maxLen
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1702 maxloc = min( maxloc, instrlen )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1703 while loc < maxloc and instring[loc] in bodychars:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1704 loc += 1
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1705
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1706 throwException = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1707 if loc - start < self.minLen:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1708 throwException = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1709 if self.maxSpecified and loc < instrlen and instring[loc] in bodychars:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1710 throwException = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1711 if self.asKeyword:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1712 if (start>0 and instring[start-1] in bodychars) or (loc<instrlen and instring[loc] in bodychars):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1713 throwException = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1714
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1715 if throwException:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1716 #~ raise ParseException( instring, loc, self.errmsg )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1717 exc = self.myException
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1718 exc.loc = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1719 exc.pstr = instring
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1720 raise exc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1721
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1722 return loc, instring[start:loc]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1723
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1724 def __str__( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1725 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1726 return super(Word,self).__str__()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1727 except:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1728 pass
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1729
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1730
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1731 if self.strRepr is None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1732
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1733 def charsAsStr(s):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1734 if len(s)>4:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1735 return s[:4]+"..."
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1736 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1737 return s
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1738
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1739 if ( self.initCharsOrig != self.bodyCharsOrig ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1740 self.strRepr = "W:(%s,%s)" % ( charsAsStr(self.initCharsOrig), charsAsStr(self.bodyCharsOrig) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1741 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1742 self.strRepr = "W:(%s)" % charsAsStr(self.initCharsOrig)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1743
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1744 return self.strRepr
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1745
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1746
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1747 class Regex(Token):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1748 """Token for matching strings that match a given regular expression.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1749 Defined with string specifying the regular expression in a form recognized by the inbuilt Python re module.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1750 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1751 def __init__( self, pattern, flags=0):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1752 """The parameters pattern and flags are passed to the re.compile() function as-is. See the Python re module for an explanation of the acceptable patterns and flags."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1753 super(Regex,self).__init__()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1754
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1755 if len(pattern) == 0:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1756 warnings.warn("null string passed to Regex; use Empty() instead",
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1757 SyntaxWarning, stacklevel=2)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1758
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1759 self.pattern = pattern
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1760 self.flags = flags
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1761
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1762 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1763 self.re = re.compile(self.pattern, self.flags)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1764 self.reString = self.pattern
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1765 except sre_constants.error:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1766 warnings.warn("invalid pattern (%s) passed to Regex" % pattern,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1767 SyntaxWarning, stacklevel=2)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1768 raise
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1769
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1770 self.name = _ustr(self)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1771 self.errmsg = "Expected " + self.name
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1772 #self.myException.msg = self.errmsg
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1773 self.mayIndexError = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1774 self.mayReturnEmpty = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1775
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1776 def parseImpl( self, instring, loc, doActions=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1777 result = self.re.match(instring,loc)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1778 if not result:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1779 exc = self.myException
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1780 exc.loc = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1781 exc.pstr = instring
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1782 raise exc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1783
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1784 loc = result.end()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1785 d = result.groupdict()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1786 ret = ParseResults(result.group())
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1787 if d:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1788 for k in d:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1789 ret[k] = d[k]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1790 return loc,ret
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1791
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1792 def __str__( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1793 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1794 return super(Regex,self).__str__()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1795 except:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1796 pass
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1797
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1798 if self.strRepr is None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1799 self.strRepr = "Re:(%s)" % repr(self.pattern)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1800
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1801 return self.strRepr
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1802
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1803
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1804 class QuotedString(Token):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1805 """Token for matching strings that are delimited by quoting characters.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1806 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1807 def __init__( self, quoteChar, escChar=None, escQuote=None, multiline=False, unquoteResults=True, endQuoteChar=None):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1808 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1809 Defined with the following parameters:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1810 - quoteChar - string of one or more characters defining the quote delimiting string
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1811 - escChar - character to escape quotes, typically backslash (default=None)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1812 - escQuote - special quote sequence to escape an embedded quote string (such as SQL's "" to escape an embedded ") (default=None)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1813 - multiline - boolean indicating whether quotes can span multiple lines (default=False)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1814 - unquoteResults - boolean indicating whether the matched text should be unquoted (default=True)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1815 - endQuoteChar - string of one or more characters defining the end of the quote delimited string (default=None => same as quoteChar)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1816 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1817 super(QuotedString,self).__init__()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1818
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1819 # remove white space from quote chars - wont work anyway
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1820 quoteChar = quoteChar.strip()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1821 if len(quoteChar) == 0:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1822 warnings.warn("quoteChar cannot be the empty string",SyntaxWarning,stacklevel=2)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1823 raise SyntaxError()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1824
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1825 if endQuoteChar is None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1826 endQuoteChar = quoteChar
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1827 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1828 endQuoteChar = endQuoteChar.strip()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1829 if len(endQuoteChar) == 0:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1830 warnings.warn("endQuoteChar cannot be the empty string",SyntaxWarning,stacklevel=2)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1831 raise SyntaxError()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1832
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1833 self.quoteChar = quoteChar
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1834 self.quoteCharLen = len(quoteChar)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1835 self.firstQuoteChar = quoteChar[0]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1836 self.endQuoteChar = endQuoteChar
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1837 self.endQuoteCharLen = len(endQuoteChar)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1838 self.escChar = escChar
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1839 self.escQuote = escQuote
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1840 self.unquoteResults = unquoteResults
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1841
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1842 if multiline:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1843 self.flags = re.MULTILINE | re.DOTALL
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1844 self.pattern = r'%s(?:[^%s%s]' % \
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1845 ( re.escape(self.quoteChar),
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1846 _escapeRegexRangeChars(self.endQuoteChar[0]),
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1847 (escChar is not None and _escapeRegexRangeChars(escChar) or '') )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1848 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1849 self.flags = 0
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1850 self.pattern = r'%s(?:[^%s\n\r%s]' % \
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1851 ( re.escape(self.quoteChar),
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1852 _escapeRegexRangeChars(self.endQuoteChar[0]),
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1853 (escChar is not None and _escapeRegexRangeChars(escChar) or '') )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1854 if len(self.endQuoteChar) > 1:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1855 self.pattern += (
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1856 '|(?:' + ')|(?:'.join(["%s[^%s]" % (re.escape(self.endQuoteChar[:i]),
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1857 _escapeRegexRangeChars(self.endQuoteChar[i]))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1858 for i in range(len(self.endQuoteChar)-1,0,-1)]) + ')'
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1859 )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1860 if escQuote:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1861 self.pattern += (r'|(?:%s)' % re.escape(escQuote))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1862 if escChar:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1863 self.pattern += (r'|(?:%s.)' % re.escape(escChar))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1864 self.escCharReplacePattern = re.escape(self.escChar)+"(.)"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1865 self.pattern += (r')*%s' % re.escape(self.endQuoteChar))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1866
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1867 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1868 self.re = re.compile(self.pattern, self.flags)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1869 self.reString = self.pattern
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1870 except sre_constants.error:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1871 warnings.warn("invalid pattern (%s) passed to Regex" % self.pattern,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1872 SyntaxWarning, stacklevel=2)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1873 raise
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1874
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1875 self.name = _ustr(self)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1876 self.errmsg = "Expected " + self.name
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1877 #self.myException.msg = self.errmsg
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1878 self.mayIndexError = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1879 self.mayReturnEmpty = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1880
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1881 def parseImpl( self, instring, loc, doActions=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1882 result = instring[loc] == self.firstQuoteChar and self.re.match(instring,loc) or None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1883 if not result:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1884 exc = self.myException
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1885 exc.loc = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1886 exc.pstr = instring
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1887 raise exc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1888
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1889 loc = result.end()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1890 ret = result.group()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1891
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1892 if self.unquoteResults:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1893
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1894 # strip off quotes
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1895 ret = ret[self.quoteCharLen:-self.endQuoteCharLen]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1896
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1897 if isinstance(ret,basestring):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1898 # replace escaped characters
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1899 if self.escChar:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1900 ret = re.sub(self.escCharReplacePattern,"\g<1>",ret)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1901
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1902 # replace escaped quotes
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1903 if self.escQuote:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1904 ret = ret.replace(self.escQuote, self.endQuoteChar)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1905
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1906 return loc, ret
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1907
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1908 def __str__( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1909 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1910 return super(QuotedString,self).__str__()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1911 except:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1912 pass
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1913
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1914 if self.strRepr is None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1915 self.strRepr = "quoted string, starting with %s ending with %s" % (self.quoteChar, self.endQuoteChar)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1916
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1917 return self.strRepr
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1918
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1919
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1920 class CharsNotIn(Token):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1921 """Token for matching words composed of characters *not* in a given set.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1922 Defined with string containing all disallowed characters, and an optional
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1923 minimum, maximum, and/or exact length. The default value for min is 1 (a
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1924 minimum value < 1 is not valid); the default values for max and exact
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1925 are 0, meaning no maximum or exact length restriction.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1926 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1927 def __init__( self, notChars, min=1, max=0, exact=0 ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1928 super(CharsNotIn,self).__init__()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1929 self.skipWhitespace = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1930 self.notChars = notChars
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1931
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1932 if min < 1:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1933 raise ValueError("cannot specify a minimum length < 1; use Optional(CharsNotIn()) if zero-length char group is permitted")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1934
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1935 self.minLen = min
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1936
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1937 if max > 0:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1938 self.maxLen = max
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1939 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1940 self.maxLen = _MAX_INT
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1941
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1942 if exact > 0:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1943 self.maxLen = exact
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1944 self.minLen = exact
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1945
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1946 self.name = _ustr(self)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1947 self.errmsg = "Expected " + self.name
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1948 self.mayReturnEmpty = ( self.minLen == 0 )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1949 #self.myException.msg = self.errmsg
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1950 self.mayIndexError = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1951
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1952 def parseImpl( self, instring, loc, doActions=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1953 if instring[loc] in self.notChars:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1954 #~ raise ParseException( instring, loc, self.errmsg )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1955 exc = self.myException
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1956 exc.loc = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1957 exc.pstr = instring
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1958 raise exc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1959
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1960 start = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1961 loc += 1
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1962 notchars = self.notChars
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1963 maxlen = min( start+self.maxLen, len(instring) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1964 while loc < maxlen and \
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1965 (instring[loc] not in notchars):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1966 loc += 1
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1967
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1968 if loc - start < self.minLen:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1969 #~ raise ParseException( instring, loc, self.errmsg )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1970 exc = self.myException
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1971 exc.loc = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1972 exc.pstr = instring
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1973 raise exc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1974
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1975 return loc, instring[start:loc]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1976
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1977 def __str__( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1978 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1979 return super(CharsNotIn, self).__str__()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1980 except:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1981 pass
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1982
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1983 if self.strRepr is None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1984 if len(self.notChars) > 4:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1985 self.strRepr = "!W:(%s...)" % self.notChars[:4]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1986 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1987 self.strRepr = "!W:(%s)" % self.notChars
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1988
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1989 return self.strRepr
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1990
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1991 class White(Token):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1992 """Special matching class for matching whitespace. Normally, whitespace is ignored
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1993 by pyparsing grammars. This class is included when some whitespace structures
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1994 are significant. Define with a string containing the whitespace characters to be
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1995 matched; default is " \\t\\r\\n". Also takes optional min, max, and exact arguments,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1996 as defined for the Word class."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1997 whiteStrs = {
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1998 " " : "<SPC>",
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
1999 "\t": "<TAB>",
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2000 "\n": "<LF>",
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2001 "\r": "<CR>",
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2002 "\f": "<FF>",
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2003 }
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2004 def __init__(self, ws=" \t\r\n", min=1, max=0, exact=0):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2005 super(White,self).__init__()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2006 self.matchWhite = ws
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2007 self.setWhitespaceChars( "".join([c for c in self.whiteChars if c not in self.matchWhite]) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2008 #~ self.leaveWhitespace()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2009 self.name = ("".join([White.whiteStrs[c] for c in self.matchWhite]))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2010 self.mayReturnEmpty = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2011 self.errmsg = "Expected " + self.name
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2012 #self.myException.msg = self.errmsg
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2013
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2014 self.minLen = min
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2015
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2016 if max > 0:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2017 self.maxLen = max
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2018 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2019 self.maxLen = _MAX_INT
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2020
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2021 if exact > 0:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2022 self.maxLen = exact
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2023 self.minLen = exact
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2024
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2025 def parseImpl( self, instring, loc, doActions=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2026 if not(instring[ loc ] in self.matchWhite):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2027 #~ raise ParseException( instring, loc, self.errmsg )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2028 exc = self.myException
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2029 exc.loc = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2030 exc.pstr = instring
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2031 raise exc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2032 start = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2033 loc += 1
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2034 maxloc = start + self.maxLen
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2035 maxloc = min( maxloc, len(instring) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2036 while loc < maxloc and instring[loc] in self.matchWhite:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2037 loc += 1
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2038
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2039 if loc - start < self.minLen:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2040 #~ raise ParseException( instring, loc, self.errmsg )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2041 exc = self.myException
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2042 exc.loc = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2043 exc.pstr = instring
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2044 raise exc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2045
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2046 return loc, instring[start:loc]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2047
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2048
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2049 class _PositionToken(Token):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2050 def __init__( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2051 super(_PositionToken,self).__init__()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2052 self.name=self.__class__.__name__
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2053 self.mayReturnEmpty = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2054 self.mayIndexError = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2055
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2056 class GoToColumn(_PositionToken):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2057 """Token to advance to a specific column of input text; useful for tabular report scraping."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2058 def __init__( self, colno ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2059 super(GoToColumn,self).__init__()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2060 self.col = colno
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2061
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2062 def preParse( self, instring, loc ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2063 if col(loc,instring) != self.col:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2064 instrlen = len(instring)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2065 if self.ignoreExprs:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2066 loc = self._skipIgnorables( instring, loc )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2067 while loc < instrlen and instring[loc].isspace() and col( loc, instring ) != self.col :
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2068 loc += 1
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2069 return loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2070
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2071 def parseImpl( self, instring, loc, doActions=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2072 thiscol = col( loc, instring )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2073 if thiscol > self.col:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2074 raise ParseException( instring, loc, "Text not in expected column", self )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2075 newloc = loc + self.col - thiscol
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2076 ret = instring[ loc: newloc ]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2077 return newloc, ret
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2078
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2079 class LineStart(_PositionToken):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2080 """Matches if current position is at the beginning of a line within the parse string"""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2081 def __init__( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2082 super(LineStart,self).__init__()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2083 self.setWhitespaceChars( ParserElement.DEFAULT_WHITE_CHARS.replace("\n","") )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2084 self.errmsg = "Expected start of line"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2085 #self.myException.msg = self.errmsg
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2086
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2087 def preParse( self, instring, loc ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2088 preloc = super(LineStart,self).preParse(instring,loc)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2089 if instring[preloc] == "\n":
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2090 loc += 1
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2091 return loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2092
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2093 def parseImpl( self, instring, loc, doActions=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2094 if not( loc==0 or
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2095 (loc == self.preParse( instring, 0 )) or
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2096 (instring[loc-1] == "\n") ): #col(loc, instring) != 1:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2097 #~ raise ParseException( instring, loc, "Expected start of line" )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2098 exc = self.myException
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2099 exc.loc = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2100 exc.pstr = instring
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2101 raise exc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2102 return loc, []
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2103
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2104 class LineEnd(_PositionToken):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2105 """Matches if current position is at the end of a line within the parse string"""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2106 def __init__( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2107 super(LineEnd,self).__init__()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2108 self.setWhitespaceChars( ParserElement.DEFAULT_WHITE_CHARS.replace("\n","") )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2109 self.errmsg = "Expected end of line"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2110 #self.myException.msg = self.errmsg
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2111
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2112 def parseImpl( self, instring, loc, doActions=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2113 if loc<len(instring):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2114 if instring[loc] == "\n":
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2115 return loc+1, "\n"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2116 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2117 #~ raise ParseException( instring, loc, "Expected end of line" )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2118 exc = self.myException
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2119 exc.loc = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2120 exc.pstr = instring
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2121 raise exc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2122 elif loc == len(instring):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2123 return loc+1, []
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2124 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2125 exc = self.myException
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2126 exc.loc = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2127 exc.pstr = instring
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2128 raise exc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2129
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2130 class StringStart(_PositionToken):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2131 """Matches if current position is at the beginning of the parse string"""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2132 def __init__( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2133 super(StringStart,self).__init__()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2134 self.errmsg = "Expected start of text"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2135 #self.myException.msg = self.errmsg
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2136
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2137 def parseImpl( self, instring, loc, doActions=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2138 if loc != 0:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2139 # see if entire string up to here is just whitespace and ignoreables
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2140 if loc != self.preParse( instring, 0 ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2141 #~ raise ParseException( instring, loc, "Expected start of text" )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2142 exc = self.myException
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2143 exc.loc = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2144 exc.pstr = instring
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2145 raise exc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2146 return loc, []
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2147
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2148 class StringEnd(_PositionToken):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2149 """Matches if current position is at the end of the parse string"""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2150 def __init__( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2151 super(StringEnd,self).__init__()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2152 self.errmsg = "Expected end of text"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2153 #self.myException.msg = self.errmsg
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2154
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2155 def parseImpl( self, instring, loc, doActions=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2156 if loc < len(instring):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2157 #~ raise ParseException( instring, loc, "Expected end of text" )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2158 exc = self.myException
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2159 exc.loc = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2160 exc.pstr = instring
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2161 raise exc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2162 elif loc == len(instring):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2163 return loc+1, []
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2164 elif loc > len(instring):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2165 return loc, []
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2166 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2167 exc = self.myException
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2168 exc.loc = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2169 exc.pstr = instring
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2170 raise exc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2171
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2172 class WordStart(_PositionToken):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2173 """Matches if the current position is at the beginning of a Word, and
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2174 is not preceded by any character in a given set of wordChars
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2175 (default=printables). To emulate the \b behavior of regular expressions,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2176 use WordStart(alphanums). WordStart will also match at the beginning of
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2177 the string being parsed, or at the beginning of a line.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2178 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2179 def __init__(self, wordChars = printables):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2180 super(WordStart,self).__init__()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2181 self.wordChars = _str2dict(wordChars)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2182 self.errmsg = "Not at the start of a word"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2183
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2184 def parseImpl(self, instring, loc, doActions=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2185 if loc != 0:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2186 if (instring[loc-1] in self.wordChars or
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2187 instring[loc] not in self.wordChars):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2188 exc = self.myException
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2189 exc.loc = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2190 exc.pstr = instring
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2191 raise exc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2192 return loc, []
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2193
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2194 class WordEnd(_PositionToken):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2195 """Matches if the current position is at the end of a Word, and
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2196 is not followed by any character in a given set of wordChars
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2197 (default=printables). To emulate the \b behavior of regular expressions,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2198 use WordEnd(alphanums). WordEnd will also match at the end of
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2199 the string being parsed, or at the end of a line.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2200 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2201 def __init__(self, wordChars = printables):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2202 super(WordEnd,self).__init__()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2203 self.wordChars = _str2dict(wordChars)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2204 self.skipWhitespace = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2205 self.errmsg = "Not at the end of a word"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2206
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2207 def parseImpl(self, instring, loc, doActions=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2208 instrlen = len(instring)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2209 if instrlen>0 and loc<instrlen:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2210 if (instring[loc] in self.wordChars or
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2211 instring[loc-1] not in self.wordChars):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2212 #~ raise ParseException( instring, loc, "Expected end of word" )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2213 exc = self.myException
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2214 exc.loc = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2215 exc.pstr = instring
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2216 raise exc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2217 return loc, []
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2218
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2219
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2220 class ParseExpression(ParserElement):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2221 """Abstract subclass of ParserElement, for combining and post-processing parsed tokens."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2222 def __init__( self, exprs, savelist = False ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2223 super(ParseExpression,self).__init__(savelist)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2224 if isinstance( exprs, list ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2225 self.exprs = exprs
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2226 elif isinstance( exprs, basestring ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2227 self.exprs = [ Literal( exprs ) ]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2228 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2229 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2230 self.exprs = list( exprs )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2231 except TypeError:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2232 self.exprs = [ exprs ]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2233 self.callPreparse = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2234
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2235 def __getitem__( self, i ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2236 return self.exprs[i]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2237
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2238 def append( self, other ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2239 self.exprs.append( other )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2240 self.strRepr = None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2241 return self
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2242
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2243 def leaveWhitespace( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2244 """Extends leaveWhitespace defined in base class, and also invokes leaveWhitespace on
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2245 all contained expressions."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2246 self.skipWhitespace = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2247 self.exprs = [ e.copy() for e in self.exprs ]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2248 for e in self.exprs:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2249 e.leaveWhitespace()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2250 return self
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2251
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2252 def ignore( self, other ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2253 if isinstance( other, Suppress ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2254 if other not in self.ignoreExprs:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2255 super( ParseExpression, self).ignore( other )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2256 for e in self.exprs:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2257 e.ignore( self.ignoreExprs[-1] )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2258 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2259 super( ParseExpression, self).ignore( other )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2260 for e in self.exprs:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2261 e.ignore( self.ignoreExprs[-1] )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2262 return self
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2263
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2264 def __str__( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2265 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2266 return super(ParseExpression,self).__str__()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2267 except:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2268 pass
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2269
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2270 if self.strRepr is None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2271 self.strRepr = "%s:(%s)" % ( self.__class__.__name__, _ustr(self.exprs) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2272 return self.strRepr
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2273
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2274 def streamline( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2275 super(ParseExpression,self).streamline()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2276
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2277 for e in self.exprs:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2278 e.streamline()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2279
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2280 # collapse nested And's of the form And( And( And( a,b), c), d) to And( a,b,c,d )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2281 # but only if there are no parse actions or resultsNames on the nested And's
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2282 # (likewise for Or's and MatchFirst's)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2283 if ( len(self.exprs) == 2 ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2284 other = self.exprs[0]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2285 if ( isinstance( other, self.__class__ ) and
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2286 not(other.parseAction) and
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2287 other.resultsName is None and
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2288 not other.debug ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2289 self.exprs = other.exprs[:] + [ self.exprs[1] ]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2290 self.strRepr = None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2291 self.mayReturnEmpty |= other.mayReturnEmpty
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2292 self.mayIndexError |= other.mayIndexError
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2293
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2294 other = self.exprs[-1]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2295 if ( isinstance( other, self.__class__ ) and
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2296 not(other.parseAction) and
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2297 other.resultsName is None and
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2298 not other.debug ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2299 self.exprs = self.exprs[:-1] + other.exprs[:]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2300 self.strRepr = None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2301 self.mayReturnEmpty |= other.mayReturnEmpty
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2302 self.mayIndexError |= other.mayIndexError
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2303
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2304 return self
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2305
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2306 def setResultsName( self, name, listAllMatches=False ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2307 ret = super(ParseExpression,self).setResultsName(name,listAllMatches)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2308 return ret
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2309
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2310 def validate( self, validateTrace=[] ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2311 tmp = validateTrace[:]+[self]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2312 for e in self.exprs:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2313 e.validate(tmp)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2314 self.checkRecursion( [] )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2315
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2316 class And(ParseExpression):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2317 """Requires all given ParseExpressions to be found in the given order.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2318 Expressions may be separated by whitespace.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2319 May be constructed using the '+' operator.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2320 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2321
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2322 class _ErrorStop(Empty):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2323 def __init__(self, *args, **kwargs):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2324 super(Empty,self).__init__(*args, **kwargs)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2325 self.leaveWhitespace()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2326
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2327 def __init__( self, exprs, savelist = True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2328 super(And,self).__init__(exprs, savelist)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2329 self.mayReturnEmpty = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2330 for e in self.exprs:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2331 if not e.mayReturnEmpty:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2332 self.mayReturnEmpty = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2333 break
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2334 self.setWhitespaceChars( exprs[0].whiteChars )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2335 self.skipWhitespace = exprs[0].skipWhitespace
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2336 self.callPreparse = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2337
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2338 def parseImpl( self, instring, loc, doActions=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2339 # pass False as last arg to _parse for first element, since we already
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2340 # pre-parsed the string as part of our And pre-parsing
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2341 loc, resultlist = self.exprs[0]._parse( instring, loc, doActions, callPreParse=False )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2342 errorStop = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2343 for e in self.exprs[1:]:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2344 if isinstance(e, And._ErrorStop):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2345 errorStop = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2346 continue
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2347 if errorStop:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2348 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2349 loc, exprtokens = e._parse( instring, loc, doActions )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2350 except ParseSyntaxException:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2351 raise
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2352 except ParseBaseException:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2353 pe = sys.exc_info()[1]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2354 raise ParseSyntaxException(pe)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2355 except IndexError:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2356 raise ParseSyntaxException( ParseException(instring, len(instring), self.errmsg, self) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2357 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2358 loc, exprtokens = e._parse( instring, loc, doActions )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2359 if exprtokens or exprtokens.keys():
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2360 resultlist += exprtokens
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2361 return loc, resultlist
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2362
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2363 def __iadd__(self, other ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2364 if isinstance( other, basestring ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2365 other = Literal( other )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2366 return self.append( other ) #And( [ self, other ] )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2367
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2368 def checkRecursion( self, parseElementList ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2369 subRecCheckList = parseElementList[:] + [ self ]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2370 for e in self.exprs:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2371 e.checkRecursion( subRecCheckList )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2372 if not e.mayReturnEmpty:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2373 break
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2374
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2375 def __str__( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2376 if hasattr(self,"name"):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2377 return self.name
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2378
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2379 if self.strRepr is None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2380 self.strRepr = "{" + " ".join( [ _ustr(e) for e in self.exprs ] ) + "}"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2381
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2382 return self.strRepr
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2383
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2384
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2385 class Or(ParseExpression):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2386 """Requires that at least one ParseExpression is found.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2387 If two expressions match, the expression that matches the longest string will be used.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2388 May be constructed using the '^' operator.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2389 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2390 def __init__( self, exprs, savelist = False ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2391 super(Or,self).__init__(exprs, savelist)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2392 self.mayReturnEmpty = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2393 for e in self.exprs:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2394 if e.mayReturnEmpty:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2395 self.mayReturnEmpty = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2396 break
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2397
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2398 def parseImpl( self, instring, loc, doActions=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2399 maxExcLoc = -1
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2400 maxMatchLoc = -1
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2401 maxException = None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2402 for e in self.exprs:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2403 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2404 loc2 = e.tryParse( instring, loc )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2405 except ParseException:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2406 err = sys.exc_info()[1]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2407 if err.loc > maxExcLoc:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2408 maxException = err
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2409 maxExcLoc = err.loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2410 except IndexError:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2411 if len(instring) > maxExcLoc:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2412 maxException = ParseException(instring,len(instring),e.errmsg,self)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2413 maxExcLoc = len(instring)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2414 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2415 if loc2 > maxMatchLoc:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2416 maxMatchLoc = loc2
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2417 maxMatchExp = e
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2418
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2419 if maxMatchLoc < 0:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2420 if maxException is not None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2421 raise maxException
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2422 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2423 raise ParseException(instring, loc, "no defined alternatives to match", self)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2424
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2425 return maxMatchExp._parse( instring, loc, doActions )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2426
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2427 def __ixor__(self, other ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2428 if isinstance( other, basestring ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2429 other = Literal( other )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2430 return self.append( other ) #Or( [ self, other ] )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2431
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2432 def __str__( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2433 if hasattr(self,"name"):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2434 return self.name
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2435
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2436 if self.strRepr is None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2437 self.strRepr = "{" + " ^ ".join( [ _ustr(e) for e in self.exprs ] ) + "}"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2438
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2439 return self.strRepr
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2440
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2441 def checkRecursion( self, parseElementList ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2442 subRecCheckList = parseElementList[:] + [ self ]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2443 for e in self.exprs:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2444 e.checkRecursion( subRecCheckList )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2445
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2446
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2447 class MatchFirst(ParseExpression):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2448 """Requires that at least one ParseExpression is found.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2449 If two expressions match, the first one listed is the one that will match.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2450 May be constructed using the '|' operator.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2451 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2452 def __init__( self, exprs, savelist = False ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2453 super(MatchFirst,self).__init__(exprs, savelist)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2454 if exprs:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2455 self.mayReturnEmpty = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2456 for e in self.exprs:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2457 if e.mayReturnEmpty:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2458 self.mayReturnEmpty = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2459 break
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2460 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2461 self.mayReturnEmpty = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2462
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2463 def parseImpl( self, instring, loc, doActions=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2464 maxExcLoc = -1
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2465 maxException = None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2466 for e in self.exprs:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2467 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2468 ret = e._parse( instring, loc, doActions )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2469 return ret
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2470 except ParseException as err:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2471 if err.loc > maxExcLoc:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2472 maxException = err
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2473 maxExcLoc = err.loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2474 except IndexError:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2475 if len(instring) > maxExcLoc:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2476 maxException = ParseException(instring,len(instring),e.errmsg,self)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2477 maxExcLoc = len(instring)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2478
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2479 # only got here if no expression matched, raise exception for match that made it the furthest
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2480 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2481 if maxException is not None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2482 raise maxException
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2483 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2484 raise ParseException(instring, loc, "no defined alternatives to match", self)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2485
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2486 def __ior__(self, other ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2487 if isinstance( other, basestring ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2488 other = Literal( other )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2489 return self.append( other ) #MatchFirst( [ self, other ] )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2490
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2491 def __str__( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2492 if hasattr(self,"name"):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2493 return self.name
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2494
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2495 if self.strRepr is None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2496 self.strRepr = "{" + " | ".join( [ _ustr(e) for e in self.exprs ] ) + "}"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2497
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2498 return self.strRepr
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2499
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2500 def checkRecursion( self, parseElementList ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2501 subRecCheckList = parseElementList[:] + [ self ]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2502 for e in self.exprs:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2503 e.checkRecursion( subRecCheckList )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2504
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2505
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2506 class Each(ParseExpression):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2507 """Requires all given ParseExpressions to be found, but in any order.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2508 Expressions may be separated by whitespace.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2509 May be constructed using the '&' operator.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2510 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2511 def __init__( self, exprs, savelist = True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2512 super(Each,self).__init__(exprs, savelist)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2513 self.mayReturnEmpty = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2514 for e in self.exprs:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2515 if not e.mayReturnEmpty:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2516 self.mayReturnEmpty = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2517 break
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2518 self.skipWhitespace = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2519 self.initExprGroups = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2520
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2521 def parseImpl( self, instring, loc, doActions=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2522 if self.initExprGroups:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2523 self.optionals = [ e.expr for e in self.exprs if isinstance(e,Optional) ]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2524 self.multioptionals = [ e.expr for e in self.exprs if isinstance(e,ZeroOrMore) ]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2525 self.multirequired = [ e.expr for e in self.exprs if isinstance(e,OneOrMore) ]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2526 self.required = [ e for e in self.exprs if not isinstance(e,(Optional,ZeroOrMore,OneOrMore)) ]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2527 self.required += self.multirequired
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2528 self.initExprGroups = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2529 tmpLoc = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2530 tmpReqd = self.required[:]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2531 tmpOpt = self.optionals[:]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2532 matchOrder = []
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2533
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2534 keepMatching = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2535 while keepMatching:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2536 tmpExprs = tmpReqd + tmpOpt + self.multioptionals + self.multirequired
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2537 failed = []
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2538 for e in tmpExprs:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2539 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2540 tmpLoc = e.tryParse( instring, tmpLoc )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2541 except ParseException:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2542 failed.append(e)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2543 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2544 matchOrder.append(e)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2545 if e in tmpReqd:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2546 tmpReqd.remove(e)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2547 elif e in tmpOpt:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2548 tmpOpt.remove(e)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2549 if len(failed) == len(tmpExprs):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2550 keepMatching = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2551
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2552 if tmpReqd:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2553 missing = ", ".join( [ _ustr(e) for e in tmpReqd ] )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2554 raise ParseException(instring,loc,"Missing one or more required elements (%s)" % missing )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2555
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2556 # add any unmatched Optionals, in case they have default values defined
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2557 matchOrder += list(e for e in self.exprs if isinstance(e,Optional) and e.expr in tmpOpt)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2558
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2559 resultlist = []
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2560 for e in matchOrder:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2561 loc,results = e._parse(instring,loc,doActions)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2562 resultlist.append(results)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2563
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2564 finalResults = ParseResults([])
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2565 for r in resultlist:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2566 dups = {}
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2567 for k in r.keys():
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2568 if k in finalResults.keys():
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2569 tmp = ParseResults(finalResults[k])
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2570 tmp += ParseResults(r[k])
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2571 dups[k] = tmp
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2572 finalResults += ParseResults(r)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2573 for k,v in dups.items():
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2574 finalResults[k] = v
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2575 return loc, finalResults
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2576
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2577 def __str__( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2578 if hasattr(self,"name"):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2579 return self.name
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2580
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2581 if self.strRepr is None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2582 self.strRepr = "{" + " & ".join( [ _ustr(e) for e in self.exprs ] ) + "}"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2583
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2584 return self.strRepr
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2585
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2586 def checkRecursion( self, parseElementList ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2587 subRecCheckList = parseElementList[:] + [ self ]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2588 for e in self.exprs:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2589 e.checkRecursion( subRecCheckList )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2590
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2591
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2592 class ParseElementEnhance(ParserElement):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2593 """Abstract subclass of ParserElement, for combining and post-processing parsed tokens."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2594 def __init__( self, expr, savelist=False ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2595 super(ParseElementEnhance,self).__init__(savelist)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2596 if isinstance( expr, basestring ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2597 expr = Literal(expr)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2598 self.expr = expr
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2599 self.strRepr = None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2600 if expr is not None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2601 self.mayIndexError = expr.mayIndexError
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2602 self.mayReturnEmpty = expr.mayReturnEmpty
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2603 self.setWhitespaceChars( expr.whiteChars )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2604 self.skipWhitespace = expr.skipWhitespace
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2605 self.saveAsList = expr.saveAsList
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2606 self.callPreparse = expr.callPreparse
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2607 self.ignoreExprs.extend(expr.ignoreExprs)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2608
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2609 def parseImpl( self, instring, loc, doActions=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2610 if self.expr is not None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2611 return self.expr._parse( instring, loc, doActions, callPreParse=False )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2612 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2613 raise ParseException("",loc,self.errmsg,self)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2614
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2615 def leaveWhitespace( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2616 self.skipWhitespace = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2617 self.expr = self.expr.copy()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2618 if self.expr is not None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2619 self.expr.leaveWhitespace()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2620 return self
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2621
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2622 def ignore( self, other ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2623 if isinstance( other, Suppress ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2624 if other not in self.ignoreExprs:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2625 super( ParseElementEnhance, self).ignore( other )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2626 if self.expr is not None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2627 self.expr.ignore( self.ignoreExprs[-1] )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2628 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2629 super( ParseElementEnhance, self).ignore( other )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2630 if self.expr is not None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2631 self.expr.ignore( self.ignoreExprs[-1] )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2632 return self
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2633
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2634 def streamline( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2635 super(ParseElementEnhance,self).streamline()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2636 if self.expr is not None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2637 self.expr.streamline()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2638 return self
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2639
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2640 def checkRecursion( self, parseElementList ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2641 if self in parseElementList:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2642 raise RecursiveGrammarException( parseElementList+[self] )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2643 subRecCheckList = parseElementList[:] + [ self ]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2644 if self.expr is not None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2645 self.expr.checkRecursion( subRecCheckList )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2646
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2647 def validate( self, validateTrace=[] ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2648 tmp = validateTrace[:]+[self]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2649 if self.expr is not None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2650 self.expr.validate(tmp)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2651 self.checkRecursion( [] )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2652
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2653 def __str__( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2654 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2655 return super(ParseElementEnhance,self).__str__()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2656 except:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2657 pass
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2658
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2659 if self.strRepr is None and self.expr is not None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2660 self.strRepr = "%s:(%s)" % ( self.__class__.__name__, _ustr(self.expr) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2661 return self.strRepr
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2662
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2663
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2664 class FollowedBy(ParseElementEnhance):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2665 """Lookahead matching of the given parse expression. FollowedBy
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2666 does *not* advance the parsing position within the input string, it only
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2667 verifies that the specified parse expression matches at the current
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2668 position. FollowedBy always returns a null token list."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2669 def __init__( self, expr ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2670 super(FollowedBy,self).__init__(expr)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2671 self.mayReturnEmpty = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2672
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2673 def parseImpl( self, instring, loc, doActions=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2674 self.expr.tryParse( instring, loc )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2675 return loc, []
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2676
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2677
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2678 class NotAny(ParseElementEnhance):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2679 """Lookahead to disallow matching with the given parse expression. NotAny
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2680 does *not* advance the parsing position within the input string, it only
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2681 verifies that the specified parse expression does *not* match at the current
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2682 position. Also, NotAny does *not* skip over leading whitespace. NotAny
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2683 always returns a null token list. May be constructed using the '~' operator."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2684 def __init__( self, expr ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2685 super(NotAny,self).__init__(expr)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2686 #~ self.leaveWhitespace()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2687 self.skipWhitespace = False # do NOT use self.leaveWhitespace(), don't want to propagate to exprs
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2688 self.mayReturnEmpty = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2689 self.errmsg = "Found unwanted token, "+_ustr(self.expr)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2690 #self.myException = ParseException("",0,self.errmsg,self)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2691
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2692 def parseImpl( self, instring, loc, doActions=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2693 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2694 self.expr.tryParse( instring, loc )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2695 except (ParseException,IndexError):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2696 pass
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2697 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2698 #~ raise ParseException(instring, loc, self.errmsg )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2699 exc = self.myException
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2700 exc.loc = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2701 exc.pstr = instring
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2702 raise exc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2703 return loc, []
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2704
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2705 def __str__( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2706 if hasattr(self,"name"):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2707 return self.name
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2708
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2709 if self.strRepr is None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2710 self.strRepr = "~{" + _ustr(self.expr) + "}"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2711
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2712 return self.strRepr
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2713
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2714
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2715 class ZeroOrMore(ParseElementEnhance):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2716 """Optional repetition of zero or more of the given expression."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2717 def __init__( self, expr ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2718 super(ZeroOrMore,self).__init__(expr)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2719 self.mayReturnEmpty = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2720
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2721 def parseImpl( self, instring, loc, doActions=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2722 tokens = []
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2723 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2724 loc, tokens = self.expr._parse( instring, loc, doActions, callPreParse=False )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2725 hasIgnoreExprs = ( len(self.ignoreExprs) > 0 )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2726 while 1:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2727 if hasIgnoreExprs:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2728 preloc = self._skipIgnorables( instring, loc )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2729 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2730 preloc = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2731 loc, tmptokens = self.expr._parse( instring, preloc, doActions )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2732 if tmptokens or tmptokens.keys():
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2733 tokens += tmptokens
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2734 except (ParseException,IndexError):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2735 pass
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2736
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2737 return loc, tokens
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2738
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2739 def __str__( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2740 if hasattr(self,"name"):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2741 return self.name
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2742
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2743 if self.strRepr is None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2744 self.strRepr = "[" + _ustr(self.expr) + "]..."
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2745
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2746 return self.strRepr
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2747
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2748 def setResultsName( self, name, listAllMatches=False ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2749 ret = super(ZeroOrMore,self).setResultsName(name,listAllMatches)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2750 ret.saveAsList = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2751 return ret
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2752
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2753
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2754 class OneOrMore(ParseElementEnhance):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2755 """Repetition of one or more of the given expression."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2756 def parseImpl( self, instring, loc, doActions=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2757 # must be at least one
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2758 loc, tokens = self.expr._parse( instring, loc, doActions, callPreParse=False )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2759 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2760 hasIgnoreExprs = ( len(self.ignoreExprs) > 0 )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2761 while 1:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2762 if hasIgnoreExprs:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2763 preloc = self._skipIgnorables( instring, loc )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2764 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2765 preloc = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2766 loc, tmptokens = self.expr._parse( instring, preloc, doActions )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2767 if tmptokens or tmptokens.keys():
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2768 tokens += tmptokens
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2769 except (ParseException,IndexError):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2770 pass
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2771
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2772 return loc, tokens
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2773
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2774 def __str__( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2775 if hasattr(self,"name"):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2776 return self.name
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2777
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2778 if self.strRepr is None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2779 self.strRepr = "{" + _ustr(self.expr) + "}..."
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2780
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2781 return self.strRepr
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2782
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2783 def setResultsName( self, name, listAllMatches=False ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2784 ret = super(OneOrMore,self).setResultsName(name,listAllMatches)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2785 ret.saveAsList = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2786 return ret
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2787
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2788 class _NullToken(object):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2789 def __bool__(self):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2790 return False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2791 __nonzero__ = __bool__
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2792 def __str__(self):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2793 return ""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2794
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2795 _optionalNotMatched = _NullToken()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2796 class Optional(ParseElementEnhance):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2797 """Optional matching of the given expression.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2798 A default return string can also be specified, if the optional expression
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2799 is not found.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2800 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2801 def __init__( self, exprs, default=_optionalNotMatched ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2802 super(Optional,self).__init__( exprs, savelist=False )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2803 self.defaultValue = default
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2804 self.mayReturnEmpty = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2805
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2806 def parseImpl( self, instring, loc, doActions=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2807 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2808 loc, tokens = self.expr._parse( instring, loc, doActions, callPreParse=False )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2809 except (ParseException,IndexError):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2810 if self.defaultValue is not _optionalNotMatched:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2811 if self.expr.resultsName:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2812 tokens = ParseResults([ self.defaultValue ])
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2813 tokens[self.expr.resultsName] = self.defaultValue
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2814 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2815 tokens = [ self.defaultValue ]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2816 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2817 tokens = []
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2818 return loc, tokens
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2819
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2820 def __str__( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2821 if hasattr(self,"name"):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2822 return self.name
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2823
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2824 if self.strRepr is None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2825 self.strRepr = "[" + _ustr(self.expr) + "]"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2826
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2827 return self.strRepr
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2828
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2829
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2830 class SkipTo(ParseElementEnhance):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2831 """Token for skipping over all undefined text until the matched expression is found.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2832 If include is set to true, the matched expression is also parsed (the skipped text
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2833 and matched expression are returned as a 2-element list). The ignore
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2834 argument is used to define grammars (typically quoted strings and comments) that
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2835 might contain false matches.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2836 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2837 def __init__( self, other, include=False, ignore=None, failOn=None ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2838 super( SkipTo, self ).__init__( other )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2839 self.ignoreExpr = ignore
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2840 self.mayReturnEmpty = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2841 self.mayIndexError = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2842 self.includeMatch = include
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2843 self.asList = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2844 if failOn is not None and isinstance(failOn, basestring):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2845 self.failOn = Literal(failOn)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2846 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2847 self.failOn = failOn
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2848 self.errmsg = "No match found for "+_ustr(self.expr)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2849 #self.myException = ParseException("",0,self.errmsg,self)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2850
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2851 def parseImpl( self, instring, loc, doActions=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2852 startLoc = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2853 instrlen = len(instring)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2854 expr = self.expr
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2855 failParse = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2856 while loc <= instrlen:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2857 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2858 if self.failOn:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2859 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2860 self.failOn.tryParse(instring, loc)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2861 except ParseBaseException:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2862 pass
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2863 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2864 failParse = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2865 raise ParseException(instring, loc, "Found expression " + str(self.failOn))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2866 failParse = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2867 if self.ignoreExpr is not None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2868 while 1:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2869 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2870 loc = self.ignoreExpr.tryParse(instring,loc)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2871 # print("found ignoreExpr, advance to", loc)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2872 except ParseBaseException:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2873 break
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2874 expr._parse( instring, loc, doActions=False, callPreParse=False )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2875 skipText = instring[startLoc:loc]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2876 if self.includeMatch:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2877 loc,mat = expr._parse(instring,loc,doActions,callPreParse=False)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2878 if mat:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2879 skipRes = ParseResults( skipText )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2880 skipRes += mat
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2881 return loc, [ skipRes ]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2882 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2883 return loc, [ skipText ]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2884 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2885 return loc, [ skipText ]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2886 except (ParseException,IndexError):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2887 if failParse:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2888 raise
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2889 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2890 loc += 1
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2891 exc = self.myException
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2892 exc.loc = loc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2893 exc.pstr = instring
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2894 raise exc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2895
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2896 class Forward(ParseElementEnhance):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2897 """Forward declaration of an expression to be defined later -
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2898 used for recursive grammars, such as algebraic infix notation.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2899 When the expression is known, it is assigned to the Forward variable using the '<<' operator.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2900
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2901 Note: take care when assigning to Forward not to overlook precedence of operators.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2902 Specifically, '|' has a lower precedence than '<<', so that::
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2903 fwdExpr << a | b | c
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2904 will actually be evaluated as::
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2905 (fwdExpr << a) | b | c
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2906 thereby leaving b and c out as parseable alternatives. It is recommended that you
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2907 explicitly group the values inserted into the Forward::
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2908 fwdExpr << (a | b | c)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2909 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2910 def __init__( self, other=None ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2911 super(Forward,self).__init__( other, savelist=False )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2912
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2913 def __lshift__( self, other ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2914 if isinstance( other, basestring ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2915 other = Literal(other)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2916 self.expr = other
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2917 self.mayReturnEmpty = other.mayReturnEmpty
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2918 self.strRepr = None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2919 self.mayIndexError = self.expr.mayIndexError
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2920 self.mayReturnEmpty = self.expr.mayReturnEmpty
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2921 self.setWhitespaceChars( self.expr.whiteChars )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2922 self.skipWhitespace = self.expr.skipWhitespace
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2923 self.saveAsList = self.expr.saveAsList
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2924 self.ignoreExprs.extend(self.expr.ignoreExprs)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2925 return None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2926
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2927 def leaveWhitespace( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2928 self.skipWhitespace = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2929 return self
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2930
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2931 def streamline( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2932 if not self.streamlined:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2933 self.streamlined = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2934 if self.expr is not None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2935 self.expr.streamline()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2936 return self
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2937
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2938 def validate( self, validateTrace=[] ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2939 if self not in validateTrace:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2940 tmp = validateTrace[:]+[self]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2941 if self.expr is not None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2942 self.expr.validate(tmp)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2943 self.checkRecursion([])
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2944
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2945 def __str__( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2946 if hasattr(self,"name"):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2947 return self.name
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2948
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2949 self._revertClass = self.__class__
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2950 self.__class__ = _ForwardNoRecurse
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2951 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2952 if self.expr is not None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2953 retString = _ustr(self.expr)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2954 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2955 retString = "None"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2956 finally:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2957 self.__class__ = self._revertClass
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2958 return self.__class__.__name__ + ": " + retString
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2959
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2960 def copy(self):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2961 if self.expr is not None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2962 return super(Forward,self).copy()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2963 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2964 ret = Forward()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2965 ret << self
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2966 return ret
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2967
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2968 class _ForwardNoRecurse(Forward):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2969 def __str__( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2970 return "..."
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2971
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2972 class TokenConverter(ParseElementEnhance):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2973 """Abstract subclass of ParseExpression, for converting parsed results."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2974 def __init__( self, expr, savelist=False ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2975 super(TokenConverter,self).__init__( expr )#, savelist )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2976 self.saveAsList = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2977
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2978 class Upcase(TokenConverter):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2979 """Converter to upper case all matching tokens."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2980 def __init__(self, *args):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2981 super(Upcase,self).__init__(*args)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2982 warnings.warn("Upcase class is deprecated, use upcaseTokens parse action instead",
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2983 DeprecationWarning,stacklevel=2)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2984
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2985 def postParse( self, instring, loc, tokenlist ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2986 return list(map( string.upper, tokenlist ))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2987
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2988
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2989 class Combine(TokenConverter):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2990 """Converter to concatenate all matching tokens to a single string.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2991 By default, the matching patterns must also be contiguous in the input string;
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2992 this can be disabled by specifying 'adjacent=False' in the constructor.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2993 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2994 def __init__( self, expr, joinString="", adjacent=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2995 super(Combine,self).__init__( expr )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2996 # suppress whitespace-stripping in contained parse expressions, but re-enable it on the Combine itself
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2997 if adjacent:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2998 self.leaveWhitespace()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
2999 self.adjacent = adjacent
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3000 self.skipWhitespace = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3001 self.joinString = joinString
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3002
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3003 def ignore( self, other ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3004 if self.adjacent:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3005 ParserElement.ignore(self, other)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3006 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3007 super( Combine, self).ignore( other )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3008 return self
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3009
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3010 def postParse( self, instring, loc, tokenlist ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3011 retToks = tokenlist.copy()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3012 del retToks[:]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3013 retToks += ParseResults([ "".join(tokenlist._asStringList(self.joinString)) ], modal=self.modalResults)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3014
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3015 if self.resultsName and len(retToks.keys())>0:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3016 return [ retToks ]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3017 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3018 return retToks
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3019
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3020 class Group(TokenConverter):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3021 """Converter to return the matched tokens as a list - useful for returning tokens of ZeroOrMore and OneOrMore expressions."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3022 def __init__( self, expr ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3023 super(Group,self).__init__( expr )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3024 self.saveAsList = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3025
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3026 def postParse( self, instring, loc, tokenlist ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3027 return [ tokenlist ]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3028
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3029 class Dict(TokenConverter):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3030 """Converter to return a repetitive expression as a list, but also as a dictionary.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3031 Each element can also be referenced using the first token in the expression as its key.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3032 Useful for tabular report scraping when the first column can be used as a item key.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3033 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3034 def __init__( self, exprs ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3035 super(Dict,self).__init__( exprs )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3036 self.saveAsList = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3037
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3038 def postParse( self, instring, loc, tokenlist ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3039 for i,tok in enumerate(tokenlist):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3040 if len(tok) == 0:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3041 continue
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3042 ikey = tok[0]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3043 if isinstance(ikey,int):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3044 ikey = _ustr(tok[0]).strip()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3045 if len(tok)==1:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3046 tokenlist[ikey] = _ParseResultsWithOffset("",i)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3047 elif len(tok)==2 and not isinstance(tok[1],ParseResults):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3048 tokenlist[ikey] = _ParseResultsWithOffset(tok[1],i)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3049 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3050 dictvalue = tok.copy() #ParseResults(i)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3051 del dictvalue[0]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3052 if len(dictvalue)!= 1 or (isinstance(dictvalue,ParseResults) and dictvalue.keys()):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3053 tokenlist[ikey] = _ParseResultsWithOffset(dictvalue,i)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3054 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3055 tokenlist[ikey] = _ParseResultsWithOffset(dictvalue[0],i)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3056
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3057 if self.resultsName:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3058 return [ tokenlist ]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3059 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3060 return tokenlist
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3061
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3062
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3063 class Suppress(TokenConverter):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3064 """Converter for ignoring the results of a parsed expression."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3065 def postParse( self, instring, loc, tokenlist ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3066 return []
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3067
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3068 def suppress( self ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3069 return self
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3070
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3071
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3072 class OnlyOnce(object):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3073 """Wrapper for parse actions, to ensure they are only called once."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3074 def __init__(self, methodCall):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3075 self.callable = ParserElement._normalizeParseActionArgs(methodCall)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3076 self.called = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3077 def __call__(self,s,l,t):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3078 if not self.called:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3079 results = self.callable(s,l,t)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3080 self.called = True
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3081 return results
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3082 raise ParseException(s,l,"")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3083 def reset(self):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3084 self.called = False
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3085
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3086 def traceParseAction(f):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3087 """Decorator for debugging parse actions."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3088 f = ParserElement._normalizeParseActionArgs(f)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3089 def z(*paArgs):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3090 thisFunc = f.func_name
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3091 s,l,t = paArgs[-3:]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3092 if len(paArgs)>3:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3093 thisFunc = paArgs[0].__class__.__name__ + '.' + thisFunc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3094 sys.stderr.write( ">>entering %s(line: '%s', %d, %s)\n" % (thisFunc,line(l,s),l,t) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3095 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3096 ret = f(*paArgs)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3097 except Exception:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3098 exc = sys.exc_info()[1]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3099 sys.stderr.write( "<<leaving %s (exception: %s)\n" % (thisFunc,exc) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3100 raise
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3101 sys.stderr.write( "<<leaving %s (ret: %s)\n" % (thisFunc,ret) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3102 return ret
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3103 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3104 z.__name__ = f.__name__
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3105 except AttributeError:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3106 pass
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3107 return z
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3108
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3109 #
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3110 # global helpers
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3111 #
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3112 def delimitedList( expr, delim=",", combine=False ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3113 """Helper to define a delimited list of expressions - the delimiter defaults to ','.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3114 By default, the list elements and delimiters can have intervening whitespace, and
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3115 comments, but this can be overridden by passing 'combine=True' in the constructor.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3116 If combine is set to True, the matching tokens are returned as a single token
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3117 string, with the delimiters included; otherwise, the matching tokens are returned
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3118 as a list of tokens, with the delimiters suppressed.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3119 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3120 dlName = _ustr(expr)+" ["+_ustr(delim)+" "+_ustr(expr)+"]..."
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3121 if combine:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3122 return Combine( expr + ZeroOrMore( delim + expr ) ).setName(dlName)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3123 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3124 return ( expr + ZeroOrMore( Suppress( delim ) + expr ) ).setName(dlName)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3125
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3126 def countedArray( expr ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3127 """Helper to define a counted list of expressions.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3128 This helper defines a pattern of the form::
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3129 integer expr expr expr...
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3130 where the leading integer tells how many expr expressions follow.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3131 The matched tokens returns the array of expr tokens as a list - the leading count token is suppressed.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3132 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3133 arrayExpr = Forward()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3134 def countFieldParseAction(s,l,t):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3135 n = int(t[0])
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3136 arrayExpr << (n and Group(And([expr]*n)) or Group(empty))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3137 return []
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3138 return ( Word(nums).setName("arrayLen").setParseAction(countFieldParseAction, callDuringTry=True) + arrayExpr )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3139
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3140 def _flatten(L):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3141 if type(L) is not list: return [L]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3142 if L == []: return L
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3143 return _flatten(L[0]) + _flatten(L[1:])
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3144
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3145 def matchPreviousLiteral(expr):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3146 """Helper to define an expression that is indirectly defined from
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3147 the tokens matched in a previous expression, that is, it looks
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3148 for a 'repeat' of a previous expression. For example::
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3149 first = Word(nums)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3150 second = matchPreviousLiteral(first)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3151 matchExpr = first + ":" + second
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3152 will match "1:1", but not "1:2". Because this matches a
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3153 previous literal, will also match the leading "1:1" in "1:10".
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3154 If this is not desired, use matchPreviousExpr.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3155 Do *not* use with packrat parsing enabled.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3156 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3157 rep = Forward()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3158 def copyTokenToRepeater(s,l,t):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3159 if t:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3160 if len(t) == 1:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3161 rep << t[0]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3162 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3163 # flatten t tokens
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3164 tflat = _flatten(t.asList())
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3165 rep << And( [ Literal(tt) for tt in tflat ] )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3166 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3167 rep << Empty()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3168 expr.addParseAction(copyTokenToRepeater, callDuringTry=True)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3169 return rep
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3170
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3171 def matchPreviousExpr(expr):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3172 """Helper to define an expression that is indirectly defined from
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3173 the tokens matched in a previous expression, that is, it looks
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3174 for a 'repeat' of a previous expression. For example::
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3175 first = Word(nums)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3176 second = matchPreviousExpr(first)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3177 matchExpr = first + ":" + second
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3178 will match "1:1", but not "1:2". Because this matches by
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3179 expressions, will *not* match the leading "1:1" in "1:10";
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3180 the expressions are evaluated first, and then compared, so
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3181 "1" is compared with "10".
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3182 Do *not* use with packrat parsing enabled.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3183 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3184 rep = Forward()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3185 e2 = expr.copy()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3186 rep << e2
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3187 def copyTokenToRepeater(s,l,t):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3188 matchTokens = _flatten(t.asList())
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3189 def mustMatchTheseTokens(s,l,t):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3190 theseTokens = _flatten(t.asList())
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3191 if theseTokens != matchTokens:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3192 raise ParseException("",0,"")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3193 rep.setParseAction( mustMatchTheseTokens, callDuringTry=True )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3194 expr.addParseAction(copyTokenToRepeater, callDuringTry=True)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3195 return rep
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3196
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3197 def _escapeRegexRangeChars(s):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3198 #~ escape these chars: ^-]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3199 for c in r"\^-]":
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3200 s = s.replace(c,_bslash+c)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3201 s = s.replace("\n",r"\n")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3202 s = s.replace("\t",r"\t")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3203 return _ustr(s)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3204
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3205 def oneOf( strs, caseless=False, useRegex=True ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3206 """Helper to quickly define a set of alternative Literals, and makes sure to do
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3207 longest-first testing when there is a conflict, regardless of the input order,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3208 but returns a MatchFirst for best performance.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3209
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3210 Parameters:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3211 - strs - a string of space-delimited literals, or a list of string literals
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3212 - caseless - (default=False) - treat all literals as caseless
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3213 - useRegex - (default=True) - as an optimization, will generate a Regex
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3214 object; otherwise, will generate a MatchFirst object (if caseless=True, or
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3215 if creating a Regex raises an exception)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3216 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3217 if caseless:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3218 isequal = ( lambda a,b: a.upper() == b.upper() )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3219 masks = ( lambda a,b: b.upper().startswith(a.upper()) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3220 parseElementClass = CaselessLiteral
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3221 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3222 isequal = ( lambda a,b: a == b )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3223 masks = ( lambda a,b: b.startswith(a) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3224 parseElementClass = Literal
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3225
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3226 if isinstance(strs,(list,tuple)):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3227 symbols = list(strs[:])
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3228 elif isinstance(strs,basestring):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3229 symbols = strs.split()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3230 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3231 warnings.warn("Invalid argument to oneOf, expected string or list",
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3232 SyntaxWarning, stacklevel=2)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3233
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3234 i = 0
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3235 while i < len(symbols)-1:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3236 cur = symbols[i]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3237 for j,other in enumerate(symbols[i+1:]):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3238 if ( isequal(other, cur) ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3239 del symbols[i+j+1]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3240 break
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3241 elif ( masks(cur, other) ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3242 del symbols[i+j+1]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3243 symbols.insert(i,other)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3244 cur = other
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3245 break
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3246 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3247 i += 1
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3248
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3249 if not caseless and useRegex:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3250 #~ print (strs,"->", "|".join( [ _escapeRegexChars(sym) for sym in symbols] ))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3251 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3252 if len(symbols)==len("".join(symbols)):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3253 return Regex( "[%s]" % "".join( [ _escapeRegexRangeChars(sym) for sym in symbols] ) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3254 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3255 return Regex( "|".join( [ re.escape(sym) for sym in symbols] ) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3256 except:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3257 warnings.warn("Exception creating Regex for oneOf, building MatchFirst",
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3258 SyntaxWarning, stacklevel=2)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3259
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3260
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3261 # last resort, just use MatchFirst
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3262 return MatchFirst( [ parseElementClass(sym) for sym in symbols ] )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3263
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3264 def dictOf( key, value ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3265 """Helper to easily and clearly define a dictionary by specifying the respective patterns
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3266 for the key and value. Takes care of defining the Dict, ZeroOrMore, and Group tokens
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3267 in the proper order. The key pattern can include delimiting markers or punctuation,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3268 as long as they are suppressed, thereby leaving the significant key text. The value
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3269 pattern can include named results, so that the Dict results can include named token
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3270 fields.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3271 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3272 return Dict( ZeroOrMore( Group ( key + value ) ) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3273
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3274 def originalTextFor(expr, asString=True):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3275 """Helper to return the original, untokenized text for a given expression. Useful to
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3276 restore the parsed fields of an HTML start tag into the raw tag text itself, or to
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3277 revert separate tokens with intervening whitespace back to the original matching
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3278 input text. Simpler to use than the parse action keepOriginalText, and does not
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3279 require the inspect module to chase up the call stack. By default, returns a
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3280 string containing the original parsed text.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3281
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3282 If the optional asString argument is passed as False, then the return value is a
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3283 ParseResults containing any results names that were originally matched, and a
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3284 single token containing the original matched text from the input string. So if
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3285 the expression passed to originalTextFor contains expressions with defined
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3286 results names, you must set asString to False if you want to preserve those
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3287 results name values."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3288 locMarker = Empty().setParseAction(lambda s,loc,t: loc)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3289 matchExpr = locMarker("_original_start") + expr + locMarker("_original_end")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3290 if asString:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3291 extractText = lambda s,l,t: s[t._original_start:t._original_end]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3292 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3293 def extractText(s,l,t):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3294 del t[:]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3295 t.insert(0, s[t._original_start:t._original_end])
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3296 del t["_original_start"]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3297 del t["_original_end"]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3298 matchExpr.setParseAction(extractText)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3299 return matchExpr
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3300
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3301 # convenience constants for positional expressions
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3302 empty = Empty().setName("empty")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3303 lineStart = LineStart().setName("lineStart")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3304 lineEnd = LineEnd().setName("lineEnd")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3305 stringStart = StringStart().setName("stringStart")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3306 stringEnd = StringEnd().setName("stringEnd")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3307
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3308 _escapedPunc = Word( _bslash, r"\[]-*.$+^?()~ ", exact=2 ).setParseAction(lambda s,l,t:t[0][1])
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3309 _printables_less_backslash = "".join([ c for c in printables if c not in r"\]" ])
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3310 _escapedHexChar = Combine( Suppress(_bslash + "0x") + Word(hexnums) ).setParseAction(lambda s,l,t:unichr(int(t[0],16)))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3311 _escapedOctChar = Combine( Suppress(_bslash) + Word("0","01234567") ).setParseAction(lambda s,l,t:unichr(int(t[0],8)))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3312 _singleChar = _escapedPunc | _escapedHexChar | _escapedOctChar | Word(_printables_less_backslash,exact=1)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3313 _charRange = Group(_singleChar + Suppress("-") + _singleChar)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3314 _reBracketExpr = Literal("[") + Optional("^").setResultsName("negate") + Group( OneOrMore( _charRange | _singleChar ) ).setResultsName("body") + "]"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3315
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3316 _expanded = lambda p: (isinstance(p,ParseResults) and ''.join([ unichr(c) for c in range(ord(p[0]),ord(p[1])+1) ]) or p)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3317
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3318 def srange(s):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3319 r"""Helper to easily define string ranges for use in Word construction. Borrows
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3320 syntax from regexp '[]' string range definitions::
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3321 srange("[0-9]") -> "0123456789"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3322 srange("[a-z]") -> "abcdefghijklmnopqrstuvwxyz"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3323 srange("[a-z$_]") -> "abcdefghijklmnopqrstuvwxyz$_"
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3324 The input string must be enclosed in []'s, and the returned string is the expanded
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3325 character set joined into a single string.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3326 The values enclosed in the []'s may be::
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3327 a single character
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3328 an escaped character with a leading backslash (such as \- or \])
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3329 an escaped hex character with a leading '\0x' (\0x21, which is a '!' character)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3330 an escaped octal character with a leading '\0' (\041, which is a '!' character)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3331 a range of any of the above, separated by a dash ('a-z', etc.)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3332 any combination of the above ('aeiouy', 'a-zA-Z0-9_$', etc.)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3333 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3334 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3335 return "".join([_expanded(part) for part in _reBracketExpr.parseString(s).body])
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3336 except:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3337 return ""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3338
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3339 def matchOnlyAtCol(n):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3340 """Helper method for defining parse actions that require matching at a specific
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3341 column in the input text.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3342 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3343 def verifyCol(strg,locn,toks):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3344 if col(locn,strg) != n:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3345 raise ParseException(strg,locn,"matched token not at column %d" % n)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3346 return verifyCol
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3347
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3348 def replaceWith(replStr):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3349 """Helper method for common parse actions that simply return a literal value. Especially
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3350 useful when used with transformString().
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3351 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3352 def _replFunc(*args):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3353 return [replStr]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3354 return _replFunc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3355
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3356 def removeQuotes(s,l,t):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3357 """Helper parse action for removing quotation marks from parsed quoted strings.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3358 To use, add this parse action to quoted string using::
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3359 quotedString.setParseAction( removeQuotes )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3360 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3361 return t[0][1:-1]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3362
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3363 def upcaseTokens(s,l,t):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3364 """Helper parse action to convert tokens to upper case."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3365 return [ tt.upper() for tt in map(_ustr,t) ]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3366
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3367 def downcaseTokens(s,l,t):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3368 """Helper parse action to convert tokens to lower case."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3369 return [ tt.lower() for tt in map(_ustr,t) ]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3370
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3371 def keepOriginalText(s,startLoc,t):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3372 """Helper parse action to preserve original parsed text,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3373 overriding any nested parse actions."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3374 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3375 endloc = getTokensEndLoc()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3376 except ParseException:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3377 raise ParseFatalException("incorrect usage of keepOriginalText - may only be called as a parse action")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3378 del t[:]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3379 t += ParseResults(s[startLoc:endloc])
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3380 return t
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3381
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3382 def getTokensEndLoc():
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3383 """Method to be called from within a parse action to determine the end
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3384 location of the parsed tokens."""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3385 import inspect
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3386 fstack = inspect.stack()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3387 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3388 # search up the stack (through intervening argument normalizers) for correct calling routine
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3389 for f in fstack[2:]:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3390 if f[3] == "_parseNoCache":
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3391 endloc = f[0].f_locals["loc"]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3392 return endloc
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3393 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3394 raise ParseFatalException("incorrect usage of getTokensEndLoc - may only be called from within a parse action")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3395 finally:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3396 del fstack
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3397
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3398 def _makeTags(tagStr, xml):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3399 """Internal helper to construct opening and closing tag expressions, given a tag name"""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3400 if isinstance(tagStr,basestring):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3401 resname = tagStr
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3402 tagStr = Keyword(tagStr, caseless=not xml)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3403 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3404 resname = tagStr.name
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3405
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3406 tagAttrName = Word(alphas,alphanums+"_-:")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3407 if (xml):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3408 tagAttrValue = dblQuotedString.copy().setParseAction( removeQuotes )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3409 openTag = Suppress("<") + tagStr + \
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3410 Dict(ZeroOrMore(Group( tagAttrName + Suppress("=") + tagAttrValue ))) + \
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3411 Optional("/",default=[False]).setResultsName("empty").setParseAction(lambda s,l,t:t[0]=='/') + Suppress(">")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3412 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3413 printablesLessRAbrack = "".join( [ c for c in printables if c not in ">" ] )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3414 tagAttrValue = quotedString.copy().setParseAction( removeQuotes ) | Word(printablesLessRAbrack)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3415 openTag = Suppress("<") + tagStr + \
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3416 Dict(ZeroOrMore(Group( tagAttrName.setParseAction(downcaseTokens) + \
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3417 Optional( Suppress("=") + tagAttrValue ) ))) + \
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3418 Optional("/",default=[False]).setResultsName("empty").setParseAction(lambda s,l,t:t[0]=='/') + Suppress(">")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3419 closeTag = Combine(_L("</") + tagStr + ">")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3420
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3421 openTag = openTag.setResultsName("start"+"".join(resname.replace(":"," ").title().split())).setName("<%s>" % tagStr)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3422 closeTag = closeTag.setResultsName("end"+"".join(resname.replace(":"," ").title().split())).setName("</%s>" % tagStr)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3423
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3424 return openTag, closeTag
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3425
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3426 def makeHTMLTags(tagStr):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3427 """Helper to construct opening and closing tag expressions for HTML, given a tag name"""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3428 return _makeTags( tagStr, False )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3429
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3430 def makeXMLTags(tagStr):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3431 """Helper to construct opening and closing tag expressions for XML, given a tag name"""
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3432 return _makeTags( tagStr, True )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3433
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3434 def withAttribute(*args,**attrDict):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3435 """Helper to create a validating parse action to be used with start tags created
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3436 with makeXMLTags or makeHTMLTags. Use withAttribute to qualify a starting tag
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3437 with a required attribute value, to avoid false matches on common tags such as
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3438 <TD> or <DIV>.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3439
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3440 Call withAttribute with a series of attribute names and values. Specify the list
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3441 of filter attributes names and values as:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3442 - keyword arguments, as in (class="Customer",align="right"), or
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3443 - a list of name-value tuples, as in ( ("ns1:class", "Customer"), ("ns2:align","right") )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3444 For attribute names with a namespace prefix, you must use the second form. Attribute
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3445 names are matched insensitive to upper/lower case.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3446
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3447 To verify that the attribute exists, but without specifying a value, pass
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3448 withAttribute.ANY_VALUE as the value.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3449 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3450 if args:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3451 attrs = args[:]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3452 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3453 attrs = attrDict.items()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3454 attrs = [(k,v) for k,v in attrs]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3455 def pa(s,l,tokens):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3456 for attrName,attrValue in attrs:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3457 if attrName not in tokens:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3458 raise ParseException(s,l,"no matching attribute " + attrName)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3459 if attrValue != withAttribute.ANY_VALUE and tokens[attrName] != attrValue:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3460 raise ParseException(s,l,"attribute '%s' has value '%s', must be '%s'" %
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3461 (attrName, tokens[attrName], attrValue))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3462 return pa
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3463 withAttribute.ANY_VALUE = object()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3464
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3465 opAssoc = _Constants()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3466 opAssoc.LEFT = object()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3467 opAssoc.RIGHT = object()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3468
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3469 def operatorPrecedence( baseExpr, opList ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3470 """Helper method for constructing grammars of expressions made up of
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3471 operators working in a precedence hierarchy. Operators may be unary or
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3472 binary, left- or right-associative. Parse actions can also be attached
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3473 to operator expressions.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3474
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3475 Parameters:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3476 - baseExpr - expression representing the most basic element for the nested
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3477 - opList - list of tuples, one for each operator precedence level in the
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3478 expression grammar; each tuple is of the form
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3479 (opExpr, numTerms, rightLeftAssoc, parseAction), where:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3480 - opExpr is the pyparsing expression for the operator;
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3481 may also be a string, which will be converted to a Literal;
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3482 if numTerms is 3, opExpr is a tuple of two expressions, for the
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3483 two operators separating the 3 terms
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3484 - numTerms is the number of terms for this operator (must
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3485 be 1, 2, or 3)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3486 - rightLeftAssoc is the indicator whether the operator is
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3487 right or left associative, using the pyparsing-defined
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3488 constants opAssoc.RIGHT and opAssoc.LEFT.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3489 - parseAction is the parse action to be associated with
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3490 expressions matching this operator expression (the
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3491 parse action tuple member may be omitted)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3492 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3493 ret = Forward()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3494 lastExpr = baseExpr | ( Suppress('(') + ret + Suppress(')') )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3495 for i,operDef in enumerate(opList):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3496 opExpr,arity,rightLeftAssoc,pa = (operDef + (None,))[:4]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3497 if arity == 3:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3498 if opExpr is None or len(opExpr) != 2:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3499 raise ValueError("if numterms=3, opExpr must be a tuple or list of two expressions")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3500 opExpr1, opExpr2 = opExpr
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3501 thisExpr = Forward()#.setName("expr%d" % i)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3502 if rightLeftAssoc == opAssoc.LEFT:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3503 if arity == 1:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3504 matchExpr = FollowedBy(lastExpr + opExpr) + Group( lastExpr + OneOrMore( opExpr ) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3505 elif arity == 2:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3506 if opExpr is not None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3507 matchExpr = FollowedBy(lastExpr + opExpr + lastExpr) + Group( lastExpr + OneOrMore( opExpr + lastExpr ) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3508 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3509 matchExpr = FollowedBy(lastExpr+lastExpr) + Group( lastExpr + OneOrMore(lastExpr) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3510 elif arity == 3:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3511 matchExpr = FollowedBy(lastExpr + opExpr1 + lastExpr + opExpr2 + lastExpr) + \
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3512 Group( lastExpr + opExpr1 + lastExpr + opExpr2 + lastExpr )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3513 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3514 raise ValueError("operator must be unary (1), binary (2), or ternary (3)")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3515 elif rightLeftAssoc == opAssoc.RIGHT:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3516 if arity == 1:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3517 # try to avoid LR with this extra test
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3518 if not isinstance(opExpr, Optional):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3519 opExpr = Optional(opExpr)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3520 matchExpr = FollowedBy(opExpr.expr + thisExpr) + Group( opExpr + thisExpr )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3521 elif arity == 2:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3522 if opExpr is not None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3523 matchExpr = FollowedBy(lastExpr + opExpr + thisExpr) + Group( lastExpr + OneOrMore( opExpr + thisExpr ) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3524 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3525 matchExpr = FollowedBy(lastExpr + thisExpr) + Group( lastExpr + OneOrMore( thisExpr ) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3526 elif arity == 3:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3527 matchExpr = FollowedBy(lastExpr + opExpr1 + thisExpr + opExpr2 + thisExpr) + \
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3528 Group( lastExpr + opExpr1 + thisExpr + opExpr2 + thisExpr )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3529 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3530 raise ValueError("operator must be unary (1), binary (2), or ternary (3)")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3531 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3532 raise ValueError("operator must indicate right or left associativity")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3533 if pa:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3534 matchExpr.setParseAction( pa )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3535 thisExpr << ( matchExpr | lastExpr )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3536 lastExpr = thisExpr
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3537 ret << lastExpr
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3538 return ret
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3539
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3540 dblQuotedString = Regex(r'"(?:[^"\n\r\\]|(?:"")|(?:\\x[0-9a-fA-F]+)|(?:\\.))*"').setName("string enclosed in double quotes")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3541 sglQuotedString = Regex(r"'(?:[^'\n\r\\]|(?:'')|(?:\\x[0-9a-fA-F]+)|(?:\\.))*'").setName("string enclosed in single quotes")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3542 quotedString = Regex(r'''(?:"(?:[^"\n\r\\]|(?:"")|(?:\\x[0-9a-fA-F]+)|(?:\\.))*")|(?:'(?:[^'\n\r\\]|(?:'')|(?:\\x[0-9a-fA-F]+)|(?:\\.))*')''').setName("quotedString using single or double quotes")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3543 unicodeString = Combine(_L('u') + quotedString.copy())
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3544
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3545 def nestedExpr(opener="(", closer=")", content=None, ignoreExpr=quotedString):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3546 """Helper method for defining nested lists enclosed in opening and closing
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3547 delimiters ("(" and ")" are the default).
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3548
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3549 Parameters:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3550 - opener - opening character for a nested list (default="("); can also be a pyparsing expression
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3551 - closer - closing character for a nested list (default=")"); can also be a pyparsing expression
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3552 - content - expression for items within the nested lists (default=None)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3553 - ignoreExpr - expression for ignoring opening and closing delimiters (default=quotedString)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3554
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3555 If an expression is not provided for the content argument, the nested
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3556 expression will capture all whitespace-delimited content between delimiters
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3557 as a list of separate values.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3558
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3559 Use the ignoreExpr argument to define expressions that may contain
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3560 opening or closing characters that should not be treated as opening
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3561 or closing characters for nesting, such as quotedString or a comment
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3562 expression. Specify multiple expressions using an Or or MatchFirst.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3563 The default is quotedString, but if no expressions are to be ignored,
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3564 then pass None for this argument.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3565 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3566 if opener == closer:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3567 raise ValueError("opening and closing strings cannot be the same")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3568 if content is None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3569 if isinstance(opener,basestring) and isinstance(closer,basestring):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3570 if len(opener) == 1 and len(closer)==1:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3571 if ignoreExpr is not None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3572 content = (Combine(OneOrMore(~ignoreExpr +
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3573 CharsNotIn(opener+closer+ParserElement.DEFAULT_WHITE_CHARS,exact=1))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3574 ).setParseAction(lambda t:t[0].strip()))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3575 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3576 content = (empty+CharsNotIn(opener+closer+ParserElement.DEFAULT_WHITE_CHARS
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3577 ).setParseAction(lambda t:t[0].strip()))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3578 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3579 if ignoreExpr is not None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3580 content = (Combine(OneOrMore(~ignoreExpr +
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3581 ~Literal(opener) + ~Literal(closer) +
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3582 CharsNotIn(ParserElement.DEFAULT_WHITE_CHARS,exact=1))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3583 ).setParseAction(lambda t:t[0].strip()))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3584 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3585 content = (Combine(OneOrMore(~Literal(opener) + ~Literal(closer) +
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3586 CharsNotIn(ParserElement.DEFAULT_WHITE_CHARS,exact=1))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3587 ).setParseAction(lambda t:t[0].strip()))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3588 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3589 raise ValueError("opening and closing arguments must be strings if no content expression is given")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3590 ret = Forward()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3591 if ignoreExpr is not None:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3592 ret << Group( Suppress(opener) + ZeroOrMore( ignoreExpr | ret | content ) + Suppress(closer) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3593 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3594 ret << Group( Suppress(opener) + ZeroOrMore( ret | content ) + Suppress(closer) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3595 return ret
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3596
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3597 def indentedBlock(blockStatementExpr, indentStack, indent=True):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3598 """Helper method for defining space-delimited indentation blocks, such as
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3599 those used to define block statements in Python source code.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3600
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3601 Parameters:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3602 - blockStatementExpr - expression defining syntax of statement that
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3603 is repeated within the indented block
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3604 - indentStack - list created by caller to manage indentation stack
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3605 (multiple statementWithIndentedBlock expressions within a single grammar
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3606 should share a common indentStack)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3607 - indent - boolean indicating whether block must be indented beyond the
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3608 the current level; set to False for block of left-most statements
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3609 (default=True)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3610
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3611 A valid block must contain at least one blockStatement.
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3612 """
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3613 def checkPeerIndent(s,l,t):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3614 if l >= len(s): return
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3615 curCol = col(l,s)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3616 if curCol != indentStack[-1]:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3617 if curCol > indentStack[-1]:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3618 raise ParseFatalException(s,l,"illegal nesting")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3619 raise ParseException(s,l,"not a peer entry")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3620
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3621 def checkSubIndent(s,l,t):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3622 curCol = col(l,s)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3623 if curCol > indentStack[-1]:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3624 indentStack.append( curCol )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3625 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3626 raise ParseException(s,l,"not a subentry")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3627
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3628 def checkUnindent(s,l,t):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3629 if l >= len(s): return
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3630 curCol = col(l,s)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3631 if not(indentStack and curCol < indentStack[-1] and curCol <= indentStack[-2]):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3632 raise ParseException(s,l,"not an unindent")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3633 indentStack.pop()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3634
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3635 NL = OneOrMore(LineEnd().setWhitespaceChars("\t ").suppress())
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3636 INDENT = Empty() + Empty().setParseAction(checkSubIndent)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3637 PEER = Empty().setParseAction(checkPeerIndent)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3638 UNDENT = Empty().setParseAction(checkUnindent)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3639 if indent:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3640 smExpr = Group( Optional(NL) +
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3641 FollowedBy(blockStatementExpr) +
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3642 INDENT + (OneOrMore( PEER + Group(blockStatementExpr) + Optional(NL) )) + UNDENT)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3643 else:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3644 smExpr = Group( Optional(NL) +
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3645 (OneOrMore( PEER + Group(blockStatementExpr) + Optional(NL) )) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3646 blockStatementExpr.ignore(_bslash + LineEnd())
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3647 return smExpr
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3648
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3649 alphas8bit = srange(r"[\0xc0-\0xd6\0xd8-\0xf6\0xf8-\0xff]")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3650 punc8bit = srange(r"[\0xa1-\0xbf\0xd7\0xf7]")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3651
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3652 anyOpenTag,anyCloseTag = makeHTMLTags(Word(alphas,alphanums+"_:"))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3653 commonHTMLEntity = Combine(_L("&") + oneOf("gt lt amp nbsp quot").setResultsName("entity") +";").streamline()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3654 _htmlEntityMap = dict(zip("gt lt amp nbsp quot".split(),'><& "'))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3655 replaceHTMLEntity = lambda t : t.entity in _htmlEntityMap and _htmlEntityMap[t.entity] or None
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3656
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3657 # it's easy to get these comment structures wrong - they're very common, so may as well make them available
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3658 cStyleComment = Regex(r"/\*(?:[^*]*\*+)+?/").setName("C style comment")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3659
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3660 htmlComment = Regex(r"<!--[\s\S]*?-->")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3661 restOfLine = Regex(r".*").leaveWhitespace()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3662 dblSlashComment = Regex(r"\/\/(\\\n|.)*").setName("// comment")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3663 cppStyleComment = Regex(r"/(?:\*(?:[^*]*\*+)+?/|/[^\n]*(?:\n[^\n]*)*?(?:(?<!\\)|\Z))").setName("C++ style comment")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3664
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3665 javaStyleComment = cppStyleComment
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3666 pythonStyleComment = Regex(r"#.*").setName("Python style comment")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3667 _noncomma = "".join( [ c for c in printables if c != "," ] )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3668 _commasepitem = Combine(OneOrMore(Word(_noncomma) +
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3669 Optional( Word(" \t") +
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3670 ~Literal(",") + ~LineEnd() ) ) ).streamline().setName("commaItem")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3671 commaSeparatedList = delimitedList( Optional( quotedString | _commasepitem, default="") ).setName("commaSeparatedList")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3672
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3673
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3674 if __name__ == "__main__":
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3675
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3676 def test( teststring ):
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3677 try:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3678 tokens = simpleSQL.parseString( teststring )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3679 tokenlist = tokens.asList()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3680 print (teststring + "->" + str(tokenlist))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3681 print ("tokens = " + str(tokens))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3682 print ("tokens.columns = " + str(tokens.columns))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3683 print ("tokens.tables = " + str(tokens.tables))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3684 print (tokens.asXML("SQL",True))
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3685 except ParseBaseException:
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3686 err = sys.exc_info()[1]
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3687 print (teststring + "->")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3688 print (err.line)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3689 print (" "*(err.column-1) + "^")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3690 print (err)
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3691 print()
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3692
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3693 selectToken = CaselessLiteral( "select" )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3694 fromToken = CaselessLiteral( "from" )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3695
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3696 ident = Word( alphas, alphanums + "_$" )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3697 columnName = delimitedList( ident, ".", combine=True ).setParseAction( upcaseTokens )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3698 columnNameList = Group( delimitedList( columnName ) )#.setName("columns")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3699 tableName = delimitedList( ident, ".", combine=True ).setParseAction( upcaseTokens )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3700 tableNameList = Group( delimitedList( tableName ) )#.setName("tables")
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3701 simpleSQL = ( selectToken + \
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3702 ( '*' | columnNameList ).setResultsName( "columns" ) + \
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3703 fromToken + \
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3704 tableNameList.setResultsName( "tables" ) )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3705
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3706 test( "SELECT * from XYZZY, ABC" )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3707 test( "select * from SYS.XYZZY" )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3708 test( "Select A from Sys.dual" )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3709 test( "Select AA,BB,CC from Sys.dual" )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3710 test( "Select A, B, C from Sys.dual" )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3711 test( "Select A, B, C from Sys.dual" )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3712 test( "Xelect A, B, C from Sys.dual" )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3713 test( "Select A, B, C frox Sys.dual" )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3714 test( "Select" )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3715 test( "Select ^^^ frox Sys.dual" )
12010fcc4e38 pyparsing_py3'
catherine@Drou
parents:
diff changeset
3716 test( "Select A, B, C from Sys.dual, Table2 " )