Skip to content

Commit ec956f3

Browse files
add test
1 parent 4777f83 commit ec956f3

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

source/openpulse/openpulse/parser.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,12 @@
5151
from ._antlr.openpulseParser import openpulseParser
5252
from ._antlr.openpulseParserVisitor import openpulseParserVisitor
5353

54+
5455
class OpenPulseParsingError(Exception):
5556
"""An error raised by the AST visitor during the AST-generation phase. This is raised in cases where the
5657
given program could not be correctly parsed."""
5758

59+
5860
def parse(input_: str, permissive: bool = False) -> ast.Program:
5961
"""
6062
Parse a complete OpenPulse program from a string.
@@ -71,7 +73,9 @@ def parse(input_: str, permissive: bool = False) -> ast.Program:
7173
return qasm3_ast
7274

7375

74-
def parse_openpulse(input_: str, in_defcal: bool, permissive: bool = True) -> openpulse_ast.CalibrationBlock:
76+
def parse_openpulse(
77+
input_: str, in_defcal: bool, permissive: bool = True
78+
) -> openpulse_ast.CalibrationBlock:
7579
lexer = openpulseLexer(InputStream(input_))
7680
stream = CommonTokenStream(lexer)
7781
parser = openpulseParser(stream)

source/openpulse/tests/test_openpulse_parser.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
UnaryOperator,
3939
WaveformType,
4040
)
41-
from openpulse.parser import parse
41+
from openpulse.parser import parse, OpenPulseParsingError
4242
from openqasm3.visitor import QASMVisitor
4343

4444

@@ -368,6 +368,29 @@ def test_switch_in_cal_block():
368368
assert _remove_spans(program) == expected
369369

370370

371+
def test_permissive_parsing(capsys):
372+
p = """
373+
cal {
374+
int;
375+
}
376+
"""
377+
378+
with pytest.raises(AttributeError, match=r"'NoneType' object has no attribute 'line'"):
379+
# In this case, we do get an exception, but this is somewhat incidental --
380+
# the antlr parser gives us a `None` value where we expect a `Statement`
381+
parse(p, permissive=True)
382+
# The actual ANTLR failure is reported via stderr
383+
captured = capsys.readouterr()
384+
assert captured.err.strip() == "line 2:9 no viable alternative at input 'int;'"
385+
386+
with pytest.raises(OpenPulseParsingError):
387+
# This is stricter -- we fail as soon as ANTLR sees a problem
388+
parse(p)
389+
captured = capsys.readouterr()
390+
# The actual ANTLR failure is reported via stderr
391+
assert captured.err.strip() == "line 2:9 no viable alternative at input 'int;'"
392+
393+
371394
@pytest.mark.parametrize(
372395
"p",
373396
[

0 commit comments

Comments
 (0)