UNPKG

pddl-workspace

Version:
223 lines 8.97 kB
"use strict"; /* -------------------------------------------------------------------------------------------- * Copyright (c) Jan Dolejsi. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. * ------------------------------------------------------------------------------------------ */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.NunjucksPreProcessor = exports.Jinja2PreProcessor = exports.PythonPreProcessor = exports.CommandPreProcessor = exports.PreProcessor = exports.PreProcessingError = void 0; const process = __importStar(require("child_process")); const path = __importStar(require("path")); const nunjucks = __importStar(require("nunjucks")); const fs = __importStar(require("fs")); class PreProcessingError { constructor(message, line, column) { this.message = message; this.line = line; this.column = column; } get name() { return 'pre-processing_error'; } } exports.PreProcessingError = PreProcessingError; class PreProcessor { constructor(metaDataLine, metaDataLineOffset) { this.metaDataLine = metaDataLine; this.metaDataLineOffset = metaDataLineOffset; } removeMetaDataLine(text) { const pattern = /^;;\s*!pre-parsing:/; return text.split('\n').map(line => pattern.test(line) ? "; Generated from a PDDL template and a data file" : line).join('\n'); } } exports.PreProcessor = PreProcessor; /** * Shell command based pre-processor. */ class CommandPreProcessor extends PreProcessor { constructor(command, args, metaDataLine, metaDataLineOffset) { super(metaDataLine, metaDataLineOffset); this.command = command; this.args = args; } toString() { return `${this.command} ` + this.args.join(' '); } static fromJson(json) { return new CommandPreProcessor(json["command"], json["args"], ''); } getInputFiles() { return []; } getLabel() { return this.command; } handleError(error) { throw new PreProcessingError(error.message, 0, 0); } // eslint-disable-next-line @typescript-eslint/no-explicit-any handleErrorAsync(error, reject) { reject(new PreProcessingError(error.message, 0, 0)); } async transform(input, workingDirectory, outputWindow) { // eslint-disable-next-line @typescript-eslint/no-this-alias const that = this; return new Promise(function (resolve, reject) { var _a, _b; const childProcess = process.execFile(that.command, that.args, { cwd: workingDirectory }, (error, stdout, stderr) => { if (stderr) { outputWindow.appendLine(stderr); } if (error) { outputWindow.appendLine('Failed to transform the problem file.'); outputWindow.appendLine(error.message); that.handleErrorAsync(error, reject); resolve(input); } else { resolve(that.removeMetaDataLine(stdout)); return; } }); (_a = childProcess.stdin) === null || _a === void 0 ? void 0 : _a.write(input); (_b = childProcess.stdin) === null || _b === void 0 ? void 0 : _b.end(); }); } } exports.CommandPreProcessor = CommandPreProcessor; /** * Python-based pre-processor */ class PythonPreProcessor extends CommandPreProcessor { constructor(pythonPath, script, args, metaDataLine, metaDataLineOffset) { super(pythonPath, [script].concat(args), metaDataLine, metaDataLineOffset); } getInputFiles() { return this.args; } getLabel() { return this.args.join(' '); } // eslint-disable-next-line @typescript-eslint/no-unused-vars static fromJson(_json) { throw new Error("For Jinja2 pre-processor, use the constructor instead"); } // eslint-disable-next-line @typescript-eslint/no-explicit-any handleErrorAsync(error, reject) { const errorLines = error.message.split('\n'); const templateSyntaxError = errorLines.find(row => row.startsWith(PythonPreProcessor.JINJA2_TEMPLATE_SYNTAX_ERROR_PREFIX)); if (templateSyntaxError) { reject(new PreProcessingError(templateSyntaxError.substring(PythonPreProcessor.JINJA2_TEMPLATE_SYNTAX_ERROR_PREFIX.length), 0, 0)); return; } const pythonJsonError = errorLines.find(row => row.startsWith(PythonPreProcessor.PYTHON_JSON_DECODER)); if (pythonJsonError) { reject(new PreProcessingError('JSON Error: ' + pythonJsonError.substring(PythonPreProcessor.PYTHON_JSON_DECODER.length), 0, 0)); return; } super.handleErrorAsync(error, reject); } } exports.PythonPreProcessor = PythonPreProcessor; PythonPreProcessor.JINJA2_TEMPLATE_SYNTAX_ERROR_PREFIX = 'jinja2.exceptions.TemplateSyntaxError: '; PythonPreProcessor.PYTHON_JSON_DECODER = 'json.decoder.JSONDecodeError: '; /** * Jinja2 pre-processor */ class Jinja2PreProcessor extends PythonPreProcessor { constructor(pythonPath, extensionRoot, dataFileName, metaDataLine, metaDataLineOffset) { super(pythonPath, path.join(extensionRoot, "scripts", "transform_jinja2.py"), [dataFileName], metaDataLine, metaDataLineOffset); this.dataFileName = dataFileName; } // eslint-disable-next-line @typescript-eslint/no-unused-vars static fromJson(_json) { throw new Error("For Jinja2 pre-processor, use the constructor instead"); } getInputFiles() { return [this.dataFileName]; } getLabel() { return this.dataFileName; } } exports.Jinja2PreProcessor = Jinja2PreProcessor; /** * Nunjucks based pre-processor */ class NunjucksPreProcessor extends PreProcessor { constructor(dataFileName, metaDataLine, metaDataLineOffset, preserveWhitespace) { super(metaDataLine, metaDataLineOffset); this.dataFileName = dataFileName; this.nunjucksEnv = nunjucks.configure({ trimBlocks: false, lstripBlocks: !preserveWhitespace, throwOnUndefined: true }); this.nunjucksEnv.addFilter('map', function (array, attribute) { return array.map((item) => item[attribute]); }); this.nunjucksEnv.addFilter('setAttribute', function (dictionary, key, value) { dictionary[key] = value; return dictionary; }); } getInputFiles() { return [this.dataFileName]; } getLabel() { return this.dataFileName; } toString() { return `Nunjucks ${this.dataFileName}`; } // eslint-disable-next-line @typescript-eslint/no-unused-vars async transform(input, workingDirectory, _outputWindow) { const dataPath = path.join(workingDirectory, this.dataFileName); const dataText = await fs.promises.readFile(dataPath); let data; try { data = JSON.parse(dataText.toLocaleString()); } catch (error) { return `Failed to read from '${dataPath}'.`; } try { const translated = this.nunjucksEnv.renderString(input, { data: data }); return this.removeMetaDataLine(translated); } catch (error) { const error1 = error; const pattern = /\((.+)\)\s+\[Line\s+(\d+),\s+Column\s+(\d+)\]/; const match = pattern.exec(error1.message); if (match) { throw new PreProcessingError(match[1], parseInt(match[2]) - 1, parseInt(match[3]) - 1); } else { throw new PreProcessingError(error1.message, 0, 0); } } } } exports.NunjucksPreProcessor = NunjucksPreProcessor; //# sourceMappingURL=PreProcessors.js.map