Spaces:
Running
Running
| /** | |
| * 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(); | |