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.7 KiB

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