-
Notifications
You must be signed in to change notification settings - Fork 233
/
Copy pathnearleyc.js
executable file
·38 lines (31 loc) · 1.34 KB
/
nearleyc.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
#!/usr/bin/env node
var fs = require('fs');
var nearley = require('../lib/nearley.js');
var opts = require('commander');
var Compile = require('../lib/compile.js');
var StreamWrapper = require('../lib/stream.js');
var version = require('../package.json').version;
opts.version(version, '-v, --version')
.arguments('<file.ne>')
.option('-o, --out [filename.js]', 'File to output to (defaults to stdout)', false)
.option('-e, --export [name]', 'Variable to set parser to', 'grammar')
.option('-q, --quiet', 'Suppress linter')
.option('--nojs', 'Do not compile postprocessors')
.parse(process.argv);
var input = opts.args[0] ? fs.createReadStream(opts.args[0]) : process.stdin;
var output = opts.out ? fs.createWriteStream(opts.out) : process.stdout;
var parserGrammar = nearley.Grammar.fromCompiled(require('../lib/nearley-language-bootstrapped.js'));
var parser = new nearley.Parser(parserGrammar);
var generate = require('../lib/generate.js');
var lint = require('../lib/lint.js');
input
.pipe(new StreamWrapper(parser))
.on('finish', function() {
parser.feed('\n');
var c = Compile(
parser.results[0],
Object.assign({version: version}, opts)
);
if (!opts.quiet) lint(c, {'out': process.stderr, 'version': version});
output.write(generate(c, opts.export));
});