Something went wrong. Try again.
A parser and stringifier for the KDL Document Language bram.tngl.io/kdl
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412import {InvalidKdlQueryError} from "../error.js";import {Identifier} from "../model.js";import {Accessor} from "../model/query/accessor.js";import {Comparison} from "../model/query/comparison.js";import {Filter} from "../model/query/filter.js";import {Matcher} from "../model/query/matcher.js";import {Query, Selector} from "../model/query/query.js";
import { consume, parseIdentifier as _parseIdentifier, parseNodeSpace, parseTag, parseValue, pop,} from "./parse.js";import { T_BOM, T_CLOSE_PAREN, T_CLOSE_SQUARE, T_NUMBER_DECIMAL, T_OPEN_PAREN, T_OPEN_SQUARE, T_QUERY_OPERATOR,} from "./tokenize.js";
/** @import { SelectorOperator } from '../model/query/query.js'; */
/** @import { ParserCtx } from "./parse.js"; *//** @import { Token } from "./token.js"; */
/** * @param {ParserCtx | Token} ctx * @param {string} message */export function mkError(ctx, message) { return new InvalidKdlQueryError(message, { token: "current" in ctx ? (ctx.current.value ?? ctx.lastToken) : ctx, });}
/** @param {ParserCtx} ctx */function parseIdentifier(ctx) { if (ctx.current.value?.type !== T_QUERY_OPERATOR) { return _parseIdentifier(ctx); }
// Certain operators can be identifiers: const text = ctx.current.value.text; if ( text === "||" || text === ">" || text === ">>" || text === "<" || text === "<<" ) { pop(ctx);
let identifier = _parseIdentifier(ctx);
if (identifier) { identifier.name = text + identifier.name; identifier.representation = text + identifier.representation; } else { identifier = new Identifier(text); identifier.representation = text; }
return identifier; }
return;}
/** @param {ParserCtx} ctx */function parseInteger(ctx) { const token = consume(ctx, T_NUMBER_DECIMAL); if (!token) { return; }
const text = token.text; if (text[0] === "-" || text[0] === "+") { throw mkError(token, "Integers cannot have a sign"); }
if (text.includes(".")) { throw mkError( token, "Expected to see an integer but got a decimal instead", ); }
if (text.includes("e") || text.includes("E")) { throw mkError(token, "Integers cannot have an exponent"); }
return Number.parseInt(text.replaceAll("_", ""));}
/** @param {ParserCtx} ctx */function parseAccessor(ctx) { const name = parseIdentifier(ctx)?.name; if (name == null) { return; }
if (!consume(ctx, T_OPEN_PAREN)) { return Accessor.property(name); }
parseNodeSpace(ctx);
let accessor;
switch (name) { case "name": accessor = Accessor.name(); break; case "tag": accessor = Accessor.tag(); break; case "val": { const index = parseInteger(ctx); if (index == null) { throw mkError( ctx.lastToken, "The val() accessor requires an index parameter", ); }
accessor = Accessor.argument(index); break; } case "prop": { const prop = parseIdentifier(ctx); if (!prop) { throw mkError(ctx, "The prop() accessor requires a property name"); }
accessor = Accessor.property(prop.name); break; } default: throw mkError(ctx, `Invalid accessor ${name}()`); }
parseNodeSpace(ctx);
if (!consume(ctx, T_CLOSE_PAREN)) { throw mkError(ctx, "Expected closing parenthesis"); }
return accessor;}
/** @param {ParserCtx} ctx */function parseComparison(ctx) { const operator = consume(ctx, T_QUERY_OPERATOR)?.text; if (!operator) { return; }
if (!parseNodeSpace(ctx)) { throw mkError(ctx, "Missing whitespace after comparison operator"); }
const value = parseValue(ctx) ?? parseTag(ctx);
switch (operator) { case "=": case "!=": case ">": case "<": case ">=": case "<=": case "^=": case "$=": case "*=": { if (!value) { throw mkError( ctx, `Operator ${operator} requires a right hand side value`, ); }
return Comparison.create(operator, value); } default: throw mkError(ctx, `Operator ${operator} cannot be used in a comparison`); }}
/** @param {ParserCtx} ctx */function parseAccessorMatcher(ctx) { if (!consume(ctx, T_OPEN_SQUARE)) { return; }
parseNodeSpace(ctx);
const accessor = parseAccessor(ctx); if (!accessor) { if (!consume(ctx, T_CLOSE_SQUARE)) { throw mkError(ctx, "Expected an accessor"); }
return Matcher.always(); }
let comparison; if (parseNodeSpace(ctx)) { comparison = parseComparison(ctx); parseNodeSpace(ctx); } else { comparison = null; }
if (!consume(ctx, T_CLOSE_SQUARE)) { if (!comparison && ctx.current.value?.type === T_QUERY_OPERATOR) { throw mkError(ctx, "Missing whitespace before comparison operator"); }
throw mkError(ctx, "Expected ]"); }
return Matcher.accessor(accessor, comparison ?? null);}
/** @param {ParserCtx} ctx */function parseTypeMatcher(ctx) { if (!consume(ctx, T_OPEN_PAREN)) { return; }
parseNodeSpace(ctx);
const tag = parseIdentifier(ctx)?.name ?? null;
parseNodeSpace(ctx);
if (!consume(ctx, T_CLOSE_PAREN)) { throw mkError(ctx, "Invalid tag"); }
return Matcher.type(tag);}
/** @param {ParserCtx} ctx */function parseNodeNameMatcher(ctx) { const name = parseIdentifier(ctx)?.name; if (name == null) { return; }
return Matcher.nodeName(name);}
/** @param {ParserCtx} ctx */function parseFilter(ctx) { const typeMatcher = parseTypeMatcher(ctx); const nameMatcher = parseNodeNameMatcher(ctx); const accessorMatcher = parseAccessorMatcher(ctx);
/** @type {[Matcher, ...Matcher[]]} */ let matchers;
if (typeMatcher) { matchers = [typeMatcher]; nameMatcher && matchers.push(nameMatcher); accessorMatcher && matchers.push(accessorMatcher); } else if (nameMatcher) { matchers = [nameMatcher]; accessorMatcher && matchers.push(accessorMatcher); } else if (accessorMatcher) { matchers = [accessorMatcher]; } else { return; }
let matcher; while ((matcher = parseAccessorMatcher(ctx))) { matchers.push(matcher); }
return new Filter(matchers);}
/** * @param {ParserCtx} ctx * @returns {SelectorOperator=} */function parseSelectorOperator(ctx) { if (ctx.current.done || ctx.current.value.type !== T_QUERY_OPERATOR) { return; }
const {text} = ctx.current.value; switch (text) { case ">": case ">>": case "+": case "++": pop(ctx); return text; }}
/** @param {ParserCtx} ctx */function parseSelector(ctx) { const identifier = parseIdentifier(ctx);
/** @type {[SelectorOperator, Filter][]} */ const filters = [];
/** @type {SelectorOperator=} */ let nextOperator = ">>";
if (identifier) { if (identifier.name === "top" && consume(ctx, T_OPEN_PAREN)) { parseNodeSpace(ctx); if (!consume(ctx, T_CLOSE_PAREN)) { throw mkError(ctx, "top() does not take parameters"); } } else { // Oops, we've accidentally parsed part of the first filter already
/** @type {[Matcher, ...Matcher[]]} */ let matchers = [Matcher.nodeName(identifier.name)];
let matcher; while ((matcher = parseAccessorMatcher(ctx))) { matchers.push(matcher); }
filters.push([">>", new Filter(matchers)]); }
parseNodeSpace(ctx); const op = parseSelectorOperator(ctx); parseNodeSpace(ctx);
if (!op) { if (filters.length) { return new Selector( /** @type {[[SelectorOperator, Filter], ...[SelectorOperator, Filter][]]} */ ( filters ), ); }
throw mkError(ctx, "top() must be followed by an operator"); }
nextOperator = op; }
while (nextOperator) { const filter = parseFilter(ctx); if (!filter) { throw mkError(ctx, "Expected a filter"); }
filters.push([nextOperator, filter]);
parseNodeSpace(ctx); nextOperator = parseSelectorOperator(ctx); parseNodeSpace(ctx); }
return filters.length ? new Selector( /** @type {[[SelectorOperator, Filter], ...[SelectorOperator, Filter][]]} */ ( filters ), ) : undefined;}
/** @param {ParserCtx} ctx */export function parseQuery(ctx) { consume(ctx, T_BOM);
const selector = parseSelector(ctx); if (!selector) { return; }
const selectors = [selector];
parseNodeSpace(ctx);
let separator; while ((separator = consume(ctx, T_QUERY_OPERATOR))) { if (separator.text !== "||") { throw mkError(ctx, `Invalid operator ${separator.text}`); }
parseNodeSpace(ctx);
const selector = parseSelector(ctx); if (!selector) { throw mkError(ctx, `Expected a selector after ||`); }
selectors.push(selector); parseNodeSpace(ctx); }
return new Query(selectors);}