-
Notifications
You must be signed in to change notification settings - Fork 0
/
StructuralGrammar.js
300 lines (267 loc) · 8.82 KB
/
StructuralGrammar.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
// StucturalGrammar : StructuralFramework -> Module
StructuralGrammar = function(SF) {
// CodeCatalog Snippet http://www.codecatalog.net/323/2/
var for_kv = function(object, body) {
for (var k in object) {
if (object.hasOwnProperty(k)) {
body(k, object[k]);
}
}
};
// End CodeCatalog Snippet
// CodeCatalog Snippet http://www.codecatalog.net/378/2/
var regexp_tokenizer = function(tokens) {
return function(str) {
var bestMatch = null;
var bestFunc = null;
for_kv(tokens, function(k,v) {
var m = new RegExp('^' + k)(str);
if (m && (!bestMatch || m[0].length > bestMatch[0].length)) {
bestMatch = m;
bestFunc = v;
}
});
// don't match the whole string in case we are in the middle of typing a token
// we use \0 to mean "eof" so this will pass.
if (bestFunc && bestMatch[0].length < str.length) {
return [bestFunc(bestMatch), str.slice(bestMatch[0].length)];
}
else {
return null;
}
}
};
// End CodeCatalog Snippet
// CodeCatalog Snippet http://www.codecatalog.net/391/1/
var choice_tokenizer = function(tokenizers) {
return function(str) {
for (var i = 0; i < tokenizers.length; ++i) {
var tokresult = tokenizers[i](str);
if (tokresult) {
return tokresult;
}
}
return null;
};
};
// End CodeCatalog Snippet
// CodeCatalog Snippet http://www.codecatalog.net/16/3/
var elt = function(name, attrs) {
var r = $(document.createElement(name));
if (attrs) {
for (var i in attrs) {
r.attr(i, attrs[i]);
}
}
for (var i = 2; i < arguments.length; ++i) {
r.append(arguments[i]);
}
return r;
};
// End CodeCatalog Snippet
// CodeCatalog Snippet http://www.codecatalog.net/256/1/
var text_node = function(text) { return document.createTextNode(text) };
// End CodeCatalog Snippet
// CodeCatalog Snippet http://www.codecatalog.net/357/3/
var escape_for_regexp = function(text) {
return text.replace(/[\]\[\\^$*+?{}\.()|]/g, function(x) { return '\\' + x });
};
// End CodeCatalog Snippet
// CodeCatalog Snippet http://www.codecatalog.net/394/3/
var string_tokenizer = function(str, func) {
var d = {};
d[escape_for_regexp(str)] = func;
return regexp_tokenizer(d);
};
// End CodeCatalog Snippet
// CodeCatalog Snippet http://www.codecatalog.net/383/1/
var map_tokenizer = function(f, tokenizer) {
return function(str) {
var tokresult = tokenizer(str);
if (tokresult) {
return [f(tokresult[0]), tokresult[1]];
}
else {
return null;
}
};
};
// End CodeCatalog Snippet
var prefix_tokenizer = function(tokenizers) {
return function(str) {
var ret = [];
for (var i = 0; i < tokenizers.length; i++) {
var tokresult = tokenizers[i](str);
if (tokresult) {
ret.push(tokresult[0]);
str = tokresult[1];
}
else {
return [ ret, str ];
}
}
return ret;
};
};
var nonempty_choice_tokenizer = function(tokenizers) {
if (tokenizers.length == 0) { return function(str) { return null } }
if (tokenizers.length == 1) { return tokenizers[0] }
return function(str) {
for (var i = 0; i < tokenizers.length; i++) {
var tokresult = tokenizers[i](str);
if (tokresult && tokresult[1].length < str.length) {
return tokresult;
}
}
return null; // er.. if they *all* accept the empty string, then... er...
};
};
// CodeCatalog Snippet http://www.codecatalog.net/392/1/
var wrap_fields = function(wrapper, dict) {
var ret = {};
for_kv(dict, function(k,v) {
ret[k] = k in wrapper ? wrapper[k](v) : v;
});
return ret;
};
// End CodeCatalog Snippet
// CodeCatalog Snippet http://www.codecatalog.net/368/1/
var arguments_to_array = function(argobj) {
return Array.prototype.slice.call(argobj);
};
// End CodeCatalog Snippet
// A grammar is an object that maps nonterminal names to syntactic forms,
// where syntactic forms are built up as combinators.
// The final representation returned by these combinators is simply an
// SynClass representing that symbol.
// Our intermediate representation will be a simple open fixed point.
// Grammar = String -> (Grammar -> SynClass). We will pass the final grammar
// to a point in the grammar so it can refer to other nonterminals within itself.
// As an unintended bonus, we also get grammar inheritance.
//
// For the Grammar argument, we'll use a proper function instead of an object
// so we can do renaming during inheritance.
var $$ = {};
// a synclass for an "open box"...
// this should be separated into two concepts: classes of actual exprs,
// and classes of expr generators, or something.
var box_synclass = function(cls) {
return new SF.SynClass({
render: function() {
return elt('span', {'class': 'box'}, text_node(' '));
},
parse_prefix: cls.parse_prefix,
parse_insert: function() {
return cls.parse_prefix;
}
});
};
$$.sym = function(name) {
return function(grammar) { return grammar(name) }
};
var cursor = function(expr, pos) {
return new SF.Cursor(new SF.Zipper([], expr), pos);
};
var cons_context = function(cx, cursor) {
var cxs = [cx].concat(cursor.zipper.contexts);
return new SF.Cursor(new SF.Zipper(cxs, cursor.zipper.expr), cursor.pos);
};
$$.empty = function(grammar) {
var c = new SF.SynClass({
open: function() { return this.make([]) },
parse_prefix: function(str) {
return [ cursor(c.make([]), 0), str ]
}
});
return c;
};
$$.cls = function(clsname, syn) {
return function(grammar) {
return wrap_fields({
render: function(render) {
return elt('span', {'class': clsname}, render.apply(this, arguments));
}
}, syn(grammar));
};
};
$$.literal = function(str) {
return function(grammar) {
var c = new SF.SynClass({
open: function() {
return c.make([str]);
},
parse_prefix: string_tokenizer(str, function() {
return cursor(c.make([str]), 1);
})
});
return c;
};
};
$$.token = function(rx, canon) {
return function(grammar) {
var toks = {};
toks[rx.source] = function(m) {
var tok = typeof(canon) === 'undefined' ? m[0] : canon;
return cursor($$.literal(m[0])(grammar).make([tok]), 1);
};
var c = new SF.SynClass({
open: function() { return box_synclass(c).make([]) },
parse_prefix: regexp_tokenizer(toks)
});
return c;
};
};
$$.seq = function() {
var xs = arguments_to_array(arguments);
return function(grammar) {
var get_subsyms = function() { return xs.map(function(x) { return x(grammar) }) };
var c = new SF.SynClass({
open: function() {
return this.make(get_subsyms().map(function(x) { return x.open() }));
},
parse_prefix: function(str) {
var subsyms = get_subsyms();
var rs = subsyms.map(function(x) { return x.open() });
for (var i = 0; i < subsyms.length; i++) {
var tokresult = subsyms[i].parse_prefix(str);
if (tokresult) {
rs[i] = tokresult[0];
if (tokresult[1].length < str.length) { // consumed input
rs[i] = null;
var newcx = new SF.Context(c, rs);
return [ cons_context(newcx, tokresult[0]), tokresult[1] ]
}
}
else {
return null;
}
}
// no input consumed. the sequence accepts the empty string.
return [ cursor(c.make(rs), rs.length), str ];
}
});
return c;
};
};
$$.choice = function() {
if (arguments.length == 0) { throw "Empty choice node" }
if (arguments.length == 1) { return arguments[0] }
var xs = arguments_to_array(arguments);
return function(grammar) {
var c = new SF.SynClass({
open: function() {
return box_synclass(c).make([]);
},
parse_prefix: nonempty_choice_tokenizer(xs.map(function(x) {
return x(grammar).parse_prefix
}))
});
return c;
};
};
$$.grammar = function(top, grammar) {
var lookup = function(s) { return grammar[s](lookup) };
return lookup(top);
};
return $$;
};