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

query choice operator is non-commutative

yuriv opened this issue · comments

Problem

Tree-sitter query choice operator is non-commutative. Run tree-sitter query with decl_succ.scm and var0.c it would produce proper captures then run decl_fail.scm it would produce nothing. The differences between query files are opposite argument 's order of the top-level choice operator only.

decl_succ.scm

sources.tar.gz

Steps to reproduce

git clone --depth=1 https://github.com/tree-sitter/tree-sitter-c
cd tree-sitter-c
tree-sitter generate
add tree-sitter-c parser to tree-sitter config
tree-sitter query decl_succ.scm var0.c
tree-sitter query decl_fail.scm var0.c

Actual behavior

$ cat decl_succ.scm
(declaration (primitive_type) @tt [(init_declarator) @id [(attributed_declarator) @ad (identifier) @ii]]) @dd
$ tree-sitter query decl_succ.scm var0.c
var0.c
pattern: 0
capture: 4 - dd, start: (0, 0), end: (0, 6), text: int k;
capture: 0 - tt, start: (0, 0), end: (0, 3), text: int
capture: 3 - ii, start: (0, 4), end: (0, 5), text: k

$ cat decl_fail.scm
(declaration (primitive_type) @tt [[(attributed_declarator) @ad (identifier) @ii] (init_declarator) @id]) @dd
$ tree-sitter query decl_fail.scm var0.c
var0.c

Expected behavior

$ tree-sitter query decl_succ.scm var0.c
var0.c
pattern: 0
capture: 4 - dd, start: (0, 0), end: (0, 6), text: int k;
capture: 0 - tt, start: (0, 0), end: (0, 3), text: int
capture: 3 - ii, start: (0, 4), end: (0, 5), text: k

$ tree-sitter query decl_fail.scm var0.c
var0.c
pattern: 0
capture: 4 - dd, start: (0, 0), end: (0, 6), text: int k;
capture: 0 - tt, start: (0, 0), end: (0, 3), text: int
capture: 3 - ii, start: (0, 4), end: (0, 5), text: k

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

tree-sitter 0.22.2

Operating system/version

Rocky Linux 8.9

Looks related to/ is a duplicate of #1811

It does indeed seem to be a duplicate of #1811 rather than #3239.