summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b>2016-08-09 20:01:08 +0000
committerptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b>2016-08-09 20:01:08 +0000
commitdfa93523eb2287944bb51f6545efccf1bb5eeb24 (patch)
tree099219ae2acfab4bf8f0e563e9bb010d1c03fda1
parent53d58687f318cfa9100c8b4e56e9c51725276f8a (diff)
downloadpyparsing-dfa93523eb2287944bb51f6545efccf1bb5eeb24.tar.gz
Fix regression when using packrat parsing and raising ParseSyntaxException in And._ErrorStop.
git-svn-id: svn://svn.code.sf.net/p/pyparsing/code/trunk@403 9bf210a0-9d2d-494c-87cf-cfb32e7dff7b
-rw-r--r--src/CHANGES7
-rw-r--r--src/pyparsing.py28
-rw-r--r--src/unitTests.py18
3 files changed, 42 insertions, 11 deletions
diff --git a/src/CHANGES b/src/CHANGES
index 0e48b21..13ae937 100644
--- a/src/CHANGES
+++ b/src/CHANGES
@@ -2,6 +2,13 @@
Change Log
==========
+Version 2.1.7 -
+------------------------------
+- Fixed regression reported by Andrea Censi (surfaced in PyContracts
+ tests) when using ParseSyntaxExceptions (raised when using operator '-')
+ with packrat parsing.
+
+
Version 2.1.6 -
------------------------------
- *Major packrat upgrade*, inspired by patch provided by Tal Einat -
diff --git a/src/pyparsing.py b/src/pyparsing.py
index 6a3f582..c52dedc 100644
--- a/src/pyparsing.py
+++ b/src/pyparsing.py
@@ -57,8 +57,8 @@ The pyparsing module handles some of the problems that are typically vexing when
- embedded comments
"""
-__version__ = "2.1.6"
-__versionTime__ = "07 Aug 2016 04:42 UTC"
+__version__ = "2.1.7"
+__versionTime__ = "09 Aug 2016 19:30 UTC"
__author__ = "Paul McGuire <ptmcg@users.sourceforge.net>"
import string
@@ -191,6 +191,14 @@ class ParseBaseException(Exception):
self.parserElement = elem
self.args = (pstr, loc, msg)
+ @classmethod
+ def _from_exception(cls, pe):
+ """
+ internal factory method to simplify creating one type of ParseException
+ from another - avoids having __init__ signature conflicts among subclasses
+ """
+ return cls(pe.pstr, pe.loc, pe.msg, pe.parserElement)
+
def __getattr__( self, aname ):
"""supported attributes by name are:
- lineno - returns the line number of the exception text
@@ -251,12 +259,10 @@ class ParseFatalException(ParseBaseException):
pass
class ParseSyntaxException(ParseFatalException):
- """just like C{L{ParseFatalException}}, but thrown internally when an
- C{L{ErrorStop<And._ErrorStop>}} ('-' operator) indicates that parsing is to stop immediately because
- an unbacktrackable syntax error has been found"""
- def __init__(self, pe):
- super(ParseSyntaxException, self).__init__(
- pe.pstr, pe.loc, pe.msg, pe.parserElement)
+ """just like L{ParseFatalException}, but thrown internally when an
+ L{ErrorStop<And._ErrorStop>} ('-' operator) indicates that parsing is to stop
+ immediately because an unbacktrackable syntax error has been found"""
+ pass
#~ class ReparseException(ParseBaseException):
#~ """Experimental class - parse actions can raise this exception to cause
@@ -272,7 +278,7 @@ class ParseSyntaxException(ParseFatalException):
#~ self.reparseLoc = restartLoc
class RecursiveGrammarException(Exception):
- """exception thrown by C{validate()} if the grammar could be improperly recursive"""
+ """exception thrown by L{ParserElement.validate} if the grammar could be improperly recursive"""
def __init__( self, parseElementList ):
self.parseElementTrace = parseElementList
@@ -3231,9 +3237,9 @@ class And(ParseExpression):
raise
except ParseBaseException as pe:
pe.__traceback__ = None
- raise ParseSyntaxException(pe)
+ raise ParseSyntaxException._from_exception(pe)
except IndexError:
- raise ParseSyntaxException( ParseException(instring, len(instring), self.errmsg, self) )
+ raise ParseSyntaxException(instring, len(instring), self.errmsg, self)
else:
loc, exprtokens = e._parse( instring, loc, doActions )
if exprtokens or exprtokens.haskeys():
diff --git a/src/unitTests.py b/src/unitTests.py
index a4c651f..dbcf5ae 100644
--- a/src/unitTests.py
+++ b/src/unitTests.py
@@ -2938,6 +2938,23 @@ class ExprSplitterTest(ParseTestCase):
print_("\n>>> " + line)
assert False, "invalid split on expression with maxSplits=1, corner case"
+class ParseFatalExceptionTest(ParseTestCase):
+ def runTest(self):
+
+ from pyparsing import Word, nums, ParseFatalException
+
+ success = False
+ try:
+ expr = "ZZZ" - Word(nums)
+ expr.parseString("ZZZ bad")
+ except ParseFatalException as pfe:
+ print('ParseFatalException raised correctly')
+ success = True
+ except Exception as e:
+ print(type(e))
+ print(e)
+
+ assert success, "bad handling of syntax error"
class MiscellaneousParserTests(ParseTestCase):
def runTest(self):
@@ -3148,6 +3165,7 @@ if console:
testclasses = []
#~ testclasses.append(put_test_class_here)
#~ testclasses.append(RequiredEachTest)
+
if not testclasses:
testRunner.run( makeTestSuite() )
else: