pddl-workspace
Version:
96 lines • 4.38 kB
JavaScript
;
/* --------------------------------------------------------------------------------------------
* Copyright (c) Jan Dolejsi 2019. 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.PddlConstraintsParser = void 0;
const constraints_1 = require("../constraints");
const PddlTokenizer_1 = require("./PddlTokenizer");
/* eslint-disable @typescript-eslint/no-non-null-assertion */
class PddlConstraintsParser {
parseConstraints(constraintsNode) {
const children = constraintsNode.getNonWhitespaceChildren().filter(child => (0, PddlTokenizer_1.isOpenBracket)(child.getToken()));
if (children.length === 0) {
return [];
}
if (children[0].isType(PddlTokenizer_1.PddlTokenType.OpenBracketOperator) && children[0].getToken().tokenText === '(and') {
return this.parseConstraints(children[0]);
}
return children
.map(child => this.parseChild(child))
.filter(c => c !== undefined)
.map(c => c);
}
parseChild(node) {
var _a, _b;
const children = node.getNonWhitespaceNonCommentChildren();
if (children.length === 0 && node.getToken().tokenText === '(') {
return new constraints_1.Constraint(node);
}
if (children.length > 0 && children[0].getToken().type === PddlTokenizer_1.PddlTokenType.Other) {
const token = children[0].getToken().tokenText.toLowerCase();
const strictlyAfterToken = 'strictly-after';
switch (token) {
case 'name':
case 'named-condition':
return (_a = this.parseNamedCondition(node, children.slice(1))) !== null && _a !== void 0 ? _a : new constraints_1.Constraint(node);
case strictlyAfterToken:
case 'after':
return (_b = this.parseAfter(node, children.slice(1), token === strictlyAfterToken)) !== null && _b !== void 0 ? _b : new constraints_1.Constraint(node);
default:
return new constraints_1.Constraint(node);
}
}
else {
return new constraints_1.Constraint(node);
}
}
parseNamedCondition(constraintNode, children) {
if (children.length < 2) {
return undefined;
}
if (!children[0].isType(PddlTokenizer_1.PddlTokenType.Other)) {
return undefined;
}
const name = children[0].getText();
if ((0, PddlTokenizer_1.isOpenBracket)(children[1].getToken())) {
const condition = new constraints_1.Condition(children[1]);
return new constraints_1.NamedConditionConstraint({ name: name, condition: condition }, constraintNode);
}
else {
return undefined;
}
}
parseAfter(constraintNode, children, strictly) {
if (children.length < 2) {
return undefined;
}
const predecessor = this.parseAfterGoal(children[0]);
const successor = this.parseAfterGoal(children[1]);
if (!(predecessor || successor)) {
return undefined;
}
if (strictly) {
return new constraints_1.StrictlyAfterConstraint(predecessor, successor, constraintNode);
}
else {
return new constraints_1.AfterConstraint(predecessor, successor, constraintNode);
}
}
parseAfterGoal(nameOrConditionNode) {
if (nameOrConditionNode.isType(PddlTokenizer_1.PddlTokenType.Other)) {
const name = nameOrConditionNode.getText();
return new constraints_1.NamedConditionConstraint({ name: name }, nameOrConditionNode);
}
else if ((0, PddlTokenizer_1.isOpenBracket)(nameOrConditionNode.getToken())) {
const condition = new constraints_1.Condition(nameOrConditionNode);
return new constraints_1.NamedConditionConstraint({ condition: condition }, nameOrConditionNode);
}
else {
return undefined;
}
}
}
exports.PddlConstraintsParser = PddlConstraintsParser;
//# sourceMappingURL=PddlConstraintsParser.js.map