-
Notifications
You must be signed in to change notification settings - Fork 0
/
peg-jme.js
212 lines (190 loc) · 7.53 KB
/
peg-jme.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
function makeGrammar(grammar) {
var rightAssocBinaryOpAction = "let out = a; ops.forEach(function(o){ out = {tok:o.op,args:[out,o.b]}}); return out";
var leftAssocBinaryOpAction = 'return {tok: (new types.TOp(op)),args:[a,b]}';
var baseGrammar = expander.parse(baseSource);
var newGrammar;
var atoms = [];
var binaryOps = [];
var prefixOps = [];
var postfixOps = [];
var baseRules = [];
var extraRules = [];
var specialNumbers = [];
var reservedWords = [];
var removeOps = [];
/* run through the source, picking out op and atom definitions */
function expandRules(grammar,outList) {
grammar.rules.forEach(function(rule) {
switch(rule.type) {
case 'operator':
switch(rule.kind) {
case 'binary':
binaryOps.push(rule);
break;
case 'prefix':
prefixOps.push(rule);
break;
case 'postfix':
postfixOps.push(rule);
}
break;
case 'remove-op':
removeOps.push(rule.name);
break;
case 'atom':
atoms.push({name:rule.rule.name,before:rule.before});
outList.push(rule.rule);
break;
case 'special number':
specialNumbers.push(rule);
break;
case 'reserved word':
reservedWords.push(rule);
break;
case 'rule':
outList.push(rule);
break;
}
});
}
expandRules(baseGrammar,baseRules);
grammar = grammar.trim();
if(grammar) {
var newGrammar = expander.parse(grammar);
expandRules(newGrammar,extraRules);
} else {
newGrammar = baseGrammar;
}
/* Filter out removed op names */
binaryOps = binaryOps.filter(function(rule){return removeOps.indexOf(rule.name)==-1});
prefixOps = prefixOps.filter(function(rule){return removeOps.indexOf(rule.name)==-1});
postfixOps = postfixOps.filter(function(rule){return removeOps.indexOf(rule.name)==-1});
/* Now we'll write a load of extra rules for the ops and atoms */
var opRules = [];
/* list atoms */
var ordered_atoms = [];
atoms.forEach(function(a) {
if(a.before) {
var i = ordered_atoms.indexOf(a.before);
if(i>=0) {
ordered_atoms.splice(i,0,a.name);
} else {
ordered_atoms.push(a.name);
}
} else {
ordered_atoms.push(a.name);
}
});
console.log(ordered_atoms);
opRules.push('Atom = '+ordered_atoms.join(" / "));
/* make binary ops, in order of precedence */
binaryOps.sort(function(a,b){return a.precedence > b.precedence ? 1 : a.precedence<b.precedence ? -1 : 0});
var i = 0;
var nextRule = "AtomExpression";
var level = 0;
function wrapOpName(r) {
return '"'+r.name+'"i'+(r.realName ? ' {return "'+r.realName+'"}' : '');
}
function sortByNameLength(a,b){a=a.name.length; b=b.name.length; return a>b ? -1 : a<b ? 1 : 0}
while(i<binaryOps.length) {
level += 1;
var g = [];
var op = binaryOps[i].precedence;
var j = i;
while(i<binaryOps.length && binaryOps[i].precedence===op) {
i += 1;
}
var ops = binaryOps.slice(j,i).sort(sortByNameLength);
var rightOps = ops.filter(function(r){return !r.leftAssoc}).map(wrapOpName).join(" / ");
var leftOps = ops.filter(function(r){return r.leftAssoc}).map(wrapOpName).join(" / ");
var name = "BinaryOpLevel"+level;
var rightRule = "a:"+nextRule+" ops:(ws op:("+rightOps+") ws b:"+nextRule+" {return {op: (new types.TOp(op)),b:b}})* {"+rightAssocBinaryOpAction+"}";
var leftRule = "a:"+nextRule+" ws op:("+leftOps+") ws b:"+name+"Left {"+leftAssocBinaryOpAction+"} / "+nextRule;
var rule;
if(leftOps.length) {
opRules.splice(0,0,name+"Left = "+leftRule);
}
if(rightOps.length) {
if(leftOps.length) {
rule = rightRule+ " / "+name+"Left";
} else {
rule = rightRule;
}
} else {
rule = name+"Left";
}
rule = name+' = '+rule;
opRules.splice(0,0,rule);
nextRule = name;
}
var allReservedWords = reservedWords.concat(binaryOps,prefixOps,postfixOps,specialNumbers).map(function(r){ return r.name });
opRules.splice(0,0,'ValidName = name:RawName !{return '+JSON.stringify(allReservedWords)+'.indexOf(name)>=0} {return name}');
opRules.splice(0,0,'PrefixExpression = op:('+prefixOps.map(wrapOpName).join(' / ')+') ws arg:PrefixExpression {return {tok: new types.TOp(op,false,true),args:[arg]}} / PostfixExpression');
opRules.splice(0,0,'PostfixExpression = arg:BinaryExpression ws ops:('+postfixOps.map(wrapOpName).join(' / ')+')* {for(var i=0;i<ops.length;i++){arg = {tok:new types.TOp(ops[i],true,false),args:[arg]}}; return arg}');
opRules.splice(0,0,'BinaryExpression = '+nextRule);
/* the top Expression rule */
opRules.splice(0,0,'Expression "expression" = PrefixExpression');
if(specialNumbers.length) {
specialNumbers = specialNumbers.sort(sortByNameLength);
opRules.push('Number = NumberLiteral / SpecialNumber');
opRules.push('SpecialNumber = '+specialNumbers.map(function(r){
return '"'+r.name+'"i {return {tok: new types.TNum('+r.code+')}}';
}).join(' / '));
} else {
opRules.push('Number = NumberLiteral');
}
/* compile the op/atom rules and add them to the base grammar */
var opSource = opRules.join('\n');
var opGrammar = peg.parser.parse(opSource);
baseRules = baseRules.concat(opGrammar.rules);
/* remove duplicate rules */
var seenRules = {};
extraRules.forEach(function(r) {
seenRules[r.name] = true;
});
baseRules.forEach(function(rule) {
if(!(seenRules[rule.name])) {
seenRules[rule.name] = true;
extraRules.push(rule);
}
});
function convertPasses(passes) {
var converted = {}, stage, p, name;
for (stage in passes) {
if (passes.hasOwnProperty(stage)) {
p = [];
for(name in passes[stage]) {
p.push(passes[stage][name]);
}
converted[stage] = p;
}
}
return converted;
}
newGrammar.rules = extraRules;
if(newGrammar.initializer) {
newGrammar.initializer.code = baseGrammar.initializer.code + newGrammar.initializer.code;
} else {
newGrammar.initializer = baseGrammar.initializer;
}
var options = {
allowedStartRules: ['StartExpression']
};
var parser = peg.compiler.compile(newGrammar,convertPasses(peg.compiler.passes),options);
return parser;
}
function compile(source) {
var baseParser = new nearley.Parser(window.grammar.ParserRules,window.grammar.ParserStart);
baseParser.feed(source)
var compiled = Compile(baseParser.results[0],{});
var src = generate(compiled,'grammar');
src = '(function() { var module = {exports:{}};\n'+src+'\nreturn module.exports})()';
var g = eval(src);
function parse(str) {
var p = new nearley.Parser(g.ParserRules,g.ParserStart);
p.feed(str);
return p.finish();
}
return {parse:parse,grammar:g}
}
var jme_parser = new nearley.Parser(jme_grammar.ParserRules,'main');