forked from zaach/jison2json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jison2json.js
executable file
·62 lines (47 loc) · 1.39 KB
/
jison2json.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
#!/usr/bin/env node
// Encodes Jison formatted grammars as JSON
var fs = require('fs');
var path = require('path');
var bnfParser = require('ebnf-parser');
var lexParser = require('lex-parser');
exports.main = function (argv) {
if(argv.length == 1) return;
var args = argv.slice(1);
var bnf, lex;
console.log(args);
if (args.length) {
bnf = fs.readFileSync(path.resolve(args[0]), 'utf8');
if (args[1]) {
console.log('???lex');
lex = fs.readFileSync(path.resolve(args[1]), 'utf8');
}
console.log(processGrammar(bnf, lex));
} else {
var read = false;
input(function (bnf) {
read = true;
console.log(processGrammar(bnf));
});
}
};
exports.convert = processGrammar;
function processGrammar (rawGrammar, lex) {
var grammar = bnfParser.parse(rawGrammar);
if (lex) grammar.lex = lexParser.parse(lex);
// trick to reposition `bnf` after `lex` in serialized JSON
grammar.bnf = grammar.bnf;
return JSON.stringify(grammar, null, ' ');
}
function input (cb) {
var stdin = process.openStdin(),
data = '';
stdin.setEncoding('utf8');
stdin.addListener('data', function (chunk) {
data += chunk;
});
stdin.addListener('end', function () {
cb(data);
});
};
if (require.main === module)
exports.main(process.argv.slice(1));