-
Notifications
You must be signed in to change notification settings - Fork 1
/
tscc-cli.js
96 lines (89 loc) · 2.59 KB
/
tscc-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
'use strict';
var fs = require('fs');
//var Promise = require('bluebird');
var tscc = require('../lib/tscc.js').main;
var parseArgs = require('./arg.js');
var pkg = require('../package.json');
var help =
`usage: ${pkg.name} [options] file
or ${pkg.name} --help|-h
options:
-o, --output Specify output file.
-t, --test Run test on the given input string.
-d, --detail-time Print the time costs of different phases.
--dfa Print lexical DFA tables in the output file.
--show-lah Show look-ahead tokens of the items when printing parse table.
--show-full-itemsets Show all the items of each item set when printing parse table.
By default, only kernel items will be printed.
-h, --help Print this help message and exit.
`;
function readFile(fname){
return new Promise(function(accept, reject){
fs.readFile(fname, function(err, data){
err ? reject(err) : accept(data.toString('utf-8'));
});
});
}
function writeFile(fname, data){
return new Promise(function(accept, reject){
fs.writeFile(fname, data, function(err){
err ? reject(err) : accept();
});
});
}
function main(arg){
var stdout = {
write: function(s){
process.stdout.write(s);
},
writeln: function(s){
console.log(s || '');
}
}
var files = [];
return readFile(arg.input)
.then(function(input){
var status = tscc({
inputFile: arg.input,
input: input,
outputFile: arg.output,
stdout: stdout,
writeFile: function(path, content){
files.push(writeFile(path, content));
},
testInput: arg.test,
printDetailedTime: arg.detailedTime,
printDFA: arg.dfa,
showlah: arg.showlah,
showFullItemsets: arg.showFullItemsets
});
return Promise.all(files)
.then(function(){
return status;
});
});
}
module.exports = function(options){
options.shift();
options.shift();
try {
var arg = parseArgs(options);
}
catch(e){
console.log(e.toString());
console.log(`try "${pkg.name} --help" for help`);
process.exit(-1);
}
if(arg.help){
console.log(help);
process.exit(0);
}
return main(arg)
.then(function(status){
process.exit(status);
})
.catch(function(e){
console.log(e.toString());
process.exit(-1);
});
}