From ca0841fd70047473d1d0ea0fdf936b7f7d9474f5 Mon Sep 17 00:00:00 2001 From: Jake Lishman Date: Wed, 14 May 2025 18:23:45 +0100 Subject: [PATCH] Add switch keywords --- .../notes/switch-case-4ea93b309024bee4.yaml | 4 +++ src/openqasm_pygments/qasm3.py | 2 +- tests/test_qasm3_lexer.py | 31 +++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/switch-case-4ea93b309024bee4.yaml diff --git a/releasenotes/notes/switch-case-4ea93b309024bee4.yaml b/releasenotes/notes/switch-case-4ea93b309024bee4.yaml new file mode 100644 index 0000000..bfbc9e8 --- /dev/null +++ b/releasenotes/notes/switch-case-4ea93b309024bee4.yaml @@ -0,0 +1,4 @@ +--- +features: + - | + The new keywords ``switch``, ``case`` and ``default`` now lex correctly. diff --git a/src/openqasm_pygments/qasm3.py b/src/openqasm_pygments/qasm3.py index 5469885..0aa7dc4 100644 --- a/src/openqasm_pygments/qasm3.py +++ b/src/openqasm_pygments/qasm3.py @@ -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", ), diff --git a/tests/test_qasm3_lexer.py b/tests/test_qasm3_lexer.py index a00d48e..8eb67d7 100644 --- a/tests/test_qasm3_lexer.py +++ b/tests/test_qasm3_lexer.py @@ -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