Skip to content

Commit 4602c99

Browse files
committed
Improve error messages
1 parent 27d9dca commit 4602c99

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

CppHeaderParser/CppHeaderParser.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,9 @@ def __new__(cls, *args, **kwargs):
290290

291291

292292
class CppParseError(Exception):
293-
pass
293+
def __init__(self, msg, tok=None):
294+
Exception.__init__(self, msg)
295+
self.tok = tok
294296

295297

296298
class CppTemplateParam(dict):
@@ -2815,15 +2817,21 @@ def __init__(self, headerFileName, argType="file", encoding=None, **kwargs):
28152817
except Exception as e:
28162818
if debug:
28172819
raise
2820+
context = ""
2821+
if isinstance(e, CppParseError):
2822+
context = ": " + str(e)
2823+
if e.tok:
2824+
tok = e.tok
2825+
28182826
if tok:
2819-
filename, lineno = tok.value.location
2827+
filename, lineno = tok.location
28202828
msg = (
2821-
'Not able to parse %s on line %d evaluating "%s"\nError around: %s'
2822-
% (filename, lineno, tok.value, " ".join(self.nameStack))
2829+
"Not able to parse %s on line %d evaluating '%s'%s\nError around: %s"
2830+
% (filename, lineno, tok.value, context, " ".join(self.nameStack))
28232831
)
28242832
else:
2825-
msg = "Error parsing %s\nError around: %s" % (
2826-
self.headerFileName,
2833+
msg = "Error parsing %s%s\nError around: %s" % (
2834+
self.headerFileName, context,
28272835
" ".join(self.nameStack),
28282836
)
28292837

@@ -2872,12 +2880,12 @@ def _parse_error(self, tokens, expected):
28722880
else:
28732881
errtok = tokens[-1]
28742882
if expected:
2875-
expected = ", expected " + expected
2883+
expected = ", expected '" + expected + "'"
28762884

2877-
msg = "unexpected %s%s" % (errtok.value, expected)
2885+
msg = "unexpected '%s'%s" % (errtok.value, expected)
28782886

28792887
# TODO: better error message
2880-
return CppParseError(msg)
2888+
return CppParseError(msg, errtok)
28812889

28822890
def _next_token_must_be(self, *tokenTypes):
28832891
tok = self.lex.token()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy