1
0
silverbullet/plugs/directive/parser.ts

123 lines
3.7 KiB
TypeScript
Raw Normal View History

2022-07-06 10:18:47 +00:00
import {
collectNodesOfType,
findNodeOfType,
ParseTree,
2022-07-06 10:18:47 +00:00
replaceNodesMatching,
2022-10-14 13:11:33 +00:00
} from "$sb/lib/tree.ts";
2022-07-06 10:18:47 +00:00
// @ts-ignore auto generated
2022-10-14 13:11:33 +00:00
import { ParsedQuery, QueryFilter } from "$sb/lib/query.ts";
2022-07-06 10:18:47 +00:00
export function parseQuery(queryTree: ParseTree): ParsedQuery {
// const n = lezerToParseTree(query, parser.parse(query).topNode);
2022-07-06 10:18:47 +00:00
// Clean the tree a bit
replaceNodesMatching(queryTree, (n) => {
2022-07-06 10:18:47 +00:00
if (!n.type) {
2022-10-14 13:11:33 +00:00
const trimmed = n.text!.trim();
2022-07-06 10:18:47 +00:00
if (!trimmed) {
return null;
}
n.text = trimmed;
}
});
// console.log("Parsed", JSON.stringify(n, null, 2));
const queryNode = queryTree.children![0];
2022-10-14 13:11:33 +00:00
const parsedQuery: ParsedQuery = {
2022-07-06 10:18:47 +00:00
table: queryNode.children![0].children![0].text!,
filter: [],
ordering: [],
2022-07-06 10:18:47 +00:00
};
const orderByNodes = collectNodesOfType(queryNode, "OrderClause");
for (const orderByNode of orderByNodes) {
2022-10-14 13:11:33 +00:00
const nameNode = findNodeOfType(orderByNode, "Name");
const orderBy = nameNode!.children![0].text!;
const orderNode = findNodeOfType(orderByNode, "OrderDirection");
const orderDesc = orderNode
2022-07-06 10:18:47 +00:00
? orderNode.children![0].text! === "desc"
: false;
parsedQuery.ordering.push({ orderBy, orderDesc });
}
/**
* @deprecated due to PR #387
* We'll take the first ordering and send that as the deprecated
* fields orderBy and orderDesc. This way it will be backward
* Plugs using the old ParsedQuery.
* Remove this block completely when ParsedQuery no longer have
* those two fields
*/
if (parsedQuery.ordering.length > 0) {
parsedQuery.orderBy = parsedQuery.ordering[0].orderBy;
parsedQuery.orderDesc = parsedQuery.ordering[0].orderDesc;
2022-07-06 10:18:47 +00:00
}
/** @end-deprecation due to PR #387 */
2022-10-14 13:11:33 +00:00
const limitNode = findNodeOfType(queryNode, "LimitClause");
2022-07-06 10:18:47 +00:00
if (limitNode) {
2022-10-14 13:11:33 +00:00
const nameNode = findNodeOfType(limitNode, "Number");
2022-07-06 10:18:47 +00:00
parsedQuery.limit = valueNodeToVal(nameNode!);
}
2022-10-14 13:11:33 +00:00
const filterNodes = collectNodesOfType(queryNode, "FilterExpr");
for (const filterNode of filterNodes) {
2022-07-06 10:18:47 +00:00
let val: any = undefined;
2022-10-14 13:11:33 +00:00
const valNode = filterNode.children![2].children![0];
2022-07-06 10:18:47 +00:00
val = valueNodeToVal(valNode);
2022-10-14 13:11:33 +00:00
const f: QueryFilter = {
2022-07-06 10:18:47 +00:00
prop: filterNode.children![0].children![0].text!,
op: filterNode.children![1].text!,
value: val,
};
parsedQuery.filter.push(f);
}
2022-10-14 13:11:33 +00:00
const selectNode = findNodeOfType(queryNode, "SelectClause");
2022-07-06 10:18:47 +00:00
if (selectNode) {
parsedQuery.select = [];
collectNodesOfType(selectNode, "Name").forEach((t) => {
parsedQuery.select!.push(t.children![0].text!);
});
}
2022-10-14 13:11:33 +00:00
const renderNode = findNodeOfType(queryNode, "RenderClause");
2022-07-06 10:18:47 +00:00
if (renderNode) {
2022-08-09 13:37:47 +00:00
let renderNameNode = findNodeOfType(renderNode, "PageRef");
if (!renderNameNode) {
renderNameNode = findNodeOfType(renderNode, "String");
}
2022-07-06 10:18:47 +00:00
parsedQuery.render = valueNodeToVal(renderNameNode!);
}
return parsedQuery;
}
export function valueNodeToVal(valNode: ParseTree): any {
switch (valNode.type) {
case "Number":
return +valNode.children![0].text!;
case "Bool":
return valNode.children![0].text! === "true";
case "Null":
return null;
case "Name":
return valNode.children![0].text!;
case "Regex": {
const val = valNode.children![0].text!;
return val.substring(1, val.length - 1);
}
case "String": {
const stringVal = valNode.children![0].text!;
return stringVal.substring(1, stringVal.length - 1);
}
case "PageRef": {
const pageRefVal = valNode.children![0].text!;
return pageRefVal.substring(2, pageRefVal.length - 2);
}
case "List": {
return collectNodesOfType(valNode, "Value").map((t) =>
valueNodeToVal(t.children![0])
);
}
}
}