emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Help needed with query


From: João Paulo Labegalini de Carvalho
Subject: Re: Help needed with query
Date: Sat, 10 Dec 2022 09:02:32 -0700

HI Perry,

On Sat, Dec 10, 2022 at 7:43 AM Perry Smith <pedz@easesoftware.com> wrote:
Can someone help explain constructs like this?

           ((query "(for_statement initializer: (_) @indent)") parent-bol 5)

This query aims to match the initializer part of a for statement. The initializer: is a key to one of the for_statement node's fields/children nodes.

The capture @indent is used to "mark" the matched initializer for indentation.
 
The ‘_’ seems to be a “match all” but I’m currently using just ‘_’  and not ‘(_)’ and it seems to be working for me.  What is the difference?

From the Tree-Sitter documentation for Wildcard nodes:

"A wildcard node is represented with an underscore (_), it matches any node. This is similar to . in regular expressions. There are two types, (_) will match any named node, and _ will match any named or anonymous node."

For example (from here):

if_statement: ($) => seq("if", "(", $._expression, ")", $._statement);


if_statement is a named node, but its children: if, (, and ) are anonymous nodes.


    • The @indent seems to be necessary to get the query to match.  Things do not seem to match without it but it doesn’t need to be @indent… it can be @foo.  So, what is it doing and does the name matter?

You can use any identifier as the capture name. However, special names are used to identify targets for fontification and indentation.

For instance, when using queries to fontify text, the capture name is either a font-face or a function that returns a font-face.
 
I can’t recall now but when I was working on the font lock rules, I needed matching constructs and a type of “match anything except” type of construct.

I am not versed in tree-sitter enough, but I don't recall such a construct. You could write a function that first matches general instances of the construct and then filter out those that have the "undesired"  properties.

--
João Paulo L. de Carvalho
Ph.D Computer Science |  IC-UNICAMP | Campinas , SP - Brazil
Postdoctoral Research Fellow | University of Alberta | Edmonton, AB - Canada
joao.carvalho@ic.unicamp.br
joao.carvalho@ualberta.ca

reply via email to

[Prev in Thread] Current Thread [Next in Thread]