diff --git a/crates/qasm2/src/lex.rs b/crates/qasm2/src/lex.rs index a6b3a6abac90..024681b877ff 100644 --- a/crates/qasm2/src/lex.rs +++ b/crates/qasm2/src/lex.rs @@ -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 } diff --git a/releasenotes/notes/fix-qasm2-final-comment-f0904c3e13215a00.yaml b/releasenotes/notes/fix-qasm2-final-comment-f0904c3e13215a00.yaml new file mode 100644 index 000000000000..53f114caf3e4 --- /dev/null +++ b/releasenotes/notes/fix-qasm2-final-comment-f0904c3e13215a00.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + OpenQASM 2 programs that end in comments with no terminating newline character will now parse + successfully. Fixed `#10770 `__. diff --git a/test/python/qasm2/test_structure.py b/test/python/qasm2/test_structure.py index 813dbd1e96d2..805e5c3bf8f0 100644 --- a/test/python/qasm2/test_structure.py +++ b/test/python/qasm2/test_structure.py @@ -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 ", "\n\t", "\r\n\t") + 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):