Something went wrong. Try again.
A parser and stringifier for the KDL Document Language bram.tngl.io/kdl
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289import {InvalidKdlError} from "../../error.js";
import {isInvalidCharacter, isNewLine, T_BOM} from "./types.js";
/** @import {Location, Token} from "../token.js" */
/** @type {Intl.Segmenter=} */let segmenter;
/** @param {string} text */function* iterateGraphemes(text) { // We don't have to pass in any locale(s), but then the segmenter would // default to the configured locale(s) in node or the OS, and do we want to // yield different behaviour based on Node / OS settings? Let's go with no for (const segment of (segmenter ??= new Intl.Segmenter("en")).segment( text, )) { yield segment.segment; }}
/** * @typedef {object} TokenizeContext * @prop {string} text * @prop {number} line * @prop {number} column * @prop {number} offset * @prop {number} length * @prop {Iterator<string, void, any> | null} iterator * @prop {{done?: boolean; value?: string | void}} currentIter * @prop {number} current * @prop {Location} start * @prop {boolean} graphemeLocations * @prop {InvalidKdlError[] | null} errorsInToken * @prop {import('../../flags.js').ParserFlags} flags */
/** * @param {string} text * @param {object} opts * @param {boolean} [opts.graphemeLocations] * @param {import('../../flags.js').ParserFlags} opts.flags * @returns {TokenizeContext} */export function createContext(text, {flags, graphemeLocations = false}) { /** @type {TokenizeContext["iterator"]} */ let iterator; /** @type {TokenizeContext["currentIter"]} */ let currentIter; /** @type {TokenizeContext["current"]} */ let current; const {length} = text;
if (graphemeLocations) { iterator = iterateGraphemes(text); const ci = (currentIter = iterator.next()); current = ci.done ? NaN : /** @type {number} */ (ci.value.codePointAt(0)); } else { iterator = null; currentIter = {done: length === 0}; current = text.codePointAt(0) ?? NaN; }
return { text, graphemeLocations,
line: 1, column: 1, offset: 0,
length,
iterator, currentIter,
/** * The first code point of the iterator's last result, or NaN if the iterator has ended * * While the last result can consist of multiple code points, we can limit * ourselves to only looking at the first code point: * * - We don't have special handling for any grapheme that contains multiple * code points, those are all either string content or part of an identifier * - "\r\n" is the only exception, which we want to count as a single newline, * and by looking at the first code point we can easily handle that. */ current,
start: {line: 1, column: 1, offset: 0},
errorsInToken: null,
flags, };}
/** * @param {TokenizeContext} ctx * @returns {Generator<Token, void>} */export function* init(ctx) { if (consumeCodePoint(ctx, 0xfeff)) { // Byte-Order Mark // don't let BOM count as column ctx.column = 1; yield mkToken(ctx, T_BOM, null); }
if (isInvalidCharacter(ctx.current)) { throw mkError(ctx, `Invalid character \\u${ctx.current.toString(16)}`); }}
/** * @param {TokenizeContext} ctx */export function pop(ctx) { ctx.offset += ctx.currentIter.value?.length ?? (ctx.current < 0x10000 ? 1 : 2); ctx.column++;
let current; if (ctx.iterator == null) { current = ctx.text.codePointAt(ctx.offset); if (current == null) { current = NaN; ctx.currentIter.done = true; } } else { const currentIter = (ctx.currentIter = ctx.iterator.next()); current = currentIter.value?.codePointAt(0) ?? NaN; }
ctx.current = current;
if (isInvalidCharacter(current)) { if ((current >= 0xd800 && current <= 0xdfff) || current > 0x10ffff) { // Non-scalar value, cannot be represented whatsoever pushError(ctx, `Invalid character \\u${current.toString(16)}`); } else { pushError( ctx, `Invalid character \\u${current.toString(16)}, this character is not allowed but can be included in strings as \\u{${current.toString(16)}}`, ); } }
return current;}
/** * @param {TokenizeContext} ctx * @param {(codePoint: number) => boolean} test */export function consume(ctx, test) { if (test(ctx.current)) { const previous = ctx.current; pop(ctx); return previous; }}
/** * Consume the current code point if it matches the given code point * * @param {TokenizeContext} ctx * @param {number} codePoint */export function consumeCodePoint(ctx, codePoint) { if (ctx.current === codePoint) { pop(ctx); return codePoint; }}
/** * @param {TokenizeContext} ctx */export function consumeNewline(ctx) { if (!isNewLine(ctx.current)) { return false; }
// consume \r\n as a single newline if ( !ctx.graphemeLocations && ctx.current === 0x0d && ctx.text.codePointAt(ctx.offset + 1) === 0x0a ) { ctx.iterator?.next(); ctx.offset++; }
pop(ctx);
ctx.column = 1; ctx.line++;
return true;}
/** * @param {TokenizeContext} ctx * @param {number} codePoint */export function zerOrMoreCodePoint(ctx, codePoint) { while (ctx.current === codePoint) { pop(ctx); }}
/** * @param {TokenizeContext} ctx * @param {(codePoint: number) => boolean} test */export function zerOrMore(ctx, test) { while (test(ctx.current)) { pop(ctx); }}
/** * @param {TokenizeContext} ctx * @param {number} type * @param {string | null} error * @returns {Token} */export function mkToken(ctx, type, error) { const {line, column, offset} = ctx; const end = {line, column, offset}; const s = ctx.start;
ctx.start = end;
const errors = ctx.errorsInToken; ctx.errorsInToken = null;
/** @type {Token} */ const token = { type, text: ctx.text.slice(s.offset, end.offset), start: s, end, errors, };
if (error) { if (!token.errors) { token.errors = []; } token.errors.push(new InvalidKdlError(error, {token})); }
return token;}
/** * Create an error based on the current token and location * * @param {TokenizeContext} ctx * @param {string} message */export function mkError(ctx, message) { const {line, column, offset} = ctx; const start = {line, column, offset}; let end;
if (offset < ctx.length) { if (isNewLine(ctx.current)) { end = {line: line + 1, column: 1, offset: offset + 1}; } else { end = {line, column: column + 1, offset: offset + 1}; } }
return new InvalidKdlError(`${message}`, {start, end});}
/** * Create an error based on the current token and location * * @param {TokenizeContext} ctx * @param {string} message */export function pushError(ctx, message) { (ctx.errorsInToken ??= []).push(mkError(ctx, message));}