Giter Site home page Giter Site logo

Comments (6)

maxbrunsfeld avatar maxbrunsfeld commented on May 12, 2024

I've gotten rid of the TreePropertyCursor and replaced it with a new tree query API. There is already a binding for the query API in py-tree-sitter although a little bit of functionality is still missing.

Similar to the TreePropertyCursor, the purpose of a query is to allow you to look for certain patterns in a syntax tree, and iterate over all of the matches. With the old properties API, you could write the patterns as CSS selectors. But now, with the query API, there is a more general, flexible pattern-matching syntax.

Here are some links that might be helpful:

If you're interested in tracking scopes and local variables, that can be done with tree queries. The tree-sitter-highlight library (introduced in the second PR above) tracks local variables for some languages, if they have a locals.scm query file. Here's an example of one: https://github.com/tree-sitter/tree-sitter-ruby/blob/master/queries/locals.scm.

from py-tree-sitter.

anandsork avatar anandsork commented on May 12, 2024

Thanks @maxbrunsfeld .. Will check...

from py-tree-sitter.

anandsork avatar anandsork commented on May 12, 2024

@maxbrunsfeld
Can we pass the query to parse somehow? I would like to tag few nodes while walking. In Query API way, I have to fire the query everytime in that node (where I expect tagging would occur).
Instead of highlighting, I would like to have this information while I am walking through the children. Is there any way?

from py-tree-sitter.

maxbrunsfeld avatar maxbrunsfeld commented on May 12, 2024

You use a query cursor to walk the tree, iterating over the matching nodes. You would no longer walk the tree with the tree cursor.

from py-tree-sitter.

anandsork avatar anandsork commented on May 12, 2024

@maxbrunsfeld

Not sure how to ask the questions.. So adding a query here

I am trying to find out the member variables (class, instance etc..) of a class. The query I am doing is based on attribute (Self Function calls come as well, but I figured out a way to filter those)..

((attribute
  object: (identifier) @obj 
  attribute: (identifier) @property)
  (match? @obj "^self$"))

I am able to get most of the variables.

However the capture also give me inner class attributes as well

  1. How can I suppress the captures with a condition (If the indirect parent is a class_def which does not have specific name)?
  2. Can I add multiple capture-names (@capture-name)?
    I wanted to add a specific capture-name to all identifiers which has the pattern of
    (class_definition <Anylevel> (class_definition <AnyLevel Identifier> @innerClassIdent))
    If I find @innerClassIdent & @property both, I might ignore it..
    The main problem in above query is <AnyLevel>. I am not sure how to find all Identifiers & tag them.. I had tried * & **, but they did not work.

The sample code is

class D:
   def abc(self):
       self.i = 0
   class C:
      def xyz(self):
         self.j = 0

I would like to get "i" only, but not "j"

from py-tree-sitter.

anandsork avatar anandsork commented on May 12, 2024

I found a wayout. Querying the inner ClassDefinition with the query like below..
(The * has some special significance, it seems.. Even **, *** has some level related meaning. However I could not found the documentation around it)

(class_definition
  (* (class_definition (identifier)* @inner) ) )

One can get the inner class Node StartPoint & EndPoint.. There are different ways as well (getting child with class_definition type).
I can then filter the @property nodes (retrieved using the query shared in earlier post) with the line number or range.

Not sure if this is optimal way. But it might work I think. Comments welcome.

Thanks..

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