|
| 1 | +#!/usr/bin/env node |
| 2 | +import fs from "node:fs"; |
| 3 | +import path from "node:path"; |
| 4 | +import process from "node:process"; |
| 5 | + |
| 6 | +import { compileSource } from "./src/compiler.mjs"; |
| 7 | +import { MdrCompileError } from "./src/error.mjs"; |
| 8 | + |
| 9 | +const args = process.argv.slice(2); |
| 10 | +const options = { |
| 11 | + inputPath: "", |
| 12 | + astOutPath: "", |
| 13 | + dumpTokens: false, |
| 14 | +}; |
| 15 | + |
| 16 | +if (args.length === 0 || args.includes("--help") || args.includes("-h")) { |
| 17 | + printUsage(); |
| 18 | + process.exit(args.length === 0 ? 1 : 0); |
| 19 | +} |
| 20 | + |
| 21 | +for (let i = 0; i < args.length; i += 1) { |
| 22 | + const arg = args[i]; |
| 23 | + if (arg === "--tokens") { |
| 24 | + options.dumpTokens = true; |
| 25 | + continue; |
| 26 | + } |
| 27 | + if (arg === "--out") { |
| 28 | + const next = args[i + 1]; |
| 29 | + if (next == null) { |
| 30 | + console.error("missing output path after --out"); |
| 31 | + process.exit(1); |
| 32 | + } |
| 33 | + options.astOutPath = next; |
| 34 | + i += 1; |
| 35 | + continue; |
| 36 | + } |
| 37 | + if (arg.startsWith("-")) { |
| 38 | + console.error(`unknown option: ${arg}`); |
| 39 | + process.exit(1); |
| 40 | + } |
| 41 | + if (options.inputPath !== "") { |
| 42 | + console.error("only one input file is supported"); |
| 43 | + process.exit(1); |
| 44 | + } |
| 45 | + options.inputPath = arg; |
| 46 | +} |
| 47 | + |
| 48 | +if (options.inputPath === "") { |
| 49 | + console.error("missing input file"); |
| 50 | + printUsage(); |
| 51 | + process.exit(1); |
| 52 | +} |
| 53 | + |
| 54 | +const absoluteInput = path.resolve(options.inputPath); |
| 55 | +const source = fs.readFileSync(absoluteInput, "utf8"); |
| 56 | + |
| 57 | +try { |
| 58 | + const { tokens, ast } = compileSource(source, absoluteInput); |
| 59 | + const astJson = JSON.stringify(ast, null, 2); |
| 60 | + if (options.astOutPath !== "") { |
| 61 | + const outPath = path.resolve(options.astOutPath); |
| 62 | + fs.mkdirSync(path.dirname(outPath), { recursive: true }); |
| 63 | + fs.writeFileSync(outPath, `${astJson}\n`, "utf8"); |
| 64 | + } else { |
| 65 | + process.stdout.write(`${astJson}\n`); |
| 66 | + } |
| 67 | + |
| 68 | + if (options.dumpTokens) { |
| 69 | + process.stdout.write(`${JSON.stringify(tokens, null, 2)}\n`); |
| 70 | + } |
| 71 | +} catch (error) { |
| 72 | + if (error instanceof MdrCompileError) { |
| 73 | + printCompileError(error, source); |
| 74 | + process.exit(1); |
| 75 | + } |
| 76 | + throw error; |
| 77 | +} |
| 78 | + |
| 79 | +function printUsage() { |
| 80 | + process.stdout.write("Usage: node cli.mjs <input.mdr> [--out <file>] [--tokens]\n"); |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * @param {MdrCompileError} error |
| 85 | + * @param {string} source |
| 86 | + */ |
| 87 | +function printCompileError(error, source) { |
| 88 | + const lines = source.split(/\r?\n/u); |
| 89 | + const lineText = lines[error.line - 1] ?? ""; |
| 90 | + const pointer = `${" ".repeat(Math.max(error.column - 1, 0))}^`; |
| 91 | + |
| 92 | + console.error(`${error.filePath}:${error.line}:${error.column}: ${error.message}`); |
| 93 | + console.error(lineText); |
| 94 | + console.error(pointer); |
| 95 | +} |
0 commit comments