Skip to content

Commit fb71e5a

Browse files
add test
1 parent 85d345a commit fb71e5a

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
@@ -36,7 +36,7 @@
3636
UnaryOperator,
3737
WaveformType,
3838
)
39-
from openpulse.parser import parse
39+
from openpulse.parser import parse, OpenPulseParsingError
4040
from openqasm3.visitor import QASMVisitor
4141

4242

@@ -329,6 +329,29 @@ def test_pragma_in_cal_block():
329329
assert _remove_spans(program) == expected
330330

331331

332+
def test_permissive_parsing(capsys):
333+
p = """
334+
cal {
335+
int;
336+
}
337+
"""
338+
339+
with pytest.raises(AttributeError, match=r"'NoneType' object has no attribute 'line'"):
340+
# In this case, we do get an exception, but this is somewhat incidental --
341+
# the antlr parser gives us a `None` value where we expect a `Statement`
342+
parse(p, permissive=True)
343+
# The actual ANTLR failure is reported via stderr
344+
captured = capsys.readouterr()
345+
assert captured.err.strip() == "line 2:9 no viable alternative at input 'int;'"
346+
347+
with pytest.raises(OpenPulseParsingError):
348+
# This is stricter -- we fail as soon as ANTLR sees a problem
349+
parse(p)
350+
captured = capsys.readouterr()
351+
# The actual ANTLR failure is reported via stderr
352+
assert captured.err.strip() == "line 2:9 no viable alternative at input 'int;'"
353+
354+
332355
@pytest.mark.parametrize(
333356
"p",
334357
[

0 commit comments

Comments
 (0)