GithubHelp home page GithubHelp logo

Comments (5)

jmataraz avatar jmataraz commented on April 27, 2024

FWIW, I tried running a similar query on query.rs that is part of this project:

M-x tree-sitter-mode
M-: (ts-query-captures (ts-make-query (tree-sitter-require 'rust) [(function_item (identifier) @function)]) (ts-root-node tree-sitter-tree))

and this generated the expected vector of results. Something busted with the c/cpp grammar maybe?

from elisp-tree-sitter.

ubolonton avatar ubolonton commented on April 27, 2024

The C++ grammar is different:

  function_definition: 
    primitive_type: 
    function_declarator: 
      scoped_identifier: 
        namespace_identifier: 
        identifier: 
      parameter_list: 
        parameter_declaration: 
          primitive_type: 
          identifier: 
        parameter_declaration: 
          primitive_type: 
          identifier: 
        parameter_declaration: 
          primitive_type: 
          identifier: 

This query works:

[(function_definition
  (function_declarator
   (scoped_identifier) @function))]

Also, if you are using the latest version, captures are now cons cells, not vectors.

from elisp-tree-sitter.

jmataraz avatar jmataraz commented on April 27, 2024

I wasn't getting the query syntax. I reread the S-expression description on tree-sitter and it makes more sense now. Thanks for the help!

from elisp-tree-sitter.

jmataraz avatar jmataraz commented on April 27, 2024

One more question, actually. I found that the example above missed out on regular functions that aren't part of a class or namespace. I considered two approaches:

  1. Add another S-expression to the query: (function_definition (function_declarator (identifer) @function)
  2. Change the query to capture the function_declarator and then pull the first child with (ts-get-nth-child node 0), which is always "identifier" or "scoped_identifier" and works for both cases.

In general, which is more performant? Are queries really cheap and fast? Faster than processing the node/capture when it comes back?

from elisp-tree-sitter.

ubolonton avatar ubolonton commented on April 27, 2024

The first approach should be more performant, for 2 reasons:

  • Rust is faster than Lisp.
  • Crossing the module boundary (calling ts- functions) has some overhead.

from elisp-tree-sitter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.