-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.js
152 lines (128 loc) · 5.66 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
var ns = {};
ns.parse = function parse(tokens) {
tokens = ns.removeCommentsAndIndentedNewlines(tokens);
var statements = [];
var statement;
var cursor = 0;
while(!statement || statement.type != 'eof') {
statement = ns.parseStatement(tokens, cursor);
statements.push(statement);
cursor += statement.length;
}
var grammar = {};
statements.forEach(function(stmt) {
if(stmt.type == 'statement')
grammar[stmt.identifier] = stmt.expression;
});
return grammar;
}
ns.parseStatement = function parseStatement(tokens, cursor) {
var newlines = 0;
while(tokens[cursor+newlines].type == 'newline') ++newlines;
cursor += newlines;
if(tokens[cursor].type == 'eof') return { type: 'eof', length: 1 + newlines };
if(tokens[cursor].type != 'identifier') throw new Error('identifier expected: ' + tokens[cursor].id);
if(tokens[cursor+1].type == 'eof' || tokens[cursor+1].type != 'operator' || tokens[cursor+1].value != '=') throw new Error('\'=\' expected after ' + tokens[cursor].id);
if(tokens[cursor+2].type == 'eof') throw new Error('expression expected after ' + tokens[cursor+1].id);
var identifier = tokens[cursor].value;
var expression = ns.parseExpression(tokens, cursor+2, tokens.length);
var cursorAfterExpression = cursor+2+expression.length;
var tokenAfterExpression = tokens[cursorAfterExpression];
if(tokenAfterExpression.type == 'operator' && tokenAfterExpression.value == '=') {
if(tokens[cursorAfterExpression+1].type == 'function-body') {
expression.evaluate = tokens[cursorAfterExpression+1].value;
expression.length += 2;
}
else throw new Error('function-body expected, got ' + tokens[cursorAfterExpression+1].type);
}
return { type: 'statement', identifier: identifier, expression: expression, length: newlines + expression.length + 2 };
}
ns.parseExpression = function parseExpression(tokens, cursor, maxEnd, args /* inGroup: bool */) {
var start = cursor;
var end = cursor;
while(end < maxEnd && tokens[end].type != 'newline' && tokens[end].type != 'eof' && (tokens[end].type != 'operator' || tokens[end].value != '=')) ++end;
var groupedTokens = [];
for(cursor = start; cursor < end;) {
//TODO: Error whenever opening brackets differ from closing brackets
if(tokens[cursor].type == 'operator' && (tokens[cursor].value == ')' || tokens[cursor].value == ']')) {
if(args && args.inGroup) break;
else throw new Error('unexpected "' + tokens[cursor].value + '" at ' + tokens[cursor].id);
}
var group = ns.parseGroup(tokens, cursor, end);
if(group) {
groupedTokens.push(group);
cursor += group.length;
}
else {
groupedTokens.push(tokens[cursor]);
++cursor;
}
}
var repeatedTokens = [];
var repeatToken = null;
for(var i = 0; i < groupedTokens.length; ++i) {
if(groupedTokens[i].type == 'repetition-operator') {
if(repeatToken) throw new Error('unexpected double repetition-operator at ' + groupedTokens[i].id);
repeatToken = groupedTokens[i];
}
else {
if(repeatToken) {
repeatedTokens.push({ type: 'repeated-token', item: groupedTokens[i], minimum: repeatToken.minimum, maximum: repeatToken.maximum });
repeatToken = null;
}
else repeatedTokens.push(groupedTokens[i]);
}
}
var describedTokens = [];
var next;
var last;
for(var i = 0; i < repeatedTokens.length; ++i) {
if(repeatedTokens[i].type == 'descriptor-operator') {
next = repeatedTokens[i+1];
last = describedTokens.pop();
if(i == 0) throw new Error('unexpected descriptor-operator at ' + repeatedTokens[i].id);
if(!next || next.type != 'identifier') throw new Error('identifier expected at ' + (next && next.id));
describedTokens.push({ type: 'described-token', inner: last, descriptor: next.value });
++i;
}
else describedTokens.push(repeatedTokens[i]);
}
var alternatives = ns.parseAlternatives(describedTokens);
if(cursor >= end && args && args.inGroup) throw new Error('unexpected end of line at ' + end);
return { type: 'expression', length: cursor - start, alternatives: alternatives };
}
ns.parseGroup = function parseGroup(tokens, begin, maxEnd) {
if(!(tokens[begin].type == 'operator' && (tokens[begin].value == '(' || tokens[begin].value == '['))) return;
var isOptional = tokens[begin].value == '[';
var expression = ns.parseExpression(tokens, begin + 1, maxEnd, { inGroup: true, isOptional: isOptional });
return { type: 'group', expression: expression, isOptional: isOptional, length: expression.length + 2 };
}
ns.parseAlternatives = function parseAlternatives(tokens) {
if(tokens.length == 0) throw new Error('unexpected end of sequence ' + JSON.stringify(tokens, null, 2));
var sequences = [[]];
for(var i=0; i<tokens.length; ++i) {
if(tokens[i].type == 'operator' && tokens[i].value == '/') {
if(sequences[sequences.length-1].length == 0)
throw new Error('unexpected end of sequence at ' + tokens[i] && tokens[i].id)
sequences.push([]);
continue;
}
sequences[sequences.length-1].push(tokens[i]);
}
return sequences;
}
ns.trimCommentsAndIndentedNewlines = function trimCommentsAndIndentedNewlines(tokens) {
var cursor = 0;
while(tokens[cursor] && (tokens[cursor].type == 'comment' || (tokens[cursor].type == 'newline' && tokens[cursor].indent == true))) ++cursor;
return cursor;
}
ns.removeCommentsAndIndentedNewlines = function removeCommentsAndIndentedNewlines(tokens) {
var ret = [];
for(var i=0; i<tokens.length; ++i) {
if(tokens[i] && (tokens[i].type == 'comment' || (tokens[i].type == 'newline' && tokens[i].indent == true)))
continue;
ret.push(tokens[i]);
}
return ret;
}
module.exports = ns;