-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathvisualize.js
51 lines (45 loc) · 1.22 KB
/
visualize.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
var regexp = require("./regexp.js");
function main() {
var opts = require('tav').set({
re: {
note: "The regular expression to visualize (required)",
value: ''
},
type: {
note: "The type of graph required (nfa|dfa) (default: dfa)",
value: 'dfa'
},
attrs: {
note: "Attributes to pass to the graph definition function",
value: ''
}
}, "Regular expression visualizer");
opts.re = opts.re.trim();
if (!opts.re) {
console.error("You must specify a regular expression using --re=...");
return 1;
}
if (['dfa', 'nfa'].indexOf(opts.type) == -1) {
console.error("You must specify a valid graph output type using --type=(dfa|nfa)");
return 1;
}
var attrs = { };
opts.attrs.split(/,/).forEach(function(attr) {
attr = attr.trim();
if (attr.length == 0) return;
var parts = attr.split(/=/);
if (parts[0].length == 0 || parts[1].length == 0) return;
attrs[parts[0]] = parts[1];
});
if (opts.type == 'dfa') {
var REdfa = new regexp.RegExpDFA(opts.re);
// TODO: Handle errors
console.log(REdfa.toDot(attrs));
} else {
var REnfa = new regexp.RegExpNFA(opts.re);
// TODO: Handle errors
console.log(REnfa.toDot(attrs));
}
return 0;
}
process.exit(main());