exceptions.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. from .utils import logger, NO_VALUE
  2. from typing import Mapping, Iterable, Callable, Union, TypeVar, Tuple, Any, List, Set, Optional, Collection, TYPE_CHECKING
  3. if TYPE_CHECKING:
  4. from .lexer import Token
  5. from .parsers.lalr_interactive_parser import InteractiveParser
  6. from .tree import Tree
  7. ###{standalone
  8. class LarkError(Exception):
  9. pass
  10. class ConfigurationError(LarkError, ValueError):
  11. pass
  12. def assert_config(value, options: Collection, msg='Got %r, expected one of %s'):
  13. if value not in options:
  14. raise ConfigurationError(msg % (value, options))
  15. class GrammarError(LarkError):
  16. pass
  17. class ParseError(LarkError):
  18. pass
  19. class LexError(LarkError):
  20. pass
  21. T = TypeVar('T')
  22. class UnexpectedInput(LarkError):
  23. """UnexpectedInput Error.
  24. Used as a base class for the following exceptions:
  25. - ``UnexpectedCharacters``: The lexer encountered an unexpected string
  26. - ``UnexpectedToken``: The parser received an unexpected token
  27. - ``UnexpectedEOF``: The parser expected a token, but the input ended
  28. After catching one of these exceptions, you may call the following helper methods to create a nicer error message.
  29. """
  30. line: int
  31. column: int
  32. pos_in_stream = None
  33. state: Any
  34. _terminals_by_name = None
  35. interactive_parser: 'InteractiveParser'
  36. def get_context(self, text: str, span: int=40) -> str:
  37. """Returns a pretty string pinpointing the error in the text,
  38. with span amount of context characters around it.
  39. Note:
  40. The parser doesn't hold a copy of the text it has to parse,
  41. so you have to provide it again
  42. """
  43. pos = self.pos_in_stream or 0
  44. start = max(pos - span, 0)
  45. end = pos + span
  46. if not isinstance(text, bytes):
  47. before = text[start:pos].rsplit('\n', 1)[-1]
  48. after = text[pos:end].split('\n', 1)[0]
  49. return before + after + '\n' + ' ' * len(before.expandtabs()) + '^\n'
  50. else:
  51. before = text[start:pos].rsplit(b'\n', 1)[-1]
  52. after = text[pos:end].split(b'\n', 1)[0]
  53. return (before + after + b'\n' + b' ' * len(before.expandtabs()) + b'^\n').decode("ascii", "backslashreplace")
  54. def match_examples(self, parse_fn: 'Callable[[str], Tree]',
  55. examples: Union[Mapping[T, Iterable[str]], Iterable[Tuple[T, Iterable[str]]]],
  56. token_type_match_fallback: bool=False,
  57. use_accepts: bool=True
  58. ) -> Optional[T]:
  59. """Allows you to detect what's wrong in the input text by matching
  60. against example errors.
  61. Given a parser instance and a dictionary mapping some label with
  62. some malformed syntax examples, it'll return the label for the
  63. example that bests matches the current error. The function will
  64. iterate the dictionary until it finds a matching error, and
  65. return the corresponding value.
  66. For an example usage, see `examples/error_reporting_lalr.py`
  67. Parameters:
  68. parse_fn: parse function (usually ``lark_instance.parse``)
  69. examples: dictionary of ``{'example_string': value}``.
  70. use_accepts: Recommended to keep this as ``use_accepts=True``.
  71. """
  72. assert self.state is not None, "Not supported for this exception"
  73. if isinstance(examples, Mapping):
  74. examples = examples.items()
  75. candidate = (None, False)
  76. for i, (label, example) in enumerate(examples):
  77. assert not isinstance(example, str), "Expecting a list"
  78. for j, malformed in enumerate(example):
  79. try:
  80. parse_fn(malformed)
  81. except UnexpectedInput as ut:
  82. if ut.state == self.state:
  83. if (
  84. use_accepts
  85. and isinstance(self, UnexpectedToken)
  86. and isinstance(ut, UnexpectedToken)
  87. and ut.accepts != self.accepts
  88. ):
  89. logger.debug("Different accepts with same state[%d]: %s != %s at example [%s][%s]" %
  90. (self.state, self.accepts, ut.accepts, i, j))
  91. continue
  92. if (
  93. isinstance(self, (UnexpectedToken, UnexpectedEOF))
  94. and isinstance(ut, (UnexpectedToken, UnexpectedEOF))
  95. ):
  96. if ut.token == self.token: # Try exact match first
  97. logger.debug("Exact Match at example [%s][%s]" % (i, j))
  98. return label
  99. if token_type_match_fallback:
  100. # Fallback to token types match
  101. if (ut.token.type == self.token.type) and not candidate[-1]:
  102. logger.debug("Token Type Fallback at example [%s][%s]" % (i, j))
  103. candidate = label, True
  104. if candidate[0] is None:
  105. logger.debug("Same State match at example [%s][%s]" % (i, j))
  106. candidate = label, False
  107. return candidate[0]
  108. def _format_expected(self, expected):
  109. if self._terminals_by_name:
  110. d = self._terminals_by_name
  111. expected = [d[t_name].user_repr() if t_name in d else t_name for t_name in expected]
  112. return "Expected one of: \n\t* %s\n" % '\n\t* '.join(expected)
  113. class UnexpectedEOF(ParseError, UnexpectedInput):
  114. """An exception that is raised by the parser, when the input ends while it still expects a token.
  115. """
  116. expected: 'List[Token]'
  117. def __init__(self, expected, state=None, terminals_by_name=None):
  118. super(UnexpectedEOF, self).__init__()
  119. self.expected = expected
  120. self.state = state
  121. from .lexer import Token
  122. self.token = Token("<EOF>", "") # , line=-1, column=-1, pos_in_stream=-1)
  123. self.pos_in_stream = -1
  124. self.line = -1
  125. self.column = -1
  126. self._terminals_by_name = terminals_by_name
  127. def __str__(self):
  128. message = "Unexpected end-of-input. "
  129. message += self._format_expected(self.expected)
  130. return message
  131. class UnexpectedCharacters(LexError, UnexpectedInput):
  132. """An exception that is raised by the lexer, when it cannot match the next
  133. string of characters to any of its terminals.
  134. """
  135. allowed: Set[str]
  136. considered_tokens: Set[Any]
  137. def __init__(self, seq, lex_pos, line, column, allowed=None, considered_tokens=None, state=None, token_history=None,
  138. terminals_by_name=None, considered_rules=None):
  139. super(UnexpectedCharacters, self).__init__()
  140. # TODO considered_tokens and allowed can be figured out using state
  141. self.line = line
  142. self.column = column
  143. self.pos_in_stream = lex_pos
  144. self.state = state
  145. self._terminals_by_name = terminals_by_name
  146. self.allowed = allowed
  147. self.considered_tokens = considered_tokens
  148. self.considered_rules = considered_rules
  149. self.token_history = token_history
  150. if isinstance(seq, bytes):
  151. self.char = seq[lex_pos:lex_pos + 1].decode("ascii", "backslashreplace")
  152. else:
  153. self.char = seq[lex_pos]
  154. self._context = self.get_context(seq)
  155. def __str__(self):
  156. message = "No terminal matches '%s' in the current parser context, at line %d col %d" % (self.char, self.line, self.column)
  157. message += '\n\n' + self._context
  158. if self.allowed:
  159. message += self._format_expected(self.allowed)
  160. if self.token_history:
  161. message += '\nPrevious tokens: %s\n' % ', '.join(repr(t) for t in self.token_history)
  162. return message
  163. class UnexpectedToken(ParseError, UnexpectedInput):
  164. """An exception that is raised by the parser, when the token it received
  165. doesn't match any valid step forward.
  166. Parameters:
  167. token: The mismatched token
  168. expected: The set of expected tokens
  169. considered_rules: Which rules were considered, to deduce the expected tokens
  170. state: A value representing the parser state. Do not rely on its value or type.
  171. interactive_parser: An instance of ``InteractiveParser``, that is initialized to the point of failure,
  172. and can be used for debugging and error handling.
  173. Note: These parameters are available as attributes of the instance.
  174. """
  175. expected: Set[str]
  176. considered_rules: Set[str]
  177. def __init__(self, token, expected, considered_rules=None, state=None, interactive_parser=None, terminals_by_name=None, token_history=None):
  178. super(UnexpectedToken, self).__init__()
  179. # TODO considered_rules and expected can be figured out using state
  180. self.line = getattr(token, 'line', '?')
  181. self.column = getattr(token, 'column', '?')
  182. self.pos_in_stream = getattr(token, 'start_pos', None)
  183. self.state = state
  184. self.token = token
  185. self.expected = expected # XXX deprecate? `accepts` is better
  186. self._accepts = NO_VALUE
  187. self.considered_rules = considered_rules
  188. self.interactive_parser = interactive_parser
  189. self._terminals_by_name = terminals_by_name
  190. self.token_history = token_history
  191. @property
  192. def accepts(self) -> Set[str]:
  193. if self._accepts is NO_VALUE:
  194. self._accepts = self.interactive_parser and self.interactive_parser.accepts()
  195. return self._accepts
  196. def __str__(self):
  197. message = ("Unexpected token %r at line %s, column %s.\n%s"
  198. % (self.token, self.line, self.column, self._format_expected(self.accepts or self.expected)))
  199. if self.token_history:
  200. message += "Previous tokens: %r\n" % self.token_history
  201. return message
  202. class VisitError(LarkError):
  203. """VisitError is raised when visitors are interrupted by an exception
  204. It provides the following attributes for inspection:
  205. Parameters:
  206. rule: the name of the visit rule that failed
  207. obj: the tree-node or token that was being processed
  208. orig_exc: the exception that cause it to fail
  209. Note: These parameters are available as attributes
  210. """
  211. obj: 'Union[Tree, Token]'
  212. orig_exc: Exception
  213. def __init__(self, rule, obj, orig_exc):
  214. message = 'Error trying to process rule "%s":\n\n%s' % (rule, orig_exc)
  215. super(VisitError, self).__init__(message)
  216. self.rule = rule
  217. self.obj = obj
  218. self.orig_exc = orig_exc
  219. class MissingVariableError(LarkError):
  220. pass
  221. ###}