diff options
author | ptmcg <ptmcg@austin.rr.com> | 2021-09-28 02:24:12 -0500 |
---|---|---|
committer | ptmcg <ptmcg@austin.rr.com> | 2021-09-28 02:24:12 -0500 |
commit | 47133a6148a0ac34bbda183d250bad2e4ac2490f (patch) | |
tree | 5d3cb9576292e15d7385417d2de40bc3d3f4a6a8 /pyparsing/core.py | |
parent | d2cb388b1b66a70713d42af3006be17c63fb3a74 (diff) | |
download | pyparsing-git-47133a6148a0ac34bbda183d250bad2e4ac2490f.tar.gz |
Code cleanup
Diffstat (limited to 'pyparsing/core.py')
-rw-r--r-- | pyparsing/core.py | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/pyparsing/core.py b/pyparsing/core.py index 17428bf..25ecf91 100644 --- a/pyparsing/core.py +++ b/pyparsing/core.py @@ -3318,6 +3318,22 @@ class LineStart(_PositionToken): super().__init__() self.errmsg = "Expected start of line" + def __add__(self, other): + return AtLineStart(other) + + def __sub__(self, other): + return AtLineStart(other) - Empty() + + def preParse(self, instring, loc): + if loc == 0: + return loc + else: + if instring[loc : loc + 1] == "\n" and "\n" in self.whiteChars: + ret = loc + 1 + else: + ret = super().preParse(instring, loc) + return ret + def parseImpl(self, instring, loc, doActions=True): if col(loc, instring) == 1: return loc, [] @@ -3356,6 +3372,12 @@ class StringStart(_PositionToken): super().__init__() self.errmsg = "Expected start of text" + def __add__(self, other): + return AtStringStart(other) + + def __sub__(self, other): + return AtStringStart(other) - Empty() + def parseImpl(self, instring, loc, doActions=True): if loc != 0: # see if entire string up to here is just whitespace and ignoreables @@ -4196,6 +4218,60 @@ class ParseElementEnhance(ParserElement): leaveWhitespace = leave_whitespace +class AtStringStart(ParseElementEnhance): + """Matches if expression matches at the beginning of the parse + string:: + + AtStringStart(Word(nums)).parse_string("123") + # prints ["123"] + + AtStringStart(Word(nums)).parse_string(" 123") + # raises ParseException + """ + + def __init__(self, expr): + super().__init__(expr) + self.callPreparse = False + + def parseImpl(self, instring, loc, doActions=True): + if loc != 0: + raise ParseException(instring, loc, "not found at string start") + return super().parseImpl(instring, loc, doActions) + + +class AtLineStart(ParseElementEnhance): + r"""Matches if an expression matches at the beginning of a line within + the parse string + + Example:: + + test = '''\ + AAA this line + AAA and this line + AAA but not this one + B AAA and definitely not this one + ''' + + for t in (AtLineStart('AAA') + restOfLine).search_string(test): + print(t) + + prints:: + + ['AAA', ' this line'] + ['AAA', ' and this line'] + + """ + + def __init__(self, expr): + super().__init__(expr) + self.callPreparse = False + + def parseImpl(self, instring, loc, doActions=True): + if col(loc, instring) != 1: + raise ParseException(instring, loc, "not found at line start") + return super().parseImpl(instring, loc, doActions) + + class FollowedBy(ParseElementEnhance): """Lookahead matching of the given parse expression. ``FollowedBy`` does *not* advance the parsing position within @@ -5211,6 +5287,12 @@ class Suppress(TokenConverter): else: return super().__add__(other) + def __sub__(self, other): + if isinstance(self.expr, _PendingSkip): + return Suppress(SkipTo(other)) - other + else: + return super().__sub__(other) + def postParse(self, instring, loc, tokenlist): return [] |