-
Notifications
You must be signed in to change notification settings - Fork 453
/
Copy pathcli.js
executable file
·187 lines (164 loc) · 4.89 KB
/
cli.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
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
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env node
function getCommandlineOptions () {
"use strict";
var version = require('../package.json').version;
var opts = require("nomnom")
.script('jison')
.option('file', {
flag : true,
position : 0,
help : 'file containing a grammar'
})
.option('lexfile', {
flag : true,
position : 1,
help : 'file containing a lexical grammar'
})
.option('json', {
abbr : 'j',
flag : true,
help : 'force jison to expect a grammar in JSON format'
})
.option('outfile', {
abbr : 'o',
metavar : 'FILE',
help : 'Filename and base module name of the generated parser'
})
.option('debug', {
abbr : 't',
flag : true,
default:
false,
help : 'Debug mode'
})
.option('module-type', {
abbr : 'm',
default:
'commonjs',
metavar : 'TYPE',
help : 'The type of module to generate (commonjs, amd, js)'
})
.option('parser-type', {
abbr : 'p',
default:
'lalr',
metavar : 'TYPE',
help : 'The type of algorithm to use for the parser (lr0, slr,' +
'lalr, lr)'
})
.option('version', {
abbr : 'V',
flag : true,
help : 'print version and exit',
callback : function () {
return version;
}
}).parse();
return opts;
}
var cli = module.exports;
cli.main = function cliMain(opts) {
"use strict";
opts = opts || {};
function processGrammar(raw, lex, opts) {
var grammar,
parser;
grammar = cli.processGrammars(raw, lex, opts.json);
parser = cli.generateParserString(opts, grammar);
return parser;
}
function processInputFile () {
var fs = require('fs');
var path = require('path');
// getting raw files
var lex;
if (opts.lexfile) {
lex = fs.readFileSync(path.normalize(opts.lexfile), 'utf8');
}
var raw = fs.readFileSync(path.normalize(opts.file), 'utf8');
// making best guess at json mode
opts.json = path.extname(opts.file) === '.json' || opts.json;
// setting output file name and module name based on input file name
// if they aren't specified.
var name = path.basename((opts.outfile || opts.file));
name = name.replace(/\..*$/g, '');
opts.outfile = opts.outfile || (name + '.js');
if (!opts.moduleName && name) {
opts.moduleName = name.replace(/-\w/g,
function (match) {
return match.charAt(1).toUpperCase();
});
}
var parser = processGrammar(raw, lex, opts);
fs.writeFileSync(opts.outfile, parser);
}
function readin(cb) {
var stdin = process.openStdin(),
data = '';
stdin.setEncoding('utf8');
stdin.addListener('data', function (chunk) {
data += chunk;
});
stdin.addListener('end', function () {
cb(data);
});
}
function processStdin () {
readin(function (raw) {
console.log(processGrammar(raw, null, opts));
});
}
// if an input file wasn't given, assume input on stdin
if (opts.file) {
processInputFile();
} else {
processStdin();
}
};
cli.generateParserString = function generateParserString(opts, grammar) {
"use strict";
opts = opts || {};
var jison = require('./jison.js');
var settings = grammar.options || {};
if (opts['parser-type']) {
settings.type = opts['parser-type'];
}
if (opts.moduleName) {
settings.moduleName = opts.moduleName;
}
settings.debug = opts.debug;
if (!settings.moduleType) {
settings.moduleType = opts['module-type'];
}
var generator = new jison.Generator(grammar, settings);
return generator.generate(settings);
};
cli.processGrammars = function processGrammars(file, lexFile, jsonMode) {
"use strict";
lexFile = lexFile || false;
jsonMode = jsonMode || false;
var ebnfParser = require('ebnf-parser');
var cjson = require('cjson');
var grammar;
try {
if (jsonMode) {
grammar = cjson.parse(file);
} else {
grammar = ebnfParser.parse(file);
}
} catch (e) {
throw new Error('Could not parse jison grammar');
}
try {
if (lexFile) {
grammar.lex = require('lex-parser').parse(lexFile);
}
} catch (e) {
throw new Error('Could not parse lex grammar');
}
return grammar;
};
if (require.main === module) {
var opts = getCommandlineOptions();
cli.main(opts);
}