diff options
Diffstat (limited to 'pyparsing.py')
-rw-r--r-- | pyparsing.py | 554 |
1 files changed, 283 insertions, 271 deletions
diff --git a/pyparsing.py b/pyparsing.py index c20f2a0..7844f12 100644 --- a/pyparsing.py +++ b/pyparsing.py @@ -28,16 +28,18 @@ __doc__ = \ pyparsing module - Classes and methods to define and execute parsing grammars ============================================================================= -The pyparsing module is an alternative approach to creating and executing simple grammars, -vs. the traditional lex/yacc approach, or the use of regular expressions. With pyparsing, you -don't need to learn a new syntax for defining grammars or matching expressions - the parsing module -provides a library of classes that you use to construct the grammar directly in Python. +The pyparsing module is an alternative approach to creating and +executing simple grammars, vs. the traditional lex/yacc approach, or the +use of regular expressions. With pyparsing, you don't need to learn +a new syntax for defining grammars or matching expressions - the parsing +module provides a library of classes that you use to construct the +grammar directly in Python. Here is a program to parse "Hello, World!" (or any greeting of the form -C{"<salutation>, <addressee>!"}), built up using :class:`Word`, :class:`Literal`, and :class:`And` elements -(:class:`'+'<ParserElement.__add__>` operator gives :class:`And` expressions, strings are auto-converted to -:class:`Literal` expressions):: - +:code:`"<salutation>, <addressee>!"`), built up using :class:`Word`, +:class:`Literal`, and :class:`And` elements +(:code:`ParserElement.__add__` operator gives :class:`And` expressions, +strings are auto-converted to :class:`Literal` expressions):: from pyparsing import Word, alphas # define grammar of a greeting @@ -47,7 +49,6 @@ C{"<salutation>, <addressee>!"}), built up using :class:`Word`, :class:`Literal` print (hello, "->", greet.parseString(hello)) The program outputs the following:: - Hello, World! -> ['Hello', ',', 'World', '!'] The Python representation of the grammar is quite readable, owing to the self-explanatory @@ -278,6 +279,7 @@ class ParseException(ParseBaseException): - line - returns the line containing the exception text Example:: + try: Word(nums).setName("integer").parseString("ABC") except ParseException as pe: @@ -285,6 +287,7 @@ class ParseException(ParseBaseException): print("column: {}".format(pe.col)) prints:: + Expected integer (at char 0), (line:1, col:1) column: 1 """ @@ -335,11 +338,12 @@ class _ParseResultsWithOffset(object): class ParseResults(object): """ Structured parse results, to provide multiple means of access to the parsed data: - - as a list (C{len(results)}) - - by list index (C{results[0], results[1]}, etc.) - - by attribute (C{results.<resultsName>} - see :class:`ParserElement.setResultsName`) + - as a list (:code:`len(results)`) + - by list index (:code:`results[0], results[1]`, etc.) + - by attribute (:code:`results.<resultsName>` - see :class:`ParserElement.setResultsName`) Example:: + integer = Word(nums) date_str = (integer.setResultsName("year") + '/' + integer.setResultsName("month") + '/' @@ -359,7 +363,9 @@ class ParseResults(object): test("'month' in result") test("'minutes' in result") test("result.dump()", str) + prints:: + list(result) -> ['1999', '/', '12', '/', '31'] result[0] -> '1999' result['month'] -> '12' @@ -521,14 +527,14 @@ class ParseResults(object): def pop( self, *args, **kwargs): """ - Removes and returns item at specified index (default=C{last}). - Supports both ``list`` and ``dict`` semantics for C{pop()}. If passed no - argument or an integer argument, it will use ``list`` semantics - and pop tokens from the list of parsed tokens. If passed a - non-integer argument (most likely a string), it will use ``dict`` - semantics and pop the corresponding value from any defined - results names. A second default return value argument is - supported, just as in C{dict.pop()}. + Removes and returns item at specified index (default=:code:`last`). + Supports both ``list`` and ``dict`` semantics for :code:`pop()`. If + passed no argument or an integer argument, it will use ``list`` + semantics and pop tokens from the list of parsed tokens. If passed + a non-integer argument (most likely a string), it will use ``dict`` + semantics and pop the corresponding value from any defined results + names. A second default return value argument is supported, just as in + :code:`dict.pop()`. Example:: def remove_first(tokens): @@ -574,10 +580,10 @@ class ParseResults(object): def get(self, key, defaultValue=None): """ Returns named result matching the given key, or if there is no - such name, then returns the given C{defaultValue} or ``None`` if no - C{defaultValue} is specified. + such name, then returns the given :code:`defaultValue` or ``None`` if no + :code:`defaultValue` is specified. - Similar to C{dict.get()}. + Similar to :code:`dict.get()`. Example:: integer = Word(nums) @@ -597,7 +603,7 @@ class ParseResults(object): """ Inserts new element at location index in the list of parsed tokens. - Similar to C{list.insert()}. + Similar to :code:`list.insert()`. Example:: print(OneOrMore(Word(nums)).parseString("0 123 321")) # -> ['0', '123', '321'] @@ -765,7 +771,7 @@ class ParseResults(object): def copy( self ): """ - Returns a new copy of a C{ParseResults} object. + Returns a new copy of a :code:`ParseResults` object. """ ret = ParseResults( self.__toklist ) ret.__tokdict = dict(self.__tokdict.items()) @@ -881,9 +887,9 @@ class ParseResults(object): def dump(self, indent='', depth=0, full=True): """ - Diagnostic method for listing out the contents of a C{ParseResults}. - Accepts an optional C{indent} argument so that this string can be embedded - in a nested display of other data. + Diagnostic method for listing out the contents of + a :code:`ParseResults`. Accepts an optional :code:`indent` argument so + that this string can be embedded in a nested display of other data. Example:: integer = Word(nums) @@ -926,9 +932,9 @@ class ParseResults(object): def pprint(self, *args, **kwargs): """ - Pretty-printer for parsed results as a list, using the C{pprint} module. + Pretty-printer for parsed results as a list, using the :code:`pprint` module. Accepts additional positional or keyword args as defined for the - C{pprint.pprint} method. (U{http://docs.python.org/3/library/pprint.html#pprint.pprint}) + `:code:`pprint.pprint` method<http://docs.python.org/3/library/pprint.html#pprint.pprint>`__. Example:: ident = Word(alphas, alphanums) @@ -981,10 +987,11 @@ def col (loc,strg): The first column is number 1. Note: the default parsing behavior is to expand tabs in the input string - before starting the parsing process. See :class:`I{ParserElement.parseString`<ParserElement.parseString>} for more information - on parsing strings containing C{<TAB>}s, and suggested methods to maintain a - consistent view of the parsed string, the parse location, and line and column - positions within the parsed string. + before starting the parsing process. See + :class:`ParserElement.parseString` for more + information on parsing strings containing :code:`<TAB>`s, and suggested + methods to maintain a consistent view of the parsed string, the parse + location, and line and column positions within the parsed string. """ s = strg return 1 if 0<loc<len(s) and s[loc-1] == '\n' else loc - s.rfind("\n", 0, loc) @@ -994,10 +1001,10 @@ def lineno(loc,strg): The first line is number 1. Note: the default parsing behavior is to expand tabs in the input string - before starting the parsing process. See :class:`I{ParserElement.parseString`<ParserElement.parseString>} for more information - on parsing strings containing C{<TAB>}s, and suggested methods to maintain a - consistent view of the parsed string, the parse location, and line and column - positions within the parsed string. + before starting the parsing process. See :class:`ParserElement.parseString` + for more information on parsing strings containing :code:`<TAB>`s, and + suggested methods to maintain a consistent view of the parsed string, the + parse location, and line and column positions within the parsed string. """ return strg.count("\n",0,loc) + 1 @@ -1177,8 +1184,9 @@ class ParserElement(object): def copy( self ): """ - Make a copy of this C{ParserElement}. Useful for defining different parse actions - for the same parsing pattern, using copies of the original parse element. + Make a copy of this :code:`ParserElement`. Useful for defining + different parse actions for the same parsing pattern, using copies of + the original parse element. Example:: integer = Word(nums).setParseAction(lambda toks: int(toks[0])) @@ -1188,7 +1196,7 @@ class ParserElement(object): print(OneOrMore(integerK | integerM | integer).parseString("5K 100 640K 256M")) prints:: [5120, 100, 655360, 268435456] - Equivalent form of C{expr.copy()} is just C{expr()}:: + Equivalent form of :code:`expr.copy()` is just :code:`expr()`:: integerM = integer().addParseAction(lambda toks: toks[0]*1024*1024) + Suppress("M") """ cpy = copy.copy( self ) @@ -1216,13 +1224,13 @@ class ParserElement(object): """ Define name for referencing matching tokens as a nested attribute of the returned parse results. - NOTE: this returns a *copy* of the original C{ParserElement} object; + NOTE: this returns a *copy* of the original :code:`ParserElement` object; this is so that the client can define a basic element, such as an integer, and reference it in multiple places with different names. You can also set results names using the abbreviated syntax, - C{expr("name")} in place of C{expr.setResultsName("name")} - - see :class:`I{__call__`<__call__>}. + :code:`expr("name")` in place of :code:`expr.setResultsName("name")` + - see :class:`__call__`. Example:: date_str = (integer.setResultsName("year") + '/' @@ -1242,7 +1250,7 @@ class ParserElement(object): def setBreak(self,breakFlag = True): """Method to invoke the Python pdb debugger when this element is - about to be parsed. Set C{breakFlag} to True to enable, False to + about to be parsed. Set :code:`breakFlag` to True to enable, False to disable. """ if breakFlag: @@ -1261,23 +1269,23 @@ class ParserElement(object): def setParseAction( self, *fns, **kwargs ): """ Define one or more actions to perform when successfully matching parse element definition. - Parse action fn is a callable method with 0-3 arguments, called as C{fn(s,loc,toks)}, - C{fn(loc,toks)}, C{fn(toks)}, or just C{fn()}, where: + Parse action fn is a callable method with 0-3 arguments, called as :code:`fn(s,loc,toks)`, + :code:`fn(loc,toks)`, :code:`fn(toks)`, or just :code:`fn()`, where: - s = the original string being parsed (see note below) - loc = the location of the matching substring - - toks = a list of the matched tokens, packaged as a C{:class:`ParseResults`} object + - toks = a list of the matched tokens, packaged as a :code:`:class:`ParseResults`` object If the functions in fns modify the tokens, they can return them as the return value from fn, and the modified list of tokens will replace the original. Otherwise, fn does not need to return any value. Optional keyword arguments: - - callDuringTry = (default=C{False}) indicate if parse action should be run during lookaheads and alternate testing + - callDuringTry = (default=:code:`False`) indicate if parse action should be run during lookaheads and alternate testing Note: the default parsing behavior is to expand tabs in the input string - before starting the parsing process. See :class:`I{parseString`<parseString>} for more information - on parsing strings containing C{<TAB>}s, and suggested methods to maintain a - consistent view of the parsed string, the parse location, and line and column - positions within the parsed string. + before starting the parsing process. See :class:`parseString for more + information on parsing strings containing :code:`<TAB>`s, and suggested + methods to maintain a consistent view of the parsed string, the parse + location, and line and column positions within the parsed string. Example:: integer = Word(nums) @@ -1298,9 +1306,9 @@ class ParserElement(object): def addParseAction( self, *fns, **kwargs ): """ - Add one or more parse actions to expression's list of parse actions. See :class:`I{setParseAction`<setParseAction>}. + Add one or more parse actions to expression's list of parse actions. See :class:`setParseAction`. - See examples in :class:`I{copy`<copy>}. + See examples in :class:`copy`. """ self.parseAction += list(map(_trim_arity, list(fns))) self.callDuringTry = self.callDuringTry or kwargs.get("callDuringTry", False) @@ -1308,8 +1316,8 @@ class ParserElement(object): def addCondition(self, *fns, **kwargs): """Add a boolean predicate function to expression's list of parse actions. See - :class:`I{setParseAction`<setParseAction>} for function call signatures. Unlike C{setParseAction}, - functions passed to C{addCondition} need to return boolean success/fail of the condition. + :class:`setParseAction` for function call signatures. Unlike :code:`setParseAction`, + functions passed to :code:`addCondition` need to return boolean success/fail of the condition. Optional keyword arguments: - message = define a custom message to be used in the raised exception @@ -1336,12 +1344,12 @@ class ParserElement(object): def setFailAction( self, fn ): """Define action to perform if parsing fails at this expression. Fail acton fn is a callable function that takes the arguments - C{fn(s,loc,expr,err)} where: + :code:`fn(s,loc,expr,err)` where: - s = string being parsed - loc = location where expression match was attempted and failed - expr = the parse expression that failed - err = the exception thrown - The function returns no value. It may throw C{:class:`ParseFatalException`} + The function returns no value. It may throw :code:`:class:`ParseFatalException`` if it is desired to stop parsing immediately.""" self.failAction = fn return self @@ -1602,7 +1610,7 @@ class ParserElement(object): both valid results and parsing exceptions. Parameters: - - cache_size_limit - (default=C{128}) - if an integer value is provided + - cache_size_limit - (default=:code:`128`) - if an integer value is provided will limit the size of the packrat cache; if None is passed, then the cache size will be unbounded; if 0 is passed, the cache will be effectively disabled. @@ -1610,10 +1618,10 @@ class ParserElement(object): This speedup may break existing programs that use parse actions that have side-effects. For this reason, packrat parsing is disabled when you first import pyparsing. To activate the packrat feature, your - program must call the class method C{ParserElement.enablePackrat()}. If - your program uses C{psyco} to "compile as you go", you must call - C{enablePackrat} before calling C{psyco.full()}. If you do not do this, - Python will crash. For best results, call C{enablePackrat()} immediately + program must call the class method :code:`ParserElement.enablePackrat()`. If + your program uses :code:`psyco` to "compile as you go", you must call + :code:`enablePackrat` before calling :code:`psyco.full()`. If you do not do this, + Python will crash. For best results, call :code:`enablePackrat()` immediately after importing pyparsing. Example:: @@ -1635,21 +1643,21 @@ class ParserElement(object): expression has been built. If you want the grammar to require that the entire input string be - successfully parsed, then set C{parseAll} to True (equivalent to ending - the grammar with C{:class:`StringEnd()`}). + successfully parsed, then set :code:`parseAll` to True (equivalent to ending + the grammar with :code:`:class:`StringEnd()``). - Note: C{parseString} implicitly calls C{expandtabs()} on the input string, + Note: :code:`parseString` implicitly calls :code:`expandtabs()` on the input string, in order to report proper column numbers in parse actions. If the input string contains tabs and - the grammar uses parse actions that use the C{loc} argument to index into the + the grammar uses parse actions that use the :code:`loc` argument to index into the string being parsed, you can ensure you have a consistent view of the input string by: - - calling C{parseWithTabs} on your grammar before calling C{parseString} - (see :class:`I{parseWithTabs`<parseWithTabs>}) - - define your parse action using the full C{(s,loc,toks)} signature, and - reference the input string using the parse action's C{s} argument + - calling :code:`parseWithTabs` on your grammar before calling :code:`parseString` + (see :class:`parseWithTabs`) + - define your parse action using the full :code:`(s,loc,toks)` signature, and + reference the input string using the parse action's :code:`s` argument - explictly expand the tabs in your input string before calling - C{parseString} + :code:`parseString` Example:: Word('a').parseString('aaaaabaaa') # -> ['aaaaa'] @@ -1682,11 +1690,11 @@ class ParserElement(object): """ Scan the input string for expression matches. Each match will return the matching tokens, start location, and end location. May be called with optional - C{maxMatches} argument, to clip scanning after 'n' matches are found. If - C{overlap} is specified, then overlapping matches will be reported. + :code:`maxMatches` argument, to clip scanning after 'n' matches are found. If + :code:`overlap` is specified, then overlapping matches will be reported. Note that the start and end locations are reported relative to the string - being parsed. See :class:`I{parseString`<parseString>} for more information on parsing + being parsed. See :class:`parseString` for more information on parsing strings with embedded tabs. Example:: @@ -1751,12 +1759,12 @@ class ParserElement(object): def transformString( self, instring ): """ - Extension to C{:class:`scanString`}, to modify matching text with modified tokens that may - be returned from a parse action. To use C{transformString}, define a grammar and + Extension to :code:`:class:`scanString``, to modify matching text with modified tokens that may + be returned from a parse action. To use :code:`transformString`, define a grammar and attach a parse action to it that modifies the returned token list. - Invoking C{transformString()} on a target string will then scan for matches, + Invoking :code:`transformString()` on a target string will then scan for matches, and replace the matched text patterns according to the logic in the parse - action. C{transformString()} returns the resulting transformed string. + action. :code:`transformString()` returns the resulting transformed string. Example:: wd = Word(alphas) @@ -1794,9 +1802,9 @@ class ParserElement(object): def searchString( self, instring, maxMatches=_MAX_INT ): """ - Another extension to C{:class:`scanString`}, simplifying the access to the tokens found + Another extension to :code:`:class:`scanString``, simplifying the access to the tokens found to match the given parse expression. May be called with optional - C{maxMatches} argument, to clip searching after 'n' matches are found. + :code:`maxMatches` argument, to clip searching after 'n' matches are found. Example:: # a capitalized word starts with an uppercase letter, followed by zero or more lowercase letters @@ -1822,8 +1830,8 @@ class ParserElement(object): def split(self, instring, maxsplit=_MAX_INT, includeSeparators=False): """ Generator method to split a string using the given expression as a separator. - May be called with optional C{maxsplit} argument, to limit the number of splits; - and the optional C{includeSeparators} argument (default=C{False}), if the separating + May be called with optional :code:`maxsplit` argument, to limit the number of splits; + and the optional :code:`includeSeparators` argument (default=:code:`False`), if the separating matching text should be included in the split results. Example:: @@ -1843,7 +1851,7 @@ class ParserElement(object): def __add__(self, other ): """ - Implementation of + operator - returns C{:class:`And`}. Adding strings to a ParserElement + Implementation of + operator - returns :code:`:class:`And``. Adding strings to a ParserElement converts them to :class:`Literal`s by default. Example:: @@ -1863,7 +1871,7 @@ class ParserElement(object): def __radd__(self, other ): """ - Implementation of + operator when left operand is not a C{:class:`ParserElement`} + Implementation of + operator when left operand is not a :code:`:class:`ParserElement`` """ if isinstance( other, basestring ): other = ParserElement._literalStringClass( other ) @@ -1875,7 +1883,7 @@ class ParserElement(object): def __sub__(self, other): """ - Implementation of - operator, returns C{:class:`And`} with error stop + Implementation of - operator, returns :code:`:class:`And`` with error stop """ if isinstance( other, basestring ): other = ParserElement._literalStringClass( other ) @@ -1887,7 +1895,7 @@ class ParserElement(object): def __rsub__(self, other ): """ - Implementation of - operator when left operand is not a C{:class:`ParserElement`} + Implementation of - operator when left operand is not a :code:`:class:`ParserElement`` """ if isinstance( other, basestring ): other = ParserElement._literalStringClass( other ) @@ -1899,23 +1907,23 @@ class ParserElement(object): def __mul__(self,other): """ - Implementation of * operator, allows use of C{expr * 3} in place of - C{expr + expr + expr}. Expressions may also me multiplied by a 2-integer - tuple, similar to C{{min,max}} multipliers in regular expressions. Tuples + Implementation of * operator, allows use of :code:`expr * 3` in place of + :code:`expr + expr + expr`. Expressions may also me multiplied by a 2-integer + tuple, similar to :code:`{min,max`} multipliers in regular expressions. Tuples may also include ``None`` as in: - - C{expr*(n,None)} or C{expr*(n,)} is equivalent - to C{expr*n + :class:`ZeroOrMore`(expr)} - (read as "at least n instances of C{expr}") - - C{expr*(None,n)} is equivalent to C{expr*(0,n)} - (read as "0 to n instances of C{expr}") - - C{expr*(None,None)} is equivalent to C{:class:`ZeroOrMore`(expr)} - - C{expr*(1,None)} is equivalent to C{:class:`OneOrMore`(expr)} - - Note that C{expr*(None,n)} does not raise an exception if + - :code:`expr*(n,None)` or :code:`expr*(n,)` is equivalent + to :code:`expr*n + :class:`ZeroOrMore`(expr)` + (read as "at least n instances of :code:`expr`") + - :code:`expr*(None,n)` is equivalent to :code:`expr*(0,n)` + (read as "0 to n instances of :code:`expr`") + - :code:`expr*(None,None)` is equivalent to :code:`:class:`ZeroOrMore`(expr)` + - :code:`expr*(1,None)` is equivalent to :code:`:class:`OneOrMore`(expr)` + + Note that :code:`expr*(None,n)` does not raise an exception if more than n exprs exist in the input stream; that is, - C{expr*(None,n)} does not enforce a maximum number of expr + :code:`expr*(None,n)` does not enforce a maximum number of expr occurrences. If this behavior is desired, then write - C{expr*(None,n) + ~expr} + :code:`expr*(None,n) + ~expr` """ if isinstance(other,int): minElements, optElements = other,0 @@ -1970,7 +1978,7 @@ class ParserElement(object): def __or__(self, other ): """ - Implementation of | operator - returns C{:class:`MatchFirst`} + Implementation of | operator - returns :code:`:class:`MatchFirst`` """ if isinstance( other, basestring ): other = ParserElement._literalStringClass( other ) @@ -1982,7 +1990,7 @@ class ParserElement(object): def __ror__(self, other ): """ - Implementation of | operator when left operand is not a C{:class:`ParserElement`} + Implementation of | operator when left operand is not a :code:`:class:`ParserElement`` """ if isinstance( other, basestring ): other = ParserElement._literalStringClass( other ) @@ -1994,7 +2002,7 @@ class ParserElement(object): def __xor__(self, other ): """ - Implementation of ^ operator - returns C{:class:`Or`} + Implementation of ^ operator - returns :code:`:class:`Or`` """ if isinstance( other, basestring ): other = ParserElement._literalStringClass( other ) @@ -2006,7 +2014,7 @@ class ParserElement(object): def __rxor__(self, other ): """ - Implementation of ^ operator when left operand is not a C{:class:`ParserElement`} + Implementation of ^ operator when left operand is not a :code:`:class:`ParserElement`` """ if isinstance( other, basestring ): other = ParserElement._literalStringClass( other ) @@ -2018,7 +2026,7 @@ class ParserElement(object): def __and__(self, other ): """ - Implementation of & operator - returns C{:class:`Each`} + Implementation of & operator - returns :code:`:class:`Each`` """ if isinstance( other, basestring ): other = ParserElement._literalStringClass( other ) @@ -2030,7 +2038,7 @@ class ParserElement(object): def __rand__(self, other ): """ - Implementation of & operator when left operand is not a C{:class:`ParserElement`} + Implementation of & operator when left operand is not a :code:`:class:`ParserElement`` """ if isinstance( other, basestring ): other = ParserElement._literalStringClass( other ) @@ -2042,18 +2050,18 @@ class ParserElement(object): def __invert__( self ): """ - Implementation of ~ operator - returns C{:class:`NotAny`} + Implementation of ~ operator - returns :code:`:class:`NotAny`` """ return NotAny( self ) def __call__(self, name=None): """ - Shortcut for C{:class:`setResultsName`}, with C{listAllMatches=False}. + Shortcut for :code:`:class:`setResultsName``, with :code:`listAllMatches=False`. - If C{name} is given with a trailing C{'*'} character, then C{listAllMatches} will be - passed as C{True}. + If :code:`name` is given with a trailing :code:`'*'` character, then :code:`listAllMatches` will be + passed as :code:`True`. - If C{name} is omitted, same as calling C{:class:`copy`}. + If :code:`name` is omitted, same as calling :code:`:class:`copy``. Example:: # these are equivalent @@ -2067,7 +2075,7 @@ class ParserElement(object): def suppress( self ): """ - Suppresses the output of this C{ParserElement}; useful to keep punctuation from + Suppresses the output of this :code:`ParserElement`; useful to keep punctuation from cluttering up returned output. """ return Suppress( self ) @@ -2075,7 +2083,7 @@ class ParserElement(object): def leaveWhitespace( self ): """ Disables the skipping of whitespace before matching the characters in the - C{ParserElement}'s defined pattern. This is normally only used internally by + :code:`ParserElement`'s defined pattern. This is normally only used internally by the pyparsing module, but may be needed in some whitespace-sensitive grammars. """ self.skipWhitespace = False @@ -2092,9 +2100,9 @@ class ParserElement(object): def parseWithTabs( self ): """ - Overrides default behavior to expand C{<TAB>}s to spaces before parsing the input string. - Must be called before C{parseString} when the input grammar contains elements that - match C{<TAB>} characters. + Overrides default behavior to expand :code:`<TAB>`s to spaces before parsing the input string. + Must be called before :code:`parseString` when the input grammar contains elements that + match :code:`<TAB>` characters. """ self.keepTabs = True return self @@ -2135,7 +2143,7 @@ class ParserElement(object): def setDebug( self, flag=True ): """ Enable display of debugging messages while doing pattern matching. - Set C{flag} to True to enable, False to disable. + Set :code:`flag` to True to enable, False to disable. Example:: wd = Word(alphas).setName("alphaword") @@ -2161,11 +2169,11 @@ class ParserElement(object): The output shown is that produced by the default debug actions - custom debug actions can be specified using :class:`setDebugActions`. Prior to attempting - to match the C{wd} expression, the debugging message C{"Match <exprname> at loc <n>(<line>,<col>)"} - is shown. Then if the parse succeeds, a C{"Matched"} message is shown, or an C{"Exception raised"} + to match the :code:`wd` expression, the debugging message :code:`"Match <exprname> at loc <n>(<line>,<col>)"` + is shown. Then if the parse succeeds, a :code:`"Matched"` message is shown, or an :code:`"Exception raised"` message is shown. Also note the use of :class:`setName` to assign a human-readable name to the expression, which makes debugging and exception messages easier to understand - for instance, the default - name created for the C{Word} expression without calling C{setName} is C{"W:(ABCD...)"}. + name created for the :code:`Word` expression without calling :code:`setName` is :code:`"W:(ABCD...)"`. """ if flag: self.setDebugActions( _defaultStartDebugAction, _defaultSuccessDebugAction, _defaultExceptionDebugAction ) @@ -2240,7 +2248,7 @@ class ParserElement(object): Parameters: - testString - to test against this expression for a match - - parseAll - (default=C{True}) - flag to pass to C{:class:`parseString`} when running tests + - parseAll - (default=:code:`True`) - flag to pass to :code:`:class:`parseString`` when running tests Example:: expr = Word(nums) @@ -2260,16 +2268,16 @@ class ParserElement(object): Parameters: - tests - a list of separate test strings, or a multiline string of test strings - - parseAll - (default=C{True}) - flag to pass to C{:class:`parseString`} when running tests - - comment - (default=C{'#'}) - expression for indicating embedded comments in the test + - parseAll - (default=:code:`True`) - flag to pass to :code:`:class:`parseString`` when running tests + - comment - (default=:code:`'#'`) - expression for indicating embedded comments in the test string; pass None to disable comment filtering - - fullDump - (default=C{True}) - dump results as list followed by results names in nested outline; + - fullDump - (default=:code:`True`) - dump results as list followed by results names in nested outline; if False, only dump nested list - - printResults - (default=C{True}) prints test output to stdout - - failureTests - (default=C{False}) indicates if these tests are expected to fail parsing + - printResults - (default=:code:`True`) prints test output to stdout + - failureTests - (default=:code:`False`) indicates if these tests are expected to fail parsing Returns: a (success, results) tuple, where success indicates that all tests succeeded - (or failed if C{failureTests} is True), and the results contain a list of lines of each + (or failed if :code:`failureTests` is True), and the results contain a list of lines of each test's output Example:: @@ -2386,7 +2394,7 @@ class ParserElement(object): class Token(ParserElement): """ - Abstract C{ParserElement} subclass, for defining atomic matching patterns. + Abstract :code:`ParserElement` subclass, for defining atomic matching patterns. """ def __init__( self ): super(Token,self).__init__( savelist=False ) @@ -2462,13 +2470,13 @@ ParserElement._literalStringClass = Literal class Keyword(Token): """ Token to exactly match a specified string as a keyword, that is, it must be - immediately followed by a non-keyword character. Compare with C{:class:`Literal`}: - - C{Literal("if")} will match the leading C{'if'} in C{'ifAndOnlyIf'}. - - C{Keyword("if")} will not; it will only match the leading C{'if'} in C{'if x=1'}, or C{'if(y==2)'} + immediately followed by a non-keyword character. Compare with :code:`:class:`Literal``: + - :code:`Literal("if")` will match the leading :code:`'if'` in :code:`'ifAndOnlyIf'`. + - :code:`Keyword("if")` will not; it will only match the leading :code:`'if'` in :code:`'if x=1'`, or :code:`'if(y==2)'` Accepts two optional constructor arguments in addition to the keyword string: - - C{identChars} is a string of characters that would be valid identifier characters, + - :code:`identChars` is a string of characters that would be valid identifier characters, defaulting to all alphanumerics + "_" and "$" - - C{caseless} allows case-insensitive matching, default is C{False}. + - :code:`caseless` allows case-insensitive matching, default is :code:`False`. Example:: Keyword("start").parseString("start") # -> ['start'] @@ -2568,15 +2576,15 @@ class CaselessKeyword(Keyword): class CloseMatch(Token): """ A variation on :class:`Literal` which matches "close" matches, that is, - strings with at most 'n' mismatching characters. C{CloseMatch} takes parameters: - - C{match_string} - string to be matched - - C{maxMismatches} - (C{default=1}) maximum number of mismatches allowed to count as a match + strings with at most 'n' mismatching characters. :code:`CloseMatch` takes parameters: + - :code:`match_string` - string to be matched + - :code:`maxMismatches` - (:code:`default=1`) maximum number of mismatches allowed to count as a match The results from a successful parse will contain the matched text from the input string and the following named results: - - C{mismatches} - a list of the positions within the match_string where mismatches were found - - C{original} - the original match_string used to compare against the input string + - :code:`mismatches` - a list of the positions within the match_string where mismatches were found + - :code:`original` - the original match_string used to compare against the input string - If C{mismatches} is an empty list, then the match was an exact match. + If :code:`mismatches` is an empty list, then the match was an exact match. Example:: patt = CloseMatch("ATCATCGAATGGA") @@ -2632,19 +2640,19 @@ class Word(Token): Defined with string containing all allowed initial characters, an optional string containing allowed body characters (if omitted, defaults to the initial character set), and an optional minimum, - maximum, and/or exact length. The default value for C{min} is 1 (a - minimum value < 1 is not valid); the default values for C{max} and C{exact} + maximum, and/or exact length. The default value for :code:`min` is 1 (a + minimum value < 1 is not valid); the default values for :code:`max` and :code:`exact` are 0, meaning no maximum or exact length restriction. An optional - C{excludeChars} parameter can list characters that might be found in - the input C{bodyChars} string; useful to define a word of all printables + :code:`excludeChars` parameter can list characters that might be found in + the input :code:`bodyChars` string; useful to define a word of all printables except for one or two characters, for instance. :class:`srange` is useful for defining custom character set strings for defining - C{Word} expressions, using range notation from regular expression character sets. + :code:`Word` expressions, using range notation from regular expression character sets. - A common mistake is to use C{Word} to match a specific literal string, as in - C{Word("Address")}. Remember that C{Word} uses the string argument to define - I{sets} of matchable characters. This expression would match "Add", "AAA", + A common mistake is to use :code:`Word` to match a specific literal string, as in + :code:`Word("Address")`. Remember that :code:`Word` uses the string argument to define + *sets* of matchable characters. This expression would match "Add", "AAA", "dAred", or any other word made up of the characters 'A', 'd', 'r', 'e', and 's'. To match an exact literal string, use :class:`Literal` or :class:`Keyword`. @@ -2787,7 +2795,7 @@ class Word(Token): class Char(Word): """ - A short-cut class for defining C{Word(characters, exact=1)}, + A short-cut class for defining :code:`Word(characters, exact=1)`, when defining a match of any single character in a string of characters. """ def __init__(self, charset): @@ -2800,14 +2808,14 @@ class Regex(Token): r""" Token for matching strings that match a given regular expression. Defined with string specifying the regular expression in a form recognized by the inbuilt Python re module. - If the given regex contains named groups (defined using C{(?P<name>...)}), these will be preserved as + If the given regex contains named groups (defined using :code:`(?P<name>...)`), these will be preserved as named parse results. Example:: realnum = Regex(r"[+-]?\d+\.\d*") date = Regex(r'(?P<year>\d{4})-(?P<month>\d\d?)-(?P<day>\d\d?)') # ref: http://stackoverflow.com/questions/267399/how-do-you-match-only-valid-roman-numerals-with-a-regular-expression - roman = Regex(r"M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})") + roman = Regex(r"M{0,4}(CM|CD|D?:code:`0,3`)(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})") make_html = Regex(r"(\w+):(.*?):").sub(r"<\1>\2</\1>") print(make_html.transformString("h1:main title:")) @@ -2815,7 +2823,7 @@ class Regex(Token): """ compiledREtype = type(re.compile("[A-Z]")) def __init__( self, pattern, flags=0, asGroupList=False, asMatch=False): - """The parameters C{pattern} and C{flags} are passed to the C{re.compile()} function as-is. See the Python C{re} module for an explanation of the acceptable patterns and flags.""" + """The parameters :code:`pattern` and :code:`flags` are passed to the :code:`re.compile()` function as-is. See the Python :code:`re` module for an explanation of the acceptable patterns and flags.""" super(Regex,self).__init__() if isinstance(pattern, basestring): @@ -2882,7 +2890,7 @@ class Regex(Token): def sub(self, repl): """ Return Regex with an attached parse action to transform the parsed - result as if called using C{re.sub(expr, repl, string)}. + result as if called using :code:`re.sub(expr, repl, string)`. """ if self.asGroupList: warnings.warn("cannot use sub() with Regex(asGroupList=True)", @@ -2910,10 +2918,10 @@ class QuotedString(Token): - quoteChar - string of one or more characters defining the quote delimiting string - escChar - character to escape quotes, typically backslash (default=``None``) - escQuote - special quote sequence to escape an embedded quote string (such as SQL's "" to escape an embedded ") (default=``None``) - - multiline - boolean indicating whether quotes can span multiple lines (default=C{False}) - - unquoteResults - boolean indicating whether the matched text should be unquoted (default=C{True}) + - multiline - boolean indicating whether quotes can span multiple lines (default=:code:`False`) + - unquoteResults - boolean indicating whether the matched text should be unquoted (default=:code:`True`) - endQuoteChar - string of one or more characters defining the end of the quote delimited string (default=``None`` => same as quoteChar) - - convertWhitespaceEscapes - convert escaped whitespace (C{'\t'}, C{'\n'}, etc.) to actual whitespace (default=C{True}) + - convertWhitespaceEscapes - convert escaped whitespace (:code:`'\t'`, :code:`'\n'`, etc.) to actual whitespace (default=:code:`True`) Example:: qs = QuotedString('"') @@ -3041,11 +3049,11 @@ class QuotedString(Token): class CharsNotIn(Token): """ - Token for matching words composed of characters I{not} in a given set (will + Token for matching words composed of characters *not* in a given set (will include whitespace in matched characters if not listed in the provided exclusion set - see example). Defined with string containing all disallowed characters, and an optional - minimum, maximum, and/or exact length. The default value for C{min} is 1 (a - minimum value < 1 is not valid); the default values for C{max} and C{exact} + minimum, maximum, and/or exact length. The default value for :code:`min` is 1 (a + minimum value < 1 is not valid); the default values for :code:`max` and :code:`exact` are 0, meaning no maximum or exact length restriction. Example:: @@ -3115,8 +3123,8 @@ class White(Token): Special matching class for matching whitespace. Normally, whitespace is ignored by pyparsing grammars. This class is included when some whitespace structures are significant. Define with a string containing the whitespace characters to be - matched; default is C{" \\t\\r\\n"}. Also takes optional C{min}, C{max}, and C{exact} arguments, - as defined for the C{:class:`Word`} class. + matched; default is :code:`" \\t\\r\\n"`. Also takes optional :code:`min`, :code:`max`, and :code:`exact` arguments, + as defined for the :code:`:class:`Word`` class. """ whiteStrs = { " " : "<SPC>", @@ -3280,9 +3288,9 @@ class StringEnd(_PositionToken): class WordStart(_PositionToken): """ Matches if the current position is at the beginning of a Word, and - is not preceded by any character in a given set of C{wordChars} - (default=C{printables}). To emulate the C{\b} behavior of regular expressions, - use C{WordStart(alphanums)}. C{WordStart} will also match at the beginning of + is not preceded by any character in a given set of :code:`wordChars` + (default=:code:`printables`). To emulate the :code:`\b` behavior of regular expressions, + use :code:`WordStart(alphanums)`. :code:`WordStart` will also match at the beginning of the string being parsed, or at the beginning of a line. """ def __init__(self, wordChars = printables): @@ -3300,9 +3308,9 @@ class WordStart(_PositionToken): class WordEnd(_PositionToken): """ Matches if the current position is at the end of a Word, and - is not followed by any character in a given set of C{wordChars} - (default=C{printables}). To emulate the C{\b} behavior of regular expressions, - use C{WordEnd(alphanums)}. C{WordEnd} will also match at the end of + is not followed by any character in a given set of :code:`wordChars` + (default=:code:`printables`). To emulate the :code:`\b` behavior of regular expressions, + use :code:`WordEnd(alphanums)`. :code:`WordEnd` will also match at the end of the string being parsed, or at the end of a line. """ def __init__(self, wordChars = printables): @@ -3353,7 +3361,7 @@ class ParseExpression(ParserElement): return self def leaveWhitespace( self ): - """Extends C{leaveWhitespace} defined in base class, and also invokes C{leaveWhitespace} on + """Extends :code:`leaveWhitespace` defined in base class, and also invokes :code:`leaveWhitespace` on all contained expressions.""" self.skipWhitespace = False self.exprs = [ e.copy() for e in self.exprs ] @@ -3434,10 +3442,10 @@ class ParseExpression(ParserElement): class And(ParseExpression): """ - Requires all given C{ParseExpression}s to be found in the given order. + Requires all given :code:`ParseExpression`s to be found in the given order. Expressions may be separated by whitespace. - May be constructed using the C{'+'} operator. - May also be constructed using the C{'-'} operator, which will suppress backtracking. + May be constructed using the :code:`'+'` operator. + May also be constructed using the :code:`'-'` operator, which will suppress backtracking. Example:: integer = Word(nums) @@ -3510,9 +3518,9 @@ class And(ParseExpression): class Or(ParseExpression): """ - Requires that at least one C{ParseExpression} is found. + Requires that at least one :code:`ParseExpression` is found. If two expressions match, the expression that matches the longest string will be used. - May be constructed using the C{'^'} operator. + May be constructed using the :code:`'^'` operator. Example:: # construct Or using '^' operator @@ -3589,9 +3597,9 @@ class Or(ParseExpression): class MatchFirst(ParseExpression): """ - Requires that at least one C{ParseExpression} is found. + Requires that at least one :code:`ParseExpression` is found. If two expressions match, the first one listed is the one that will match. - May be constructed using the C{'|'} operator. + May be constructed using the :code:`'|'` operator. Example:: # construct MatchFirst using '|' operator @@ -3657,9 +3665,9 @@ class MatchFirst(ParseExpression): class Each(ParseExpression): """ - Requires all given C{ParseExpression}s to be found, but in any order. + Requires all given :code:`ParseExpression`s to be found, but in any order. Expressions may be separated by whitespace. - May be constructed using the C{'&'} operator. + May be constructed using the :code:`'&'` operator. Example:: color = oneOf("RED ORANGE YELLOW GREEN BLUE PURPLE BLACK WHITE BROWN") @@ -3781,7 +3789,7 @@ class Each(ParseExpression): class ParseElementEnhance(ParserElement): """ - Abstract subclass of C{ParserElement}, for combining and post-processing parsed tokens. + Abstract subclass of :code:`ParserElement`, for combining and post-processing parsed tokens. """ def __init__( self, expr, savelist=False ): super(ParseElementEnhance,self).__init__(savelist) @@ -3858,10 +3866,10 @@ class ParseElementEnhance(ParserElement): class FollowedBy(ParseElementEnhance): """ - Lookahead matching of the given parse expression. C{FollowedBy} - does I{not} advance the parsing position within the input string, it only + Lookahead matching of the given parse expression. :code:`FollowedBy` + does *not* advance the parsing position within the input string, it only verifies that the specified parse expression matches at the current - position. C{FollowedBy} always returns a null token list. If any + position. :code:`FollowedBy` always returns a null token list. If any results names are defined in the lookahead expression, those *will* be returned for access by name. @@ -3887,10 +3895,10 @@ class FollowedBy(ParseElementEnhance): class PrecededBy(ParseElementEnhance): """ - Lookbehind matching of the given parse expression. C{PrecededBy} + Lookbehind matching of the given parse expression. :code:`PrecededBy` does not advance the parsing position within the input string, it only verifies that the specified parse expression matches prior to the current - position. C{PrecededBy} always returns a null token list, but if + position. :code:`PrecededBy` always returns a null token list, but if a results name is defined on the given expression, it is returned. Parameters: @@ -3960,10 +3968,10 @@ class PrecededBy(ParseElementEnhance): class NotAny(ParseElementEnhance): """ - Lookahead to disallow matching with the given parse expression. C{NotAny} - does I{not} advance the parsing position within the input string, it only - verifies that the specified parse expression does I{not} match at the current - position. Also, C{NotAny} does I{not} skip over leading whitespace. C{NotAny} + Lookahead to disallow matching with the given parse expression. :code:`NotAny` + does *not* advance the parsing position within the input string, it only + verifies that the specified parse expression does *not* match at the current + position. Also, :code:`NotAny` does *not* skip over leading whitespace. :code:`NotAny` always returns a null token list. May be constructed using the '~' operator. Example:: @@ -4183,7 +4191,7 @@ class SkipTo(ParseElementEnhance): Parameters: - expr - target expression marking the end of the data to be skipped - - include - (default=C{False}) if True, the target expression is also parsed + - include - (default=:code:`False`) if True, the target expression is also parsed (the skipped text and target expression are returned as a 2-element list). - ignore - (default=``None``) used to define grammars (typically quoted strings and comments) that might contain false matches to the target expression @@ -4296,20 +4304,23 @@ class Forward(ParseElementEnhance): """ Forward declaration of an expression to be defined later - used for recursive grammars, such as algebraic infix notation. - When the expression is known, it is assigned to the C{Forward} variable using the '<<' operator. + When the expression is known, it is assigned to the :code:`Forward` variable using the '<<' operator. - Note: take care when assigning to C{Forward} not to overlook precedence of operators. + Note: take care when assigning to :code:`Forward` not to overlook precedence of operators. Specifically, '|' has a lower precedence than '<<', so that:: fwdExpr << a | b | c + will actually be evaluated as:: (fwdExpr << a) | b | c + thereby leaving b and c out as parseable alternatives. It is recommended that you - explicitly group the values inserted into the C{Forward}:: + explicitly group the values inserted into the :code:`Forward`:: fwdExpr << (a | b | c) + Converting to use the '<<=' operator instead will avoid this problem. See :class:`ParseResults.pprint` for an example of a recursive parser created using - C{Forward}. + :code:`Forward`. """ def __init__( self, other=None ): super(Forward,self).__init__( other, savelist=False ) @@ -4379,7 +4390,7 @@ class _ForwardNoRecurse(Forward): class TokenConverter(ParseElementEnhance): """ - Abstract subclass of C{ParseExpression}, for converting parsed results. + Abstract subclass of :code:`ParseExpression`, for converting parsed results. """ def __init__( self, expr, savelist=False ): super(TokenConverter,self).__init__( expr )#, savelist ) @@ -4389,7 +4400,7 @@ class Combine(TokenConverter): """ Converter to concatenate all matching tokens to a single string. By default, the matching patterns must also be contiguous in the input string; - this can be disabled by specifying C{'adjacent=False'} in the constructor. + this can be disabled by specifying :code:`'adjacent=False'` in the constructor. Example:: real = Word(nums) + '.' + Word(nums) @@ -4431,7 +4442,7 @@ class Combine(TokenConverter): class Group(TokenConverter): """ - Converter to return the matched tokens as a list - useful for returning tokens of C{:class:`ZeroOrMore`} and C{:class:`OneOrMore`} expressions. + Converter to return the matched tokens as a list - useful for returning tokens of :code:`:class:`ZeroOrMore`` and :code:`:class:`OneOrMore`` expressions. Example:: ident = Word(alphas) @@ -4561,8 +4572,9 @@ def traceParseAction(f): """ Decorator for debugging parse actions. - When the parse action is called, this decorator will print C{">> entering I{method-name}(line:I{current_source_line}, I{parse_location}, I{matched_tokens})".} - When the parse action completes, the decorator will print C{"<<"} followed by the returned value, or any exception that the parse action raised. + When the parse action is called, this decorator will print :code:`">> + entering :code:`method-name`(line:*current_source_line*, *parse_location*, *matched_tokens*)". + When the parse action completes, the decorator will print :code:`"<<"` followed by the returned value, or any exception that the parse action raised. Example:: wd = Word(alphas) @@ -4605,8 +4617,8 @@ def delimitedList( expr, delim=",", combine=False ): """ Helper to define a delimited list of expressions - the delimiter defaults to ','. By default, the list elements and delimiters can have intervening whitespace, and - comments, but this can be overridden by passing C{combine=True} in the constructor. - If C{combine} is set to C{True}, the matching tokens are returned as a single token + comments, but this can be overridden by passing :code:`combine=True` in the constructor. + If :code:`combine` is set to :code:`True`, the matching tokens are returned as a single token string, with the delimiters included; otherwise, the matching tokens are returned as a list of tokens, with the delimiters suppressed. @@ -4628,7 +4640,7 @@ def countedArray( expr, intExpr=None ): where the leading integer tells how many expr expressions follow. The matched tokens returns the array of expr tokens as a list - the leading count token is suppressed. - If C{intExpr} is specified, it should be a pyparsing expression that produces an integer value. + If :code:`intExpr` is specified, it should be a pyparsing expression that produces an integer value. Example:: countedArray(Word(alphas)).parseString('2 ab cd ef') # -> ['ab', 'cd'] @@ -4668,10 +4680,10 @@ def matchPreviousLiteral(expr): first = Word(nums) second = matchPreviousLiteral(first) matchExpr = first + ":" + second - will match C{"1:1"}, but not C{"1:2"}. Because this matches a - previous literal, will also match the leading C{"1:1"} in C{"1:10"}. - If this is not desired, use C{matchPreviousExpr}. - Do I{not} use with packrat parsing enabled. + will match :code:`"1:1"`, but not :code:`"1:2"`. Because this matches a + previous literal, will also match the leading :code:`"1:1"` in :code:`"1:10"`. + If this is not desired, use :code:`matchPreviousExpr`. + Do *not* use with packrat parsing enabled. """ rep = Forward() def copyTokenToRepeater(s,l,t): @@ -4696,11 +4708,11 @@ def matchPreviousExpr(expr): first = Word(nums) second = matchPreviousExpr(first) matchExpr = first + ":" + second - will match C{"1:1"}, but not C{"1:2"}. Because this matches by - expressions, will I{not} match the leading C{"1:1"} in C{"1:10"}; + will match :code:`"1:1"`, but not :code:`"1:2"`. Because this matches by + expressions, will *not* match the leading :code:`"1:1"` in :code:`"1:10"`; the expressions are evaluated first, and then compared, so - C{"1"} is compared with C{"10"}. - Do I{not} use with packrat parsing enabled. + :code:`"1"` is compared with :code:`"10"`. + Do *not* use with packrat parsing enabled. """ rep = Forward() e2 = expr.copy() @@ -4728,14 +4740,14 @@ def oneOf( strs, caseless=False, useRegex=True ): """ Helper to quickly define a set of alternative Literals, and makes sure to do longest-first testing when there is a conflict, regardless of the input order, - but returns a C{:class:`MatchFirst`} for best performance. + but returns a :code:`:class:`MatchFirst`` for best performance. Parameters: - strs - a string of space-delimited literals, or a collection of string literals - - caseless - (default=C{False}) - treat all literals as caseless - - useRegex - (default=C{True}) - as an optimization, will generate a Regex - object; otherwise, will generate a C{MatchFirst} object (if C{caseless=True}, or - if creating a C{Regex} raises an exception) + - caseless - (default=:code:`False`) - treat all literals as caseless + - useRegex - (default=:code:`True`) - as an optimization, will generate a Regex + object; otherwise, will generate a :code:`MatchFirst` object (if :code:`caseless=True`, or + if creating a :code:`Regex` raises an exception) Example:: comp_oper = oneOf("< = > <= >= !=") @@ -4800,10 +4812,10 @@ def oneOf( strs, caseless=False, useRegex=True ): def dictOf( key, value ): """ Helper to easily and clearly define a dictionary by specifying the respective patterns - for the key and value. Takes care of defining the C{:class:`Dict`}, C{:class:`ZeroOrMore`}, and C{:class:`Group`} tokens + for the key and value. Takes care of defining the :code:`:class:`Dict``, :code:`:class:`ZeroOrMore``, and :code:`:class:`Group`` tokens in the proper order. The key pattern can include delimiting markers or punctuation, as long as they are suppressed, thereby leaving the significant key text. The value - pattern can include named results, so that the C{Dict} results can include named token + pattern can include named results, so that the :code:`Dict` results can include named token fields. Example:: @@ -4839,11 +4851,11 @@ def originalTextFor(expr, asString=True): revert separate tokens with intervening whitespace back to the original matching input text. By default, returns astring containing the original parsed text. - If the optional C{asString} argument is passed as C{False}, then the return value is a - C{:class:`ParseResults`} containing any results names that were originally matched, and a + If the optional :code:`asString` argument is passed as :code:`False`, then the return value is a + :code:`:class:`ParseResults`` containing any results names that were originally matched, and a single token containing the original matched text from the input string. So if - the expression passed to C{:class:`originalTextFor`} contains expressions with defined - results names, you must set C{asString} to C{False} if you want to preserve those + the expression passed to :code:`:class:`originalTextFor`` contains expressions with defined + results names, you must set :code:`asString` to :code:`False` if you want to preserve those results name values. Example:: @@ -4884,8 +4896,8 @@ def locatedExpr(expr): - locn_end = location where matched expression ends - value = the actual parsed results - Be careful if the input text contains C{<TAB>} characters, you may want to call - C{:class:`ParserElement.parseWithTabs`} + Be careful if the input text contains :code:`<TAB>` characters, you may want to call + :code:`:class:`ParserElement.parseWithTabs`` Example:: wd = Word(alphas) @@ -4925,12 +4937,12 @@ def srange(s): character set joined into a single string. The values enclosed in the []'s may be: - a single character - - an escaped character with a leading backslash (such as C{\-} or C{\]}) - - an escaped hex character with a leading C{'\x'} (C{\x21}, which is a C{'!'} character) - (C{\0x##} is also supported for backwards compatibility) - - an escaped octal character with a leading C{'\0'} (C{\041}, which is a C{'!'} character) - - a range of any of the above, separated by a dash (C{'a-z'}, etc.) - - any combination of the above (C{'aeiouy'}, C{'a-zA-Z0-9_$'}, etc.) + - an escaped character with a leading backslash (such as :code:`\-` or :code:`\]`) + - an escaped hex character with a leading :code:`'\x'` (:code:`\x21`, which is a :code:`'!'` character) + (:code:`\0x##` is also supported for backwards compatibility) + - an escaped octal character with a leading :code:`'\0'` (:code:`\041`, which is a :code:`'!'` character) + - a range of any of the above, separated by a dash (:code:`'a-z'`, etc.) + - any combination of the above (:code:`'aeiouy'`, :code:`'a-zA-Z0-9_$'`, etc.) """ _expanded = lambda p: p if not isinstance(p,ParseResults) else ''.join(unichr(c) for c in range(ord(p[0]),ord(p[1])+1)) try: @@ -4951,7 +4963,7 @@ def matchOnlyAtCol(n): def replaceWith(replStr): """ Helper method for common parse actions that simply return a literal value. Especially - useful when used with C{:class:`transformString<ParserElement.transformString>`()}. + useful when used with :code:`:class:`transformString<ParserElement.transformString>`()`. Example:: num = Word(nums).setParseAction(lambda toks: int(toks[0])) @@ -4980,7 +4992,7 @@ def tokenMap(func, *args): """ Helper to define a parse action by mapping a function to all elements of a ParseResults list.If any additional args are passed, they are forwarded to the given function as additional arguments after - the token, as in C{hex_integer = Word(hexnums).setParseAction(tokenMap(int, 16))}, which will convert the + the token, as in :code:`hex_integer = Word(hexnums).setParseAction(tokenMap(int, 16))`, which will convert the parsed data to an integer using base 16. Example (compare the last to example in :class:`ParserElement.transformString`:: @@ -5086,23 +5098,23 @@ def makeXMLTags(tagStr): def withAttribute(*args,**attrDict): """ Helper to create a validating parse action to be used with start tags created - with C{:class:`makeXMLTags`} or C{:class:`makeHTMLTags`}. Use C{withAttribute} to qualify a starting tag + with :code:`:class:`makeXMLTags`` or :code:`:class:`makeHTMLTags``. Use :code:`withAttribute` to qualify a starting tag with a required attribute value, to avoid false matches on common tags such as - C{<TD>} or C{<DIV>}. + :code:`<TD>` or :code:`<DIV>`. - Call C{withAttribute} with a series of attribute names and values. Specify the list + Call :code:`withAttribute` with a series of attribute names and values. Specify the list of filter attributes names and values as: - - keyword arguments, as in C{(align="right")}, or - - as an explicit dict with C{**} operator, when an attribute name is also a Python - reserved word, as in C{**{"class":"Customer", "align":"right"}} + - keyword arguments, as in :code:`(align="right")`, or + - as an explicit dict with :code:`**` operator, when an attribute name is also a Python + reserved word, as in :code:`**{"class":"Customer", "align":"right"`} - a list of name-value tuples, as in ( ("ns1:class", "Customer"), ("ns2:align","right") ) For attribute names with a namespace prefix, you must use the second form. Attribute names are matched insensitive to upper/lower case. - If just testing for C{class} (with or without a namespace), use C{:class:`withClass`}. + If just testing for :code:`class` (with or without a namespace), use :code:`:class:`withClass``. To verify that the attribute exists, but without specifying a value, pass - C{withAttribute.ANY_VALUE} as the value. + :code:`withAttribute.ANY_VALUE` as the value. Example:: html = ''' @@ -5150,8 +5162,8 @@ withAttribute.ANY_VALUE = object() def withClass(classname, namespace=''): """ - Simplified version of C{:class:`withAttribute`} when matching on a div class - made - difficult because C{class} is a reserved word in Python. + Simplified version of :code:`:class:`withAttribute`` when matching on a div class - made + difficult because :code:`class` is a reserved word in Python. Example:: html = ''' @@ -5212,14 +5224,14 @@ def infixNotation( baseExpr, opList, lpar=Suppress('('), rpar=Suppress(')') ): be 1, 2, or 3) - rightLeftAssoc is the indicator whether the operator is right or left associative, using the pyparsing-defined - constants C{opAssoc.RIGHT} and C{opAssoc.LEFT}. + constants :code:`opAssoc.RIGHT` and :code:`opAssoc.LEFT`. - parseAction is the parse action to be associated with expressions matching this operator expression (the parse action tuple member may be omitted); if the parse action is passed a tuple or list of functions, this is equivalent to - calling C{setParseAction(*fn)} (:class:`ParserElement.setParseAction`) - - lpar - expression for matching left-parentheses (default=C{Suppress('(')}) - - rpar - expression for matching right-parentheses (default=C{Suppress(')')}) + calling :code:`setParseAction(*fn)` (:class:`ParserElement.setParseAction`) + - lpar - expression for matching left-parentheses (default=:code:`Suppress('(')`) + - rpar - expression for matching right-parentheses (default=:code:`Suppress(')')`) Example:: # simple example of four-function arithmetic with ints and variable names @@ -5306,7 +5318,7 @@ def infixNotation( baseExpr, opList, lpar=Suppress('('), rpar=Suppress(')') ): return ret operatorPrecedence = infixNotation -"""(Deprecated) Former name of C{:class:`infixNotation`}, will be dropped in a future release.""" +"""(Deprecated) Former name of :code:`:class:`infixNotation``, will be dropped in a future release.""" dblQuotedString = Combine(Regex(r'"(?:[^"\n\r\\]|(?:"")|(?:\\(?:[^x]|x[0-9a-fA-F]+)))*')+'"').setName("string enclosed in double quotes") sglQuotedString = Combine(Regex(r"'(?:[^'\n\r\\]|(?:'')|(?:\\(?:[^x]|x[0-9a-fA-F]+)))*")+"'").setName("string enclosed in single quotes") @@ -5320,19 +5332,19 @@ def nestedExpr(opener="(", closer=")", content=None, ignoreExpr=quotedString.cop delimiters ("(" and ")" are the default). Parameters: - - opener - opening character for a nested list (default=C{"("}); can also be a pyparsing expression - - closer - closing character for a nested list (default=C{")"}); can also be a pyparsing expression + - opener - opening character for a nested list (default=:code:`"("`); can also be a pyparsing expression + - closer - closing character for a nested list (default=:code:`")"`); can also be a pyparsing expression - content - expression for items within the nested lists (default=``None``) - - ignoreExpr - expression for ignoring opening and closing delimiters (default=C{quotedString}) + - ignoreExpr - expression for ignoring opening and closing delimiters (default=:code:`quotedString`) If an expression is not provided for the content argument, the nested expression will capture all whitespace-delimited content between delimiters as a list of separate values. - Use the C{ignoreExpr} argument to define expressions that may contain + Use the :code:`ignoreExpr` argument to define expressions that may contain opening or closing characters that should not be treated as opening or closing characters for nesting, such as quotedString or a comment - expression. Specify multiple expressions using an C{:class:`Or`} or C{:class:`MatchFirst`}. + expression. Specify multiple expressions using an :code:`:class:`Or`` or :code:`:class:`MatchFirst``. The default is :class:`quotedString`, but if no expressions are to be ignored, then pass ``None`` for this argument. @@ -5417,9 +5429,9 @@ def indentedBlock(blockStatementExpr, indentStack, indent=True): should share a common indentStack) - indent - boolean indicating whether block must be indented beyond the the current level; set to False for block of left-most statements - (default=C{True}) + (default=:code:`True`) - A valid block must contain at least one C{blockStatement}. + A valid block must contain at least one :code:`blockStatement`. Example:: data = ''' @@ -5530,23 +5542,23 @@ def replaceHTMLEntity(t): # it's easy to get these comment structures wrong - they're very common, so may as well make them available cStyleComment = Combine(Regex(r"/\*(?:[^*]|\*(?!/))*") + '*/').setName("C style comment") -"Comment of the form C{/* ... */}" +"Comment of the form :code:`/* ... */`" htmlComment = Regex(r"<!--[\s\S]*?-->").setName("HTML comment") -"Comment of the form C{<!-- ... -->}" +"Comment of the form :code:`<!-- ... -->`" restOfLine = Regex(r".*").leaveWhitespace().setName("rest of line") dblSlashComment = Regex(r"//(?:\\\n|[^\n])*").setName("// comment") -"Comment of the form C{// ... (to end of line)}" +"Comment of the form :code:`// ... (to end of line)`" cppStyleComment = Combine(Regex(r"/\*(?:[^*]|\*(?!/))*") + '*/'| dblSlashComment).setName("C++ style comment") -"Comment of either form C{:class:`cStyleComment`} or C{:class:`dblSlashComment`}" +"Comment of either form :code:`:class:`cStyleComment`` or :code:`:class:`dblSlashComment``" javaStyleComment = cppStyleComment -"Same as C{:class:`cppStyleComment`}" +"Same as :code:`:class:`cppStyleComment``" pythonStyleComment = Regex(r"#.*").setName("Python style comment") -"Comment of the form C{# ... (to end of line)}" +"Comment of the form :code:`# ... (to end of line)`" _commasepitem = Combine(OneOrMore(Word(printables, excludeChars=',') + Optional( Word(" \t") + @@ -5566,13 +5578,13 @@ class pyparsing_common: - :class:`UUID<uuid>` - :class:`comma-separated list<comma_separated_list>` Parse actions: - - C{:class:`convertToInteger`} - - C{:class:`convertToFloat`} - - C{:class:`convertToDate`} - - C{:class:`convertToDatetime`} - - C{:class:`stripHTMLTags`} - - C{:class:`upcaseTokens`} - - C{:class:`downcaseTokens`} + - :code:`:class:`convertToInteger`` + - :code:`:class:`convertToFloat`` + - :code:`:class:`convertToDate`` + - :code:`:class:`convertToDatetime`` + - :code:`:class:`stripHTMLTags`` + - :code:`:class:`upcaseTokens`` + - :code:`:class:`downcaseTokens`` Example:: pyparsing_common.number.runTests(''' @@ -5736,7 +5748,7 @@ class pyparsing_common: """typical code identifier (leading alpha or '_', followed by 0 or more alphas, nums, or '_')""" ipv4_address = Regex(r'(25[0-5]|2[0-4][0-9]|1?[0-9]{1,2})(\.(25[0-5]|2[0-4][0-9]|1?[0-9]{1,2})){3}').setName("IPv4 address") - "IPv4 address (C{0.0.0.0 - 255.255.255.255})" + "IPv4 address (:code:`0.0.0.0 - 255.255.255.255`)" _ipv6_part = Regex(r'[0-9a-fA-F]{1,4}').setName("hex_integer") _full_ipv6_address = (_ipv6_part + (':' + _ipv6_part)*7).setName("full IPv6 address") @@ -5755,7 +5767,7 @@ class pyparsing_common: Helper to create a parse action for converting parsed date string to Python datetime.date Params - - - fmt - format to be passed to datetime.strptime (default=C{"%Y-%m-%d"}) + - fmt - format to be passed to datetime.strptime (default=:code:`"%Y-%m-%d"`) Example:: date_expr = pyparsing_common.iso8601_date.copy() @@ -5777,7 +5789,7 @@ class pyparsing_common: Helper to create a parse action for converting parsed datetime string to Python datetime.datetime Params - - - fmt - format to be passed to datetime.strptime (default=C{"%Y-%m-%dT%H:%M:%S.%f"}) + - fmt - format to be passed to datetime.strptime (default=:code:`"%Y-%m-%dT%H:%M:%S.%f"`) Example:: dt_expr = pyparsing_common.iso8601_datetime.copy() @@ -5794,13 +5806,13 @@ class pyparsing_common: return cvt_fn iso8601_date = Regex(r'(?P<year>\d{4})(?:-(?P<month>\d\d)(?:-(?P<day>\d\d))?)?').setName("ISO8601 date") - "ISO8601 date (C{yyyy-mm-dd})" + "ISO8601 date (:code:`yyyy-mm-dd`)" iso8601_datetime = Regex(r'(?P<year>\d{4})-(?P<month>\d\d)-(?P<day>\d\d)[T ](?P<hour>\d\d):(?P<minute>\d\d)(:(?P<second>\d\d(\.\d*)?)?)?(?P<tz>Z|[+-]\d\d:?\d\d)?').setName("ISO8601 datetime") - "ISO8601 datetime (C{yyyy-mm-ddThh:mm:ss.s(Z|+-00:00)}) - trailing seconds, milliseconds, and timezone optional; accepts separating C{'T'} or C{' '}" + "ISO8601 datetime (:code:`yyyy-mm-ddThh:mm:ss.s(Z|+-00:00)`) - trailing seconds, milliseconds, and timezone optional; accepts separating :code:`'T'` or :code:`' '`" uuid = Regex(r'[0-9a-fA-F]{8}(-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}').setName("UUID") - "UUID (C{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx})" + "UUID (:code:`xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx`)" _html_stripper = anyOpenTag.suppress() | anyCloseTag.suppress() @staticmethod |