feat!: switch upstream Zig parser

The new parser is faster, does not lag while editing, correctly parses
the entire Zig code base, and is much easier to write queries for.
This commit is contained in:
Amaan Qureshi 2024-08-29 22:37:17 -04:00
parent 585860a186
commit ba921c9aef
7 changed files with 217 additions and 163 deletions

View file

@ -464,7 +464,7 @@ We are looking for maintainers to add more parsers and to write query files for
- [x] [yang](https://github.com/Hubro/tree-sitter-yang) (maintained by @Hubro)
- [x] [yuck](https://github.com/Philipp-M/tree-sitter-yuck) (maintained by @Philipp-M, @amaanq)
- [x] [zathurarc](https://github.com/Freed-Wu/tree-sitter-zathurarc) (maintained by @Freed-Wu)
- [x] [zig](https://github.com/maxxnino/tree-sitter-zig) (maintained by @maxxnino)
- [x] [zig](https://github.com/tree-sitter-grammars/tree-sitter-zig) (maintained by @amaanq)
<!--parserinfo-->
For related information on the supported languages, including related plugins, see [this wiki page](https://github.com/nvim-treesitter/nvim-treesitter/wiki/Supported-Languages-Information).

View file

@ -873,6 +873,6 @@
"revision": "0554b4a5d313244b7fc000cbb41c04afae4f4e31"
},
"zig": {
"revision": "2bac4cc6c697d46a193905fef6d003bfa0bfabfd"
"revision": "21e2218e0ec7f4e3c0640d16bf8c67e6f0a61e18"
}
}

View file

@ -2508,10 +2508,10 @@ list.zathurarc = {
list.zig = {
install_info = {
url = "https://github.com/maxxnino/tree-sitter-zig",
url = "https://github.com/tree-sitter-grammars/tree-sitter-zig",
files = { "src/parser.c" },
},
maintainers = { "@maxxnino" },
maintainers = { "@amaanq" },
}
list.templ = {

View file

@ -1,14 +1,23 @@
[
(Block)
(ContainerDecl)
(SwitchExpr)
(InitList)
(AsmExpr)
(ErrorSetDecl)
(LINESTRING)
[
(IfPrefix)
(WhilePrefix)
(ForPrefix)
]
(block)
(switch_expression)
(initializer_list)
(asm_expression)
(multiline_string)
(if_statement)
(while_statement)
(for_statement)
(if_expression)
(else_clause)
(for_expression)
(while_expression)
(if_type_expression)
(function_signature)
(parameters)
(call_expression)
(struct_declaration)
(opaque_declaration)
(enum_declaration)
(union_declaration)
(error_set_declaration)
] @fold

View file

@ -1,104 +1,116 @@
(line_comment) @comment @spell
; Variables
(identifier) @variable
; Parameters
(parameter
name: (identifier) @variable.parameter)
(payload
(identifier) @variable.parameter)
; Types
(parameter
type: (identifier) @type)
((identifier) @type
(#lua-match? @type "^[A-Z_][a-zA-Z0-9_]*"))
(variable_declaration
(identifier) @type
"="
[
(struct_declaration)
(enum_declaration)
(union_declaration)
(opaque_declaration)
])
[
(container_doc_comment)
(doc_comment)
] @comment.documentation @spell
(builtin_type)
"anyframe"
] @type.builtin
; Constants
((identifier) @constant
(#lua-match? @constant "^[A-Z][A-Z_0-9]+$"))
[
variable: (IDENTIFIER)
variable_type_function: (IDENTIFIER)
] @variable
"null"
"unreachable"
"undefined"
] @constant.builtin
parameter: (IDENTIFIER) @variable.parameter
(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) @variable.member)
(container_field
name: (identifier) @variable.member)
(initializer_list
(assignment_expression
left: (field_expression
.
member: (identifier) @variable.member)))
; 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
[
field_member: (IDENTIFIER)
field_access: (IDENTIFIER)
] @variable.member
"c"
"..."
] @variable.builtin
; assume TitleCase is a type
([
variable_type_function: (IDENTIFIER)
field_access: (IDENTIFIER)
parameter: (IDENTIFIER)
] @type
(#lua-match? @type "^%u([%l]+[%u%l%d]*)*$"))
; assume camelCase is a function
([
variable_type_function: (IDENTIFIER)
field_access: (IDENTIFIER)
parameter: (IDENTIFIER)
] @function
(#lua-match? @function "^%l+([%u][%l%d]*)+$"))
; assume all CAPS_1 is a constant
([
variable_type_function: (IDENTIFIER)
field_access: (IDENTIFIER)
] @constant
(#lua-match? @constant "^%u[%u%d_]+$"))
function: (IDENTIFIER) @function
function_call: (IDENTIFIER) @function.call
exception: "!" @keyword.exception
((IDENTIFIER) @variable.builtin
((identifier) @variable.builtin
(#eq? @variable.builtin "_"))
(PtrTypeStart
"c" @variable.builtin)
(ContainerDecl
(ContainerDeclType
"enum")
(ContainerField
(ErrorUnionExpr
(SuffixExpr
(IDENTIFIER) @constant))))
field_constant: (IDENTIFIER) @constant
(BUILTINIDENTIFIER) @function.builtin
((BUILTINIDENTIFIER) @keyword.import
(#any-of? @keyword.import "@import" "@cImport"))
(INTEGER) @number
(FLOAT) @number.float
[
"true"
"false"
] @boolean
[
(LINESTRING)
(STRINGLITERALSINGLE)
] @string @spell
(CHAR_LITERAL) @character
(EscapeSequence) @string.escape
(FormatSequence) @string.special
(BreakLabel
(IDENTIFIER) @label)
(BlockLabel
(IDENTIFIER) @label)
(calling_convention
(identifier) @variable.builtin)
; Keywords
[
"asm"
"defer"
"errdefer"
"test"
"opaque"
"error"
"const"
"var"
@ -108,6 +120,7 @@ field_constant: (IDENTIFIER) @constant
"struct"
"union"
"enum"
"opaque"
] @keyword.type
[
@ -151,11 +164,6 @@ field_constant: (IDENTIFIER) @constant
"catch"
] @keyword.exception
[
"anytype"
(BuildinTypeExpr)
] @type.builtin
[
"volatile"
"allowzero"
@ -168,50 +176,85 @@ field_constant: (IDENTIFIER) @constant
"inline"
"noinline"
"extern"
] @keyword.modifier
[
"comptime"
"packed"
"threadlocal"
] @attribute
] @keyword.modifier
; Operator
[
"null"
"unreachable"
"undefined"
] @constant.builtin
[
(CompareOp)
(BitwiseOp)
(BitShiftOp)
(AdditionOp)
(AssignOp)
(MultiplyOp)
(PrefixOp)
"="
"*="
"*%="
"*|="
"/="
"%="
"+="
"+%="
"+|="
"-="
"-%="
"-|="
"<<="
"<<|="
">>="
"&="
"^="
"|="
"!"
"~"
"-"
"-%"
"&"
"=="
"!="
">"
">="
"<="
"<"
"&"
"^"
"|"
"<<"
">>"
"<<|"
"+"
"++"
"+%"
"-%"
"+|"
"-|"
"*"
"/"
"%"
"**"
"->"
".?"
"*%"
"*|"
"||"
".*"
".?"
"?"
".."
] @operator
[
";"
"."
","
":"
"=>"
] @punctuation.delimiter
; Literals
(character) @character
[
".."
"..."
] @punctuation.special
([
(string)
(multiline_string)
] @string
(#set! "priority" 95))
(integer) @number
(float) @number.float
(boolean) @boolean
(escape_sequence) @string.escape
; Punctuation
[
"["
"]"
@ -221,19 +264,20 @@ field_constant: (IDENTIFIER) @constant
"}"
] @punctuation.bracket
(Payload
[
";"
"."
","
":"
"=>"
"->"
] @punctuation.delimiter
(payload
"|" @punctuation.bracket)
(PtrPayload
"|" @punctuation.bracket)
; Comments
(comment) @comment @spell
(PtrIndexPayload
"|" @punctuation.bracket)
(PtrListPayload
"|" @punctuation.bracket)
(ParamType
(ErrorUnionExpr
(SuffixExpr
variable_type_function: (IDENTIFIER) @type)))
((comment) @comment.documentation
(#lua-match? @comment.documentation "^//!"))

View file

@ -1,11 +1,10 @@
[
(Block)
(ContainerDecl)
(SwitchExpr)
(InitList)
(block)
(switch_expression)
(initializer_list)
] @indent.begin
(Block
(block
"}" @indent.end)
[
@ -18,8 +17,6 @@
] @indent.branch
[
(line_comment)
(container_doc_comment)
(doc_comment)
(LINESTRING)
(comment)
(multiline_string)
] @indent.ignore

View file

@ -1,6 +1,10 @@
([
(container_doc_comment)
(doc_comment)
(line_comment)
] @injection.content
((comment) @injection.content
(#set! injection.language "comment"))
; TODO: add when asm is added
; (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"))