unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Help needed with query
@ 2022-12-10 14:43 Perry Smith
  2022-12-10 16:02 ` João Paulo Labegalini de Carvalho
  0 siblings, 1 reply; 3+ messages in thread
From: Perry Smith @ 2022-12-10 14:43 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 923 bytes --]

Can someone help explain constructs like this?

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

This rule is coming from the indent rules for c-ts-mode.

I’m doing:

           ((query "((method_parameters \"(\" _ @indent))") first-sibling 1)

and it seems to be working for me.

Two questions:
    •
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?
    • 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?


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.

Thank you for your time,
Perry


[-- Attachment #2: Message signed with OpenPGP --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Help needed with query
  2022-12-10 14:43 Help needed with query Perry Smith
@ 2022-12-10 16:02 ` João Paulo Labegalini de Carvalho
  2022-12-10 17:31   ` ***SPAM*** " Perry Smith
  0 siblings, 1 reply; 3+ messages in thread
From: João Paulo Labegalini de Carvalho @ 2022-12-10 16:02 UTC (permalink / raw)
  To: Perry Smith; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 2329 bytes --]

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
<https://tree-sitter.github.io/tree-sitter/using-parsers#named-vs-anonymous-nodes>
):

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

[-- Attachment #2: Type: text/html, Size: 3690 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: ***SPAM***  Re: Help needed with query
  2022-12-10 16:02 ` João Paulo Labegalini de Carvalho
@ 2022-12-10 17:31   ` Perry Smith
  0 siblings, 0 replies; 3+ messages in thread
From: Perry Smith @ 2022-12-10 17:31 UTC (permalink / raw)
  To: João Paulo Labegalini de Carvalho; +Cc: emacs-devel


[-- Attachment #1.1: Type: text/plain, Size: 1241 bytes --]



> On Dec 10, 2022, at 10:02, João Paulo Labegalini de Carvalho <jaopaulolc@gmail.com> wrote:
> 
> The capture @indent is used to "mark" the matched initializer for 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 knew capture name is used as the face name to fontify text but I can’t find any reference about @indent when used in an indent query — even looking at treesit.el I don’t see anything.  I’m going to try and figure it out empirically.

>  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.

I’m reading more and more… turns out:

   It is also possible to capture a node that doesn’t have a certain
field, say, a ‘function_definition’ without a ‘body’ field.

     (function_definition !body) @func-no-body

As always… thank you for your help.


[-- Attachment #1.2: Type: text/html, Size: 2086 bytes --]

[-- Attachment #2: Message signed with OpenPGP --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-12-10 17:31 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-10 14:43 Help needed with query Perry Smith
2022-12-10 16:02 ` João Paulo Labegalini de Carvalho
2022-12-10 17:31   ` ***SPAM*** " Perry Smith

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).