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.

149 lines
5.6 KiB

  1. from .utils import STRING_TYPE, logger
  2. ###{standalone
  3. class LarkError(Exception):
  4. pass
  5. class GrammarError(LarkError):
  6. pass
  7. class ParseError(LarkError):
  8. pass
  9. class LexError(LarkError):
  10. pass
  11. class UnexpectedEOF(ParseError):
  12. def __init__(self, expected):
  13. self.expected = expected
  14. message = ("Unexpected end-of-input. Expected one of: \n\t* %s\n" % '\n\t* '.join(x.name for x in self.expected))
  15. super(UnexpectedEOF, self).__init__(message)
  16. class UnexpectedInput(LarkError):
  17. pos_in_stream = None
  18. def get_context(self, text, span=40):
  19. pos = self.pos_in_stream
  20. start = max(pos - span, 0)
  21. end = pos + span
  22. if not isinstance(text, bytes):
  23. before = text[start:pos].rsplit('\n', 1)[-1]
  24. after = text[pos:end].split('\n', 1)[0]
  25. return before + after + '\n' + ' ' * len(before) + '^\n'
  26. else:
  27. before = text[start:pos].rsplit(b'\n', 1)[-1]
  28. after = text[pos:end].split(b'\n', 1)[0]
  29. return (before + after + b'\n' + b' ' * len(before) + b'^\n').decode("ascii", "backslashreplace")
  30. def match_examples(self, parse_fn, examples, token_type_match_fallback=False, use_accepts=False):
  31. """ Given a parser instance and a dictionary mapping some label with
  32. some malformed syntax examples, it'll return the label for the
  33. example that bests matches the current error.
  34. It's recommended to call this with `use_accepts=True`. The default is False for backwards compatibility.
  35. """
  36. assert self.state is not None, "Not supported for this exception"
  37. if isinstance(examples, dict):
  38. examples = examples.items()
  39. candidate = (None, False)
  40. for i, (label, example) in enumerate(examples):
  41. assert not isinstance(example, STRING_TYPE)
  42. for j, malformed in enumerate(example):
  43. try:
  44. parse_fn(malformed)
  45. except UnexpectedInput as ut:
  46. if ut.state == self.state:
  47. if use_accepts and ut.accepts != self.accepts:
  48. logger.debug("Different accepts with same state[%d]: %s != %s at example [%s][%s]" %
  49. (self.state, self.accepts, ut.accepts, i, j))
  50. continue
  51. try:
  52. if ut.token == self.token: # Try exact match first
  53. logger.debug("Exact Match at example [%s][%s]" % (i, j))
  54. return label
  55. if token_type_match_fallback:
  56. # Fallback to token types match
  57. if (ut.token.type == self.token.type) and not candidate[-1]:
  58. logger.debug("Token Type Fallback at example [%s][%s]" % (i, j))
  59. candidate = label, True
  60. except AttributeError:
  61. pass
  62. if not candidate[0]:
  63. logger.debug("Same State match at example [%s][%s]" % (i, j))
  64. candidate = label, False
  65. return candidate[0]
  66. class UnexpectedCharacters(LexError, UnexpectedInput):
  67. def __init__(self, seq, lex_pos, line, column, allowed=None, considered_tokens=None, state=None, token_history=None):
  68. self.line = line
  69. self.column = column
  70. self.pos_in_stream = lex_pos
  71. self.state = state
  72. self.allowed = allowed
  73. self.considered_tokens = considered_tokens
  74. if isinstance(seq, bytes):
  75. _s = seq[lex_pos:lex_pos+1].decode("ascii", "backslashreplace")
  76. else:
  77. _s = seq[lex_pos]
  78. message = "No terminal defined for '%s' at line %d col %d" % (_s, line, column)
  79. message += '\n\n' + self.get_context(seq)
  80. if allowed:
  81. message += '\nExpecting: %s\n' % allowed
  82. if token_history:
  83. message += '\nPrevious tokens: %s\n' % ', '.join(repr(t) for t in token_history)
  84. super(UnexpectedCharacters, self).__init__(message)
  85. class UnexpectedToken(ParseError, UnexpectedInput):
  86. def __init__(self, token, expected, considered_rules=None, state=None, puppet=None):
  87. self.line = getattr(token, 'line', '?')
  88. self.column = getattr(token, 'column', '?')
  89. self.pos_in_stream = getattr(token, 'pos_in_stream', None)
  90. self.state = state
  91. self.token = token
  92. self.expected = expected # XXX deprecate? `accepts` is better
  93. self.considered_rules = considered_rules
  94. self.puppet = puppet
  95. # TODO Only calculate `accepts()` when we need to display it to the user
  96. # This will improve performance when doing automatic error handling
  97. self.accepts = puppet and puppet.accepts()
  98. message = ("Unexpected token %r at line %s, column %s.\n"
  99. "Expected one of: \n\t* %s\n"
  100. % (token, self.line, self.column, '\n\t* '.join(self.accepts or self.expected)))
  101. super(UnexpectedToken, self).__init__(message)
  102. class VisitError(LarkError):
  103. """VisitError is raised when visitors are interrupted by an exception
  104. It provides the following attributes for inspection:
  105. - obj: the tree node or token it was processing when the exception was raised
  106. - orig_exc: the exception that cause it to fail
  107. """
  108. def __init__(self, rule, obj, orig_exc):
  109. self.obj = obj
  110. self.orig_exc = orig_exc
  111. message = 'Error trying to process rule "%s":\n\n%s' % (rule, orig_exc)
  112. super(VisitError, self).__init__(message)
  113. ###}