Skip to content

Commit

Permalink
Fix parsing all comment types (#1290)
Browse files Browse the repository at this point in the history
* Fix parsing all comment types

* style
  • Loading branch information
notmgsk authored Dec 28, 2020
1 parent 2209a1a commit 543ab14
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pyquil/_parser/grammar.lark
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
quil : (all_instr _NEWLINE?)*

?all_instr : def_gate
?all_instr : COMMENT
| def_gate
| def_circuit
| def_frame
| def_waveform
Expand Down Expand Up @@ -41,6 +42,8 @@ quil : (all_instr _NEWLINE?)*
| classical_comparison
| include

COMMENT : "#" /[^\n]*/ NEWLINE

def_gate : "DEFGATE" name [ variables ] ":" matrix -> def_gate_matrix
| "DEFGATE" name "AS" "MATRIX" [ variables ] ":" matrix -> def_gate_matrix
| "DEFGATE" name "AS" "PERMUTATION" ":" _NEWLINE_TAB matrix_row -> def_gate_as_permutation
Expand Down
28 changes: 28 additions & 0 deletions pyquil/tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import numpy as np
import pytest

from pyquil import Program
from pyquil.gates import (
ADD,
AND,
Expand Down Expand Up @@ -668,3 +669,30 @@ def test_parse_defgate_as_pauli():
parse("DEFGATE RY(%theta) 0 AS PAULI-SUM:\n Y(-%theta/2) q")

assert excp.value.token == Token("INT", "0")


@pytest.mark.parametrize(
"program",
[
"""# comment on first line
H 0
""",
"""H 0 # mid-line comment on first line
H 0
""",
"""H 0
# comment on second line
H 0
""",
"""H 0
H 0 # mid-line comment on second line
""",
"""H 0
# comment on last line with newline
""",
"""H 0
# comment on last line without newline""",
],
)
def test_parse_comments(program):
Program(program)

0 comments on commit 543ab14

Please sign in to comment.