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. 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..a00d48e 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