Пример #1
0
 def verifyCol(strg, locn, toks):
     if col(locn, strg) != n:
         raise ParseException(strg, locn,
                              "matched token not at column %d" % n)
Пример #2
0
 def cvt_fn(t, l, s):
     try:
         return datetime.strptime(s[t.start:t.end], fmt)
     except ValueError as ve:
         raise ParseException(t.type, l, s, str(ve))
Пример #3
0
    def parseImpl(self, string, start, doActions=True):
        causes = []
        matches = []

        for e in self.alternate:
            if isinstance(e, Fast):
                for ee in e.get_short_list(string, start):
                    try:
                        end = ee._parse(string, start).end
                        matches.append((end, ee))
                    except ParseException as err:
                        causes.append(err)
            else:
                try:
                    end = e._parse(string, start).end
                    matches.append((end, e))
                except ParseException as err:
                    causes.append(err)

        if not matches:
            raise ParseException(
                self,
                start,
                string,
                msg="no defined alternatives to match",
                cause=causes,
            )
        if len(matches) == 1:
            _, expr = matches[0]
            result = expr._parse(string, start, doActions)
            return ParseResults(self, result.start, result.end, [result])

        if matches:
            # re-evaluate all matches in descending order of length of match, in case attached actions
            # might change whether or how much they match of the input.
            matches.sort(key=itemgetter(0), reverse=True)

            if not doActions:
                # no further conditions or parse actions to change the selection of
                # alternative, so the first match will be the best match
                _, expr = matches[0]
                result = expr._parse(string, start, doActions)
                return ParseResults(self, result.start, result.end, [result])

            longest = -1, None
            for loc, expr in matches:
                if loc <= longest[0]:
                    # already have a longer match than this one will deliver, we are done
                    return longest

                try:
                    result = expr._parse(string, start, doActions)
                except ParseException as err:
                    causes.append(err)
                else:
                    if result.end >= loc:
                        return ParseResults(self, result.start, result.end,
                                            [result])
                    # didn't match as much as before
                    elif result.end > longest[0]:
                        longest = (
                            result.end,
                            ParseResults(self, result.start, result.end,
                                         [result]),
                        )

            if longest != (-1, None):
                return longest
Пример #4
0
 def mustMatchTheseTokens(t, l, s):
     theseTokens = _flatten(t)
     if theseTokens != matchTokens:
         raise ParseException("", 0, "")
Пример #5
0
 def parseImpl(self, string, loc, doActions=True):
     if string.startswith(self.match, loc):
         return loc + len(self.match), ParseResults(self, [self.match])
     raise ParseException(self, loc, string)
Пример #6
0
 def parseImpl(self, string, loc, doActions=True):
     if string[loc] == self.match:
         return loc + 1, ParseResults(self, [self.match])
     raise ParseException(self, loc, string)
Пример #7
0
 def parseImpl(self, string, loc, doActions=True):
     raise ParseException(self, loc, string)
Пример #8
0
 def parseImpl(self, string, loc, doActions=True):
     if self.pattern.match(string, loc):
         return loc + len(self.match), ParseResults(self, [self.match])
     raise ParseException(self, loc, string)
Пример #9
0
    def parseImpl(self, string, loc, doActions=True):
        l = len(string)
        if loc >= l:
            return l, ParseResults(self, [])

        raise ParseException(self, loc, string)
Пример #10
0
 def parseImpl(self, string, loc, doActions=True):
     if loc != 0:
         # see if entire string up to here is just whitespace and ignoreables
         if loc != self.engine.skip(string, 0):
             raise ParseException(self, loc, string)
     return loc, []
Пример #11
0
 def parseImpl(self, string, loc, doActions=True):
     if col(loc, string) == 1:
         return loc, ParseResults(self, [])
     raise ParseException(self, loc, string)