diff options
author | Paul McGuire <ptmcg@austin.rr.com> | 2015-10-14 08:39:27 +0000 |
---|---|---|
committer | Paul McGuire <ptmcg@austin.rr.com> | 2015-10-14 08:39:27 +0000 |
commit | 8796e78be7d0320e71a9cb76d7fb1c70fe25bf90 (patch) | |
tree | 69ec1cc6df1772dc34c034085596a8eab1f32a6b /src | |
parent | bd53d0527ffea1b08ac76bba450f001fe0aa3648 (diff) | |
download | pyparsing-git-8796e78be7d0320e71a9cb76d7fb1c70fe25bf90.tar.gz |
Add ParserElement.runTests, quick-and-dirty test bench for testing an expression against a series of sample input strings
Diffstat (limited to 'src')
-rw-r--r-- | src/CHANGES | 5 | ||||
-rw-r--r-- | src/pyparsing.py | 20 |
2 files changed, 24 insertions, 1 deletions
diff --git a/src/CHANGES b/src/CHANGES index 66f3304..7f54de6 100644 --- a/src/CHANGES +++ b/src/CHANGES @@ -29,6 +29,11 @@ Version 2.0.4 - per suggestions from Williamzjc - thanks William! (Oh yeah, I'm not
supporting Python 2.3 with this code any more...)
+- Added ParserElement.runTests, a little test bench for quickly running
+ an expression against a list of sample input strings. Basically, I got
+ tired of writing the same test code over and over, and finally added it
+ as a test point method on ParserElement.
+
Version 2.0.3 -
---------------------------
diff --git a/src/pyparsing.py b/src/pyparsing.py index 3cdb378..303d4db 100644 --- a/src/pyparsing.py +++ b/src/pyparsing.py @@ -58,7 +58,7 @@ The pyparsing module handles some of the problems that are typically vexing when """
__version__ = "2.0.4"
-__versionTime__ = "13 Sep 2015 14:58"
+__versionTime__ = "14 Oct 2015 03:33"
__author__ = "Paul McGuire <ptmcg@users.sourceforge.net>"
import string
@@ -1546,7 +1546,25 @@ class ParserElement(object): def __rne__(self,other):
return not (self == other)
+ def runTests(self, tests):
+ """Execute the parse expression on a series of test strings, showing each
+ test, the parsed results or where the parse failed. Quick and easy way to
+ run a parse expression against a list of sample strings.
+ """
+ for t in tests:
+ print t
+ try:
+ print self.parseString(t).dump()
+ except ParseException as pe:
+ if '\n' in t:
+ print line(pe.loc, t)
+ print ' '*(col(pe.loc,t)-1) + '^'
+ else:
+ print ' '*pe.loc + '^'
+ print pe
+ print
+
class Token(ParserElement):
"""Abstract C{ParserElement} subclass, for defining atomic matching patterns."""
def __init__( self ):
|