-
-
Save willcrichton/de595e0820d275468691f2fcc25e0ce7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SitterTree { | |
tree: Parser.Tree | |
node_types: {[key: string]: NodeType} | |
constructor(tree: Parser.Tree) { | |
this.tree = tree; | |
this.node_types = {}; | |
} | |
private node_group(): NodeGroup { | |
const group = new NodeGroup(Object.values(this.node_types).sort((a, b) => a.id - b.id)); | |
return group.extend(styleTags({ | |
'" string': "string", | |
"identifier": "variableName", | |
"assignment": "definition" | |
})); | |
} | |
private build_node(node: Parser.SyntaxNode): number[] { | |
const buffer = _.flatten(node.children.map((child) => this.build_node(child))); | |
if (!this.node_types.hasOwnProperty(node.type)) { | |
this.node_types[node.type] = new NodeType(node.type, {}, Object.keys(this.node_types).length); | |
} | |
const node_type = this.node_types[node.type]; | |
return buffer.concat([node_type.id, node.startIndex, node.endIndex, buffer.length + 4]); | |
} | |
build(): Tree { | |
const buffer = this.build_node(this.tree.rootNode); | |
const group = this.node_group(); | |
return Tree.build({buffer, group}); | |
} | |
} |
Sorry, I don't remember anything about this code. Good luck!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I arrived here from https://discuss.codemirror.net/t/mapping-tree-sitter-trees-to-lezer-trees/2362/5
Is this a general solution for translating any tree-sitter grammar to lezer grammar?
If yes, could you please add a few lines of instructions on how to get started?
Thanks a lot in advance.