From c5671149fc7c01aeade63811b5c67d38f2461bf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Mon, 5 Sep 2022 10:50:32 +0900 Subject: [PATCH] tree-sitter-solidity: Use upstream, update queries --- languages.toml | 2 +- runtime/queries/solidity/highlights.scm | 134 +++++++++++++++--------- runtime/queries/solidity/locals.scm | 9 ++ 3 files changed, 92 insertions(+), 53 deletions(-) create mode 100644 runtime/queries/solidity/locals.scm diff --git a/languages.toml b/languages.toml index 069215340a5b8..b19fde6a31af9 100644 --- a/languages.toml +++ b/languages.toml @@ -1192,7 +1192,7 @@ language-server = { command = "solc", args = ["--lsp"] } [[grammar]] name = "solidity" -source = { git = "https://github.com/slinlee/tree-sitter-solidity", rev = "f3a002274744e859bf64cf3524985f8c31ea84fd" } +source = { git = "https://github.com/JoranHonig/tree-sitter-solidity", rev = "9004b86531cb424bd379424cf7266a4585f2af7d" } [[language]] name = "gleam" diff --git a/runtime/queries/solidity/highlights.scm b/runtime/queries/solidity/highlights.scm index 544cf3d94d1af..08178c362a039 100644 --- a/runtime/queries/solidity/highlights.scm +++ b/runtime/queries/solidity/highlights.scm @@ -1,15 +1,16 @@ -(comment) @comment +; identifiers +; ----------- +(identifier) @variable +(yul_identifier) @variable ; Pragma (pragma_directive) @tag -(solidity_version_comparison_operator ">=" @tag) -(solidity_version_comparison_operator "<=" @tag) -(solidity_version_comparison_operator "=" @tag) -(solidity_version_comparison_operator "~" @tag) -(solidity_version_comparison_operator "^" @tag) +(solidity_version_comparison_operator _ @tag) ; Literals +; -------- + [ (string) (hex_string_literal) @@ -26,26 +27,38 @@ (false) ] @constant.builtin +(comment) @comment + + +; Definitions and references +; ----------- -; Type (type_name) @type (primitive_type) @type -(struct_declaration struct_name: (identifier) @type) -(enum_declaration enum_type_name: (identifier) @type) +(user_defined_type (identifier) @type) + ; Color payable in payable address conversion as type and not as keyword (payable_conversion_expression "payable" @type) -(emit_statement . (identifier) @type) -; Handles ContractA, ContractB in function foo() override(ContractA, contractB) {} -(override_specifier (identifier) @type) ; Ensures that delimiters in mapping( ... => .. ) are not colored like types (type_name "(" @punctuation.bracket "=>" @punctuation.delimiter ")" @punctuation.bracket) - - -; Functions and parameters +; Definitions +(struct_declaration + name: (identifier) @type) +(enum_declaration + name: (identifier) @type) +(contract_declaration + name: (identifier) @type) +(library_declaration + name: (identifier) @type) +(interface_declaration + name: (identifier) @type) +(event_definition + name: (identifier) @type) (function_definition - function_name: (identifier) @function) + name: (identifier) @function) + (modifier_definition name: (identifier) @function) (yul_evm_builtin) @function.builtin @@ -55,37 +68,38 @@ (fallback_receive_definition "receive" @constructor) (fallback_receive_definition "fallback" @constructor) -(modifier_invocation (identifier) @function) +(struct_member name: (identifier) @variable.other.member) +(enum_value) @constant -; Handles expressions like structVariable.g(); -(call_expression . (member_expression (property_identifier) @function.method)) +; Invocations +(emit_statement . (identifier) @type) +(modifier_invocation (identifier) @function) -; Handles expressions like g(); +(call_expression . (member_expression property: (identifier) @function.method)) (call_expression . (identifier) @function) ; Function parameters -(event_paramater name: (identifier) @variable.parameter) ; TODO fix spelling once fixed upstream -(function_definition - function_name: (identifier) @variable.parameter) +(call_struct_argument name: (identifier) @field) +(event_paramater name: (identifier) @variable.parameter) +(parameter name: (identifier) @variable.parameter) ; Yul functions (yul_function_call function: (yul_identifier) @function) - -; Yul function parameters (yul_function_definition . (yul_identifier) @function (yul_identifier) @variable.parameter) -(meta_type_expression "type" @keyword) -(member_expression (property_identifier) @variable.other.member) -(property_identifier) @variable.other.member -(struct_expression ((identifier) @variable.other.member . ":")) -(enum_value) @variable.other.member +; Structs and members +(member_expression property: (identifier) @variable.other.member) +(struct_expression type: ((identifier) @type .)) +(struct_field_assignment name: (identifier) @variable.other.member) +; Tokens +; ------- ; Keywords +(meta_type_expression "type" @keyword) [ "pragma" - "import" "contract" "interface" "library" @@ -95,19 +109,6 @@ "event" "using" "assembly" - "switch" - "case" - "default" - "break" - "continue" - "if" - "else" - "for" - "while" - "do" - "try" - "catch" - "return" "emit" "public" "internal" @@ -117,20 +118,47 @@ "view" "payable" "modifier" - "returns" "memory" "storage" "calldata" - "function" "var" - (constant) + "constant" (virtual) (override_specifier) (yul_leave) ] @keyword -(import_directive "as" @keyword) -(import_directive "from" @keyword) +[ + "for" + "while" + "do" +] @keyword.control.repeat + +[ + "break" + "continue" + "if" + "else" + "switch" + "case" + "default" +] @keyword.control.conditional + +[ + "try" + "catch" +] @keyword.control.exception + +[ + "return" + "returns" +] @keyword.control.return + +"function" @keyword.function + +"import" @keyword.control.import +(import_directive "as" @keyword.control.import) +(import_directive "from" @keyword.control.import) (event_paramater "indexed" @keyword) ; TODO fix spelling once fixed upstream ; Punctuation @@ -142,7 +170,7 @@ "]" "{" "}" -] @punctuation.bracket +] @punctuation.bracket [ @@ -185,5 +213,7 @@ "--" ] @operator -(identifier) @variable -(yul_identifier) @variable +[ + "delete" + "new" +] @keyword.operator diff --git a/runtime/queries/solidity/locals.scm b/runtime/queries/solidity/locals.scm new file mode 100644 index 0000000000000..29bdc02cb9955 --- /dev/null +++ b/runtime/queries/solidity/locals.scm @@ -0,0 +1,9 @@ +(function_definition) @local.scope +(block_statement) @local.scope + +(function_definition (parameter name: (identifier) @local.definition)) + +; still have to support tuple assignments +(assignment_expression left: (identifier) @local.definition) + +(identifier) @local.reference \ No newline at end of file