diff options
author | Paul McGuire <ptmcg@austin.rr.com> | 2019-04-01 22:52:44 -0500 |
---|---|---|
committer | Paul McGuire <ptmcg@austin.rr.com> | 2019-04-01 22:52:44 -0500 |
commit | 2cedb21cc3eff2a57f0d781dc38e679589664d3a (patch) | |
tree | ae7dabb8564b5ac719f2566660a396378a25f357 | |
parent | bf3e1b95adfbdf81bd24ec193ca901f63d260cbd (diff) | |
download | pyparsing-git-2cedb21cc3eff2a57f0d781dc38e679589664d3a.tar.gz |
General code cleanup; have unit test module exit(0 or 1) depending on test success, to trigger CI pass/fail; fix bug that crept into withAttribute; minor performance tweaks in Regex and Word
-rw-r--r-- | CHANGES | 4 | ||||
-rw-r--r-- | pyparsing.py | 59 | ||||
-rw-r--r-- | simple_unit_tests.py | 4 | ||||
-rw-r--r-- | unitTests.py | 8 |
4 files changed, 46 insertions, 29 deletions
@@ -2,7 +2,7 @@ Change Log ========== -Version 2.4.0 - March, 2019 +Version 2.4.0 - April, 2019 --------------------------- - Well, it looks like the API change that was introduced in 2.3.1 was more drastic than expected, so for a friendlier forward upgrade path, this @@ -69,7 +69,7 @@ Version 2.3.1 - January, 2019 # parse a string with a numeric second value instead of alpha expr.parseString("123 355") except pp.ParseException as pe: - print_(pp.ParseException.explain(pe)) + print(pp.ParseException.explain(pe)) Prints: 123 355 diff --git a/pyparsing.py b/pyparsing.py index 705e9ac..ebea155 100644 --- a/pyparsing.py +++ b/pyparsing.py @@ -94,7 +94,7 @@ classes inherit from. Use the docstrings for examples of how to: """ __version__ = "2.4.0" -__versionTime__ = "30 Mar 2019 07:57 UTC" +__versionTime__ = "02 Apr 2019 02:07 UTC" __author__ = "Paul McGuire <ptmcg@users.sourceforge.net>" import string @@ -762,7 +762,7 @@ class ParseResults(object): print(patt.addParseAction(make_palindrome).parseString("lskdj sdlkjf lksd")) # -> 'lskdjsdlkjflksddsklfjkldsjdksl' """ if isinstance(itemseq, ParseResults): - self += itemseq + self.__iadd__(itemseq) else: self.__toklist.extend(itemseq) @@ -2936,7 +2936,7 @@ class Word(Token): loc = result.end() return loc, result.group() - if not(instring[ loc ] in self.initChars): + if instring[loc] not in self.initChars: raise ParseException(instring, loc, self.errmsg, self) start = loc @@ -2951,9 +2951,9 @@ class Word(Token): throwException = False if loc - start < self.minLen: throwException = True - if self.maxSpecified and loc < instrlen and instring[loc] in bodychars: + elif self.maxSpecified and loc < instrlen and instring[loc] in bodychars: throwException = True - if self.asKeyword: + elif self.asKeyword: if (start>0 and instring[start-1] in bodychars) or (loc<instrlen and instring[loc] in bodychars): throwException = True @@ -3050,24 +3050,41 @@ class Regex(Token): self.mayReturnEmpty = True self.asGroupList = asGroupList self.asMatch = asMatch + if self.asGroupList: + self.parseImpl = self.parseImplAsGroupList + if self.asMatch: + self.parseImpl = self.parseImplAsMatch - def parseImpl( self, instring, loc, doActions=True ): + def parseImpl(self, instring, loc, doActions=True): result = self.re.match(instring,loc) if not result: raise ParseException(instring, loc, self.errmsg, self) loc = result.end() - if self.asMatch: - ret = result - elif self.asGroupList: - ret = result.groups() - else: - ret = ParseResults(result.group()) - d = result.groupdict() - if d: - for k, v in d.items(): - ret[k] = v - return loc,ret + ret = ParseResults(result.group()) + d = result.groupdict() + if d: + for k, v in d.items(): + ret[k] = v + return loc, ret + + def parseImplAsGroupList(self, instring, loc, doActions=True): + result = self.re.match(instring,loc) + if not result: + raise ParseException(instring, loc, self.errmsg, self) + + loc = result.end() + ret = result.groups() + return loc, ret + + def parseImplAsMatch(self, instring, loc, doActions=True): + result = self.re.match(instring,loc) + if not result: + raise ParseException(instring, loc, self.errmsg, self) + + loc = result.end() + ret = result + return loc, ret def __str__( self ): try: @@ -3392,7 +3409,7 @@ class White(Token): self.minLen = exact def parseImpl( self, instring, loc, doActions=True ): - if not(instring[ loc ] in self.matchWhite): + if instring[loc] not in self.matchWhite: raise ParseException(instring, loc, self.errmsg, self) start = loc loc += 1 @@ -3664,10 +3681,6 @@ class ParseExpression(ParserElement): return self - def setResultsName( self, name, listAllMatches=False ): - ret = super(ParseExpression,self).setResultsName(name,listAllMatches) - return ret - def validate( self, validateTrace=[] ): tmp = validateTrace[:]+[self] for e in self.exprs: @@ -5400,7 +5413,7 @@ def _makeTags(tagStr, xml, + Optional(Suppress("=") + tagAttrValue)))) + Optional("/",default=[False])("empty").setParseAction(lambda s,l,t:t[0]=='/') + suppress_GT) - closeTag = Combine(suppress_LT + _L("/") + tagStr + ">") + closeTag = Combine(_L("</") + tagStr + ">", adjacent=False) openTag = openTag("start"+"".join(resname.replace(":"," ").title().split())).setName("<%s>" % resname) closeTag = closeTag("end"+"".join(resname.replace(":"," ").title().split())).setName("</%s>" % resname) diff --git a/simple_unit_tests.py b/simple_unit_tests.py index 888b4a8..28bdb16 100644 --- a/simple_unit_tests.py +++ b/simple_unit_tests.py @@ -458,4 +458,6 @@ if __name__ == '__main__': # make into a suite and run it - this will run the tests in the same order # they are declared in this module suite = unittest.TestSuite(cls() for cls in test_case_classes) - unittest.TextTestRunner().run(suite) + result = unittest.TextTestRunner().run(suite) + + exit(0 if result.wasSuccessful() else 1) diff --git a/unitTests.py b/unitTests.py index 9a44193..baae01d 100644 --- a/unitTests.py +++ b/unitTests.py @@ -2346,7 +2346,7 @@ class WithAttributeParseActionTest(ParseTestCase): <a B="x">3</a> <a b="X">4</a> <a b="y">5</a> - <a class="boo">8</a> + <a class="boo">8</ a> """ tagStart, tagEnd = makeHTMLTags("a") @@ -4308,7 +4308,9 @@ if __name__ == '__main__': ] if not testclasses: - testRunner.run(makeTestSuite()) + result = testRunner.run(makeTestSuite()) else: BUFFER_OUTPUT = False - testRunner.run(makeTestSuiteTemp(testclasses)) + result = testRunner.run(makeTestSuiteTemp(testclasses)) + + exit(0 if result.wasSuccessful() else 1) |