This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 

78 řádky
1.9 KiB

  1. import re
  2. class GrammarError(Exception):
  3. pass
  4. class ParseError(Exception):
  5. pass
  6. class UnexpectedToken(ParseError):
  7. def __init__(self, token, expected, seq, index):
  8. self.token = token
  9. self.expected = expected
  10. self.line = getattr(token, 'line', '?')
  11. self.column = getattr(token, 'column', '?')
  12. try:
  13. context = ' '.join(['%r(%s)' % (t.value, t.type) for t in seq[index:index+5]])
  14. except AttributeError:
  15. context = seq[index:index+5]
  16. except TypeError:
  17. context = "<no context>"
  18. message = ("Unexpected token %r at line %s, column %s.\n"
  19. "Expected: %s\n"
  20. "Context: %s" % (token, self.line, self.column, expected, context))
  21. super(UnexpectedToken, self).__init__(message)
  22. def is_terminal(sym):
  23. return isinstance(sym, tuple) or sym.isupper() or sym[0] == '$'
  24. class LexerConf:
  25. def __init__(self, tokens, ignore, postlex):
  26. self.tokens = tokens
  27. self.ignore = ignore
  28. self.postlex = postlex
  29. class ParserConf:
  30. def __init__(self, rules, callback, start):
  31. assert all(len(r)==3 for r in rules)
  32. self.rules = rules
  33. self.callback = callback
  34. self.start = start
  35. class Pattern(object):
  36. def __init__(self, value):
  37. self.value = value
  38. def __repr__(self):
  39. return repr(self.value)
  40. class PatternStr(Pattern):
  41. def to_regexp(self):
  42. return re.escape(self.value)
  43. priority = 0
  44. class PatternRE(Pattern):
  45. def to_regexp(self):
  46. return self.value
  47. priority = 1
  48. class TokenDef(object):
  49. def __init__(self, name, pattern):
  50. assert isinstance(pattern, Pattern), pattern
  51. self.name = name
  52. self.pattern = pattern
  53. def __repr__(self):
  54. return '%s(%r, %r)' % (type(self).__name__, self.name, self.pattern)