Something went wrong. Try again.
A parser and stringifier for the KDL Document Language bram.tngl.io/kdl
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571import {Document} from "./document.js";import {Entry} from "./entry.js";import {Identifier} from "./identifier.js";import {Tag} from "./tag.js";import {reverseIterate} from "./utils.js";import {Value} from "./value.js";
/** * @param {Node} node */function getOrCreateDocument(node) { return node.children ?? (node.children = new Document());}
/** * A node is a node name, followed by zero or more arguments and/or properties, and children */export class Node { /** * Create a new node with the given name * * @param {string} name * @returns {Node} */ static create(name) { return new Node(new Identifier(name)); }
/** * @readonly * @hidden */ type = "node";
/** * @readonly * @hidden */ static type = "node";
/** * The name (also known as "tag name") of this node * * @type {Identifier} */ name;
/** * Tag attached to this node, if any * * @type {Tag | null} */ tag = null;
/** * Entries of the node * * @type {Entry[]} */ entries;
/** * Children of the node * * An empty array means the children block is present but empty, * if the value is `null` then there is no children block. * * @type {Document | null} */ children;
/** * Leading whitespace * * @type {string=} * @hidden */ leading;
/** * Trailing whitespace * * @type {string=} * @hidden */ trailing;
/** * Whitespace between the last entry and the children * * @type {string=} * @hidden */ beforeChildren;
/** * Whitespace between the tag and the node name * * @type {string=} * @hidden */ betweenTagAndName;
/** * @param {Identifier} name * @param {Entry[]} [entries] * @param {Document | null} [children] */ constructor(name, entries = [], children = null) { this.name = name; this.entries = entries; this.children = children; }
/** * Create an identical copy of this node * * @param {object} [options] * @param {boolean} [options.shallow] If true, only copy this node but don't clone this node's children. * @returns {Node} */ clone({shallow = false} = {}) { const clone = new Node( this.name.clone(), shallow ? [] : this.entries.map((entry) => entry.clone()), this.children?.clone({shallow}), ); if (this.tag) { clone.tag = this.tag.clone(); }
clone.leading = this.leading; clone.betweenTagAndName = this.betweenTagAndName; clone.beforeChildren = this.beforeChildren; clone.trailing = this.trailing;
return clone; }
/** * Return the tag of this node, if any * * @returns {string | null} */ getTag() { return this.tag ? this.tag.name : null; }
/** * Set the tag of this node to the given tag * * @param {string | null | undefined} tag */ setTag(tag) { this.tag = tag != null ? new Tag(tag) : null; }
/** * Return the name of this node * * @returns {string} */ getName() { return this.name.name; }
/** * Set the name of this node to the given name * * @param {string} name */ setName(name) { this.name.setName(name); }
/** * Return whether this node has arguments * * @returns {boolean} */ hasArguments() { return this.entries.some((entry) => entry.isArgument()); }
/** * Return a snapshot of all arguments of this node * * Changes to the returned array are not reflected back onto this node * itself, and updates to the node won't reflect in the returned array. * * @returns {Value['value'][]} */ getArguments() { return this.getArgumentEntries().map((entry) => entry.getValue()); }
/** * Return a snapshot of all arguments of this node * * Changes to the returned array are not reflected back onto this node * itself, and updates to the node won't reflect in the returned array. * * @returns {Entry[]} */ getArgumentEntries() { return this.entries.filter((entry) => entry.isArgument()); }
/** * Return the value at the given index, if present * * This index counts towards the arguments only, i.e. if the node has five * entries, three of which are arguments then passing `1` returns the second * argument, regardless of the whether properties and arguments are * interspersed or not. * * @param {number} index * @returns {boolean} */ hasArgument(index) { return this.getArgumentEntry(index) != null; }
/** * Return the argument at the given index, if present * * This index counts towards the arguments only, i.e. if the node has five * entries, three of which are arguments then passing `1` returns the second * argument, regardless of the whether properties and arguments are * interspersed or not. * * @param {number} index * @returns {Value['value'] | undefined} */ getArgument(index) { return this.getArgumentEntry(index)?.getValue(); }
/** * Return the argument entry at the given index, if present * * This index counts towards the arguments only, i.e. if the node has five * entries, three of which are arguments then passing `1` returns the second * argument, regardless of the whether properties and arguments are * interspersed or not. * * @param {number} index * @returns {Entry | undefined} */ getArgumentEntry(index) { for (const entry of this.entries) { if (!entry.isArgument()) { continue; }
if (index === 0) { return entry; }
index--; } }
/** * Add the given value as argument to this node * * The argument is added at the given index, or at the end. * This index counts towards the arguments only, i.e. if the node has five * entries, three of which are arguments then inserting an argument between * the second and third can be achieved by passing `2` regardless of the * whether properties and arguments are interspersed or not. * * @param {Value['value']} value The value to insert as argument * @param {string | null} [tag] The tag to attach to the argument, if any * @param {number} [index] The index */ addArgument(value, tag, index) { const entry = Entry.createArgument(value); entry.value.setTag(tag);
if (index != null) { for (const [i, existingEntry] of this.entries.entries()) { if (!existingEntry.isArgument()) { continue; }
if (index === 0) { this.entries.splice(i, 0, entry); return; }
index--; } }
this.entries.push(entry); }
/** * Remove the argument at the given index * * The index counts towards the arguments only, i.e. if the node has five * entries, three of which are arguments then the last argument can be * removed by passing `2`, regardless of whether the third argument is also * the third entry. * * @param {number} index */ removeArgument(index) { for (const [i, entry] of this.entries.entries()) { if (!entry.isArgument()) { continue; }
if (index === 0) { this.entries.splice(i, 1); return; }
index--; } }
/** * Return whether this node has properties * * @returns {boolean} */ hasProperties() { return this.entries.some((entry) => entry.isProperty()); }
/** * Return a snapshot of all properties of this node * * Changes to the returned object are not reflected back onto this node * itself, and updates to the node won't reflect in the returned object. * * @returns {Map<string, Value['value']>} */ getProperties() { return new Map( this.getPropertyEntries().map((entry) => [ /** @type {string} */ (entry.getName()), entry.getValue(), ]), ); }
/** * Return a snapshot of all properties of this node * * Changes to the returned array are not reflected back onto this node * itself, and updates to the node won't reflect in the returned array. * * @returns {Entry[]} */ getPropertyEntries() { return this.entries.filter((entry) => entry.isProperty()); }
/** * Return a snapshot of all properties of this node * * Changes to the returned map are not reflected back onto this node * itself, and updates to the node won't reflect in the returned map. * * @returns {Map<string, Entry>} */ getPropertyEntryMap() { return new Map( this.getPropertyEntries().map((entry) => [ /** @type {string} */ (entry.getName()), entry, ]), ); }
/** * Return whether this node has the given property * * @param {string} name * @returns {boolean} */ hasProperty(name) { return this.getPropertyEntry(name) != null; }
/** * Return the value of the property with the given name, or undefined * if it doesn't exist. * * @param {string} name * @returns {Value['value'] | undefined} */ getProperty(name) { return this.getPropertyEntry(name)?.getValue(); }
/** * Return the property entry with the given name, or undefined if it doesn't * exist. * * @param {string} name * @returns {Entry | undefined} */ getPropertyEntry(name) { for (const entry of reverseIterate(this.entries)) { if (entry.getName() === name) { return entry; } }
return undefined; }
/** * Set the given property on this node * * This function updates the property entry with the given name, * if it exists. * * @param {string} name * @param {Value['value']} value * @param {string | null} [tag] */ setProperty(name, value, tag) { for (const entry of reverseIterate(this.entries)) { if (entry.getName() === name) { entry.setValue(value); entry.value.setTag(tag); return; } }
const entry = Entry.createProperty(name, value); entry.value.setTag(tag); this.entries.push(entry); }
/** * Delete the property with the given name * * @param {string} name */ deleteProperty(name) { this.entries = this.entries.filter((entry) => entry.getName() !== name); }
/** * Return whether this node has child nodes * * @returns {boolean} */ hasChildren() { return this.children != null && !this.children.isEmpty(); }
/** * Add the given node at the end of this node's children * * @param {Node | Document} node */ appendNode(node) { getOrCreateDocument(this).appendNode(node); }
/** * Insert the given node to the node's children before the referenceNode, or at the end if no reference is passed * * @param {Node | Document} newNode * @param {Node | null} referenceNode * @throws {Error} If the given referenceNode is not part of this node's children */ insertNodeBefore(newNode, referenceNode) { getOrCreateDocument(this).insertNodeBefore(newNode, referenceNode); }
/** * Insert the given node to the node's children after the referenceNode, or at the beginning if no reference is passed * * @param {Node | Document} newNode * @param {Node | null} referenceNode * @throws {Error} If the given referenceNode is not part of this document */ insertNodeAfter(newNode, referenceNode) { getOrCreateDocument(this).insertNodeAfter(newNode, referenceNode); }
/** * Remove the given node from this node's children * * @param {Node} node * @throws {Error} if the given node is not in this node's children */ removeNode(node) { if (this.children == null) { throw new Error("Node to remove is not in document"); }
this.children.removeNode(node); }
/** * Replace the old node with the new node in this node's children * * @param {Node} oldNode * @param {Node | Document} newNode * @throws {Error} if the oldNode is not in this node's children */ replaceNode(oldNode, newNode) { if (this.children == null) { throw new Error("Node to replace is not in document"); }
this.children.replaceNode(oldNode, newNode); }
/** * Return all nodes with the given node name * * Changes to the returned array are not reflected back onto this document * itself, and updates to the document won't reflect in the returned array. * * @param {string} name * @returns {Node[]} */ findNodesByName(name) { return this.children != null ? this.children.findNodesByName(name) : []; }
/** * Return the last node in this node's children with the given name * * This function returns the last node instead of first to be in line with * how properties are defined in the KDL specification where the last * property with the given name is used and the rest is shadowed. * * @param {string} name * @returns {Node | undefined} */ findNodeByName(name) { return this.children?.findNodeByName(name); }
/** * Return the last node in this node's children with the given name, matching the parameter * * If the parameter is `undefined`, this method looks for a node with any single * arguments. If a parameter is passed, this method looks for a node with * a single parameter, equal to the given parameter. * * @param {string} name * @param {Value['value']} [parameter] * @returns {Node | undefined} */ findParameterizedNode(name, parameter) { return this.children?.findParameterizedNode(name, parameter); }
/** * Remove all nodes with the given name from this document * * @param {string} name * @returns {void} */ removeNodesByName(name) { this.children?.removeNodesByName(name); }}