-
I wonder if tree-sitter supports "unparsing", in the same sense as If not, what's the best way to generate such an "unparser" from grammar files? A high-level overview would suffice. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 5 replies
-
Not supported. You can use python (or any other language) to read the grammar.json file in the src directory to get the meaning of each AST node, and generate code from that. Would require some extra manual per-language work if an external scanner is involved. |
Beta Was this translation helpful? Give feedback.
-
As @ahelwer said it isn't supported in the same sense of like it exists in Python because Tree-sitter doesn't store original text pieces in all leaf nodes, instead of that it just stores ranges that every node occupy in an original text.
|
Beta Was this translation helpful? Give feedback.
-
@nalzok You're looking for code generation given an AST. Under the hood, Python's async def foo(bar):
return bar which results in the following s-expression tree: As you can see, |
Beta Was this translation helpful? Give feedback.
-
#1657 implements an "unparser" on top of tree-sitter for original and modified ASTs. The implementation is non-trivial--the grammar is embedded into the classes to reproduce the source, and class definitions and parse trees are programmatically modified to store additional information based on the grammars. |
Beta Was this translation helpful? Give feedback.
As @ahelwer said it isn't supported in the same sense of like it exists in Python because Tree-sitter doesn't store original text pieces in all leaf nodes, instead of that it just stores ranges that every node occupy in an original text.
It depends what is a purpose of unparsing: