Comments (6)
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:
- tree-sitter/tree-sitter#444
- tree-sitter/tree-sitter#448
- The docs on tree queries
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.
Thanks @maxbrunsfeld .. Will check...
from py-tree-sitter.
@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.
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.
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
- How can I suppress the captures with a condition (If the indirect parent is a class_def which does not have specific name)?
- 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.
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)
- Miss distutils in Windows HOT 1
- Documentation is required.
- Is it possible to understand if an object is an instance of a certain class that may be defined in another file?
- How to use tree-sitter in Windows 10? HOT 6
- Special case not managed by the parser, when an expression is split without a backslash and the second line is dedented. HOT 4
- Example mismatch HOT 1
- Captures are not grouped HOT 3
- How to sync node after code edit? HOT 1
- README.md needs updates
- Tree-sitter Fails with Core Dump on Processing Large Input Code File HOT 4
- query failing in py-tree-sitter but compiling in tree-sitter playground HOT 1
- a __main__.py to build `vendor/tree-sitter-X` from command line HOT 1
- [bug] Cannot build a library in a directory containing two parsers. HOT 5
- The Python parser appears to be parsing comments and strings with unmatched parentheses as code HOT 2
- py-tree-sitter is 10x slower than lezer-parser HOT 9
- FileNotFoundError: [Errno 2] No such file or directory: 'tree-sitter-java/src/parser.c' HOT 2
- UTF-16 encoding support is wanted HOT 1
- Can't install tree-sitter-python HOT 1
- [tree-sitter-python] `No matching distribution found for tree-sitter-python` HOT 1
- Broken (?) README instructions to pip install pre-compiled binary wheels HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from py-tree-sitter.