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.

281 lines
10 KiB

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