Skip to content

Commit

Permalink
Handle final comments with no terminating newline in OpenQASM 2
Browse files Browse the repository at this point in the history
Previously, if an OpenQASM 2 program to be parsed ended in a comment
with no terminating '\n', the lexer would fail to notice the end of the
file and would instead attempt to emit the second '/' of the comment
introduction as a 'Slash` token.
  • Loading branch information
jakelishman committed Sep 5, 2023
1 parent 1606ca3 commit 4b17865
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
7 changes: 5 additions & 2 deletions crates/qasm2/src/lex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,8 +665,11 @@ impl TokenStream {
b'}' => TokenType::RBrace,
b'/' => {
if let Some(b'/') = self.peek_byte()? {
self.advance_line()?;
return self.next(context);
return if self.advance_line()? == 0 {
Ok(None)
} else {
self.next(context)
}
} else {
TokenType::Slash
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
fixes:
- |
OpenQASM 2 programs that end in comments with no terminating newline character will now parse
successfully. Fixed `#10770 <https://github.com/Qiskit/qiskit/issues/10770>`__.
14 changes: 13 additions & 1 deletion test/python/qasm2/test_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,22 @@
from . import gate_builder


class TestEmpty(QiskitTestCase):
@ddt.ddt
class TestWhitespace(QiskitTestCase):
def test_allows_empty(self):
self.assertEqual(qiskit.qasm2.loads(""), QuantumCircuit())

@ddt.data("", "\n", "\r\n", "\n ")
def test_empty_except_comment(self, terminator):
program = "// final comment" + terminator
self.assertEqual(qiskit.qasm2.loads(program), QuantumCircuit())

@ddt.data("", "\n", "\r\n", "\n ")
def test_final_comment(self, terminator):
# This is similar to the empty-circuit test, except that we also have an instruction.
program = "qreg q[2]; // final comment" + terminator
self.assertEqual(qiskit.qasm2.loads(program), QuantumCircuit(QuantumRegister(2, "q")))


class TestVersion(QiskitTestCase):
def test_complete_version(self):
Expand Down

0 comments on commit 4b17865

Please sign in to comment.