2022-11-01 14:01:28 +00:00
|
|
|
import type { ParseTree } from "$sb/lib/tree.ts";
|
|
|
|
import type { Language, SyntaxNode } from "./deps.ts";
|
2022-10-17 13:48:21 +00:00
|
|
|
|
2022-04-11 18:34:09 +00:00
|
|
|
export function lezerToParseTree(
|
|
|
|
text: string,
|
|
|
|
n: SyntaxNode,
|
2022-10-10 12:50:21 +00:00
|
|
|
offset = 0,
|
2022-04-11 18:34:09 +00:00
|
|
|
): ParseTree {
|
|
|
|
let children: ParseTree[] = [];
|
|
|
|
let nodeText: string | undefined;
|
|
|
|
let child = n.firstChild;
|
|
|
|
while (child) {
|
|
|
|
children.push(lezerToParseTree(text, child));
|
|
|
|
child = child.nextSibling;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (children.length === 0) {
|
|
|
|
children = [
|
|
|
|
{
|
|
|
|
from: n.from + offset,
|
|
|
|
to: n.to + offset,
|
|
|
|
text: text.substring(n.from, n.to),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
} else {
|
2022-10-17 13:48:21 +00:00
|
|
|
const newChildren: ParseTree[] = [];
|
2022-04-11 18:34:09 +00:00
|
|
|
let index = n.from;
|
2022-10-17 13:48:21 +00:00
|
|
|
for (const child of children) {
|
|
|
|
const s = text.substring(index, child.from);
|
2022-04-11 18:34:09 +00:00
|
|
|
if (s) {
|
|
|
|
newChildren.push({
|
|
|
|
from: index + offset,
|
|
|
|
to: child.from! + offset,
|
|
|
|
text: s,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
newChildren.push(child);
|
|
|
|
index = child.to!;
|
|
|
|
}
|
2022-10-17 13:48:21 +00:00
|
|
|
const s = text.substring(index, n.to);
|
2022-04-11 18:34:09 +00:00
|
|
|
if (s) {
|
|
|
|
newChildren.push({ from: index + offset, to: n.to + offset, text: s });
|
|
|
|
}
|
|
|
|
children = newChildren;
|
|
|
|
}
|
|
|
|
|
2022-10-17 13:48:21 +00:00
|
|
|
const result: ParseTree = {
|
2022-04-11 18:34:09 +00:00
|
|
|
type: n.name,
|
|
|
|
from: n.from + offset,
|
|
|
|
to: n.to + offset,
|
|
|
|
};
|
|
|
|
if (children.length > 0) {
|
|
|
|
result.children = children;
|
|
|
|
}
|
|
|
|
if (nodeText) {
|
|
|
|
result.text = nodeText;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function parse(language: Language, text: string): ParseTree {
|
2022-10-17 13:48:21 +00:00
|
|
|
const tree = lezerToParseTree(text, language.parser.parse(text).topNode);
|
2022-04-11 18:34:09 +00:00
|
|
|
// replaceNodesMatching(tree, (n): MarkdownTree | undefined | null => {
|
|
|
|
// if (n.type === "FencedCode") {
|
|
|
|
// let infoN = findNodeMatching(n, (n) => n.type === "CodeInfo");
|
|
|
|
// let language = infoN!.children![0].text;
|
|
|
|
// let textN = findNodeMatching(n, (n) => n.type === "CodeText");
|
|
|
|
// let text = textN!.children![0].text!;
|
|
|
|
//
|
|
|
|
// console.log(language, text);
|
|
|
|
// switch (language) {
|
|
|
|
// case "yaml":
|
|
|
|
// let parsed = StreamLanguage.define(yaml).parser.parse(text);
|
|
|
|
// let subTree = treeToAST(text, parsed.topNode, n.from);
|
|
|
|
// // console.log(JSON.stringify(subTree, null, 2));
|
|
|
|
// subTree.type = "yaml";
|
|
|
|
// return subTree;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// return;
|
|
|
|
// });
|
|
|
|
return tree;
|
|
|
|
}
|