@informalsystems/quint
Version:
Core tool for the Quint specification language
56 lines • 2.16 kB
JavaScript
;
/* ----------------------------------------------------------------------------------
* Copyright 2023 Informal Systems
* Licensed under the Apache License, Version 2.0.
* See LICENSE in the project root for license information.
* --------------------------------------------------------------------------------- */
Object.defineProperty(exports, "__esModule", { value: true });
exports.fileSourceResolver = void 0;
/**
* Source resolution for Quint. This module deals with loading sources from strings
* and files.
*
* @author Igor Konnov
*
* @module
*/
const either_1 = require("@sweet-monads/either");
const path_1 = require("path");
const fs_1 = require("fs");
const eol_1 = require("eol");
/**
* Read the source code in UTF-8 from the filesystem via NodeJS API.
* @param sourceCode an optional map of paths to source code,
* to be updated when reading new files
* @param replacer an optional path replacement function,
* which is used to produce a source name
* @returns A filesystem resolver. For each path, it returns
* either `left(errorMessage)`, or `right(fileContents)`.
*/
function fileSourceResolver(sourceCode = new Map(), replacer = path => path) {
return {
lookupPath: (basepath, importPath) => {
return {
normalizedPath: (0, path_1.normalize)((0, path_1.join)(basepath, importPath)),
toSourceName: () => {
return replacer(path_1.posix.join(basepath, importPath));
},
};
},
load: (lookupPath) => {
try {
const code = (0, eol_1.lf)((0, fs_1.readFileSync)(lookupPath.normalizedPath, 'utf8'));
sourceCode.set(lookupPath.normalizedPath, code);
return (0, either_1.right)(code);
}
catch (err) {
return (0, either_1.left)(err.message);
}
},
stempath: (lookupPath) => {
return (0, path_1.dirname)(lookupPath.normalizedPath);
},
};
}
exports.fileSourceResolver = fileSourceResolver;
//# sourceMappingURL=sourceResolver.js.map