pddl-workspace
Version:
119 lines • 5.55 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.VariablesParser = exports.parseVariableDeclaration = exports.parseParameters = void 0;
const language_1 = require("../language");
const PddlTokenizer_1 = require("./PddlTokenizer");
const DocumentPositionResolver_1 = require("../DocumentPositionResolver");
const util_1 = require("../utils/util");
function parseParameters(fullSymbolName) {
const parameterPattern = /((\?[\w][\w-]*\s+)+)-\s+([\w][\w-]*)/g;
const parameters = [];
let group;
while (group = parameterPattern.exec(fullSymbolName)) {
const variables = group[1];
const type = group[3];
variables.split(/(\s+)/)
.filter(term => term.trim().length)
.map(variable => variable.substr(1).trim()) // skip the question-mark
.forEach(variable => parameters.push(new language_1.Parameter(variable, type)));
}
return parameters;
}
exports.parseParameters = parseParameters;
function parseVariableDeclaration(fullName) {
const parameters = parseParameters(fullName);
return new language_1.Variable(fullName, parameters);
}
exports.parseVariableDeclaration = parseVariableDeclaration;
/** Parses the `:predicates` and `:functions` section. */
class VariablesParser {
constructor(predicatesNode, positionResolver) {
this.positionResolver = positionResolver;
this.variables = new Array();
this.chunks = new Array();
this.currentVariableNodes = new Array();
this.variableNodeEncountered = false;
this.consecutiveVerticalWhitespaceCount = 0;
// first split the list of children to chunks describing one variable
this.chunkByVerticalWhitespace(predicatesNode);
this.variables = util_1.Util.flatMap(this.chunks.map(chunk => this.processChunk(chunk)));
}
chunkByVerticalWhitespace(predicatesNode) {
for (const node of predicatesNode.getNestedChildren()) {
if (node.getToken().type === PddlTokenizer_1.PddlTokenType.Whitespace) {
const verticalWhitespaceCount = node.getText().split(/\r?\n/).length - 1;
// did we encountered end of the line AFTER the variable declaration?
if (verticalWhitespaceCount > 0 && this.variableNodeEncountered) {
// this is the end of one variable declaration
this.addCurrentVariableChunkAndReset();
}
this.consecutiveVerticalWhitespaceCount += verticalWhitespaceCount;
if (this.consecutiveVerticalWhitespaceCount >= 2) {
// empty line encountered, reset
this.reset();
}
}
else {
// reset the EOL counter as this is not a vertical whitespace
this.consecutiveVerticalWhitespaceCount = 0;
if ((0, PddlTokenizer_1.isOpenBracket)(node.getToken())) {
this.variableNodeEncountered = true;
}
this.currentVariableNodes.push(node);
}
}
// push the last chunk
if (this.currentVariableNodes.length) {
this.addCurrentVariableChunkAndReset();
}
}
processChunk(chunk) {
const documentation = new Array();
const variableNodes = [];
for (const node of chunk) {
if (node.isType(PddlTokenizer_1.PddlTokenType.Comment)) {
const indexOfSemicolon = node.getText().indexOf(';');
if (indexOfSemicolon > -1) {
const textAfterSemicolon = node.getText().substr(indexOfSemicolon + 1).trim();
documentation.push(textAfterSemicolon);
}
}
else if ((0, PddlTokenizer_1.isOpenBracket)(node.getToken())) {
variableNodes.push(node);
}
}
return variableNodes.map(node => this.createVariable(node, documentation));
}
createVariable(node, documentation) {
const fullSymbolName = node.getText().replace(/[\(\)]/g, '');
const variable = parseVariableDeclaration(fullSymbolName);
variable.setDocumentation(documentation);
const startPosition = this.positionResolver.resolveToPosition(node.getStart());
const endPosition = this.positionResolver.resolveToPosition(node.getEnd());
variable.setLocation(new DocumentPositionResolver_1.PddlRange({ start: startPosition, end: endPosition }));
return variable;
}
addCurrentVariableChunkAndReset() {
this.chunks.push(this.currentVariableNodes);
this.reset();
}
/** Resets the current variable chunk */
reset() {
this.currentVariableNodes = [];
this.variableNodeEncountered = false;
this.consecutiveVerticalWhitespaceCount = 0;
}
static isVerticalWhitespace(node) {
return node.getToken().type === PddlTokenizer_1.PddlTokenType.Whitespace
&& /[\r\n]/.test(node.getToken().tokenText);
}
getVariables() {
return this.variables;
}
}
exports.VariablesParser = VariablesParser;
//# sourceMappingURL=VariablesParser.js.map