-
Notifications
You must be signed in to change notification settings - Fork 33
/
parser.js
379 lines (308 loc) · 10.1 KB
/
parser.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
function Parser(/*String | CompiledGrammar*/ aGrammar)
{
if (typeof aGrammar.valueOf() === "string")
this.compiledGrammar = new (require("./compiledgrammar"))(aGrammar);
else
this.compiledGrammar = aGrammar;
return this;
}
module.exports = Parser;
Parser.prototype.parse = function(input)
{
return parse(this.compiledGrammar, input);
}
var NAME = 0,
DOT = 1,
CHARACTER_CLASS = 2,
ORDERED_CHOICE = 3,
SEQUENCE = 4,
STRING_LITERAL = 5,
ZERO_OR_MORE = 6,
ONE_OR_MORE = 7,
OPTIONAL = 8,
NEGATIVE_LOOK_AHEAD = 9,
POSITIVE_LOOK_AHEAD = 10,
ERROR_NAME = 11,
ERROR_CHOICE = 12;
function parse(aCompiledGrammar, input, name)
{
var node = new SyntaxNode("#document", input, 0, 0),
table = aCompiledGrammar.table,
nameToUID = aCompiledGrammar.nameToUID;
name = name || "start";
// This is a stupid check.
if (aCompiledGrammar.nameToUID["EOF"] !== undefined)
table[0] = [SEQUENCE, nameToUID[name], nameToUID["EOF"]];
if (!evaluate(new context(input, table), node, table, 0))
{
// This is a stupid check.
if (aCompiledGrammar.nameToUID["EOF"] !== undefined)
table[0] = [SEQUENCE, nameToUID["%" + name], nameToUID["EOF"]];
node.children.length = 0;
evaluate(new context(input, table), node, table, 0);
node.traverse(
{
traverseTextNodes:false,
enteredNode:function(node)
{
if (node.error)
console.log(node.message() + "\n");
}
});
}
return node;
}
exports.parse = parse;
function context(input, table)
{
this.position = 0;
this.input = input;
this.memos = [];
for (var i=0;i<table.length;++i)
this.memos[i] = [];
}
function evaluate(context, parent, rules, rule_id)
{
var rule = rules[rule_id],
type = rule[0],
input_length = context.input.length,
memos = context.memos[rule_id];
var uid = context.position,
entry = memos[uid];
if (entry === false)
return false;
else if (entry === true)
return true;
else if (entry)
{
if (parent)
parent.children.push(entry.node);
context.position = entry.position;
return true;
}
switch (type)
{
case NAME:
case ERROR_NAME:
var node = new SyntaxNode(rule[1], context.input, context.position, 0, rule[3]);
if (!evaluate(context, node, rules, rule[2]))
{
memos[uid] = false;
return false;
}
node.range.length = context.position - node.range.location;
memos[uid] = { node:node, position:context.position };
if (parent)
parent.children.push(node);
return true;
case CHARACTER_CLASS:
var character = context.input.charAt(context.position);
if (typeof rule[1].valueOf() === "string")
rule[1] = new RegExp(rule[1], "g");
if (character.match(rule[1]))
{
if (parent)
parent.children.push(character);
++context.position;
return true;
}
memos[uid] = false;
return false;
case SEQUENCE:
var index = 1,
count = rule.length;
for (; index < count; ++index)
if (!evaluate(context, parent, rules, rule[index]))
{
memos[uid] = false;
return false;
}
return true;
case ORDERED_CHOICE:
case ERROR_CHOICE:
var index = 1,
count = rule.length,
position = context.position;
for (; index < count; ++index)
{
// cache opportunity here.
var child_count = parent && parent.children.length;
if (evaluate(context, parent, rules, rule[index]))
return true;
if (parent)
parent.children.length = child_count;
context.position = position;
}
memos[uid] = false;
return false;
case STRING_LITERAL:
var string = rule[1],
string_length = string.length;
if (string_length + context.position > input_length)
{
memos[uid] = false;
return false;
}
var index = 0;
for (; index < string_length; ++context.position, ++index)
if (context.input.charCodeAt(context.position) !== string.charCodeAt(index))
{
context.position -= index;
memos[uid] = false;
return false;
}
// memos[uid] = string;
if (parent)
parent.children.push(string);
return true;
case DOT:
if (context.position < input_length)
{
if (parent)
parent.children.push(context.input.charAt(context.position));
++context.position;
return true;
}
memos[uid] = false;
return false;
case POSITIVE_LOOK_AHEAD:
case NEGATIVE_LOOK_AHEAD:
var position = context.position,
result = evaluate(context, null, rules, rule[1]) === (type === POSITIVE_LOOK_AHEAD);
context.position = position;
memos[uid] = result;
return result;
case ZERO_OR_MORE:
var child,
position = context.position,
childCount = parent && parent.children.length;
while (evaluate(context, parent, rules, rule[1]))
{
position = context.position,
childCount = parent && parent.children.length;
}
context.position = position;
if (parent)
parent.children.length = childCount;
return true;
case ONE_OR_MORE:
var position = context.position,
childCount = parent && parent.children.length;
if (!evaluate(context, parent, rules, rule[1]))
{
memos[uid] = false;
context.position = position;
if (parent)
parent.children.length = childCount;
return false;
}
position = context.position,
childCount = parent && parent.children.length;
while (evaluate(context, parent, rules, rule[1]))
{
position = context.position;
childCount = parent && parent.children.length;
}
context.position = position;
if (parent)
parent.children.length = childCount;
return true;
case OPTIONAL:
var position = context.position,
childCount = parent && parent.children.length;
if (!evaluate(context, parent, rules, rule[1]))
{
context.position = position;
if (parent)
parent.children.length = childCount;
}
return true;
}
}
function SyntaxNode(/*String*/ aName, /*String*/ aSource, /*Number*/ aLocation, /*Number*/ aLength, /*String*/anErrorMessage)
{
this.name = aName;
this.source = aSource;
this.range = { location:aLocation, length:aLength };
this.children = [];
if (anErrorMessage)
this.error = anErrorMessage;
}
SyntaxNode.prototype.report = function()
{
var source = this.source,
lineNumber = 1,
index = 0,
start = 0,
length = source.length,
range = this.range;
for (; index < range.location; ++index)
if (source.charAt(index) === '\n')
{
++lineNumber;
start = index + 1;
}
for (; index < length; ++index)
if (source.charAt(index) === '\n')
break;
var visualization = source.substring(start, index) + "\n";
visualization += (new Array(this.range.location - start + 1)).join(" ");
visualization += (new Array(Math.min(range.length, visualization.length) + 1)).join("^");
return {
visualization: visualization,
lineNumber: lineNumber
};
}
SyntaxNode.prototype.message = function()
{
var report = this.report(),
message = report.visualization + "\n";
message += "ERROR line " + report.lineNumber + ": " + this.error;
return message;
}
SyntaxNode.prototype.toString = function(/*String*/ spaces)
{
if (!spaces)
spaces = "";
var string = spaces + this.name + " <" + this.innerText() + "> ",
children = this.children,
index = 0,
count = children.length;
for (; index < count; ++index)
{
var child = children[index];
if (typeof child === "string")
string += "\n" + spaces + "\t" + child;
else
string += "\n" + children[index].toString(spaces + '\t');
}
return string;
}
SyntaxNode.prototype.innerText = function()
{
var range = this.range;
return this.source.substr(range.location, range.length);
}
SyntaxNode.prototype.traverse = function(walker)
{
if (!walker.enteredNode || walker.enteredNode(this) !== false)
{
var children = this.children,
index = 0,
count = children && children.length;
for (; index < count; ++index)
{
var child = children[index];
if (typeof child !== "string")
child.traverse(walker);
else if (walker.traversesTextNodes)
{
walker.enteredNode(child);
if (walker.exitedNode)
walker.exitedNode(child);
}
}
}
if (walker.exitedNode)
walker.exitedNode(this);
}