Skip to content

Commit cbf6e72

Browse files
Merge pull request #32 from openqasm/improve-parser-error-messages
Improve parser error messages
2 parents 5471569 + a38b0f8 commit cbf6e72

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

source/openpulse/openpulse/parser.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,14 @@ def parse_openpulse(
8787
try:
8888
tree = parser.calibrationBlock()
8989
except (RecognitionException, ParseCancellationException) as exc:
90-
raise OpenPulseParsingError() from exc
90+
msg = ""
91+
# With BailErrorStrategy, we should be able to recover and report
92+
# information about the offending token.
93+
if isinstance(exc, ParseCancellationException) and exc.args:
94+
tok = getattr(exc.args[0], "offendingToken", None)
95+
if tok is not None:
96+
msg = f"Unexpected token '{tok.text}' at line {tok.line}, column {tok.start}."
97+
raise OpenPulseParsingError(msg) from exc
9198
result = (
9299
OpenPulseNodeVisitor(in_defcal).visitCalibrationBlock(tree)
93100
if tree.children

source/openpulse/tests/test_openpulse_parser.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ def test_permissive_parsing(capsys):
383383
captured = capsys.readouterr()
384384
assert captured.err.strip() == "line 2:9 no viable alternative at input 'int;'"
385385

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

0 commit comments

Comments
 (0)