From 5febed1a3a04219342017feeb88f41fa67b3fb3e Mon Sep 17 00:00:00 2001 From: Lukas Kurz Date: Tue, 13 May 2025 23:26:52 +0200 Subject: [PATCH 1/3] Lex annotation namespaces --- src/openqasm_pygments/qasm3.py | 2 +- tests/test_qasm3_lexer.py | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/openqasm_pygments/qasm3.py b/src/openqasm_pygments/qasm3.py index 5597d77..5469885 100644 --- a/src/openqasm_pygments/qasm3.py +++ b/src/openqasm_pygments/qasm3.py @@ -75,7 +75,7 @@ def _defcalgrammar_callback(self, match): tokens = { "root": [ (r"^[ \t]*#?pragma", token.Comment.Preproc, "pragma"), - (r"^[ \t]*@\w+", token.Name.Decorator, "annotation"), + (r"^[ \t]*@\w+(\.\w+)*", token.Name.Decorator, "annotation"), (r"[ \r\n\t]+", token.Whitespace), (r"\bOPENQASM\b", token.Comment.Preproc, "version"), (r"//.*$", token.Comment.Single), diff --git a/tests/test_qasm3_lexer.py b/tests/test_qasm3_lexer.py index 07ddec6..9e4a4da 100644 --- a/tests/test_qasm3_lexer.py +++ b/tests/test_qasm3_lexer.py @@ -68,6 +68,23 @@ def test_for_loop_variable_not_callable(lexer_qasm3): ] +def test_annotation_namespace(lexer_qasm3): + text = """\ +@annotation +@namespace.annotation +@namespace1.namespace2.annotation +qubit q; +""" + assert _remove_whitespace(lexer_qasm3.get_tokens(text)) == [ + (token.Name.Decorator, "@annotation"), + (token.Name.Decorator, "@namespace.annotation"), + (token.Name.Decorator, "@namespace1.namespace2.annotation"), + (token.Keyword.Type, "qubit"), + (token.Name, "q"), + (token.Punctuation, ";") + ] + + class TestPulseLexerDelegation: def test_inferred_known_alias(self, lexer_qasm3): # This uses a very (!) non-standard pulse-grammar lexer to test delegation From dbd099aad466353f43c47357c78d0d8f4342333c Mon Sep 17 00:00:00 2001 From: Lukas Kurz Date: Wed, 14 May 2025 17:34:20 +0200 Subject: [PATCH 2/3] Add release note --- .../notes/lex-namespace-annotations-34e856acd9837226.yaml | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 releasenotes/notes/lex-namespace-annotations-34e856acd9837226.yaml diff --git a/releasenotes/notes/lex-namespace-annotations-34e856acd9837226.yaml b/releasenotes/notes/lex-namespace-annotations-34e856acd9837226.yaml new file mode 100644 index 0000000..ef242ef --- /dev/null +++ b/releasenotes/notes/lex-namespace-annotations-34e856acd9837226.yaml @@ -0,0 +1,4 @@ +--- +fixes: + - | + Annotation keywords will now be correctly lexed as a single ``Token.Name.Decorator`` token. From 6d88a980cd4c879487f1284854ca0e370bc21081 Mon Sep 17 00:00:00 2001 From: Lukas Kurz Date: Wed, 14 May 2025 17:42:00 +0200 Subject: [PATCH 3/3] Fix lint --- tests/test_qasm3_lexer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_qasm3_lexer.py b/tests/test_qasm3_lexer.py index 9e4a4da..a00d48e 100644 --- a/tests/test_qasm3_lexer.py +++ b/tests/test_qasm3_lexer.py @@ -81,7 +81,7 @@ def test_annotation_namespace(lexer_qasm3): (token.Name.Decorator, "@namespace1.namespace2.annotation"), (token.Keyword.Type, "qubit"), (token.Name, "q"), - (token.Punctuation, ";") + (token.Punctuation, ";"), ]