diff options
author | Paul McGuire <ptmcg@austin.rr.com> | 2019-07-07 21:31:46 -0500 |
---|---|---|
committer | Paul McGuire <ptmcg@austin.rr.com> | 2019-07-07 21:31:46 -0500 |
commit | 4ecdf1b69b4316ee7356e1f7b433a0495f7df621 (patch) | |
tree | dc01824c8b9d06ebf92911f01384224d7e7a9d92 | |
parent | 3f94c1245d318e0b18b991d72ab52d14f27cd4cf (diff) | |
download | pyparsing-git-4ecdf1b69b4316ee7356e1f7b433a0495f7df621.tar.gz |
Augment ParseException str() output to include what character was found at the error location, to help diagnose errors.
-rw-r--r-- | CHANGES | 17 | ||||
-rw-r--r-- | pyparsing.py | 13 |
2 files changed, 26 insertions, 4 deletions
@@ -58,6 +58,20 @@ Version 2.4.1 - July, 2019 This form is only valid when used with the '|' operator. +- Improved exception messages to show what was actually found, not + just what was expected. + + word = pp.Word(pp.alphas) + pp.OneOrMore(word).parseString("aaa bbb 123", parseAll=True) + + Former exception message: + + pyparsing.ParseException: Expected end of text (at char 8), (line:1, col:9) + + New exception message: + + pyparsing.ParseException: Expected end of text, found '1' (at char 8), (line:1, col:9) + - Added ParseResults.from_dict classmethod, to simplify creation of a ParseResults with results names. May be called with a dict argument, or with a series of named arguments (or both). This @@ -104,7 +118,8 @@ Version 2.4.1 - July, 2019 delegate to it to model state-specific properties and behavior. - Added example nested_markup.py, showing a simple wiki markup with - nested markup directives. + nested markup directives, and illustrating the use of '...' for + skipping over input to match the next expression. - Rewrote delta_time.py example (renamed from deltaTime.py) to fix some omitted formats and upgrade to latest pyparsing idioms, diff --git a/pyparsing.py b/pyparsing.py index 27df353..eabfead 100644 --- a/pyparsing.py +++ b/pyparsing.py @@ -96,7 +96,7 @@ classes inherit from. Use the docstrings for examples of how to: """ __version__ = "2.4.1" -__versionTime__ = "07 Jul 2019 06:35 UTC" +__versionTime__ = "08 Jul 2019 02:00 UTC" __author__ = "Paul McGuire <ptmcg@users.sourceforge.net>" import string @@ -290,8 +290,15 @@ class ParseBaseException(Exception): raise AttributeError(aname) def __str__( self ): - return "%s (at char %d), (line:%d, col:%d)" % \ - ( self.msg, self.loc, self.lineno, self.column ) + if self.pstr: + if self.loc >= len(self.pstr): + foundstr = ', found end of text' + else: + foundstr = ', found %r' % self.pstr[self.loc:self.loc+1] + else: + foundstr = '' + return "%s%s (at char %d), (line:%d, col:%d)" % \ + ( self.msg, foundstr, self.loc, self.lineno, self.column ) def __repr__( self ): return _ustr(self) def markInputline( self, markerString = ">!<" ): |