tree-sitter: Polish queries

This commit is contained in:
Tobias Hunger 2023-02-10 19:44:22 +01:00 committed by Tobias Hunger
parent ca7b2e0bb8
commit a454ffe8f9
5 changed files with 127 additions and 166 deletions

View file

@ -4,7 +4,7 @@ empty testcase
--------------------------------------------------------------------------------
(document)
(sourcefile)
================================================================================
Line comment
@ -12,7 +12,7 @@ Line comment
// Line comment
--------------------------------------------------------------------------------
(document
(sourcefile
(comment))
================================================================================
@ -22,11 +22,10 @@ Line comment with line continuation
import { foo } from "bar";
--------------------------------------------------------------------------------
(document
(sourcefile
(comment)
(import_statement
(type_identifier
(user_type_identifier))
(user_type_identifier)
(string_value)))
================================================================================
@ -36,7 +35,7 @@ Multiline comment
comment */
--------------------------------------------------------------------------------
(document
(sourcefile
(comment))
================================================================================
@ -47,7 +46,7 @@ Several comments
/* c2 // c5 */
--------------------------------------------------------------------------------
(document
(sourcefile
(comment)
(comment)
(comment)

View file

@ -30,36 +30,30 @@ TestCase := Rectangle {
}
--------------------------------------------------------------------------------
(document
(sourcefile
(global_definition
(type_identifier
(user_type_identifier))
(user_type_identifier)
(property
(type
(type_identifier
(user_type_identifier)))
(builtin_type_identifier))
(var_identifier)
(value
(int_value)))
(property
(type
(type_identifier
(user_type_identifier)))
(builtin_type_identifier))
(var_identifier)
(value
(int_value)))
(callback
(function_identifier)))
(component_definition
(type_identifier
(user_type_identifier))
(type_identifier
(user_type_identifier))
(user_type_identifier)
(user_type_identifier)
(block
(property
(type
(type_identifier
(user_type_identifier)))
(builtin_type_identifier))
(var_identifier)
(var_identifier
(post_identifier)))
@ -67,8 +61,7 @@ TestCase := Rectangle {
(var_identifier)
(for_range)
(component
(type_identifier
(user_type_identifier))
(user_type_identifier)
(block
(assignment_expr
(var_identifier)
@ -83,10 +76,8 @@ TestCase := Rectangle {
(value
(length_value)))))))))
(component_definition
(type_identifier
(user_type_identifier))
(type_identifier
(user_type_identifier))
(user_type_identifier)
(user_type_identifier)
(block
(callback
(function_identifier))
@ -99,22 +90,18 @@ TestCase := Rectangle {
(function_identifier)
(call_signature))))))
(component
(type_identifier
(user_type_identifier))
(user_type_identifier)
(block))
(component
(var_identifier)
(type_identifier
(user_type_identifier))
(user_type_identifier)
(block))
(component
(type_identifier
(user_type_identifier))
(user_type_identifier)
(block))
(property
(type
(type_identifier
(user_type_identifier)))
(builtin_type_identifier))
(var_identifier)
(add_binary_expression
(mult_binary_expression
@ -128,8 +115,7 @@ TestCase := Rectangle {
(int_value))))
(property
(type
(type_identifier
(user_type_identifier)))
(builtin_type_identifier))
(var_identifier)
(var_identifier
(post_identifier))))))
@ -153,50 +139,42 @@ id_lookup
}
--------------------------------------------------------------------------------
(document
(sourcefile
(component_definition
(type_identifier
(user_type_identifier))
(type_identifier
(user_type_identifier))
(user_type_identifier)
(user_type_identifier)
(block
(property
(type
(type_identifier
(user_type_identifier)))
(builtin_type_identifier))
(var_identifier)
(value
(int_value)))
(property
(type
(type_identifier
(user_type_identifier)))
(builtin_type_identifier))
(var_identifier)
(value
(int_value)))
(component
(var_identifier)
(type_identifier
(user_type_identifier))
(user_type_identifier)
(block
(property
(type
(type_identifier
(user_type_identifier)))
(builtin_type_identifier))
(var_identifier)
(value
(int_value)))
(property
(type
(type_identifier
(user_type_identifier)))
(builtin_type_identifier))
(var_identifier)
(value
(int_value)))
(property
(type
(type_identifier
(user_type_identifier)))
(builtin_type_identifier))
(var_identifier)
(add_binary_expression
(add_binary_expression
@ -223,15 +201,13 @@ id_lookup
(int_value)))))))
(property
(type
(type_identifier
(user_type_identifier)))
(builtin_type_identifier))
(var_identifier)
(var_identifier
(post_identifier)))
(property
(type
(type_identifier
(user_type_identifier)))
(builtin_type_identifier))
(var_identifier)
(add_binary_expression
(add_binary_expression

View file

@ -49,8 +49,8 @@ module.exports = grammar({
"{",
commaSep(
seq(
$.type_identifier,
optional(seq("as", $.type_identifier)),
$._type_identifier,
optional(seq("as", $._type_identifier)),
),
),
"}",
@ -66,8 +66,8 @@ module.exports = grammar({
"{",
commaSep(
seq(
$.type_identifier,
optional(seq("as", $.type_identifier)),
$._type_identifier,
optional(seq("as", $._type_identifier)),
),
),
"}",
@ -83,7 +83,7 @@ module.exports = grammar({
component: ($) =>
seq(
optional(seq(field("id", $.var_identifier), ":=")),
field("type", $.type_identifier),
field("type", $.user_type_identifier),
$.block,
),
@ -92,17 +92,17 @@ module.exports = grammar({
seq(
// new syntax
"component",
field("name", $.type_identifier),
field("name", $._type_identifier),
optional(
seq("inherits", field("base_type", $.type_identifier)),
seq("inherits", field("base_type", $._type_identifier)),
),
$.block,
),
seq(
// old syntax
field("name", $.type_identifier),
field("name", $._type_identifier),
":=",
field("base_type", $.type_identifier),
field("base_type", $._type_identifier),
$.block,
),
),
@ -183,7 +183,7 @@ module.exports = grammar({
global_definition: ($) =>
seq(
"global",
field("name", $.type_identifier),
field("name", $._type_identifier),
optional(":="), // old syntax!
"{",
repeat(
@ -200,7 +200,7 @@ module.exports = grammar({
struct_definition: ($) =>
seq(
"struct",
field("name", $.type_identifier),
field("name", $._type_identifier),
optional(":="), // old syntax!
$.type_anon_struct,
),
@ -339,7 +339,8 @@ module.exports = grammar({
),
type_list: ($) => seq("[", commaSep($.type), optional(","), "]"),
type: ($) => choice($.type_identifier, $.type_list, $.type_anon_struct),
type: ($) =>
choice($._type_identifier, $.type_list, $.type_anon_struct),
for_range: ($) => choice($._int_number, $.value_list, $.var_identifier),
@ -429,7 +430,7 @@ module.exports = grammar({
"callback",
field("name", $.function_identifier),
optional($.call_signature),
optional(seq("->", field("return_type", $.type_identifier))),
optional(seq("->", field("return_type", $._type_identifier))),
";",
),
@ -481,27 +482,27 @@ module.exports = grammar({
";",
),
typed_identifier: ($) =>
seq(field("name", $.simple_identifier), ":", field("type", $.type)),
function_signature: ($) =>
seq(
"(",
field(
"parameters",
optional(
seq(
commaSep1(seq($._identifier, ":", $.type)),
optional(","),
),
),
optional(seq(commaSep1($.typed_identifier), optional(","))),
),
")",
),
parameter: ($) => $._expression,
call_signature: ($) =>
seq(
"(",
field(
"parameters",
optional(seq(commaSep1($._expression), optional(","))),
optional(seq(commaSep1($.parameter), optional(","))),
),
")",
),
@ -558,35 +559,31 @@ module.exports = grammar({
visibility_modifier: (_) => choice("private", "in", "out", "in-out"),
_identifier: (_) => /[a-zA-Z_][a-zA-Z0-9_-]*/,
simple_identifier: ($) => $._identifier,
// prefix_identifier: ($) => $._identifier,
post_identifier: ($) => choice($._identifier, $.function_call),
// Do not use strings here: Otherwise you can not assign to variables
// with one of these names
_builtin_type_identifier: (_) =>
prec(
2,
choice(
/int/,
/float/,
/bool/,
/string/,
/color/,
/brush/,
/physical-length/,
/length/,
/duration/,
/angle/,
/easing/,
/percent/,
/image/,
/relative-font-size/,
),
builtin_type_identifier: (_) =>
choice(
"int",
"float",
"bool",
"string",
"color",
"brush",
"physical-length",
"length",
"duration",
"angle",
"easing",
"percent",
"image",
"relative-font-size",
),
user_type_identifier: ($) => prec(1, $._identifier),
type_identifier: ($) =>
choice($.user_type_identifier, $._builtin_type_identifier),
_type_identifier: ($) =>
choice($.builtin_type_identifier, $.user_type_identifier),
value_list: ($) =>
seq("[", commaSep($._expression), optional(","), "]"),

View file

@ -1,76 +1,49 @@
; Copyright © SixtyFPS GmbH <info@slint-ui.com>
; SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-commercial
(user_type_identifier) @type
(comment) @comment @spell
(var_identifier) @variable
; Different types:
(string_value) @string @spell
(bool_value) @boolean
(int_value) @number
[(float_value) (percent_value)] @float
(var_identifier
(post_identifier) @variable)
(builtin_type_identifier) @type.builtin
(reference_identifier) @variable.builtin
(user_type_identifier) @spell
(type [(type_list) (user_type_identifier) (type_anon_struct)]) @type
[(comparison_operator) (mult_prec_operator) (add_prec_operator) (unary_prec_operator) (assignment_prec_operator)] @operator
(function_identifier) @function
; Functions and callbacks
(function_call) @function.call
(parameter) @parameter
(function ("function" @keyword))
(callback ("callback" @keyword))
(block ("return" @keyword.return))
(reference_identifier) @keyword
(visibility_modifier) @include
; definitions
(callback name: ((_) @spell))
(function name: ((_) @spell))
(property name: ((_) @property @spell))
(component id: ((_) @variable @spell))
(struct_definition (type_anon_struct name: ((_) @field)))
(global_definition (["global" ":="] @include @keyword))
(struct_definition (["struct" ":="] @include @keyword))
(property (["property" "<" ">"] @keyword))
(visibility_modifier) @type.qualifier
(comment) @comment
; Keywords:
[ ";" "." "," ] @punctuation.delimiter
[ "(" ")" "[" "]" "{" "}" ] @punctuation.bracket
(value) @number
(ternary_expression (["?" ":"] @conditional.ternary))
(if_statement (["if" ":" "else"] @conditional))
(for_loop (["for" "in" ":"] @repeat @keyword))
[
"in"
"for"
] @repeat
"@" @keyword
[
"import"
"from"
] @include
[
"if"
"else"
] @conditional
[
"root"
"parent"
"self"
] @variable.builtin
[
"true"
"false"
] @boolean
[
"struct"
"property"
"callback"
"in"
"animate"
"states"
"when"
"out"
"transitions"
"global"
] @keyword
; Punctuation
[
","
"."
] @punctuation.delimiter
; Brackets
[
"("
")"
"["
"]"
"{"
"}"
] @punctuation.bracket
(animate_statement (["animate"] @keyword))
(component_definition (["component" "inherits" ":="] @keyword))
(export_statement (["export" "as"] @include @keyword))
(import_statement (["import" "from" "as"] @include @keyword))
(states_definition (["states" "when"] @keyword))
(transitions_definition (["transitions" "in" "out"] @keyword))

View file

@ -0,0 +1,16 @@
; Copyright © SixtyFPS GmbH <info@slint-ui.com>
; SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-commercial
; Functions and callbacks
(typed_identifier name: ((_) @definition.parameter))
; definitions
(callback name: ((_) @definition.method) (#set! "definition.method.scope" "parent"))
(function name: ((_) @definition.method) (#set! "definition.method.scope" "parent"))
(property name: ((_) @definition.var) (#set! "definition.var.scope" "parent"))
(component id: ((_) @definition.var) (#set! "definition.var.scope" "parent"))
(global_definition name: ((user_type_identifier) @definition.type) (#set! "definition.method.scope" "parent"))
(struct_definition name: ((user_type_identifier) @definition.type) (#set! "definition.type.scope" "global"))
(struct_definition (type_anon_struct name: ((_) @definition.var)))
(block) @scope