forked from projectfluent/fluent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathebnf.js
72 lines (56 loc) · 1.36 KB
/
ebnf.js
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
import fs from "fs";
import readline from "readline";
import parse_args from "minimist";
import ebnf from "../lib/ebnf.js";
const argv = parse_args(process.argv.slice(2), {
boolean: ["help"],
alias: {
help: "h",
},
});
if (argv.help) {
exit_help(0);
}
const [file_path] = argv._;
if (file_path === "-") {
from_stdin();
} else if (file_path) {
from_file(file_path);
} else {
exit_help(1);
}
function exit_help(exit_code) {
console.log(`
Usage: node -r esm ebnf.js [OPTIONS] <FILE>
When FILE is "-", read text from stdin.
Examples:
node -r esm ebnf.js path/to/grammar.js
cat path/to/grammar.js | node -r esm ebnf.js -
Options:
-h, --help Display help and quit.
`);
process.exit(exit_code);
}
function from_stdin() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: "fluent>",
});
const lines = [];
rl.on("line", line => lines.push(line));
rl.on("close", () =>
print_ebnf(lines.join("\n") + "\n"));
}
function from_file(file_path) {
fs.readFile(file_path, "utf8", (err, content) => {
if (err) {
throw err;
}
print_ebnf(content);
});
}
function print_ebnf(source) {
// Each EBNF rule already ends with \n.
process.stdout.write(ebnf(source));
}