-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtypes.js
117 lines (100 loc) · 3.44 KB
/
types.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
function Sym(type, data) {
this.type = type;
this.data = data;
}
Sym.prototype.equals = function(other) {
return other.type === this.type && other.data === this.data;
}
Sym.prototype.toString = function(){
return this.data.toString(); //return this.type + '(' + this.data + ')';
}
function NT(data) { return new Sym('NT', data); }
function T(data) { return new Sym('T', data); }
function reprEscape(str) { // does not handle unicode or exceptional cases properly.
return str.replace(/['\\]/g, function(c) { return '\\' + c; })
.replace(/\n/g, '\\n').replace(/\r/g, '\\r');
}
function Rule(name, production) {
if(!(this instanceof Rule)) return new Rule(name, production);
this.name = name; // LHS
this.production = production; // RHS\
}
Rule.prototype.equals = function(other) {
if(other.name !== this.name) return false;
if(other.production.length !== this.production.length) return false;
for(var i=0; i<other.production.length; ++i) {
if(!other.production[i].equals(this.production[i])) return false;
}
return true;
}
Rule.prototype.toString = function() {
return this.name + ' -> ' + this.production.join('');
}
Rule.prototype.repr = function() {
var out = 'Rule(\'' + reprEscape(this.name) + '\', [';
for(var i=0; i<this.production.length; ++i) {
if(i>0) out += ', ';
out += this.production[i].type + '(\'' + reprEscape(this.production[i].data) + '\')';
}
out += '])';
return out;
}
function Grammar(rules, start) { // if not given, start is LHS of the first rule.
if(!(this instanceof Grammar)) return new Grammar(rules, start);
this.rules = rules;
this.start = start || rules[0].name; // TODO warn
this.symbolMap = {}; // initially just rules for each symbol; eventually can contain annotations like 'nullable'
this.symbolsList = start?[start]:[];
if(start) this.symbolMap[start] = {rules: []};
for(var i=0; i<this.rules.length; ++i) {
var sym = this.rules[i].name;
if(!(sym in this.symbolMap)) {
this.symbolMap[sym] = {rules: []};
this.symbolsList.push(sym);
}
for(var j=0; j<this.rules[i].production.length; ++j) {
var rhsSym = this.rules[i].production[j];
if(rhsSym.type == 'NT' && !(rhsSym.data in this.symbolMap)) {
this.symbolMap[rhsSym.data] = {rules: []};
this.symbolsList.push(rhsSym.data);
}
}
this.symbolMap[sym].rules.push(this.rules[i]);
}
}
Grammar.prototype.repr = function() {
var out = 'Grammar([\n ';
for(var i=0; i<this.rules.length; ++i) {
if(i>0) out += ',\n ';
out += this.rules[i].repr();
}
out += '\n], \'' + reprEscape(this.start) + '\')';
return out;
}
// get a map from symbols to a list of the rules they appear in the RHS of
// if a symbol appears in a RHS more than once, that rule will appear more than once in the list
// modifies the grammar to have _reverseMap property, for caching
Grammar.prototype.getReverseMap = function() {
if(!this.hasOwnProperty('_reverseMap')) {
this._reverseMap = {};
for(var i=0; i<this.symbolsList.length; ++i) {
this._reverseMap[this.symbolsList[i]] = [];
}
for(var i=0; i<this.rules.length; ++i) {
var rule = this.rules[i];
for(var j=0; j<rule.production.length; ++j) {
if(rule.production[j].type === 'NT') {
this._reverseMap[rule.production[j].data].push(rule);
}
}
}
}
return this._reverseMap;
}
module.exports = {
Sym: Sym,
NT: NT,
T: T,
Rule: Rule,
Grammar: Grammar
}