@helios-lang/compiler-utils
Advanced tools
Comparing version
{ | ||
"name": "@helios-lang/compiler-utils", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"description": "Helios language compiler library", | ||
@@ -22,3 +22,3 @@ "main": "src/index.js", | ||
"dependencies": { | ||
"@helios-lang/codec-utils": "^0.3.1", | ||
"@helios-lang/codec-utils": "^0.3.2", | ||
"@helios-lang/type-utils": "^0.2.8" | ||
@@ -25,0 +25,0 @@ }, |
@@ -125,3 +125,7 @@ export { | ||
* @prop {string} content | ||
* @prop {string} name | ||
* @prop {string} name - provided filename (or script name parsed from script header) | ||
* @prop {string} moduleName - script/module name, parsed from the script header | ||
* @prop {string} purpose - script purpose, parsed from the script header | ||
* @prop {string} [project] - optional project name in which the module is defined | ||
* @prop {string} [moreInfo] - optional additional info about the source | ||
* @prop {number} length | ||
@@ -128,0 +132,0 @@ * @prop {number[]} lineEndLocations |
@@ -9,5 +9,6 @@ import { segmentArray } from "@helios-lang/codec-utils" | ||
/** | ||
* @typedef {{ | ||
* name?: string | ||
* }} SourceOptions | ||
* @typedef {object} SourceOptions | ||
* @property {string} [name] - optional file name; defaults to the module name parsed from the source's `<purpose> <name>` header | ||
* @property {string} [project] - optional project name; can be used to add transpancy for advanced cross-project source-file reference use-cases | ||
* @property {string} [moreInfo] - additional textual info about the source, useful in advanced code-generation cases | ||
*/ | ||
@@ -23,6 +24,5 @@ | ||
* @param {object} options | ||
* @param {string} [options.name] | ||
* The file name of the source. | ||
* If not specified the name is extracted from the source header | ||
* | ||
* @param {string} [options.name] optional file name; defaults to the module name parsed from the source's `<purpose> <name>` header | ||
* @param {string} [options.project] optional project name; can be used to add transpancy for advanced cross-project source-file reference use-cases | ||
* @param {string} [options.moreInfo] additional textual info about the source, useful in advanced code-generation cases | ||
* @returns {Source} | ||
@@ -60,2 +60,30 @@ */ | ||
/** | ||
* module name of the source | ||
* @readonly | ||
* @type {string} | ||
*/ | ||
moduleName | ||
/** | ||
* declared script purpose of the source | ||
* @readonly | ||
* @type {string} | ||
*/ | ||
purpose | ||
/** | ||
* optional project name of the source | ||
* @readonly | ||
* @type {string | undefined} | ||
*/ | ||
project | ||
/** | ||
* additional textual info about the source, used in advanced code-generation cases | ||
* @readonly | ||
* @type {string | undefined} | ||
*/ | ||
moreInfo | ||
/** | ||
* Number of characters in each chunk | ||
@@ -98,3 +126,9 @@ * @private | ||
this.length = asCodePoints.length | ||
this.name = options.name ?? "unknown" | ||
const parsedInfo = minimalScriptInfo(content) | ||
this.name = options.name ?? parsedInfo.moduleName | ||
this.moduleName = parsedInfo.moduleName | ||
this.purpose = parsedInfo.purpose | ||
this.project = options.project | ||
this.moreInfo = options.moreInfo | ||
this._lineEndLocations = undefined | ||
@@ -213,1 +247,22 @@ } | ||
} | ||
/** | ||
* extracts script purpose and name from a source string without dependency on full parser | ||
* @param {string} src | ||
* @returns {{ purpose: string, moduleName: string }} | ||
*/ | ||
function minimalScriptInfo(src) { | ||
const [_, purpose, moduleName] = | ||
src.match( | ||
/(module|minting|spending|staking|testing|mixed|voting)\s+(\w+)/m | ||
) || [] | ||
if (!purpose) { | ||
throw new Error("missing or invalid script header: no purpose found") | ||
} | ||
if (!moduleName || !/^[a-zA-Z][_a-zA-Z0-9]*$/.test(moduleName)) { | ||
throw new Error( | ||
`invalid script header: invalid module name found: ${moduleName || "‹missing›"}` | ||
) | ||
} | ||
return { purpose, moduleName } | ||
} |
148410
1.7%4495
1.19%