-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgenerate-railroad.js
More file actions
176 lines (144 loc) · 4.59 KB
/
generate-railroad.js
File metadata and controls
176 lines (144 loc) · 4.59 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env node
const fs = require("node:fs");
const { spawnSync } = require("node:child_process");
const path = require("node:path");
// Customization section
const DEFAULT_INPUT_ABNF = "grammar/jsonc.abnf";
const DEFAULT_PROCESSED_ABNF = "grammar/jsonc-processed.abnf";
const DEFAULT_OUTPUT_HTML = "grammar/railroad-diagram.html";
// Rules to inline from their %x... definitions as literal ABNF strings.
// Add more rule names here to apply the same transformation.
const INLINE_HEX_RULES = [
"multi-line-comment-start",
"multi-line-comment-end",
"asterisk",
"escape"
];
function escapeRegExp(value) {
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
function decodeAbnfHexSequence(value) {
const trimmed = value.trim();
if (!/^%x[0-9A-Fa-f]+(?:\.[0-9A-Fa-f]+)*$/.test(trimmed)) {
throw new Error(`Unsupported ABNF hex sequence: ${value}`);
}
const bytes = trimmed
.slice(2)
.split(".")
.map((part) => parseInt(part, 16));
return String.fromCodePoint(...bytes);
}
function inlineHexRuleAsLiteral(source, ruleName) {
const escapedRuleName = escapeRegExp(ruleName);
const ruleRegex = new RegExp(
`^\\s*${escapedRuleName}\\s*=\\s*(%x[0-9A-Fa-f]+(?:\\.[0-9A-Fa-f]+)*)\\b.*$`,
"m",
);
const ruleMatch = source.match(ruleRegex);
if (!ruleMatch) {
throw new Error(`Rule ${ruleName} was not found.`);
}
const hexSequence = ruleMatch[1];
const literalChars = decodeAbnfHexSequence(hexSequence);
// For backslash or other problematic characters, keep them as hex format
// ABNF doesn't support backslash escaping in quoted strings
let replacement;
if (literalChars === "\\") {
replacement = hexSequence;
} else {
// For other characters, escape only double quotes (not backslashes)
const escapedLiteralChars = literalChars.replace(/"/g, '\\"');
replacement = `"${escapedLiteralChars}"`;
}
const removeRuleRegex = new RegExp(`^\\s*${escapedRuleName}\\s*=.*(?:\\r?\\n|$)`, "m");
const withoutRule = source.replace(removeRuleRegex, "");
const useRuleRegex = new RegExp(
`(?<![A-Za-z0-9-])${escapedRuleName}(?![A-Za-z0-9-])`,
"g",
);
// Replace only grammar expressions: RHS after '=' or continuation lines.
return withoutRule
.split(/\r?\n/)
.map((line) => {
const eqIndex = line.indexOf("=");
if (eqIndex !== -1) {
const lhs = line.slice(0, eqIndex + 1);
const rhs = line.slice(eqIndex + 1).replace(useRuleRegex, replacement);
return `${lhs}${rhs}`;
}
if (/^\s/.test(line)) {
return line.replace(useRuleRegex, replacement);
}
return line;
})
.join("\n");
}
function processAbnfSource(source) {
let processed = source;
for (const ruleName of INLINE_HEX_RULES) {
processed = inlineHexRuleAsLiteral(processed, ruleName);
}
return processed;
}
const args = process.argv.slice(2);
const titleIndex = args.indexOf("--title");
let title;
if (titleIndex !== -1) {
if (titleIndex + 1 >= args.length) {
console.error("Missing value for --title");
process.exit(1);
}
title = args[titleIndex + 1];
args.splice(titleIndex, 2);
}
const input = args[0] || DEFAULT_INPUT_ABNF;
const output = args[1] || DEFAULT_OUTPUT_HTML;
const processedAbnf = DEFAULT_PROCESSED_ABNF;
const inputPath = path.resolve(__dirname, input);
const outputPath = path.resolve(__dirname, output);
const processedPath = path.resolve(__dirname, processedAbnf);
let source;
try {
source = fs.readFileSync(inputPath, "utf8");
} catch (error) {
console.error(`Failed to read input ABNF: ${error.message}`);
process.exit(1);
}
let processed;
try {
processed = processAbnfSource(source);
} catch (error) {
console.error(`Failed to process ABNF source: ${error.message}`);
process.exit(1);
}
if (typeof processed !== "string") {
console.error("Failed to process ABNF source: processAbnfSource must return a string.");
process.exit(1);
}
try {
fs.mkdirSync(path.dirname(processedPath), { recursive: true });
fs.writeFileSync(processedPath, processed, "utf8");
} catch (error) {
console.error(`Failed to write processed ABNF: ${error.message}`);
process.exit(1);
}
const cliPath = path.join(
__dirname,
"node_modules",
"railroad-diagram-generator-js",
"bin",
"cli.js",
);
const cliArgs = [cliPath, "generate", processedPath, outputPath];
if (title) {
cliArgs.push("--title", title);
}
const result = spawnSync(process.execPath, cliArgs, {
cwd: __dirname,
stdio: "inherit",
});
if (result.error) {
console.error(`Failed to run railroad generator: ${result.error.message}`);
process.exit(1);
}
process.exit(result.status === null ? 1 : result.status);