11 _collapse_string_to_ranges
,
14 from .unicode import pyparsing_unicode
as ppu
17 class ExceptionWordUnicode(ppu
.Latin1
, ppu
.LatinA
, ppu
.LatinB
, ppu
.Greek
, ppu
.Cyrillic
):
21 _extract_alphanums
= _collapse_string_to_ranges(ExceptionWordUnicode
.alphanums
)
22 _exception_word_extractor
= re
.compile("([" + _extract_alphanums
+ "]{1,16})|.")
25 class ParseBaseException(Exception):
26 """base exception class for all parsing runtime exceptions"""
31 parser_element
: typing
.Any
# "ParserElement"
32 args
: typing
.Tuple
[str, int, typing
.Optional
[str]]
42 # Performance tuning: we construct a *lot* of these, so keep this
43 # constructor as small and fast as possible
48 msg
: typing
.Optional
[str] = None,
58 self
.parser_element
= elem
59 self
.args
= (pstr
, loc
, msg
)
62 def explain_exception(exc
, depth
=16):
64 Method to take an exception and translate the Python internal traceback into a list
65 of the pyparsing expressions that caused the exception to be raised.
69 - exc - exception raised during parsing (need not be a ParseException, in support
70 of Python exceptions that might be raised in a parse action)
71 - depth (default=16) - number of levels back in the stack trace to list expression
72 and function names; if None, the full stack trace names will be listed; if 0, only
73 the failing input line, marker, and exception string will be shown
75 Returns a multi-line string listing the ParserElements and/or function names in the
76 exception's stack trace.
79 from .core
import ParserElement
82 depth
= sys
.getrecursionlimit()
84 if isinstance(exc
, ParseBaseException
):
86 ret
.append(" " * (exc
.column
- 1) + "^")
87 ret
.append(f
"{type(exc).__name__}: {exc}")
90 callers
= inspect
.getinnerframes(exc
.__traceback
__, context
=depth
)
92 for i
, ff
in enumerate(callers
[-depth
:]):
95 f_self
= frm
.f_locals
.get("self", None)
96 if isinstance(f_self
, ParserElement
):
97 if not frm
.f_code
.co_name
.startswith(
98 ("parseImpl", "_parseNoCache")
101 if id(f_self
) in seen
:
105 self_type
= type(f_self
)
107 f
"{self_type.__module__}.{self_type.__name__} - {f_self}"
110 elif f_self
is not None:
111 self_type
= type(f_self
)
112 ret
.append(f
"{self_type.__module__}.{self_type.__name__}")
116 if code
.co_name
in ("wrapper", "<module>"):
119 ret
.append(code
.co_name
)
125 return "\n".join(ret
)
128 def _from_exception(cls
, pe
):
130 internal factory method to simplify creating one type of ParseException
131 from another - avoids having __init__ signature conflicts among subclasses
133 return cls(pe
.pstr
, pe
.loc
, pe
.msg
, pe
.parser_element
)
136 def line(self
) -> str:
138 Return the line of text where the exception occurred.
140 return line(self
.loc
, self
.pstr
)
143 def lineno(self
) -> int:
145 Return the 1-based line number of text where the exception occurred.
147 return lineno(self
.loc
, self
.pstr
)
150 def col(self
) -> int:
152 Return the 1-based column on the line of text where the exception occurred.
154 return col(self
.loc
, self
.pstr
)
157 def column(self
) -> int:
159 Return the 1-based column on the line of text where the exception occurred.
161 return col(self
.loc
, self
.pstr
)
163 # pre-PEP8 compatibility
165 def parserElement(self
):
166 return self
.parser_element
168 @parserElement.setter
169 def parserElement(self
, elem
):
170 self
.parser_element
= elem
172 def __str__(self
) -> str:
174 if self
.loc
>= len(self
.pstr
):
175 foundstr
= ", found end of text"
177 # pull out next word at error location
178 found_match
= _exception_word_extractor
.match(self
.pstr
, self
.loc
)
179 if found_match
is not None:
180 found
= found_match
.group(0)
182 found
= self
.pstr
[self
.loc
: self
.loc
+ 1]
183 foundstr
= (", found %r" % found
).replace(r
"\\", "\\")
186 return f
"{self.msg}{foundstr} (at char {self.loc}), (line:{self.lineno}, col:{self.column})"
192 self
, marker_string
: typing
.Optional
[str] = None, *, markerString
: str = ">!<"
195 Extracts the exception line from the input string, and marks
196 the location of the exception with a special symbol.
198 markerString
= marker_string
if marker_string
is not None else markerString
200 line_column
= self
.column
- 1
203 (line_str
[:line_column
], markerString
, line_str
[line_column
:])
205 return line_str
.strip()
207 def explain(self
, depth
=16) -> str:
209 Method to translate the Python internal traceback into a list
210 of the pyparsing expressions that caused the exception to be raised.
214 - depth (default=16) - number of levels back in the stack trace to list expression
215 and function names; if None, the full stack trace names will be listed; if 0, only
216 the failing input line, marker, and exception string will be shown
218 Returns a multi-line string listing the ParserElements and/or function names in the
219 exception's stack trace.
223 expr = pp.Word(pp.nums) * 3
225 expr.parse_string("123 456 A789")
226 except pp.ParseException as pe:
227 print(pe.explain(depth=0))
233 ParseException: Expected W:(0-9), found 'A' (at char 8), (line:1, col:9)
235 Note: the diagnostic output will include string representations of the expressions
236 that failed to parse. These representations will be more helpful if you use `set_name` to
237 give identifiable names to your expressions. Otherwise they will use the default string
238 forms, which may be cryptic to read.
240 Note: pyparsing's default truncation of exception tracebacks may also truncate the
241 stack of expressions that are displayed in the ``explain`` output. To get the full listing
242 of parser expressions, you may have to set ``ParserElement.verbose_stacktrace = True``
244 return self
.explain_exception(self
, depth
)
247 @replaced_by_pep8(mark_input_line
)
248 def markInputline(self
): ...
252 class ParseException(ParseBaseException
):
254 Exception thrown when a parse expression doesn't match the input string
259 Word(nums).set_name("integer").parse_string("ABC")
260 except ParseException as pe:
262 print("column: {}".format(pe.column))
266 Expected integer (at char 0), (line:1, col:1)
272 class ParseFatalException(ParseBaseException
):
274 User-throwable exception thrown when inconsistent parse content
275 is found; stops all parsing immediately
279 class ParseSyntaxException(ParseFatalException
):
281 Just like :class:`ParseFatalException`, but thrown internally
282 when an :class:`ErrorStop<And._ErrorStop>` ('-' operator) indicates
283 that parsing is to stop immediately because an unbacktrackable
284 syntax error has been found.
288 class RecursiveGrammarException(Exception):
290 Exception thrown by :class:`ParserElement.validate` if the
291 grammar could be left-recursive; parser may need to enable
292 left recursion using :class:`ParserElement.enable_left_recursion<ParserElement.enable_left_recursion>`
295 def __init__(self
, parseElementList
):
296 self
.parseElementTrace
= parseElementList
298 def __str__(self
) -> str:
299 return f
"RecursiveGrammarException: {self.parseElementTrace}"