pddl-workspace
Version:
52 lines • 2.11 kB
JavaScript
/* --------------------------------------------------------------------------------------------
* Copyright (c) Jan Dolejsi. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PddlSyntaxTree = void 0;
const PddlSyntaxNode_1 = require("./PddlSyntaxNode");
/** Represents a syntax tree of a PDDL document. */
class PddlSyntaxTree {
constructor() {
this.root = PddlSyntaxNode_1.PddlSyntaxNode.createRoot();
}
getRootNode() {
return this.root;
}
/**
* Finds the node at given index in the document text.
* @param symbolIndex index in the document text, where the node is located
*/
getNodeAt(symbolIndex) {
return this.getChildNodeAt(this.root, symbolIndex);
}
getChildNodeAt(parent, symbolIndex) {
if (!parent.includesIndex(symbolIndex)) {
throw new Error(`Index ${symbolIndex} is not included in the scope of ${parent.toString()}.`);
}
if (!parent.hasChildren()) {
return parent;
}
else if (parent.getToken().includesIndex(symbolIndex)) {
return parent;
}
else {
// todo: use binary search among children
const firstMatchingChild = parent.getChildren().find(node => node.includesIndex(symbolIndex));
if (!firstMatchingChild) {
throw new Error("Assertion failed: there should be a child at index: " + symbolIndex);
}
return this.getChildNodeAt(firstMatchingChild, symbolIndex);
}
}
getDefineNode() {
return this.getRootNode().getFirstOpenBracket('define');
}
getDefineNodeOrThrow() {
return this.getRootNode().getFirstOpenBracketOrThrow('define');
}
}
exports.PddlSyntaxTree = PddlSyntaxTree;
PddlSyntaxTree.EMPTY = new PddlSyntaxTree();
//# sourceMappingURL=PddlSyntaxTree.js.map