Skip to content

Add switch keywords #6

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

Merged
merged 1 commit into from
May 14, 2025
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
4 changes: 4 additions & 0 deletions releasenotes/notes/switch-case-4ea93b309024bee4.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
features:
- |
The new keywords ``switch``, ``case`` and ``default`` now lex correctly.
2 changes: 1 addition & 1 deletion src/openqasm_pygments/qasm3.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def _defcalgrammar_callback(self, match):
("let", token.Keyword.Declaration),
(
words(
"break continue end if else for while box return in".split(),
"break continue end if else for while box return in switch case default".split(),
prefix="\\b",
suffix="\\b",
),
Expand Down
31 changes: 31 additions & 0 deletions tests/test_qasm3_lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,37 @@ def test_annotation_namespace(lexer_qasm3):
]


def test_switch_case(lexer_qasm3):
text = """\
switch (i) {
case 1, 2 {}
case 3 {}
default {}
}
"""
assert _remove_whitespace(lexer_qasm3.get_tokens(text)) == [
(token.Keyword, "switch"),
(token.Punctuation, "("),
(token.Name, "i"),
(token.Punctuation, ")"),
(token.Punctuation, "{"),
(token.Keyword, "case"),
(token.Literal.Number, "1"),
(token.Punctuation, ","),
(token.Literal.Number, "2"),
(token.Punctuation, "{"),
(token.Punctuation, "}"),
(token.Keyword, "case"),
(token.Literal.Number, "3"),
(token.Punctuation, "{"),
(token.Punctuation, "}"),
(token.Keyword, "default"),
(token.Punctuation, "{"),
(token.Punctuation, "}"),
(token.Punctuation, "}"),
]


class TestPulseLexerDelegation:
def test_inferred_known_alias(self, lexer_qasm3):
# This uses a very (!) non-standard pulse-grammar lexer to test delegation
Expand Down