pddl-workspace
Version:
153 lines • 6.42 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.isOpenBracket = exports.PddlTokenizer = exports.PddlToken = exports.TextRange = exports.PddlTokenType = void 0;
var PddlTokenType;
(function (PddlTokenType) {
/** Open bracket with the operator name, e.g. `(+` or `(:action` or `(increase` */
PddlTokenType["OpenBracketOperator"] = "OPEN_BRACKET_OPERATOR";
/** Open bracket `(` */
PddlTokenType["OpenBracket"] = "OPEN_BRACKET";
/** Close bracket `)` */
PddlTokenType["CloseBracket"] = "CLOSE_BRACKET";
/** Keyword e.g. `:parameters` or `:effect` */
PddlTokenType["Keyword"] = "KEYWORD";
/** Dash character. */
PddlTokenType["Dash"] = "DASH";
/** Parameter name e.g. `?p1`. */
PddlTokenType["Parameter"] = "PARAMETER";
/** Vertical or horizontal whitespace. */
PddlTokenType["Whitespace"] = "WHITESPACE";
/** Other unclassified token. */
PddlTokenType["Other"] = "OTHER";
/** Comment i.e. anything after `;`, including the semicolon */
PddlTokenType["Comment"] = "COMMENT";
/** Document is the root node type. */
PddlTokenType["Document"] = "DOCUMENT";
})(PddlTokenType || (exports.PddlTokenType = PddlTokenType = {}));
class TextRange {
includesIndex(symbolIndex) {
if (symbolIndex < this.getStart()) {
return false;
}
if (this.getEnd() === undefined) {
return true;
}
else {
return symbolIndex <= this.getEnd();
}
}
}
exports.TextRange = TextRange;
/** PDDL syntax token. */
class PddlToken extends TextRange {
/**
* Constructs.
* @param type token type
* @param tokenText token content
* @param start first character of the token
*/
constructor(type, tokenText, start) {
super();
this.type = type;
this.tokenText = tokenText;
this.start = start;
this.end = start + tokenText.length;
}
/**
* Creates token from a regex match result.
* @param type token type
* @param match regex match
*/
static from(type, match) {
return new PddlToken(type, match[0], match.index);
}
getStart() {
return this.start;
}
getEnd() {
return this.end;
}
toString() {
return `PddlToken{type: ${this.type}, text: '${this.tokenText}', range: ${this.start}~${this.end}}`;
}
}
exports.PddlToken = PddlToken;
/** Tokenizes PDDL documents. */
class PddlTokenizer {
/**
* Starts tokenizing.
* @param pddlText input PDDL text
* @param callback callback to call when a new token is encountered
* @param lastIndexOfInterest last index of interest or `undefined` to parse the entire document
*/
constructor(pddlText, callback, lastIndexOfInterest) {
const pddlPattern = /\(\s*(:\w[\w-]*|[-\/+*]|[><]=?|define|domain|problem|and|or|not|at start|at end|over all|at|=|assign|increase|decrease|always|sometime|forall|exists|when|within|at-most-once|sometime-before|always-within|supply-demand)(?!-)|\(|:[\w-]+|\(|\)|;|\?\w[\w-]*|[-+]?[0-9]*\.?[0-9]+|-|#t|\w[\w-]*|[\s]+/g;
const endOfLinePattern = /(\n|\r\n)/g;
let endOfLastToken = 0;
lastIndexOfInterest = lastIndexOfInterest === undefined ? Number.MAX_SAFE_INTEGER : lastIndexOfInterest;
let match;
while (match = pddlPattern.exec(pddlText)) {
if (match.index > endOfLastToken) {
callback(new PddlToken(PddlTokenType.Other, pddlText.substring(endOfLastToken, match.index), endOfLastToken));
}
let commentLength = 0;
if (match[0] === '-') {
callback(PddlToken.from(PddlTokenType.Dash, match));
}
else if (match[0] === '(') {
callback(PddlToken.from(PddlTokenType.OpenBracket, match));
}
else if (match[0].startsWith('(')) {
callback(PddlToken.from(PddlTokenType.OpenBracketOperator, match));
}
else if (match[0].match(/^\)$/)) {
callback(PddlToken.from(PddlTokenType.CloseBracket, match));
}
else if (match[0].match(/\?\w[\w-]*$/)) {
callback(PddlToken.from(PddlTokenType.Parameter, match));
}
else if (match[0].match(/^:[\w-]+$/)) {
callback(PddlToken.from(PddlTokenType.Keyword, match));
}
else if (match[0].startsWith(';')) {
// this is where a comment starts
endOfLinePattern.lastIndex = match.index + 1;
const endOfLineMatch = endOfLinePattern.exec(pddlText);
if (endOfLineMatch) {
// end of line found
commentLength = endOfLineMatch.index - match.index;
}
else {
// the file(!) ends on this comment line
commentLength = pddlText.length - match.index;
}
// extract the comment text
const comment = pddlText.substr(match.index, commentLength);
callback(new PddlToken(PddlTokenType.Comment, comment, match.index));
pddlPattern.lastIndex = match.index + commentLength;
}
else if (match[0].match(/^[\s]+$/)) {
// this is whitespace
callback(PddlToken.from(PddlTokenType.Whitespace, match));
}
else {
callback(PddlToken.from(PddlTokenType.Other, match));
}
endOfLastToken = match.index + match[0].length +
(commentLength ? commentLength - 1 : 0);
if (pddlPattern.lastIndex > lastIndexOfInterest) {
break;
}
}
}
}
exports.PddlTokenizer = PddlTokenizer;
function isOpenBracket(token) {
return token.type === PddlTokenType.OpenBracketOperator || token.type === PddlTokenType.OpenBracket;
}
exports.isOpenBracket = isOpenBracket;
//# sourceMappingURL=PddlTokenizer.js.map