Updated documentation, added tree-sitter experiment
This commit is contained in:
52
experiments/tree-sitter-1.mjs
Normal file
52
experiments/tree-sitter-1.mjs
Normal file
@@ -0,0 +1,52 @@
|
||||
import Parser from 'tree-sitter';
|
||||
import JavaScript from 'tree-sitter-javascript';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { inspect } from 'node:util';
|
||||
|
||||
// NOTE: once upon a time
|
||||
// there was some sort of
|
||||
//
|
||||
// example comment that we wanted to investigate
|
||||
|
||||
const parser = new Parser();
|
||||
parser.setLanguage(JavaScript);
|
||||
|
||||
class example {
|
||||
|
||||
#private = 123
|
||||
|
||||
/*
|
||||
This other comment
|
||||
is of the style of a
|
||||
block comment of course
|
||||
*/
|
||||
|
||||
stuff() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const source = readFileSync('./tree-sitter-1.mjs', 'utf-8');
|
||||
const tree = parser.parse(source);
|
||||
|
||||
function* iter_nodes(node) {
|
||||
yield node;
|
||||
for (let i = 0; i < node.childCount; i++) {
|
||||
yield* iter_nodes(node.child(i));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function *iter_child_nodes(node) {
|
||||
for (let i = 0; i < node.childCount; i++) {
|
||||
yield node.child(i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (const node of iter_child_nodes(tree.rootNode)) {
|
||||
//console.log({type: node.type, text: node.text?.slice(0, 40)});
|
||||
console.log(inspect(node));
|
||||
console.log({text: node.text});
|
||||
}
|
||||
Reference in New Issue
Block a user