open-standards/wai/tools/tree-sitter-openqasm/queries/highlights.scm
Transaction Science 4fd44dfbac wai/tools/tree-sitter-openqasm: OpenQASM 3 as a tree-sitter grammar
The grammar reads every statement of OpenQASM 3.1 (declarations of every
type, gates, subroutines and externs, modifiers and gphase, control flow,
timing, calibrations kept as text with their braces matched, pragmas and
annotations) and OpenQASM 2's qreg, creg, opaque, measure q -> c and
if(c==1). The parser is generated at ABI 14 with the pinned command line
(0.26.13) and committed; regenerating it reproduces it byte for byte.

Queries for highlighting, scopes and folds; 17 corpus tests checked tree
for tree, and the highlighting checked by 15 assertions.
script/check-programs.mjs parses every program in the repository's
conformance material and examples (the tracked .qasm files, the Studio's
worked examples, the language service's samples, the programs in the
command line's README and smoke script): 25 of 25 parse with no error,
and a broken program is refused, so a clean result means something.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-27 19:51:34 -04:00

170 lines
1.9 KiB
Scheme
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

; OpenQASM highlighting. Captures in the common names; an editor maps them to
; its theme.
(line_comment) @comment
(block_comment) @comment
(pragma) @keyword.directive
(annotation) @attribute
[
"OPENQASM"
"include"
"defcalgrammar"
"gate"
"opaque"
"def"
"extern"
"cal"
"defcal"
"let"
"const"
"input"
"output"
"readonly"
"mutable"
"#dim"
"if"
"else"
"for"
"in"
"while"
"switch"
"case"
"default"
"return"
"break"
"continue"
"end"
"box"
"durationof"
"measure"
"reset"
"barrier"
"delay"
"gphase"
"inv"
"pow"
"ctrl"
"negctrl"
] @keyword
[
"qubit"
"qreg"
"creg"
"bit"
"int"
"uint"
"float"
"angle"
"bool"
"complex"
"array"
"duration"
"stretch"
] @type
(boolean_literal) @boolean
[
(integer_literal)
(imaginary_literal)
(timing_literal)
] @number
(float_literal) @number.float
(string) @string
(calibration_text) @string.special
(hardware_qubit) @variable.builtin
((identifier) @constant.builtin
(#any-of? @constant.builtin "pi" "π" "tau" "τ" "euler" "ℯ"))
(gate_definition
name: (identifier) @function)
(opaque_declaration
name: (identifier) @function)
(subroutine_definition
name: (identifier) @function)
(extern_declaration
name: (identifier) @function)
(calibration_definition
target: (identifier) @function)
(gate_call
name: (identifier) @function.call)
(call_expression
function: (identifier) @function.call)
(gate_parameters
(identifier) @variable.parameter)
(parameter
name: (identifier) @variable.parameter)
(const_declaration
name: (identifier) @constant)
[
"="
"+="
"-="
"*="
"/="
"%="
"&="
"|="
"^="
"~="
"<<="
">>="
"**="
"+"
"-"
"*"
"/"
"%"
"**"
"=="
"!="
"<"
">"
"<="
">="
"&&"
"||"
"!"
"~"
"&"
"|"
"^"
"<<"
">>"
"++"
"->"
"@"
] @operator
[
"("
")"
"["
"]"
"{"
"}"
] @punctuation.bracket
[
";"
","
":"
] @punctuation.delimiter