I'm struggling with the tree-sitter query language for a few days.
Let's say my end goal is to ensure every list is sorted in a given language (the parsed language isn't important here and mustn't be)).
I would hope to obtain all the lists (to get line-related informations to print to the user) and their elements (to know if the list is really sorted) in a single tree-sitter query.
As a sample, I have this HCL block :
resource "foo" "bar" {
baz = ["watch", "list", "get"]
}
It's S-expression looks like this:
(config_file
(body
(block
(identifier)
(string_lit
(quoted_template_start)
(template_literal)
(quoted_template_end)
)
(string_lit
(quoted_template_start)
(template_literal)
(quoted_template_end)
)
(block_start)
(body
(attribute
(identifier)
(expression
(collection_value ; <---- This is the start of my `baz` list
(tuple
(tuple_start)
(expression
(literal_value
(string_lit
(quoted_template_start)
(template_literal)
(quoted_template_end)
)
)
)
(expression
(literal_value
(string_lit
(quoted_template_start)
(template_literal)
(quoted_template_end)
)
)
)
(expression
(literal_value
(string_lit
(quoted_template_start)
(template_literal)
(quoted_template_end)
)
)
)
(tuple_end)
)
)
)
)
)
(block_end)
)
)
)
- If I do
(tuple) @list
I will get a literal string like'["watch", "list", "get"]'
which I will have to parse (which seems counterproductive). - If I do
(template_literal) @element
I will get a set of string. It kinda works as-is, but if my sample contains multiple list, then I will get all the elements of all the lists without knowing which element belongs to which list. I'm loosing context. - My next idea would be to query one time to obtain the list, then a second time on each obtained list to get the elements. But I really hope there is a less naive approach.
I don't think this is of any importance but I'm using Python to run tree-sitter. The code has nothing fancy and follows the official README.