-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Code Search, I] Explore existing syntactic search #1125
Comments
We currently have to prebuild the tree-sitter-r package manually because it is not on npm. To do so, clone the repository and run npm i
npm x -- tree-sitter generate
npm x -- prebuildify --strip --arch x64 --target 20.9.0
npm x -- prebuildify --strip --arch arm64 --target 20.9.0 Then, copy the prebuilt directories from Pre-building WASM binaries is also possible using a similar setup. Sourced from this official GitHub Action. UPDATE: Actually, to build the tree-sitter-r package, it may be worth considering this documentation, specifically this part about running wasm in node.js. It seems like this would spare us from having to make these prebuilds for everything. |
The API seems pretty nice, I made this playground as a quick example: import Parser from 'tree-sitter';
import tree_sitter_r from 'tree-sitter-r';
if(require.main === module) {
const parser = new Parser();
parser.setLanguage(tree_sitter_r);
const sourceCode = `
sum <- 0
product <- 1
w <- 7
N <- 10
for (i in 1:(N-1)) {
sum <- sum + i + w
product <- product * i
}
cat("Sum:", sum, "\n")
cat("Product:", product, "\n")
`;
const tree = parser.parse(sourceCode);
console.log(tree.rootNode.toString());
} with the output being
There are also several functions for querying specific nodes or specific parts of the tree, and printing out the full tree reveals that it has a lot of additional info:
|
Here's a version of the sandbox code using wasm! import Parser from 'web-tree-sitter';
if(require.main === module) {
void doIt();
}
async function doIt() {
await Parser.init();
const parser = new Parser();
parser.setLanguage(await Parser.Language.load(`${__dirname}/tree-sitter-r.wasm`));
const sourceCode = `
sum <- 0
product <- 1
w <- 7
N <- 10
for (i in 1:(N-1)) {
sum <- sum + i + w
product <- product * i
}
cat("Sum:", sum, "\n")
cat("Product:", product, "\n")
`;
const tree = parser.parse(sourceCode);
console.log(tree.rootNode.toString());
} |
We are not the first to search for syntactic structures in the code.
For example ast-grep which relies on tree-sitter to provide a lot of amazing pattern matching capabilities. It offers an r parser r-tree-sitter as well as node.js bindings so there should be a way to use them. From what i can figure out looking at it this should be already capable of doing a lot of the syntactic matching that we are interested in (although finding "all function calls" this way is another question.
So please, just to get started, have a look at the ast-grep system and report on whether you find it suitable for our use-case of querying R code with syntactic patterns.
The text was updated successfully, but these errors were encountered: