summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b>2009-04-13 18:21:34 +0000
committerptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b>2009-04-13 18:21:34 +0000
commite6d69935d50bacafbaab7f4fa8b857c671071aaa (patch)
tree4e89f31d4280dae7e070e61e960518f0b0abcafb
parent99119b68df7445113a4bf395ef54ca0bcc652cfa (diff)
downloadpyparsing-e6d69935d50bacafbaab7f4fa8b857c671071aaa.tar.gz
Changes for pyparsing version 1.5.2
scanString handles zero-length parsed results "except" statements are more Py3K-friendly git-svn-id: svn://svn.code.sf.net/p/pyparsing/code/src@181 9bf210a0-9d2d-494c-87cf-cfb32e7dff7b
-rw-r--r--CHANGES16
-rw-r--r--HowToUsePyparsing.txt13
-rw-r--r--pyparsing.py87
-rw-r--r--pyparsing_py3.py90
-rw-r--r--unitTests.py104
5 files changed, 198 insertions, 112 deletions
diff --git a/CHANGES b/CHANGES
index f354993..6047a2c 100644
--- a/CHANGES
+++ b/CHANGES
@@ -2,10 +2,18 @@
Change Log
==========
-Version 1.5.2 - February, 2009
+Version 1.5.2 - April, 2009
------------------------------
+- Added pyparsing_py3.py module, so that Python 3 users can use
+ pyparsing by changing their pyparsing import statement to:
+
+ import pyparsing_py3
+
+ Thanks for help from Patrick Laban and his friend is Geremy
+ Condra on the pyparsing wiki.
+
- Removed __slots__ declaration on ParseBaseException, for
- compatibility with IronPython 2.0.1. "Raised" by David
+ compatibility with IronPython 2.0.1. Raised by David
Lawler on the pyparsing wiki, thanks David!
- Fixed bug in SkipTo/failOn handling - caught by eagle eye
@@ -21,6 +29,10 @@ Version 1.5.2 - February, 2009
exceptions back to caller of parseString or parseFile - thanks
to a tip from Peter Otten on comp.lang.python.
+- Changed behavior of scanString to avoid infinitely looping on
+ expressions that match zero-length strings. Prompted by a
+ question posted by ellisonbg on the wiki.
+
- Enhanced classes that take a list of expressions (And, Or,
MatchFirst, and Each) to accept generator expressions also.
This can be useful when generating lists of alternative
diff --git a/HowToUsePyparsing.txt b/HowToUsePyparsing.txt
index 39c37de..21ad72c 100644
--- a/HowToUsePyparsing.txt
+++ b/HowToUsePyparsing.txt
@@ -5,10 +5,10 @@ Using the pyparsing module
:author: Paul McGuire
:address: ptmcg@users.sourceforge.net
-:revision: 1.5.1
-:date: October, 2008
+:revision: 1.5.2
+:date: April, 2009
-:copyright: Copyright |copy| 2003-2008 Paul McGuire.
+:copyright: Copyright |copy| 2003-2009 Paul McGuire.
.. |copy| unicode:: 0xA9
@@ -63,6 +63,13 @@ The parsed tokens are returned in the following form::
['Hello', ',', 'World', '!']
+New features in 1.5.2
+---------------------
+There are no new pyparsing features in this release, it is primarily a bug-fixing release.
+This release does include changes to support running pyparsing under IronPython 2.0.1, and
+a pyparsing_py3 module for use with Python 3.
+
+
New features in 1.5.1
---------------------
Some of the significant features added in version 1.5.0 are:
diff --git a/pyparsing.py b/pyparsing.py
index 06b11d9..57e938a 100644
--- a/pyparsing.py
+++ b/pyparsing.py
@@ -59,7 +59,7 @@ The pyparsing module handles some of the problems that are typically vexing when
"""
__version__ = "1.5.2"
-__versionTime__ = "17 February 2009 19:45"
+__versionTime__ = "9 April 2009 12:21"
__author__ = "Paul McGuire <ptmcg@users.sourceforge.net>"
import string
@@ -91,20 +91,21 @@ __all__ = [
'indentedBlock', 'originalTextFor',
]
-
"""
Detect if we are running version 3.X and make appropriate changes
Robert A. Clark
"""
-if sys.version_info[0] > 2:
- _PY3K = True
+_PY3K = sys.version_info[0] > 2
+if _PY3K:
_MAX_INT = sys.maxsize
basestring = str
+ unichr = chr
+ _ustr = str
+ _str2dict = set
+ alphas = string.ascii_lowercase + string.ascii_uppercase
else:
- _PY3K = False
_MAX_INT = sys.maxint
-if not _PY3K:
def _ustr(obj):
"""Drop-in replacement for str(obj) that tries to be Unicode friendly. It first tries
str(obj). If that fails with a UnicodeEncodeError, then it tries unicode(obj). It
@@ -131,15 +132,12 @@ if not _PY3K:
# Replace unprintables with question marks?
#return unicode(obj).encode(sys.getdefaultencoding(), 'replace')
# ...
-else:
- _ustr = str
- unichr = chr
+
+ def _str2dict(strg):
+ return dict( [(c,0) for c in strg] )
+
+ alphas = string.lowercase + string.uppercase
-if not _PY3K:
- def _str2dict(strg):
- return dict( [(c,0) for c in strg] )
-else:
- _str2dict = set
def _xml_escape(data):
"""Escape &, <, >, ", ', etc. in a string of data."""
@@ -154,14 +152,10 @@ def _xml_escape(data):
class _Constants(object):
pass
-if not _PY3K:
- alphas = string.lowercase + string.uppercase
-else:
- alphas = string.ascii_lowercase + string.ascii_uppercase
nums = string.digits
hexnums = nums + "ABCDEFabcdef"
alphanums = alphas + nums
-_bslash = chr(92)
+_bslash = chr(92)
printables = "".join( [ c for c in string.printable if c not in string.whitespace ] )
class ParseBaseException(Exception):
@@ -582,14 +576,11 @@ class ParseResults(object):
out.append( "%s%s- %s: " % (indent,(' '*depth), k) )
if isinstance(v,ParseResults):
if v.keys():
- #~ out.append('\n')
out.append( v.dump(indent,depth+1) )
- #~ out.append('\n')
else:
out.append(_ustr(v))
else:
out.append(_ustr(v))
- #~ out.append('\n')
return "".join(out)
# add support for pickle protocol
@@ -927,11 +918,15 @@ class ParserElement(object):
loc,tokens = self.parseImpl( instring, preloc, doActions )
except IndexError:
raise ParseException( instring, len(instring), self.errmsg, self )
- except ParseBaseException, err:
+ except ParseBaseException:
#~ print ("Exception raised:", err)
+ err = None
if self.debugActions[2]:
+ err = sys.exc_info()[1]
self.debugActions[2]( instring, tokensStart, self, err )
if self.failAction:
+ if err is None:
+ err = sys.exc_info()[1]
self.failAction( instring, tokensStart, self, err )
raise
else:
@@ -961,9 +956,10 @@ class ParserElement(object):
self.resultsName,
asList=self.saveAsList and isinstance(tokens,(ParseResults,list)),
modal=self.modalResults )
- except ParseBaseException, err:
+ except ParseBaseException:
#~ print "Exception raised in user parse action:", err
if (self.debugActions[2] ):
+ err = sys.exc_info()[1]
self.debugActions[2]( instring, tokensStart, self, err )
raise
else:
@@ -1002,7 +998,8 @@ class ParserElement(object):
value = self._parseNoCache( instring, loc, doActions, callPreParse )
ParserElement._exprArgCache[ lookup ] = (value[0],value[1].copy())
return value
- except ParseBaseException, pe:
+ except ParseBaseException:
+ pe = sys.exc_info()[1]
ParserElement._exprArgCache[ lookup ] = pe
raise
@@ -1071,7 +1068,8 @@ class ParserElement(object):
if parseAll:
loc = self.preParse( instring, loc )
StringEnd()._parse( instring, loc )
- except ParseBaseException, exc:
+ except ParseBaseException:
+ exc = sys.exc_info()[1]
# catch and re-raise exception from here, clears out pyparsing internal stack trace
raise exc
else:
@@ -1106,10 +1104,14 @@ class ParserElement(object):
except ParseException:
loc = preloc+1
else:
- matches += 1
- yield tokens, preloc, nextLoc
- loc = nextLoc
- except ParseBaseException, pe:
+ if nextLoc > loc:
+ matches += 1
+ yield tokens, preloc, nextLoc
+ loc = nextLoc
+ else:
+ loc = preloc+1
+ except ParseBaseException:
+ pe = sys.exc_info()[1]
raise pe
def transformString( self, instring ):
@@ -1137,7 +1139,8 @@ class ParserElement(object):
lastE = e
out.append(instring[lastE:])
return "".join(map(_ustr,out))
- except ParseBaseException, pe:
+ except ParseBaseException:
+ pe = sys.exc_info()[1]
raise pe
def searchString( self, instring, maxMatches=_MAX_INT ):
@@ -1147,7 +1150,8 @@ class ParserElement(object):
"""
try:
return ParseResults([ t for t,s,e in self.scanString( instring, maxMatches ) ])
- except ParseBaseException, pe:
+ except ParseBaseException:
+ pe = sys.exc_info()[1]
raise pe
def __add__(self, other ):
@@ -1403,8 +1407,9 @@ class ParserElement(object):
f.close()
try:
return self.parseString(file_contents, parseAll)
- except ParseBaseException, exc:
+ except ParseBaseException:
# catch and re-raise exception from here, clears out pyparsing internal stack trace
+ exc = sys.exc_info()[1]
raise exc
def getException(self):
@@ -2344,9 +2349,10 @@ class And(ParseExpression):
loc, exprtokens = e._parse( instring, loc, doActions )
except ParseSyntaxException:
raise
- except ParseBaseException, pe:
+ except ParseBaseException:
+ pe = sys.exc_info()[1]
raise ParseSyntaxException(pe)
- except IndexError, ie:
+ except IndexError:
raise ParseSyntaxException( ParseException(instring, len(instring), self.errmsg, self) )
else:
loc, exprtokens = e._parse( instring, loc, doActions )
@@ -2396,7 +2402,8 @@ class Or(ParseExpression):
for e in self.exprs:
try:
loc2 = e.tryParse( instring, loc )
- except ParseException, err:
+ except ParseException:
+ err = sys.exc_info()[1]
if err.loc > maxExcLoc:
maxException = err
maxExcLoc = err.loc
@@ -2861,7 +2868,7 @@ class SkipTo(ParseElementEnhance):
while 1:
try:
loc = self.ignoreExpr.tryParse(instring,loc)
- print "found ignoreExpr, advance to", loc
+ # print "found ignoreExpr, advance to", loc
except ParseBaseException:
break
expr._parse( instring, loc, doActions=False, callPreParse=False )
@@ -3087,7 +3094,8 @@ def traceParseAction(f):
sys.stderr.write( ">>entering %s(line: '%s', %d, %s)\n" % (thisFunc,line(l,s),l,t) )
try:
ret = f(*paArgs)
- except Exception, exc:
+ except Exception:
+ exc = sys.exc_info()[1]
sys.stderr.write( "<<leaving %s (exception: %s)\n" % (thisFunc,exc) )
raise
sys.stderr.write( "<<leaving %s (ret: %s)\n" % (thisFunc,ret) )
@@ -3674,7 +3682,8 @@ if __name__ == "__main__":
print ("tokens.columns = " + str(tokens.columns))
print ("tokens.tables = " + str(tokens.tables))
print (tokens.asXML("SQL",True))
- except ParseBaseException,err:
+ except ParseBaseException:
+ err = sys.exc_info()[1]
print (teststring + "->")
print (err.line)
print (" "*(err.column-1) + "^")
diff --git a/pyparsing_py3.py b/pyparsing_py3.py
index 0f0d104..a427ac8 100644
--- a/pyparsing_py3.py
+++ b/pyparsing_py3.py
@@ -58,8 +58,8 @@ The pyparsing module handles some of the problems that are typically vexing when
- embedded comments
"""
-__version__ = "1.5.2Py3"
-__versionTime__ = "29 March 2009 23:54"
+__version__ = "1.5.2.Py3"
+__versionTime__ = "9 April 2009 12:21"
__author__ = "Paul McGuire <ptmcg@users.sourceforge.net>"
import string
@@ -91,21 +91,21 @@ __all__ = [
'indentedBlock', 'originalTextFor',
]
-
"""
Detect if we are running version 3.X and make appropriate changes
Robert A. Clark
"""
-if sys.version_info[0] > 2:
- _PY3K = True
+_PY3K = sys.version_info[0] > 2
+if _PY3K:
_MAX_INT = sys.maxsize
basestring = str
- unichr = chr
+ unichr = chr
+ _ustr = str
+ _str2dict = set
+ alphas = string.ascii_lowercase + string.ascii_uppercase
else:
- _PY3K = False
_MAX_INT = sys.maxint
-if not _PY3K:
def _ustr(obj):
"""Drop-in replacement for str(obj) that tries to be Unicode friendly. It first tries
str(obj). If that fails with a UnicodeEncodeError, then it tries unicode(obj). It
@@ -132,15 +132,12 @@ if not _PY3K:
# Replace unprintables with question marks?
#return unicode(obj).encode(sys.getdefaultencoding(), 'replace')
# ...
-else:
- _ustr = str
- unichr = chr
+
+ def _str2dict(strg):
+ return dict( [(c,0) for c in strg] )
+
+ alphas = string.lowercase + string.uppercase
-if not _PY3K:
- def _str2dict(strg):
- return dict( [(c,0) for c in strg] )
-else:
- _str2dict = set
def _xml_escape(data):
"""Escape &, <, >, ", ', etc. in a string of data."""
@@ -155,14 +152,10 @@ def _xml_escape(data):
class _Constants(object):
pass
-if not _PY3K:
- alphas = string.lowercase + string.uppercase
-else:
- alphas = string.ascii_lowercase + string.ascii_uppercase
nums = string.digits
hexnums = nums + "ABCDEFabcdef"
alphanums = alphas + nums
-_bslash = chr(92)
+_bslash = chr(92)
printables = "".join( [ c for c in string.printable if c not in string.whitespace ] )
class ParseBaseException(Exception):
@@ -583,14 +576,11 @@ class ParseResults(object):
out.append( "%s%s- %s: " % (indent,(' '*depth), k) )
if isinstance(v,ParseResults):
if v.keys():
- #~ out.append('\n')
out.append( v.dump(indent,depth+1) )
- #~ out.append('\n')
else:
out.append(_ustr(v))
else:
out.append(_ustr(v))
- #~ out.append('\n')
return "".join(out)
# add support for pickle protocol
@@ -928,11 +918,15 @@ class ParserElement(object):
loc,tokens = self.parseImpl( instring, preloc, doActions )
except IndexError:
raise ParseException( instring, len(instring), self.errmsg, self )
- except ParseBaseException as err:
+ except ParseBaseException:
#~ print ("Exception raised:", err)
+ err = None
if self.debugActions[2]:
+ err = sys.exc_info()[1]
self.debugActions[2]( instring, tokensStart, self, err )
if self.failAction:
+ if err is None:
+ err = sys.exc_info()[1]
self.failAction( instring, tokensStart, self, err )
raise
else:
@@ -962,9 +956,10 @@ class ParserElement(object):
self.resultsName,
asList=self.saveAsList and isinstance(tokens,(ParseResults,list)),
modal=self.modalResults )
- except ParseBaseException as err:
+ except ParseBaseException:
#~ print "Exception raised in user parse action:", err
if (self.debugActions[2] ):
+ err = sys.exc_info()[1]
self.debugActions[2]( instring, tokensStart, self, err )
raise
else:
@@ -1003,7 +998,8 @@ class ParserElement(object):
value = self._parseNoCache( instring, loc, doActions, callPreParse )
ParserElement._exprArgCache[ lookup ] = (value[0],value[1].copy())
return value
- except ParseBaseException as pe:
+ except ParseBaseException:
+ pe = sys.exc_info()[1]
ParserElement._exprArgCache[ lookup ] = pe
raise
@@ -1072,7 +1068,8 @@ class ParserElement(object):
if parseAll:
loc = self.preParse( instring, loc )
StringEnd()._parse( instring, loc )
- except ParseBaseException as exc:
+ except ParseBaseException:
+ exc = sys.exc_info()[1]
# catch and re-raise exception from here, clears out pyparsing internal stack trace
raise exc
else:
@@ -1107,10 +1104,14 @@ class ParserElement(object):
except ParseException:
loc = preloc+1
else:
- matches += 1
- yield tokens, preloc, nextLoc
- loc = nextLoc
- except ParseBaseException as pe:
+ if nextLoc > loc:
+ matches += 1
+ yield tokens, preloc, nextLoc
+ loc = nextLoc
+ else:
+ loc = preloc+1
+ except ParseBaseException:
+ pe = sys.exc_info()[1]
raise pe
def transformString( self, instring ):
@@ -1138,7 +1139,8 @@ class ParserElement(object):
lastE = e
out.append(instring[lastE:])
return "".join(map(_ustr,out))
- except ParseBaseException as pe:
+ except ParseBaseException:
+ pe = sys.exc_info()[1]
raise pe
def searchString( self, instring, maxMatches=_MAX_INT ):
@@ -1148,7 +1150,8 @@ class ParserElement(object):
"""
try:
return ParseResults([ t for t,s,e in self.scanString( instring, maxMatches ) ])
- except ParseBaseException as pe:
+ except ParseBaseException:
+ pe = sys.exc_info()[1]
raise pe
def __add__(self, other ):
@@ -1404,8 +1407,9 @@ class ParserElement(object):
f.close()
try:
return self.parseString(file_contents, parseAll)
- except ParseBaseException as exc:
+ except ParseBaseException:
# catch and re-raise exception from here, clears out pyparsing internal stack trace
+ exc = sys.exc_info()[1]
raise exc
def getException(self):
@@ -2345,9 +2349,10 @@ class And(ParseExpression):
loc, exprtokens = e._parse( instring, loc, doActions )
except ParseSyntaxException:
raise
- except ParseBaseException as pe:
+ except ParseBaseException:
+ pe = sys.exc_info()[1]
raise ParseSyntaxException(pe)
- except IndexError as ie:
+ except IndexError:
raise ParseSyntaxException( ParseException(instring, len(instring), self.errmsg, self) )
else:
loc, exprtokens = e._parse( instring, loc, doActions )
@@ -2397,7 +2402,8 @@ class Or(ParseExpression):
for e in self.exprs:
try:
loc2 = e.tryParse( instring, loc )
- except ParseException as err:
+ except ParseException:
+ err = sys.exc_info()[1]
if err.loc > maxExcLoc:
maxException = err
maxExcLoc = err.loc
@@ -2862,7 +2868,7 @@ class SkipTo(ParseElementEnhance):
while 1:
try:
loc = self.ignoreExpr.tryParse(instring,loc)
- print("found ignoreExpr, advance to", loc)
+ # print("found ignoreExpr, advance to", loc)
except ParseBaseException:
break
expr._parse( instring, loc, doActions=False, callPreParse=False )
@@ -3088,7 +3094,8 @@ def traceParseAction(f):
sys.stderr.write( ">>entering %s(line: '%s', %d, %s)\n" % (thisFunc,line(l,s),l,t) )
try:
ret = f(*paArgs)
- except Exception as exc:
+ except Exception:
+ exc = sys.exc_info()[1]
sys.stderr.write( "<<leaving %s (exception: %s)\n" % (thisFunc,exc) )
raise
sys.stderr.write( "<<leaving %s (ret: %s)\n" % (thisFunc,ret) )
@@ -3675,7 +3682,8 @@ if __name__ == "__main__":
print ("tokens.columns = " + str(tokens.columns))
print ("tokens.tables = " + str(tokens.tables))
print (tokens.asXML("SQL",True))
- except ParseBaseException as err:
+ except ParseBaseException:
+ err = sys.exc_info()[1]
print (teststring + "->")
print (err.line)
print (" "*(err.column-1) + "^")
diff --git a/unitTests.py b/unitTests.py
index a89793d..077e71f 100644
--- a/unitTests.py
+++ b/unitTests.py
@@ -8,6 +8,7 @@ import pprint
import pdb
TEST_USING_PACKRAT = True
+#~ TEST_USING_PACKRAT = False
# simple utility for flattening nested lists
def flatten(L):
@@ -282,18 +283,19 @@ class ParseIDLTest(ParseTestCase):
import idlParse
def test( strng, numToks, errloc=0 ):
- #~ print strng
+ print strng
try:
bnf = idlParse.CORBA_IDL_BNF()
- tokens = flatten( bnf.parseString( strng ).asList() )
- #~ print "tokens = "
- #~ pprint.pprint( tokens.asList() )
- #~ print len(tokens)
+ tokens = bnf.parseString( strng )
+ print "tokens = "
+ pprint.pprint( tokens.asList() )
+ tokens = flatten( tokens.asList() )
+ print len(tokens)
assert len(tokens) == numToks, "error matching IDL string, %s -> %s" % (strng, str(tokens) )
except ParseException, err:
- #~ print err.line
- #~ print " "*(err.column-1) + "^"
- #~ print err
+ print err.line
+ print " "*(err.column-1) + "^"
+ print err
assert numToks == 0, "unexpected ParseException while parsing %s, %s" % (strng, str(err) )
assert err.loc == errloc, "expected ParseException at %d, found exception at %d" % (errloc, err.loc)
@@ -1302,9 +1304,9 @@ class MiscellaneousParserTests(ParseTestCase):
testGrammar.parseString("BC")
testGrammar.parseString("BD")
except pyparsing.ParseException, pe:
- #~ print pe.pstr,"->",pe
+ print pe.pstr,"->",pe
assert pe.pstr == "BD", "wrong test string failed to parse"
- assert pe.loc == 1, "error in Optional matching"
+ assert pe.loc == 1, "error in Optional matching, pe.loc="+str(pe.loc)
# test validate
print "verify behavior of validate()"
@@ -1350,8 +1352,15 @@ class MiscellaneousParserTests(ParseTestCase):
print names
assert names==[None, 'B', 'B', 'A', 'B', 'B', 'A', 'B', 'C', 'B', 'A'], \
"failure in getting names for tokens"
+
+ # test ParseResults.get() method
+ print "verify behavior of ParseResults.get()"
+ res = g1.parseString(teststring)
+ print res.get("A","A not found")[0]
+ print res.get("D","!D")
+ assert res.get("A","A not found")[0] == "a", "get on existing key failed"
+ assert res.get("D","!D") == "!D", "get on missing key failed"
- # test Optional beyond end of string
print "verify handling of Optional's beyond the end of string"
testGrammar = "A" + pyparsing.Optional("B") + pyparsing.Optional("C") + pyparsing.Optional("D")
testGrammar.parseString("A")
@@ -1597,51 +1606,63 @@ class VariableParseActionArgsTest(ParseTestCase):
def __call__(self):
return
class CallableS3(object):
- @staticmethod
+ #~ @staticmethod
def __call__(s,l,t):
return t
+ __call__=staticmethod(__call__)
class CallableS2(object):
- @staticmethod
+ #~ @staticmethod
def __call__(l,t):
return t
+ __call__=staticmethod(__call__)
class CallableS1(object):
- @staticmethod
+ #~ @staticmethod
def __call__(t):
return t
+ __call__=staticmethod(__call__)
class CallableS0(object):
- @staticmethod
+ #~ @staticmethod
def __call__():
return
+ __call__=staticmethod(__call__)
class CallableC3(object):
- @classmethod
+ #~ @classmethod
def __call__(cls,s,l,t):
return t
+ __call__=classmethod(__call__)
class CallableC2(object):
- @classmethod
+ #~ @classmethod
def __call__(cls,l,t):
return t
+ __call__=classmethod(__call__)
class CallableC1(object):
- @classmethod
+ #~ @classmethod
def __call__(cls,t):
return t
+ __call__=classmethod(__call__)
class CallableC0(object):
- @classmethod
+ #~ @classmethod
def __call__(cls):
return
+ __call__=classmethod(__call__)
class parseActionHolder(object):
- @staticmethod
+ #~ @staticmethod
def pa3(s,l,t):
return t
- @staticmethod
+ pa3=staticmethod(pa3)
+ #~ @staticmethod
def pa2(l,t):
return t
- @staticmethod
+ pa2=staticmethod(pa2)
+ #~ @staticmethod
def pa1(t):
return t
- @staticmethod
+ pa1=staticmethod(pa1)
+ #~ @staticmethod
def pa0():
return
+ pa0=staticmethod(pa0)
def paArgs(*args):
print args
@@ -1789,7 +1810,16 @@ class ParseResultsDelTest(ParseTestCase):
grammar = OneOrMore(Word(nums))("ints") + OneOrMore(Word(alphas))("words")
res = grammar.parseString("123 456 ABC DEF")
- print res
+ print res.dump()
+ origInts = res.ints.asList()
+ origWords = res.words.asList()
+ del res[1]
+ del res["words"]
+ print res.dump()
+ assert res[1]=='ABC',"failed to delete 0'th element correctly"
+ assert res.ints.asList()==origInts, "updated named attributes, should have updated list only"
+ assert res.words=="", "failed to update named attribute correctly"
+ assert res[-1]=='DEF', "updated list, should have updated named attributes only"
class WithAttributeParseActionTest(ParseTestCase):
def runTest(self):
@@ -1935,8 +1965,26 @@ class NestedExpressionsTest(ParseTestCase):
print result.dump()
assert result.asList() == expected , "Lisp-ish comments (\";; <...> $\") and quoted strings didn't work. Expected: %s, got: %s" % (expected, result)
-
-
+class ParseAllTest(ParseTestCase):
+ def runTest(self):
+ from pyparsing import Word
+
+ testExpr = Word("A")
+
+ tests = [
+ ("AAAAA", False, True),
+ ("AAAAA", True, True),
+ ("AAABB", False, True),
+ ("AAABB", True, False),
+ ]
+ for s,parseAllFlag,shouldSucceed in tests:
+ try:
+ print "'%s' parseAll=%s (shouldSuceed=%s)" % (s, parseAllFlag, shouldSucceed)
+ testExpr.parseString(s,parseAllFlag)
+ assert shouldSucceed, "successfully parsed when should have failed"
+ except ParseException, pe:
+ assert not shouldSucceed, "failed to parse when should have succeeded"
+
class WordBoundaryExpressionsTest(ParseTestCase):
def runTest(self):
from pyparsing import WordEnd, WordStart, oneOf
@@ -2025,6 +2073,7 @@ def makeTestSuite():
suite.addTest( OperatorPrecedenceGrammarTest4() )
suite.addTest( ParseResultsPickleTest() )
suite.addTest( ParseResultsWithNamedTupleTest() )
+ suite.addTest( ParseResultsDelTest() )
suite.addTest( SingleArgExceptionTest() )
suite.addTest( UpcaseDowncaseUnicode() )
suite.addTest( KeepOriginalTextTest() )
@@ -2032,13 +2081,14 @@ def makeTestSuite():
suite.addTest( WithAttributeParseActionTest() )
suite.addTest( NestedExpressionsTest() )
suite.addTest( WordBoundaryExpressionsTest() )
+ suite.addTest( ParseAllTest() )
suite.addTest( MiscellaneousParserTests() )
if TEST_USING_PACKRAT:
# retest using packrat parsing (disable those tests that aren't compatible)
suite.addTest( EnablePackratParsing() )
- unpackrattables = [ EnablePackratParsing, ParseIDLTest, RepeaterTest, ]
+ unpackrattables = [ EnablePackratParsing, RepeaterTest, ]
# add tests to test suite a second time, to run with packrat parsing
# (leaving out those that we know wont work with packrat)