summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b>2016-10-06 06:14:20 +0000
committerptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b>2016-10-06 06:14:20 +0000
commit53d55c3ed2cfe70bf844a4604698b3cb9808b03c (patch)
tree2572dd62030de645ac1cf3687932ec763400336d
parentff26efcec65724844461f54a0eabd04487f6291f (diff)
downloadpyparsing-53d55c3ed2cfe70bf844a4604698b3cb9808b03c.tar.gz
Fixed bug in col, which allowed simplification of LineStart down to minimal implementation
git-svn-id: svn://svn.code.sf.net/p/pyparsing/code/trunk@447 9bf210a0-9d2d-494c-87cf-cfb32e7dff7b
-rw-r--r--src/CHANGES4
-rw-r--r--src/pyparsing.py12
-rw-r--r--src/unitTests.py40
3 files changed, 44 insertions, 12 deletions
diff --git a/src/CHANGES b/src/CHANGES
index bf964aa..8bf25f2 100644
--- a/src/CHANGES
+++ b/src/CHANGES
@@ -10,7 +10,9 @@ Version 2.1.10 -
- Fixed behavior of LineStart to be much more predictable.
LineStart can now be used to detect if the next parse position
is col 1, factoring in potential leading whitespace (which would
- cause LineStart to fail).
+ cause LineStart to fail). Also fixed a bug in col, which is
+ used in LineStart, where '\n's were erroneously considered to
+ be column 1.
- Added support for multiline test strings in runTests.
diff --git a/src/pyparsing.py b/src/pyparsing.py
index a12e1b6..c809956 100644
--- a/src/pyparsing.py
+++ b/src/pyparsing.py
@@ -61,7 +61,7 @@ The pyparsing module handles some of the problems that are typically vexing when
"""
__version__ = "2.1.10"
-__versionTime__ = "04 Oct 2016 00:19 UTC"
+__versionTime__ = "06 Oct 2016 06:04 UTC"
__author__ = "Paul McGuire <ptmcg@users.sourceforge.net>"
import string
@@ -953,7 +953,7 @@ def col (loc,strg):
positions within the parsed string.
"""
s = strg
- return 1 if loc<len(s) and s[loc] == '\n' else loc - s.rfind("\n", 0, loc)
+ return 1 if 0<loc<len(s) and s[loc-1] == '\n' else loc - s.rfind("\n", 0, loc)
def lineno(loc,strg):
"""Returns current line number within a string, counting newlines as line separators.
@@ -3092,16 +3092,10 @@ class LineStart(_PositionToken):
"""
def __init__( self ):
super(LineStart,self).__init__()
- self.setWhitespaceChars( ParserElement.DEFAULT_WHITE_CHARS.replace("\n", ""))
self.errmsg = "Expected start of line"
- def preParse( self, instring, loc ):
- # don't do any pre-parsing for this class
- return loc
-
def parseImpl( self, instring, loc, doActions=True ):
- loc, _ = Optional(White(' \t').leaveWhitespace() + Empty()).parseImpl(instring, loc)
- if loc == 0 or col(loc, instring) == 1:
+ if col(loc, instring) == 1:
return loc, []
raise ParseException(instring, loc, self.errmsg, self)
diff --git a/src/unitTests.py b/src/unitTests.py
index d97c27a..0d45390 100644
--- a/src/unitTests.py
+++ b/src/unitTests.py
@@ -1735,6 +1735,33 @@ class LineStartTest(ParseTestCase):
success = test_patt.runTests(fail_tests, failureTests=True)[0]
assert success, "failed LineStart failure mode tests (3)"
+ test = """\
+ AAA 1
+ AAA 2
+
+ AAA
+
+ B AAA
+
+ """
+
+ from textwrap import dedent
+ test = dedent(test)
+ print(test)
+
+ for t, s, e in (pp.LineStart() + 'AAA').scanString(test):
+ print(s, e, pp.lineno(s, test), pp.line(s, test), ord(test[s]))
+ print()
+ assert (test[s] == 'A'), 'failed LineStart with insignificant newlines'
+
+ with AutoReset(pp.ParserElement, "DEFAULT_WHITE_CHARS"):
+ pp.ParserElement.setDefaultWhitespaceChars(' ')
+ for t, s, e in (pp.LineStart() + 'AAA').scanString(test):
+ print(s, e, pp.lineno(s, test), pp.line(s, test), ord(test[s]))
+ print()
+ assert(test[s] == 'A'), 'failed LineStart with significant newlines'
+
+
class LineAndStringEndTest(ParseTestCase):
def runTest(self):
from pyparsing import OneOrMore,lineEnd,alphanums,Word,stringEnd,delimitedList,SkipTo
@@ -3290,6 +3317,15 @@ class DefaultKeywordCharsTest(ParseTestCase):
else:
pass
+class ColTest(ParseTestCase):
+ def runTest(self):
+ import pyparsing
+
+ test = "*\n* \n* ALF\n*\n"
+ initials = [c for i, c in enumerate(test) if pyparsing.col(i, test) == 1]
+ print_(initials)
+ assert len(initials) == 4 and all(c=='*' for c in initials), 'fail col test'
+
class MiscellaneousParserTests(ParseTestCase):
def runTest(self):
@@ -3545,8 +3581,8 @@ if console:
testRunner = TextTestRunner()
testclasses = []
- #~ testclasses.append(put_test_class_here)
- #~ testclasses.append(RequiredEachTest)
+ # testclasses.append(put_test_class_here)
+ # testclasses.append(RequiredEachTest)
if not testclasses:
testRunner.run( makeTestSuite() )