This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.2 KiB

  1. class GrammarError(Exception):
  2. pass
  3. class ParseError(Exception):
  4. pass
  5. class UnexpectedToken(ParseError):
  6. def __init__(self, token, expected, seq, index):
  7. self.token = token
  8. self.expected = expected
  9. self.line = getattr(token, 'line', '?')
  10. self.column = getattr(token, 'column', '?')
  11. try:
  12. context = ' '.join(['%r(%s)' % (t.value, t.type) for t in seq[index:index+5]])
  13. except AttributeError:
  14. context = seq[index:index+5]
  15. except TypeError:
  16. context = "<no context>"
  17. message = ("Unexpected token %r at line %s, column %s.\n"
  18. "Expected: %s\n"
  19. "Context: %s" % (token, self.line, self.column, expected, context))
  20. super(ParseError, self).__init__(message)
  21. def is_terminal(sym):
  22. return isinstance(sym, tuple) or sym.isupper() or sym[0] == '$'
  23. class LexerConf:
  24. def __init__(self, tokens, ignore, postlex):
  25. self.tokens = tokens
  26. self.ignore = ignore
  27. self.postlex = postlex
  28. class ParserConf:
  29. def __init__(self, rules, callback, start):
  30. assert all(len(r)==3 for r in rules)
  31. self.rules = rules
  32. self.callback = callback
  33. self.start = start