tree-sitter / tree-sitter

An incremental parsing system for programming tools

Home Page:https://tree-sitter.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

tree-sitter highlight unexpected precedence

ilyakochik opened this issue · comments

Problem

I am running tests on SQL BigQuery with tree-sitter test and it shows that syntax highlighting fails for unexpected matching precedence.

Steps to reproduce

select.sql (full)

SELECT a, b;
SELECT foo(args)

query/highlights.scm (full)

(function_call function: (identifier) @function)
((argument (identifier) @variable.parameter))
(identifier) @variable

parse tree (tree-sitter parse ./test/highlight/select.sql)

(source_file [0, 0] - [37, 0]
  (query_statement [0, 0] - [0, 11]
    (query_expr [0, 0] - [0, 11]
      (select [0, 0] - [0, 11]
        (select_list [0, 7] - [0, 11]
          (select_expression [0, 7] - [0, 8]
            (identifier [0, 7] - [0, 8]))
          (select_expression [0, 10] - [0, 11]
            (identifier [0, 10] - [0, 11]))))))
  (query_statement [4, 0] - [22, 20]
    (query_expr [4, 0] - [22, 20]
      (select [4, 0] - [19, 13]
        (select_list [4, 7] - [4, 16]
          (select_expression [4, 7] - [4, 16]
            (function_call [4, 7] - [4, 16]
              function: (identifier [4, 7] - [4, 10])
              argument: (argument [4, 11] - [4, 15]
                (identifier [4, 11] - [4, 15])))))

query (tree-sitter query ./queries/highlights.scm ./test/highlight/select.sql --test --row-range 0:5)

 pattern: 21
    capture: 13 - keyword, start: (0, 0), end: (0, 6), text: `SELECT`
  pattern: 14
    capture: 7 - variable, start: (0, 7), end: (0, 8), text: `a`
  pattern: 14
    capture: 7 - variable, start: (0, 10), end: (0, 11), text: `b`
  pattern: 16
    capture: 9 - punctuation.delimiter, start: (0, 11), end: (0, 12), text: `;`
  pattern: 21
    capture: 13 - keyword, start: (4, 0), end: (4, 6), text: `SELECT`
  pattern: 4
    capture: 4 - function, start: (4, 7), end: (4, 10), text: `foo`
  pattern: 14
    capture: 7 - variable, start: (4, 7), end: (4, 10), text: `foo`
  pattern: 5
    capture: 5 - variable.parameter, start: (4, 11), end: (4, 15), text: `args`
  pattern: 14
    capture: 7 - variable, start: (4, 11), end: (4, 15), text: `args`
Assertion failed: at (4, 7), found function, expected variable

If I changed ^ function to ^variable in test/highlight/select.sql I'm still getting error but this time Assertion failed: at (4, 7), found variable, expected function

Expected behaviour

  1. The more specific (function_call function: (identifier) @function) to take precedence over (identifier) @variable.
  2. tree-sitter test to either
    • assert on any of the captured patterns (e.g. ^ function test is passed once function was captured)
    • consider all listed patterns (e.g. ^ variable function) like Sublime does

Tree-sitter version (tree-sitter --version)

tree-sitter 0.22.2 (fc15f62)

Operating system/version

MacOs 14.2.1 (23C71)

All captures will be applied but only the last match is considered, regardless of specificity.

Yeah, you need to write your queries in a way that the ones you want to match come last, so you'd want the more specific ones later on

Awesome, that works with tree-sitter test. Thanks.