summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b>2008-07-02 00:20:40 +0000
committerptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b>2008-07-02 00:20:40 +0000
commitb53d5bcdf2fc30941806ba507c853a43e06a1600 (patch)
treeb58b200a8373238a7b0d11b530fff050ccc3bb5e
parent9d032b39b001e2c1cddc2d6747e2e242b422252b (diff)
downloadpyparsing-b53d5bcdf2fc30941806ba507c853a43e06a1600.tar.gz
Added __dir__ function to ParseException and ParseResults classes, for Python 2.6 and 3.0 support.
Added failOn argument to SkipTo to allow specification of strings or expressions that may not be skipped. git-svn-id: svn://svn.code.sf.net/p/pyparsing/code/src@159 9bf210a0-9d2d-494c-87cf-cfb32e7dff7b
-rw-r--r--CHANGES48
-rw-r--r--pyparsing.py30
2 files changed, 53 insertions, 25 deletions
diff --git a/CHANGES b/CHANGES
index 8ccd35d..280e696 100644
--- a/CHANGES
+++ b/CHANGES
@@ -8,15 +8,27 @@ Version 1.5.1 - ???, 2008
with parseAll argument to parseString. Posted by pboucher on the
pyparsing wiki, thanks!
+- Added failOn argument to SkipTo, so that grammars can define
+ literal strings or pyparsing expressions which, if found in the
+ skipped text, will cause SkipTo to fail. Useful to prevent
+ SkipTo from reading past terminating expression. Instigated by
+ question posed by Aki Niimura on the pyparsing wiki.
+
- Removed dependency on xml.sax.saxutils.escape, and included
internal implementation instead - proposed by Mike Droettboom on
- the pyparsing mailing list, thanks Mike! Also fixed erroneous
- mapping in replaceHTMLEntity of &quot to ', now correctly maps
+ the pyparsing mailing list, thanks Mike! Also fixed erroneous
+ mapping in replaceHTMLEntity of &quot to ', now correctly maps
to ".
- Fixed typo in ParseResults.insert, found by Alejandro Dubrovsky,
good catch!
-
+
+- Added __dir__() methods to ParseBaseException and ParseResults,
+ to support new dir() behavior in Py2.6 and Py3.0. If dir() is
+ called on a ParseResults object, the returned list will include
+ the base set of attribute names, plus any results names that are
+ defined.
+
Version 1.5.0 - June, 2008
--------------------------
@@ -26,9 +38,9 @@ FAQ's: support for forcing parsing of the complete input string
and a method to improve the mechanism of detecting where syntax
errors occur in an input string with various optional and
alternative paths. This release also includes a helper method
-to simplify definition of indentation-based grammars. With
-these changes (and the past few minor updates), I thought it was
-finally time to bump the minor rev number on pyparsing - so
+to simplify definition of indentation-based grammars. With
+these changes (and the past few minor updates), I thought it was
+finally time to bump the minor rev number on pyparsing - so
1.5.0 is now available! Read on...
- AT LAST!!! You can now call parseString and have it raise
@@ -82,23 +94,23 @@ finally time to bump the minor rev number on pyparsing - so
the pyparsing mailing list and wiki - special thanks to
Eike Welk and Thomas/Poldy on the pyparsing wiki!
-- Added indentedBlock helper method, to encapsulate the parse
- actions and indentation stack management needed to keep track of
- indentation levels. Use indentedBlock to define grammars for
+- Added indentedBlock helper method, to encapsulate the parse
+ actions and indentation stack management needed to keep track of
+ indentation levels. Use indentedBlock to define grammars for
indentation-based grouping grammars, like Python's.
-
+
indentedBlock takes up to 3 parameters:
- - blockStatementExpr - expression defining syntax of statement
- that is repeated within the indented block
- - indentStack - list created by caller to manage indentation
- stack (multiple indentedBlock expressions
+ - blockStatementExpr - expression defining syntax of statement
+ that is repeated within the indented block
+ - indentStack - list created by caller to manage indentation
+ stack (multiple indentedBlock expressions
within a single grammar should share a common indentStack)
- - indent - boolean indicating whether block must be indented
- beyond the the current level; set to False for block of
+ - indent - boolean indicating whether block must be indented
+ beyond the the current level; set to False for block of
left-most statements (default=True)
A valid block must contain at least one indented statement.
-
+
- Fixed bug in nestedExpr in which ignored expressions needed
to be set off with whitespace. Reported by Stefaan Himpe,
nice catch!
@@ -130,7 +142,7 @@ finally time to bump the minor rev number on pyparsing - so
complete Each was defined. Thanks, Michael!
- Also fixed bug in Each in which Optional's with default values
- were not getting the defaults added to the results of the
+ were not getting the defaults added to the results of the
overall Each expression.
- Fixed a bug in Optional in which results names were not
diff --git a/pyparsing.py b/pyparsing.py
index e9a87e7..9a6f2f2 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.1"
-__versionTime__ = "3 June 2008 22:11"
+__versionTime__ = "1 July 2008 16:48"
__author__ = "Paul McGuire <ptmcg@users.sourceforge.net>"
import string
@@ -154,7 +154,7 @@ else:
nums = string.digits
hexnums = nums + "ABCDEFabcdef"
alphanums = alphas + nums
-_bslash = "\\"
+_bslash = chr(92)
printables = "".join( [ c for c in string.printable if c not in string.whitespace ] )
class ParseBaseException(Exception):
@@ -202,6 +202,9 @@ class ParseBaseException(Exception):
line_str = "".join( [line_str[:line_column],
markerString, line_str[line_column:]])
return line_str.strip()
+ def __dir__(self):
+ return "loc msg pstr parserElement lineno col line " \
+ "markInputLine __str__ __repr__".split()
class ParseException(ParseBaseException):
"""exception thrown when parse expressions don't match class;
@@ -604,6 +607,8 @@ class ParseResults(object):
else:
self.__parent = None
+ def __dir__(self):
+ return dir(super(ParseResults,self)) + self.keys()
def col (loc,strg):
"""Returns current column within a string, counting newlines as line separators.
@@ -1543,7 +1548,6 @@ class Keyword(Token):
Keyword.DEFAULT_KEYWORD_CHARS = chars
setDefaultKeywordChars = staticmethod(setDefaultKeywordChars)
-
class CaselessLiteral(Literal):
"""Token to match a specified string, ignoring case of letters.
Note: the matched results will always be in the case of the given
@@ -2791,7 +2795,7 @@ class SkipTo(ParseElementEnhance):
argument is used to define grammars (typically quoted strings and comments) that
might contain false matches.
"""
- def __init__( self, other, include=False, ignore=None ):
+ def __init__( self, other, include=False, ignore=None, failOn=None ):
super( SkipTo, self ).__init__( other )
if ignore is not None:
self.expr = self.expr.copy()
@@ -2800,6 +2804,10 @@ class SkipTo(ParseElementEnhance):
self.mayIndexError = False
self.includeMatch = include
self.asList = False
+ if failOn is not None and isinstance(failOn, basestring):
+ self.failOn = Literal(failOn)
+ else:
+ self.failOn = failOn
self.errmsg = "No match found for "+_ustr(self.expr)
#self.myException = ParseException("",0,self.errmsg,self)
@@ -2807,12 +2815,17 @@ class SkipTo(ParseElementEnhance):
startLoc = loc
instrlen = len(instring)
expr = self.expr
+ failParse = False
while loc <= instrlen:
try:
+ if self.failOn:
+ failParse = True
+ self.failOn.tryParse(instring, loc)
+ failParse = False
loc = expr._skipIgnorables( instring, loc )
expr._parse( instring, loc, doActions=False, callPreParse=False )
+ skipText = instring[startLoc:loc]
if self.includeMatch:
- skipText = instring[startLoc:loc]
loc,mat = expr._parse(instring,loc,doActions,callPreParse=False)
if mat:
skipRes = ParseResults( skipText )
@@ -2821,9 +2834,12 @@ class SkipTo(ParseElementEnhance):
else:
return loc, [ skipText ]
else:
- return loc, [ instring[startLoc:loc] ]
+ return loc, [ skipText ]
except (ParseException,IndexError):
- loc += 1
+ if failParse:
+ raise
+ else:
+ loc += 1
exc = self.myException
exc.loc = loc
exc.pstr = instring