pddl-workspace
Version:
128 lines • 5.06 kB
JavaScript
"use strict";
/* --------------------------------------------------------------------------------------------
* 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.PddlSyntaxTreeBuilder = void 0;
const PddlTokenizer_1 = require("./PddlTokenizer");
const PddlSyntaxTree_1 = require("./PddlSyntaxTree");
const PddlSyntaxNode_1 = require("./PddlSyntaxNode");
/** Builds a syntax tree from PDDL syntax tokens. */
class PddlSyntaxTreeBuilder {
constructor(pddlText, symbolIndex) {
this.symbolIndex = symbolIndex;
this.offendingTokens = [];
this.tree = new PddlSyntaxTree_1.PddlSyntaxTree();
this.currentLeaf = this.tree.getRootNode();
// tslint:disable-next-line:no-unused-expression
new PddlTokenizer_1.PddlTokenizer(pddlText, token => this.onToken(token), symbolIndex);
}
getBreadcrumbs(symbolIndex) {
const breadcrumbs = [];
let nodeAtIndex = symbolIndex === undefined ? this.currentLeaf : this.tree.getNodeAt(symbolIndex);
do {
breadcrumbs.push(nodeAtIndex.getToken());
nodeAtIndex = nodeAtIndex.getParent();
} while (nodeAtIndex);
return breadcrumbs.reverse();
}
getTree() {
return this.tree;
}
getOffendingTokens() {
return this.offendingTokens;
}
getTreeAsString() {
return this.getNodeAsString(this.tree.getRootNode());
}
getNodeAsString(node) {
const childrenAsString = node.getChildren().map(c => this.getNodeAsString(c));
return [node.toString()].concat(childrenAsString.map(s => this.indent(s))).join('\n');
}
indent(s) {
return s.split('\n').map(line => " " + line).join('\n');
}
onToken(token) {
if (this.symbolIndex && (token.getStart() > this.symbolIndex)) {
return;
}
switch (token.type) {
case PddlTokenizer_1.PddlTokenType.Keyword:
this.closeKeyword();
this.addChild(token);
break;
case PddlTokenizer_1.PddlTokenType.CloseBracket:
this.closeBracket(token);
break;
default:
if (this.inLeaf()) {
this.closeCurrentSibling();
}
this.addChild(token);
break;
}
}
closeCurrentSibling() {
const parent = this.currentLeaf.getParent();
if (parent) {
this.currentLeaf = parent && parent;
}
else {
throw new Error("Assertion error: closing a leaf node that has no parent.");
}
}
addChild(token) {
const newChild = (0, PddlTokenizer_1.isOpenBracket)(token) ?
new PddlSyntaxNode_1.PddlBracketNode(token, this.currentLeaf) :
new PddlSyntaxNode_1.PddlSyntaxNode(token, this.currentLeaf);
this.currentLeaf.addChild(newChild);
this.currentLeaf = newChild;
}
isInLeafOfType(expectedTypes) {
const actualType = this.currentLeaf.getToken().type;
return expectedTypes.includes(actualType);
}
inLeaf() {
return this.isInLeafOfType([PddlTokenizer_1.PddlTokenType.Comment,
PddlTokenizer_1.PddlTokenType.Other,
PddlTokenizer_1.PddlTokenType.Parameter,
PddlTokenizer_1.PddlTokenType.Dash,
PddlTokenizer_1.PddlTokenType.Whitespace]);
}
closeBracket(closeBracketToken) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const openBracketNode = this.closeSibling(token => (0, PddlTokenizer_1.isOpenBracket)(token), _ => false);
if (openBracketNode) {
openBracketNode.setCloseBracket(closeBracketToken);
}
else {
this.offendingTokens.push(closeBracketToken);
}
}
closeKeyword() {
this.closeSibling(token => token.type === PddlTokenizer_1.PddlTokenType.Keyword, token => (0, PddlTokenizer_1.isOpenBracket)(token));
}
closeSibling(isSibling, isParent) {
// exit out of the other nested token(s)
while (!isSibling(this.currentLeaf.getToken()) && !isParent(this.currentLeaf.getToken())) {
if (this.currentLeaf.getParent() === undefined) {
return undefined;
}
else {
this.closeCurrentSibling();
}
}
// exit out the parent token
if (isSibling(this.currentLeaf.getToken()) && !isParent(this.currentLeaf.getToken())) {
const sibling = this.currentLeaf;
this.closeCurrentSibling();
return sibling;
}
else {
return undefined;
}
}
}
exports.PddlSyntaxTreeBuilder = PddlSyntaxTreeBuilder;
//# sourceMappingURL=PddlSyntaxTreeBuilder.js.map