tree-sitter / node-tree-sitter

Node.js bindings for tree-sitter
https://www.npmjs.com/package/tree-sitter
MIT License
649 stars 114 forks source link

How to include code to handle extraction? #23

Closed wongjiahau closed 6 years ago

wongjiahau commented 6 years ago

For example, in jison, we can do the following:

MathExpression 
    = Number Operator Number {$$={left: $1, op: $2, right: $3}}
    ;

So, how can I do that or achieve similar thing in node-tree-sitter? Or do you have a better suggestion?

maxbrunsfeld commented 6 years ago

Tree-sitter is a different kind of tool than bison or jison. It's not designed to run arbitrary code that produces a custom AST. Instead, it has a built-in type of AST that works the same for every supported language. This is part of what makes incremental parsing possible, and it's really useful for applications that work with code written in multiple languages. That said, it's not suitable for all parsing tasks.

What kind of code analysis are you trying to do with it?

wongjiahau commented 6 years ago

Type checking. Anyway, I figured out how to render the built-in AST into JSON.

maxbrunsfeld commented 6 years ago

Ok cool. I'm going to close this out. Let me know if you have other questions.