Spaces:
Running
Running
File size: 2,121 Bytes
466436b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
/**
* Build script for Jison parser generation
*
* Generates pre-compiled parser to public/lib/ directory for use in browser
* Following lotus project architecture:
* - Use jison npm package CLI to compile grammar
* - Pre-build parsers to static .js files
* - Serve from public directory
* - Works in both browser and Node.js
*/
import { execSync } from "child_process";
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const projectRoot = path.resolve(__dirname, "..");
/**
* Build a single parser from grammar file
* @param grammarPath - Path to .jison grammar file
* @param targetPath - Where to write the compiled parser
*/
function buildParser(grammarPath: string, targetPath: string): void {
console.log(`Building parser: ${grammarPath} → ${targetPath}`);
// Ensure output directory exists
const targetDir = path.dirname(targetPath);
if (!fs.existsSync(targetDir)) {
fs.mkdirSync(targetDir, { recursive: true });
console.log(`Created directory: ${targetDir}`);
}
// Run jison CLI to generate parser
// Using -m commonjs for CommonJS module format (works in Node.js and Vite)
// Outputs to .cjs file which will be treated as CommonJS
try {
execSync(`npx jison "${grammarPath}" -o "${targetPath.replace(".js", ".cjs")}" -m commonjs`, {
cwd: projectRoot,
stdio: "inherit"
});
console.log(`✓ Parser written to: ${targetPath.replace(".js", ".cjs")}`);
} catch (error) {
console.error(`✗ Failed to build parser:`, error);
throw error;
}
}
/**
* Main build function
*/
function main(): void {
try {
console.log("🔨 Building Jison parsers...\n");
// Build TGN parser to public/lib/
const tgnGrammarPath = path.resolve(projectRoot, "inc/tgn/tgn.jison");
const tgnOutputPath = path.resolve(projectRoot, "public/lib/tgnParser.js");
buildParser(tgnGrammarPath, tgnOutputPath);
console.log("\n✅ Done building parsers!");
} catch (error) {
console.error("❌ Error building parsers:", error);
process.exit(1);
}
}
// Run main function
main();
|