52 lines
1021 B
JavaScript
52 lines
1021 B
JavaScript
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});
|
|
} |