# module pyparsing.py\r
#\r
-# Copyright (c) 2003-2016 Paul T. McGuire\r
+# Copyright (c) 2003-2018 Paul T. McGuire\r
#\r
# Permission is hereby granted, free of charge, to any person obtaining\r
# a copy of this software and associated documentation files (the\r
__doc__ = \\r
"""\r
pyparsing module - Classes and methods to define and execute parsing grammars\r
+=============================================================================\r
\r
The pyparsing module is an alternative approach to creating and executing simple grammars,\r
vs. the traditional lex/yacc approach, or the use of regular expressions. With pyparsing, you\r
- extra or missing whitespace (the above program will also handle "Hello,World!", "Hello , World !", etc.)\r
- quoted strings\r
- embedded comments\r
+\r
+\r
+Getting Started -\r
+-----------------\r
+Visit the classes L{ParserElement} and L{ParseResults} to see the base classes that most other pyparsing\r
+classes inherit from. Use the docstrings for examples of how to:\r
+ - construct literal match expressions from L{Literal} and L{CaselessLiteral} classes\r
+ - construct character word-group expressions using the L{Word} class\r
+ - see how to create repetitive expressions using L{ZeroOrMore} and L{OneOrMore} classes\r
+ - use L{'+'<And>}, L{'|'<MatchFirst>}, L{'^'<Or>}, and L{'&'<Each>} operators to combine simple expressions into more complex ones\r
+ - associate names with your parsed results using L{ParserElement.setResultsName}\r
+ - find some helpful expression short-cuts like L{delimitedList} and L{oneOf}\r
+ - find more useful common expressions in the L{pyparsing_common} namespace class\r
"""\r
\r
-__version__ = "2.2.0"\r
-__versionTime__ = "06 Mar 2017 02:06 UTC"\r
+__version__ = "2.2.1"\r
+__versionTime__ = "18 Sep 2018 00:49 UTC"\r
__author__ = "Paul McGuire <ptmcg@users.sourceforge.net>"\r
\r
import string\r
from threading import RLock\r
\r
try:\r
+ # Python 3\r
+ from collections.abc import Iterable\r
+ from collections.abc import MutableMapping\r
+except ImportError:\r
+ # Python 2.7\r
+ from collections import Iterable\r
+ from collections import MutableMapping\r
+\r
+try:\r
from collections import OrderedDict as _OrderedDict\r
except ImportError:\r
try:\r
def __dir__(self):\r
return (dir(type(self)) + list(self.keys()))\r
\r
-collections.MutableMapping.register(ParseResults)\r
+MutableMapping.register(ParseResults)\r
\r
def col (loc,strg):\r
"""Returns current column within a string, counting newlines as line separators.\r
# special handling for Python 3.5.0 - extra deep call stack by 1\r
offset = -3 if system_version == (3,5,0) else -2\r
frame_summary = traceback.extract_stack(limit=-offset+limit-1)[offset]\r
- return [(frame_summary.filename, frame_summary.lineno)]\r
+ return [frame_summary[:2]]\r
def extract_tb(tb, limit=0):\r
frames = traceback.extract_tb(tb, limit=limit)\r
frame_summary = frames[-1]\r
- return [(frame_summary.filename, frame_summary.lineno)]\r
+ return [frame_summary[:2]]\r
else:\r
extract_stack = traceback.extract_stack\r
extract_tb = traceback.extract_tb\r
else:\r
preloc = loc\r
tokensStart = preloc\r
- if self.mayIndexError or loc >= len(instring):\r
+ if self.mayIndexError or preloc >= len(instring):\r
try:\r
loc,tokens = self.parseImpl( instring, preloc, doActions )\r
except IndexError:\r
self.resultsName,\r
asList=self.saveAsList and isinstance(tokens,(ParseResults,list)),\r
modal=self.modalResults )\r
-\r
if debugging:\r
#~ print ("Matched",self,"->",retTokens.asList())\r
if (self.debugActions[1] ):\r
\r
if isinstance( exprs, basestring ):\r
self.exprs = [ ParserElement._literalStringClass( exprs ) ]\r
- elif isinstance( exprs, collections.Iterable ):\r
+ elif isinstance( exprs, Iterable ):\r
exprs = list(exprs)\r
# if sequence of strings provided, wrap with Literal\r
if all(isinstance(expr, basestring) for expr in exprs):\r
\r
@traceParseAction\r
def remove_duplicate_chars(tokens):\r
- return ''.join(sorted(set(''.join(tokens)))\r
+ return ''.join(sorted(set(''.join(tokens))))\r
\r
wds = OneOrMore(wd).setParseAction(remove_duplicate_chars)\r
print(wds.parseString("slkdjs sld sldd sdlf sdljf"))\r
symbols = []\r
if isinstance(strs,basestring):\r
symbols = strs.split()\r
- elif isinstance(strs, collections.Iterable):\r
+ elif isinstance(strs, Iterable):\r
symbols = list(strs)\r
else:\r
warnings.warn("Invalid argument to oneOf, expected string or iterable",\r
_escapedPunc = Word( _bslash, r"\[]-*.$+^?()~ ", exact=2 ).setParseAction(lambda s,l,t:t[0][1])\r
_escapedHexChar = Regex(r"\\0?[xX][0-9a-fA-F]+").setParseAction(lambda s,l,t:unichr(int(t[0].lstrip(r'\0x'),16)))\r
_escapedOctChar = Regex(r"\\0[0-7]+").setParseAction(lambda s,l,t:unichr(int(t[0][1:],8)))\r
-_singleChar = _escapedPunc | _escapedHexChar | _escapedOctChar | Word(printables, excludeChars=r'\]', exact=1) | Regex(r"\w", re.UNICODE)\r
+_singleChar = _escapedPunc | _escapedHexChar | _escapedOctChar | CharsNotIn(r'\]', exact=1)\r
_charRange = Group(_singleChar + Suppress("-") + _singleChar)\r
_reBracketExpr = Literal("[") + Optional("^").setResultsName("negate") + Group( OneOrMore( _charRange | _singleChar ) ).setResultsName("body") + "]"\r
\r