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 #10773

Merged
merged 3 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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 ")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to test "\n\t" and "\r\n\t" too? Just thinking of other whitespace combinations to make the testing here a bit more thorough.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra cases added in c410d75

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only one of these cases that was actually buggy was the one with no whitespace at all, but it doesn't matter a great deal that there's additional cases.

Copy link
Member

@1ucian0 1ucian0 Sep 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assumed you were already afk, so I took the liberty of adding them. I hope you dont mind.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's ok, it's all our code anyway.

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
Loading