exceptions.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. # exceptions.py
  2. from __future__ import annotations
  3. import copy
  4. import re
  5. import sys
  6. import typing
  7. import warnings
  8. from functools import cached_property
  9. from .warnings import PyparsingDeprecationWarning
  10. from .unicode import pyparsing_unicode as ppu
  11. from .util import (
  12. _collapse_string_to_ranges,
  13. col,
  14. deprecate_argument,
  15. line,
  16. lineno,
  17. replaced_by_pep8,
  18. )
  19. class _ExceptionWordUnicodeSet(
  20. ppu.Latin1, ppu.LatinA, ppu.LatinB, ppu.Greek, ppu.Cyrillic
  21. ):
  22. pass
  23. _extract_alphanums = _collapse_string_to_ranges(_ExceptionWordUnicodeSet.alphanums)
  24. _exception_word_extractor = re.compile(fr"([{_extract_alphanums}]{{1,16}})|.")
  25. class ParseBaseException(Exception):
  26. """base exception class for all parsing runtime exceptions"""
  27. loc: int
  28. msg: str
  29. pstr: str
  30. parser_element: typing.Any # "ParserElement"
  31. args: tuple[str, int, typing.Optional[str]]
  32. __slots__ = (
  33. "loc",
  34. "msg",
  35. "pstr",
  36. "parser_element",
  37. "args",
  38. )
  39. # Performance tuning: we construct a *lot* of these, so keep this
  40. # constructor as small and fast as possible
  41. def __init__(
  42. self,
  43. pstr: str,
  44. loc: int = 0,
  45. msg: typing.Optional[str] = None,
  46. elem=None,
  47. ) -> None:
  48. if msg is None:
  49. msg, pstr = pstr, ""
  50. self.loc = loc
  51. self.msg = msg
  52. self.pstr = pstr
  53. self.parser_element = elem
  54. self.args = (pstr, loc, msg)
  55. @staticmethod
  56. def explain_exception(exc: Exception, depth: int = 16) -> str:
  57. """
  58. Method to take an exception and translate the Python internal traceback into a list
  59. of the pyparsing expressions that caused the exception to be raised.
  60. Parameters:
  61. - exc - exception raised during parsing (need not be a ParseException, in support
  62. of Python exceptions that might be raised in a parse action)
  63. - depth (default=16) - number of levels back in the stack trace to list expression
  64. and function names; if None, the full stack trace names will be listed; if 0, only
  65. the failing input line, marker, and exception string will be shown
  66. Returns a multi-line string listing the ParserElements and/or function names in the
  67. exception's stack trace.
  68. """
  69. import inspect
  70. from .core import ParserElement
  71. if depth is None:
  72. depth = sys.getrecursionlimit()
  73. ret: list[str] = []
  74. if isinstance(exc, ParseBaseException):
  75. ret.append(exc.line)
  76. ret.append(f"{'^':>{exc.column}}")
  77. ret.append(f"{type(exc).__name__}: {exc}")
  78. if depth <= 0 or exc.__traceback__ is None:
  79. return "\n".join(ret)
  80. callers = inspect.getinnerframes(exc.__traceback__, context=depth)
  81. seen: set[int] = set()
  82. for ff in callers[-depth:]:
  83. frm = ff[0]
  84. f_self = frm.f_locals.get("self", None)
  85. if isinstance(f_self, ParserElement):
  86. if not frm.f_code.co_name.startswith(("parseImpl", "_parseNoCache")):
  87. continue
  88. if id(f_self) in seen:
  89. continue
  90. seen.add(id(f_self))
  91. self_type = type(f_self)
  92. ret.append(f"{self_type.__module__}.{self_type.__name__} - {f_self}")
  93. elif f_self is not None:
  94. self_type = type(f_self)
  95. ret.append(f"{self_type.__module__}.{self_type.__name__}")
  96. else:
  97. code = frm.f_code
  98. if code.co_name in ("wrapper", "<module>"):
  99. continue
  100. ret.append(code.co_name)
  101. depth -= 1
  102. if not depth:
  103. break
  104. return "\n".join(ret)
  105. @classmethod
  106. def _from_exception(cls, pe) -> ParseBaseException:
  107. """
  108. internal factory method to simplify creating one type of ParseException
  109. from another - avoids having __init__ signature conflicts among subclasses
  110. """
  111. return cls(pe.pstr, pe.loc, pe.msg, pe.parser_element)
  112. @cached_property
  113. def line(self) -> str:
  114. """
  115. Return the line of text where the exception occurred.
  116. """
  117. return line(self.loc, self.pstr)
  118. @cached_property
  119. def lineno(self) -> int:
  120. """
  121. Return the 1-based line number of text where the exception occurred.
  122. """
  123. return lineno(self.loc, self.pstr)
  124. @cached_property
  125. def col(self) -> int:
  126. """
  127. Return the 1-based column on the line of text where the exception occurred.
  128. """
  129. return col(self.loc, self.pstr)
  130. @cached_property
  131. def column(self) -> int:
  132. """
  133. Return the 1-based column on the line of text where the exception occurred.
  134. """
  135. return col(self.loc, self.pstr)
  136. @cached_property
  137. def found(self) -> str:
  138. if not self.pstr:
  139. return ""
  140. if self.loc >= len(self.pstr):
  141. return "end of text"
  142. # pull out next word at error location
  143. found_match = _exception_word_extractor.match(self.pstr, self.loc)
  144. if found_match is not None:
  145. found_text = found_match[0]
  146. else:
  147. found_text = self.pstr[self.loc : self.loc + 1]
  148. return repr(found_text).replace(r"\\", "\\")
  149. # pre-PEP8 compatibility
  150. @property
  151. def parserElement(self):
  152. warnings.warn(
  153. "parserElement is deprecated, use parser_element",
  154. PyparsingDeprecationWarning,
  155. stacklevel=2,
  156. )
  157. return self.parser_element
  158. @parserElement.setter
  159. def parserElement(self, elem):
  160. warnings.warn(
  161. "parserElement is deprecated, use parser_element",
  162. PyparsingDeprecationWarning,
  163. stacklevel=2,
  164. )
  165. self.parser_element = elem
  166. def copy(self):
  167. return copy.copy(self)
  168. def formatted_message(self) -> str:
  169. """
  170. Output the formatted exception message.
  171. Can be overridden to customize the message formatting or contents.
  172. .. versionadded:: 3.2.0
  173. """
  174. found_phrase = f", found {self.found}" if self.found else ""
  175. return f"{self.msg}{found_phrase} (at char {self.loc}), (line:{self.lineno}, col:{self.column})"
  176. def __str__(self) -> str:
  177. """
  178. .. versionchanged:: 3.2.0
  179. Now uses :meth:`formatted_message` to format message.
  180. """
  181. try:
  182. return self.formatted_message()
  183. except Exception as ex:
  184. return (
  185. f"{type(self).__name__}: {self.msg}"
  186. f" ({type(ex).__name__}: {ex} while formatting message)"
  187. )
  188. def __repr__(self):
  189. return str(self)
  190. def mark_input_line(
  191. self, marker_string: typing.Optional[str] = None, **kwargs
  192. ) -> str:
  193. """
  194. Extracts the exception line from the input string, and marks
  195. the location of the exception with a special symbol.
  196. """
  197. markerString: str = deprecate_argument(kwargs, "markerString", ">!<")
  198. markerString = marker_string if marker_string is not None else markerString
  199. line_str = self.line
  200. line_column = self.column - 1
  201. if markerString:
  202. line_str = f"{line_str[:line_column]}{markerString}{line_str[line_column:]}"
  203. return line_str.strip()
  204. def explain(self, depth: int = 16) -> str:
  205. """
  206. Method to translate the Python internal traceback into a list
  207. of the pyparsing expressions that caused the exception to be raised.
  208. Parameters:
  209. - depth (default=16) - number of levels back in the stack trace to list expression
  210. and function names; if None, the full stack trace names will be listed; if 0, only
  211. the failing input line, marker, and exception string will be shown
  212. Returns a multi-line string listing the ParserElements and/or function names in the
  213. exception's stack trace.
  214. Example:
  215. .. testcode::
  216. # an expression to parse 3 integers
  217. expr = pp.Word(pp.nums) * 3
  218. try:
  219. # a failing parse - the third integer is prefixed with "A"
  220. expr.parse_string("123 456 A789")
  221. except pp.ParseException as pe:
  222. print(pe.explain(depth=0))
  223. prints:
  224. .. testoutput::
  225. 123 456 A789
  226. ^
  227. ParseException: Expected W:(0-9), found 'A789' (at char 8), (line:1, col:9)
  228. Note: the diagnostic output will include string representations of the expressions
  229. that failed to parse. These representations will be more helpful if you use `set_name` to
  230. give identifiable names to your expressions. Otherwise they will use the default string
  231. forms, which may be cryptic to read.
  232. Note: pyparsing's default truncation of exception tracebacks may also truncate the
  233. stack of expressions that are displayed in the ``explain`` output. To get the full listing
  234. of parser expressions, you may have to set ``ParserElement.verbose_stacktrace = True``
  235. """
  236. return self.explain_exception(self, depth)
  237. # Compatibility synonyms
  238. # fmt: off
  239. markInputline = replaced_by_pep8("markInputline", mark_input_line)
  240. # fmt: on
  241. class ParseException(ParseBaseException):
  242. """
  243. Exception thrown when a parse expression doesn't match the input string
  244. Example:
  245. .. testcode::
  246. integer = Word(nums).set_name("integer")
  247. try:
  248. integer.parse_string("ABC")
  249. except ParseException as pe:
  250. print(pe, f"column: {pe.column}")
  251. prints:
  252. .. testoutput::
  253. Expected integer, found 'ABC' (at char 0), (line:1, col:1) column: 1
  254. """
  255. class ParseFatalException(ParseBaseException):
  256. """
  257. User-throwable exception thrown when inconsistent parse content
  258. is found; stops all parsing immediately
  259. """
  260. class ParseSyntaxException(ParseFatalException):
  261. """
  262. Just like :class:`ParseFatalException`, but thrown internally
  263. when an :class:`ErrorStop<And._ErrorStop>` ('-' operator) indicates
  264. that parsing is to stop immediately because an unbacktrackable
  265. syntax error has been found.
  266. """
  267. class RecursiveGrammarException(Exception):
  268. """
  269. .. deprecated:: 3.0.0
  270. Only used by the deprecated :meth:`ParserElement.validate`.
  271. Exception thrown by :class:`ParserElement.validate` if the
  272. grammar could be left-recursive; parser may need to enable
  273. left recursion using :class:`ParserElement.enable_left_recursion<ParserElement.enable_left_recursion>`
  274. """
  275. def __init__(self, parseElementList) -> None:
  276. self.parseElementTrace = parseElementList
  277. def __str__(self) -> str:
  278. return f"RecursiveGrammarException: {self.parseElementTrace}"