Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle final comments with no terminating newline in OpenQASM 2 (backport #10773) #10778

Merged
merged 1 commit into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ", "\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):
Expand Down