Skip to content

Commit

Permalink
improve parser error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
braised-babbage committed Aug 2, 2024
1 parent 5471569 commit 5cedcac
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 8 additions & 1 deletion source/openpulse/openpulse/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,14 @@ def parse_openpulse(
try:
tree = parser.calibrationBlock()
except (RecognitionException, ParseCancellationException) as exc:
raise OpenPulseParsingError() from exc
msg = ''
# With BailErrorStrategy, we should be able to recover and report
# information about the offending token.
if isinstance(exc, ParseCancellationException) and exc.args:
tok = getattr(exc.args[0], 'offendingToken', None)
if tok is not None:
msg = f"Unexpected token '{tok.text}' at line {tok.line}, column {tok.start}."
raise OpenPulseParsingError(msg) from exc
result = (
OpenPulseNodeVisitor(in_defcal).visitCalibrationBlock(tree)
if tree.children
Expand Down
2 changes: 1 addition & 1 deletion source/openpulse/tests/test_openpulse_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ def test_permissive_parsing(capsys):
captured = capsys.readouterr()
assert captured.err.strip() == "line 2:9 no viable alternative at input 'int;'"

with pytest.raises(OpenPulseParsingError):
with pytest.raises(OpenPulseParsingError, match=r"Unexpected token ';' at line 2, column 10."):
# This is stricter -- we fail as soon as ANTLR sees a problem
parse(p)
captured = capsys.readouterr()
Expand Down

0 comments on commit 5cedcac

Please sign in to comment.