zig: Switch to official Zig grammar (#20004)

Closes #20001

the old outline was *weird* for many reasons so ill just show it with a
hodgepodge of zig declarations
before:

![image](https://github.com/user-attachments/assets/87395623-3e28-491c-9693-c1714da6b29f)
after:

![image](https://github.com/user-attachments/assets/1aa3f3b7-18b0-4d39-b1c6-99740e7bcd6a)

why were values shown? why werent `var`s or modifiers like pub, const,
export? it was very odd to me and inconsistent with other languages. i
chose to leave out unnamed tests, it just seemed like noise to me since
they werent distinct but i can easily revert that
unfortunately there seems to be a bug upstream which causes those
`t`/`f` decls to show 2 things
https://github.com/tree-sitter-grammars/tree-sitter-zig/issues/3

im very new to treesitter and queries so i really havent looked over the
rest of the stuff here, other than outline theyre unmodified please lmk
if theres anything wrong

Release Notes:

- Changed upstream treesitter grammar for zig
This commit is contained in:
xdBronch 2024-11-07 03:47:15 -05:00 committed by GitHub
parent 6d0aa72226
commit 029d08350e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 303 additions and 195 deletions

View file

@ -11,5 +11,5 @@ name = "zls"
language = "Zig" language = "Zig"
[grammars.zig] [grammars.zig]
repository = "https://github.com/maxxnino/tree-sitter-zig" repository = "https://github.com/tree-sitter-grammars/tree-sitter-zig"
commit = "0d08703e4c3f426ec61695d7617415fff97029bd" commit = "eb7d58c2dc4fbeea4745019dee8df013034ae66b"

View file

@ -1,7 +1,7 @@
name = "Zig" name = "Zig"
grammar = "zig" grammar = "zig"
path_suffixes = ["zig", "zon"] path_suffixes = ["zig", "zon"]
line_comments = ["// "] line_comments = ["// ", "/// ", "//! "]
autoclose_before = ";:.,=}])" autoclose_before = ";:.,=}])"
brackets = [ brackets = [
{ start = "{", end = "}", close = true, newline = true }, { start = "{", end = "}", close = true, newline = true },

View file

@ -1,126 +1,179 @@
[ ; Variables
(container_doc_comment)
(doc_comment)
] @comment.doc (identifier) @variable
[ ; Parameters
(line_comment)
] @comment
[ (parameter
variable: (IDENTIFIER) name: (identifier) @variable.parameter)
variable_type_function: (IDENTIFIER)
] @variable
;; func parameter ; Types
parameter: (IDENTIFIER) @property
[ (parameter
field_member: (IDENTIFIER) type: (identifier) @type)
field_access: (IDENTIFIER)
] @property
;; assume TitleCase is a type ((identifier) @type
( (#match? @type "^[A-Z_][a-zA-Z0-9_]*"))
(variable_declaration
(identifier) @type
"="
[ [
variable_type_function: (IDENTIFIER) (struct_declaration)
field_access: (IDENTIFIER) (enum_declaration)
parameter: (IDENTIFIER) (union_declaration)
] @type (opaque_declaration)
(#match? @type "^[A-Z]([a-z]+[A-Za-z0-9]*)+$") ])
)
;; assume camelCase is a function
(
[
variable_type_function: (IDENTIFIER)
field_access: (IDENTIFIER)
parameter: (IDENTIFIER)
] @function
(#match? @function "^[a-z]+([A-Z][a-z0-9]*)+$")
)
;; assume all CAPS_1 is a constant
(
[
variable_type_function: (IDENTIFIER)
field_access: (IDENTIFIER)
] @constant
(#match? @constant "^[A-Z][A-Z_0-9]+$")
)
[ [
function_call: (IDENTIFIER) (builtin_type)
function: (IDENTIFIER) "anyframe"
] @function ] @type.builtin
exception: "!" @keyword ; Constants
( ((identifier) @constant
(IDENTIFIER) @variable.special (#match? @constant "^[A-Z][A-Z_0-9]+$"))
(#eq? @variable.special "_")
)
(PtrTypeStart "c" @variable.special)
(
(ContainerDeclType
[
(ErrorUnionExpr)
"enum"
]
)
(ContainerField (IDENTIFIER) @constant)
)
field_constant: (IDENTIFIER) @constant
(BUILTINIDENTIFIER) @keyword
((BUILTINIDENTIFIER) @function
(#any-of? @function "@import" "@cImport"))
(INTEGER) @number
(FLOAT) @number
[ [
"true" "null"
"false" "unreachable"
] @boolean "undefined"
] @constant.builtin
(field_expression
.
member: (identifier) @constant)
(enum_declaration
(container_field
type: (identifier) @constant))
; Labels
(block_label (identifier) @label)
(break_label (identifier) @label)
; Fields
(field_initializer
.
(identifier) @variable.member)
(field_expression
(_)
member: (identifier) @property)
(field_expression
(_)
member: (identifier) @type (#match? @type "^[A-Z_][a-zA-Z0-9_]*"))
(container_field
name: (identifier) @property)
(initializer_list
(assignment_expression
left: (field_expression
.
member: (identifier) @property)))
; Functions
(builtin_identifier) @function.builtin
(call_expression
function: (identifier) @function.call)
(call_expression
function: (field_expression
member: (identifier) @function.call))
(function_declaration
name: (identifier) @function)
; Modules
(variable_declaration
(identifier) @module
(builtin_function
(builtin_identifier) @keyword.import
(#any-of? @keyword.import "@import" "@cImport")))
; Builtins
[ [
(LINESTRING) "c"
(STRINGLITERALSINGLE) "..."
] @string ] @variable.builtin
(CHAR_LITERAL) @string.special.symbol ((identifier) @variable.builtin
(EscapeSequence) @string.escape (#eq? @variable.builtin "_"))
(FormatSequence) @string.special
(BreakLabel (IDENTIFIER) @tag) (calling_convention
(BlockLabel (IDENTIFIER) @tag) (identifier) @variable.builtin)
; Keywords
[ [
"fn"
"asm" "asm"
"defer" "defer"
"errdefer" "errdefer"
"test" "test"
"error"
"const"
"var"
] @keyword
[
"struct" "struct"
"union" "union"
"enum" "enum"
"opaque" "opaque"
"error" ] @keyword.type
"try"
"catch" [
"async"
"await"
"suspend"
"nosuspend"
"resume"
] @keyword.coroutine
"fn" @keyword.function
[
"and"
"or"
"orelse"
] @keyword.operator
"return" @keyword.return
[
"if"
"else"
"switch"
] @keyword.conditional
[
"for" "for"
"while" "while"
"break" "break"
"continue" "continue"
"const" ] @keyword.repeat
"var"
[
"usingnamespace"
"export"
] @keyword.import
[
"try"
"catch"
] @keyword.exception
[
"volatile" "volatile"
"allowzero" "allowzero"
"noalias" "noalias"
@ -128,71 +181,91 @@ field_constant: (IDENTIFIER) @constant
"align" "align"
"callconv" "callconv"
"linksection" "linksection"
"comptime" "pub"
"export"
"extern"
"inline" "inline"
"noinline" "noinline"
"extern"
"comptime"
"packed" "packed"
"pub"
"threadlocal" "threadlocal"
"async" ] @keyword.modifier
"await"
"suspend" ; Operator
"nosuspend"
"resume"
"and"
"or"
"orelse"
"return"
"if"
"else"
"switch"
] @keyword
[ [
"usingnamespace" "="
] @constant "*="
"*%="
[ "*|="
"anytype" "/="
"anyframe" "%="
(BuildinTypeExpr) "+="
] @type "+%="
"+|="
[ "-="
"null" "-%="
"unreachable" "-|="
"undefined" "<<="
] @constant "<<|="
">>="
[ "&="
(CompareOp) "^="
(BitwiseOp) "|="
(BitShiftOp) "!"
(AdditionOp) "~"
(AssignOp) "-"
(MultiplyOp) "-%"
(PrefixOp) "&"
"=="
"!="
">"
">="
"<="
"<"
"&"
"^"
"|"
"<<"
">>"
"<<|"
"+"
"++"
"+%"
"-%"
"+|"
"-|"
"*" "*"
"/"
"%"
"**" "**"
"->" "*%"
".?" "*|"
"||"
".*" ".*"
".?"
"?" "?"
".."
] @operator ] @operator
[ ; Literals
";"
"."
","
":"
] @punctuation.delimiter
[ (character) @character
".."
"..." ([
] @punctuation.special (string)
(multiline_string)
] @string
(#set! "priority" 95))
(integer) @number
(float) @number.float
(boolean) @boolean
(escape_sequence) @string.escape
; Punctuation
[ [
"[" "["
@ -201,10 +274,22 @@ field_constant: (IDENTIFIER) @constant
")" ")"
"{" "{"
"}" "}"
(Payload "|")
(PtrPayload "|")
(PtrIndexPayload "|")
] @punctuation.bracket ] @punctuation.bracket
; Error [
(ERROR) @error ";"
"."
","
":"
"=>"
"->"
] @punctuation.delimiter
(payload "|" @punctuation.bracket)
; Comments
(comment) @comment
((comment) @comment.documentation
(#match? @comment.documentation "^//(/|!)"))

View file

@ -1,22 +1,17 @@
[ [
(AsmExpr) (block)
(AssignExpr) (switch_expression)
(Block) (initializer_list)
(BlockExpr)
(ContainerDecl)
(ErrorUnionExpr)
(InitList)
(SwitchExpr)
(TestDecl)
] @indent.begin ] @indent.begin
(block
"}" @indent.end)
(_ "[" "]" @end) @indent (_ "[" "]" @end) @indent
(_ "{" "}" @end) @indent (_ "{" "}" @end) @indent
(_ "(" ")" @end) @indent (_ "(" ")" @end) @indent
[ [
(line_comment) (comment)
(container_doc_comment) (multiline_string)
(doc_comment)
(LINESTRING)
] @indent.ignore ] @indent.ignore

View file

@ -1,5 +1,10 @@
[ ((comment) @injection.content
(container_doc_comment) (#set! injection.language "comment"))
(doc_comment)
(line_comment) ; TODO: add when asm is added
] @comment ; (asm_output_item (string) @injection.content
; (#set! injection.language "asm"))
; (asm_input_item (string) @injection.content
; (#set! injection.language "asm"))
; (asm_clobbers (string) @injection.content
; (#set! injection.language "asm"))

View file

@ -1,27 +1,50 @@
(Decl ( (test_declaration
FnProto( "test" @context
"fn" @context [
function: (_) @name (string)
) (identifier)
) ] @name) @item
) @item
( (function_declaration
Decl ( "pub"? @context
VarDecl ( [
"const" "extern"
variable_type_function: (_) @name "export"
(ErrorUnionExpr) @context "inline"
) "noinline"
) ]? @context
) @item "fn" @context
name: (_) @name) @item
( (source_file
TestDecl ( (variable_declaration
"test" @context "pub"? @context
[ (identifier) @name
(STRINGLITERALSINGLE) "=" (_) @context) @item)
(IDENTIFIER)
]? @name (struct_declaration
) (variable_declaration
) @item "pub"? @context
(identifier) @name
"=" (_) @context) @item)
(union_declaration
(variable_declaration
"pub"? @context
(identifier) @name
"=" (_) @context) @item)
(enum_declaration
(variable_declaration
"pub"? @context
(identifier) @name
"=" (_) @context) @item)
(opaque_declaration
(variable_declaration
"pub"? @context
(identifier) @name
"=" (_) @context) @item)
(container_field
. (_) @name) @item