1 |
|
2 | (function() {
|
3 | var Policy, Scope, config, directive, escodegen, esquery, hier, hierIs, kit, options, opts, path, rebuildDT, resolve, root, upStage;
|
4 |
|
5 | Scope = require("./scope").Scope;
|
6 |
|
7 | config = require("./config");
|
8 |
|
9 | kit = require("./kit");
|
10 |
|
11 | directive = require("./directives");
|
12 |
|
13 | esquery = require("esquery");
|
14 |
|
15 | escodegen = require("escodegen");
|
16 |
|
17 | resolve = require("resolve");
|
18 |
|
19 | path = require("path");
|
20 |
|
21 | root = module.exports = {};
|
22 |
|
23 | hier = {};
|
24 |
|
25 | hierIs = function(f, t) {
|
26 | var h, ht, i, j, len;
|
27 | (h = hier[f] != null ? hier[f] : hier[f] = [f]).push(t);
|
28 | ht = hier[t];
|
29 | if (ht != null) {
|
30 | for (j = 0, len = ht.length; j < len; j++) {
|
31 | i = ht[j];
|
32 | h.push(i);
|
33 | }
|
34 | }
|
35 | };
|
36 |
|
37 | hierIs("WhileStatement", ":loop");
|
38 |
|
39 | hierIs("DoWhileStatement", ":loop");
|
40 |
|
41 | hierIs("ForStatement", ":loop");
|
42 |
|
43 | hierIs("ForInStatement", ":loop");
|
44 |
|
45 | hierIs("VariableDeclaration", ":assignment");
|
46 |
|
47 | hierIs("FunctionExpression", "Function");
|
48 |
|
49 | hierIs("FunctionDeclaration", "Function");
|
50 |
|
51 | hierIs("AssignmentExpression", ":assignment");
|
52 |
|
53 | root.selector = {
|
54 | property: function(name, next) {
|
55 | var p;
|
56 | p = name.split(".");
|
57 | if (next == null) {
|
58 | next = function(v) {
|
59 | return v;
|
60 | };
|
61 | }
|
62 | return function(e) {
|
63 | var i, j, len;
|
64 | if (e == null) {
|
65 | return next();
|
66 | }
|
67 | for (j = 0, len = p.length; j < len; j++) {
|
68 | i = p[j];
|
69 | e = e[i];
|
70 | if (e == null) {
|
71 | break;
|
72 | }
|
73 | }
|
74 | return next(e);
|
75 | };
|
76 | },
|
77 | cases: function(sel) {
|
78 | return function(s) {
|
79 | var v;
|
80 | v = sel(s);
|
81 | if (v != null) {
|
82 | return this.cases[v];
|
83 | }
|
84 | };
|
85 | },
|
86 | prop: function(n) {
|
87 | return function(s) {
|
88 | var ref;
|
89 | return this.cases[(ref = s.generator) != null ? ref : false];
|
90 | };
|
91 | },
|
92 | generatorCall: function() {
|
93 | return false;
|
94 | },
|
95 | matchDeclName: function(s, x, tpref) {
|
96 | var m, n, r;
|
97 | m = this.match;
|
98 | n = kit.getId(s.id);
|
99 | r = (function() {
|
100 | var p;
|
101 | if (s.id != null) {
|
102 | if (n != null) {
|
103 | if ((m.name != null) && m.name[n]) {
|
104 | return true;
|
105 | }
|
106 | if ((m.qname != null) && m.qname[n]) {
|
107 | return true;
|
108 | }
|
109 | if ((m.postfix != null) && m.postfix[n[n.length - 1]]) {
|
110 | return true;
|
111 | }
|
112 | if ((m.prefix != null) && m.prefix[n[0]]) {
|
113 | return true;
|
114 | }
|
115 | }
|
116 | }
|
117 | if (s.type === "FunctionExpression" && (x.name != null)) {
|
118 | p = kit.getMembersPathIds(x.name);
|
119 | if (p.length) {
|
120 | n = p[p.length - 1];
|
121 | if (n != null) {
|
122 | if ((m.name != null) && m.name[n]) {
|
123 | return true;
|
124 | }
|
125 | if ((m.postfix != null) && m.postfix[n[n.length - 1]]) {
|
126 | return true;
|
127 | }
|
128 | if ((m.prefix != null) && m.prefix[n[0]]) {
|
129 | return true;
|
130 | }
|
131 | if ((m["package"] != null) && (p[0] != null) && m["package"][p[0]]) {
|
132 | return true;
|
133 | }
|
134 | if ((m.qname != null) && m.qname[p.join(".")]) {
|
135 | return true;
|
136 | }
|
137 | }
|
138 | }
|
139 | }
|
140 | return false;
|
141 | })();
|
142 | if (tpref != null) {
|
143 | console.log(tpref + ": match decl " + n + ": " + r);
|
144 | }
|
145 | if (r == null) {
|
146 | return;
|
147 | }
|
148 | return this.cases[r];
|
149 | },
|
150 | matchCallName: function(s, p, tpref) {
|
151 | var m, r;
|
152 | p = kit.getMembersPathIds(s.callee);
|
153 | m = this.match;
|
154 | r = (function() {
|
155 | var n;
|
156 | n = p[0];
|
157 | if (p.length === 1) {
|
158 | if (n != null) {
|
159 | if (m.libVar && n === config.packageVar) {
|
160 | return true;
|
161 | }
|
162 | }
|
163 | }
|
164 | if (m.libVar && p.length === 2 && n === config.packageName) {
|
165 | return false;
|
166 | }
|
167 | if (p.length > 0) {
|
168 | n = p[p.length - 1];
|
169 | if (n == null) {
|
170 | return false;
|
171 | }
|
172 | if ((m["package"] != null) && m["package"][p[0]]) {
|
173 | return true;
|
174 | }
|
175 | if ((m.name != null) && m.name[n]) {
|
176 | return true;
|
177 | }
|
178 | if ((m.postfix != null) && m.postfix[n[n.length - 1]]) {
|
179 | return true;
|
180 | }
|
181 | if ((m.prefix != null) && m.prefix[n[0]]) {
|
182 | return true;
|
183 | }
|
184 | if ((m.qname != null) && m.qname[p.join(".")]) {
|
185 | return true;
|
186 | }
|
187 | }
|
188 | return false;
|
189 | })();
|
190 | if (tpref != null) {
|
191 | console.log(tpref + ": match call " + (p.join('.')) + ": " + r);
|
192 | }
|
193 | if (r == null) {
|
194 | return;
|
195 | }
|
196 | return this.cases[r];
|
197 | }
|
198 | };
|
199 |
|
200 | rebuildDT = function(c) {
|
201 | var cn, copy, cv, ref, state, sv, tv, ty;
|
202 | copy = function(p, c) {
|
203 | var i, r, ref, v;
|
204 | if (!((p != null) && p.$)) {
|
205 | throw new Error("invalid transitions");
|
206 | }
|
207 | r = {};
|
208 | for (i in options) {
|
209 | if (p.$ == null) {
|
210 | throw new Error('invalid config');
|
211 | }
|
212 | v = (ref = c[i]) != null ? ref : p.$[i];
|
213 | if (v != null) {
|
214 | r[i] = v;
|
215 | }
|
216 | }
|
217 | return c.$ = r;
|
218 | };
|
219 | copy({
|
220 | $: {}
|
221 | }, c);
|
222 | for (state in c) {
|
223 | sv = c[state];
|
224 | if (!(state[0] !== '$' && !options[state])) {
|
225 | continue;
|
226 | }
|
227 | copy(c, sv);
|
228 | for (ty in sv) {
|
229 | tv = sv[ty];
|
230 | if (!(ty[0] !== '$' && !options[ty])) {
|
231 | continue;
|
232 | }
|
233 | copy(sv, tv);
|
234 | if ((tv.select != null) && (tv.select.substr != null)) {
|
235 | tv.select = upStage(root.selector, tv.select);
|
236 | }
|
237 | if (tv.cases != null) {
|
238 | ref = tv.cases;
|
239 | for (cn in ref) {
|
240 | cv = ref[cn];
|
241 | copy(tv, cv);
|
242 | }
|
243 | }
|
244 | }
|
245 | }
|
246 | };
|
247 |
|
248 | options = {
|
249 | bind: true,
|
250 | compile: true,
|
251 | coerce: true,
|
252 | expr: true,
|
253 | bindAssoc: true,
|
254 | block: true,
|
255 | loop: true,
|
256 | subScope: true,
|
257 | keepScope: true,
|
258 | varCapt: true,
|
259 | branch: true,
|
260 | ref: true,
|
261 | remove: true,
|
262 | keepForOf: true,
|
263 | mopt: true
|
264 | };
|
265 |
|
266 | Policy = (function() {
|
267 | function Policy(root1, start) {
|
268 | this.root = root1;
|
269 | if (this.root == null) {
|
270 | this.root = config.states;
|
271 | }
|
272 | this.state = start != null ? start : config.start;
|
273 | this.opts = this.root[this.state];
|
274 | this.rebuild();
|
275 | }
|
276 |
|
277 | Policy.prototype.transit = function(n) {
|
278 | var cur;
|
279 | if (config.policyTrace) {
|
280 | console.log("policy: transit to " + n);
|
281 | }
|
282 | this.state = n;
|
283 | cur = this.root[n];
|
284 | if (cur == null) {
|
285 | throw new Error("no state " + n + " defined");
|
286 | }
|
287 | this.opts = cur.$;
|
288 | return this;
|
289 | };
|
290 |
|
291 | Policy.prototype.nameDetails = function(name1, op) {
|
292 | this.name = name1;
|
293 | this.op = op;
|
294 | return this;
|
295 | };
|
296 |
|
297 | Policy.prototype.scope = function(f) {
|
298 | var oldRoot, oldState, res;
|
299 | oldRoot = this.root;
|
300 | this.root = kit.merge({}, oldRoot);
|
301 | oldState = this.state;
|
302 | res = f();
|
303 | this.state = oldState;
|
304 | this.root = oldRoot;
|
305 | this.opts = this.root[this.state].$;
|
306 | return res;
|
307 | };
|
308 |
|
309 | Policy.prototype._libRequire = function() {
|
310 | if ((this.name != null) && this.name.type === "Identifier") {
|
311 | config.packageVar = this.name.name;
|
312 | return this.transit(config.start);
|
313 | }
|
314 | };
|
315 |
|
316 | Policy.prototype.rebuild = function(changes) {
|
317 | if (changes != null) {
|
318 | kit.merge(this.root, changes);
|
319 | }
|
320 | rebuildDT(this.root);
|
321 | return this.opts = this.root[this.state].$;
|
322 | };
|
323 |
|
324 | Policy.prototype.item = function(s, fun) {
|
325 | var cur, h, i, index, j, jump, len, n, oldName, oldOp, oldOpts, oldState, ref, ref1, ref2, res, sel, selv, tpref, trace;
|
326 | trace = config.policyTrace;
|
327 | if (trace) {
|
328 | tpref = "polciy:" + (kit.shortNodeDescr(s));
|
329 | console.log(tpref + ": enter at " + this.state);
|
330 | if (this.name != null) {
|
331 | console.log(tpref + ": name " + this.name);
|
332 | }
|
333 | if (this.op != null) {
|
334 | console.log(tpref + ": op " + this.op);
|
335 | }
|
336 | }
|
337 | cur = this.root[this.state];
|
338 | h = (ref = hier[s.type]) != null ? ref : [s.type];
|
339 | for (j = 0, len = h.length; j < len; j++) {
|
340 | i = h[j];
|
341 | if (!(n = cur[i])) {
|
342 | continue;
|
343 | }
|
344 | cur = n;
|
345 | if (trace) {
|
346 | console.log(tpref + ": by type " + i + ": " + (JSON.stringify(cur)));
|
347 | }
|
348 | break;
|
349 | }
|
350 | sel = cur.select;
|
351 | if (sel != null) {
|
352 | selv = sel.call(cur, s, this, tpref);
|
353 | if (trace) {
|
354 | console.log(tpref + ": selector " + (JSON.stringify(selv)));
|
355 | }
|
356 | if (selv != null) {
|
357 | cur = selv;
|
358 | }
|
359 | }
|
360 | if (cur.move != null) {
|
361 | if (trace) {
|
362 | console.log(n + ": moving to " + cur.move);
|
363 | }
|
364 | return this.transit(cur.move).item(s, fun);
|
365 | }
|
366 | jump = (ref1 = (ref2 = cur.sub) != null ? ref2 : cur.next) != null ? ref1 : cur.inner;
|
367 | if (trace) {
|
368 | if (cur.sub) {
|
369 | console.log(tpref + ": sub-state " + cur.sub);
|
370 | }
|
371 | if (cur.inner) {
|
372 | console.log(tpref + ": inner state " + cur.inner);
|
373 | }
|
374 | if (cur.next) {
|
375 | console.log(tpref + ": next state " + cur.next);
|
376 | }
|
377 | }
|
378 | if (jump != null) {
|
379 | if (jump === cur.sub) {
|
380 | oldState = this.state;
|
381 | }
|
382 | this.state = jump;
|
383 | if (jump !== cur.inner) {
|
384 | cur = this.root[jump];
|
385 | }
|
386 | } else if (s.type === "BlockStatement") {
|
387 | oldState = this.state;
|
388 | }
|
389 | oldOp = this.op;
|
390 | oldOpts = this.opts;
|
391 | this.opts = cur.$;
|
392 | if (trace) {
|
393 | console.log(tpref + ": opts " + (JSON.stringify(this.opts)));
|
394 | }
|
395 | oldName = this.name;
|
396 | this.libVar = false;
|
397 | index = this.index;
|
398 | this.index = 0;
|
399 | res = fun();
|
400 | this.index = index + 1;
|
401 | this.name = oldName;
|
402 | this.op = oldOp;
|
403 | if (oldState != null) {
|
404 | if (trace) {
|
405 | console.log(tpref + ": restoring state " + oldState);
|
406 | }
|
407 | this.state = oldState;
|
408 | }
|
409 | this.opts = oldOpts;
|
410 | if (trace) {
|
411 | console.log(tpref + ": restoring opts " + (JSON.stringify(oldOpts)));
|
412 | }
|
413 | return res;
|
414 | };
|
415 |
|
416 | return Policy;
|
417 |
|
418 | })();
|
419 |
|
420 | Scope.prototype.option = function(v) {
|
421 | this.policy.rebuild(v);
|
422 | return this.updateVisitor();
|
423 | };
|
424 |
|
425 | Scope.prototype.profile = function(v) {
|
426 | if (v.substr != null) {
|
427 | this.policy.transit(v);
|
428 | this.updateVisitor();
|
429 | return;
|
430 | }
|
431 | throw new Error("cannot interpret profile " + v);
|
432 | };
|
433 |
|
434 | root.optionEnv = Object.create(root.selector);
|
435 |
|
436 | upStage = function(env, str) {
|
437 | var args, fun, i, v, vals;
|
438 | args = [];
|
439 | vals = [];
|
440 | for (i in env) {
|
441 | v = env[i];
|
442 | args.push(i);
|
443 | vals.push(v);
|
444 | }
|
445 | args.push("return " + str + ";");
|
446 | fun = (function(func, args, ctor) {
|
447 | ctor.prototype = func.prototype;
|
448 | var child = new ctor, result = func.apply(child, args);
|
449 | return Object(result) === result ? result : child;
|
450 | })(Function, args, function(){});
|
451 | return fun.apply(env, vals);
|
452 | };
|
453 |
|
454 | opts = directive.option = function(e) {
|
455 | var i, j, len, ref;
|
456 | ref = e["arguments"];
|
457 | for (j = 0, len = ref.length; j < len; j++) {
|
458 | i = ref[j];
|
459 | if (i.type === "Literal" && (e.value != null) && (e.value.substr != null)) {
|
460 | upStage(env, e.value);
|
461 | } else if (i.type === "ObjectExpression") {
|
462 | this.option(upStage(root.optionEnv, escodegen.generate(i)));
|
463 | } else {
|
464 | throw kit.exprError("cannot interpret option " + (escodegen.generate(i)), i);
|
465 | }
|
466 | }
|
467 | return this.emptyNode();
|
468 | };
|
469 |
|
470 | directive.profile = function(e, arg) {
|
471 | this.profile(kit.toStr(arg));
|
472 | return this.emptyNode();
|
473 | };
|
474 |
|
475 | Scope.prototype.createPolicy = function() {
|
476 | return new Policy();
|
477 | };
|
478 |
|
479 | directive.require = function(e, c) {
|
480 | var core, cp, cs, error, f, m, n, varname;
|
481 | cs = kit.exprToStr(c);
|
482 | f = resolve.sync(cs, {
|
483 | basedir: path.dirname(config.filename)
|
484 | });
|
485 | try {
|
486 | cp = resolve.sync(config.packageName, {
|
487 | basedir: path.dirname(config.filename)
|
488 | });
|
489 | core = require(cp);
|
490 | core.compileTime = true;
|
491 | } catch (error) {
|
492 | e = error;
|
493 | console.warn("couldn't load library ", config.packageName, e);
|
494 | }
|
495 | m = require(f);
|
496 | if ((m != null) && (m._compile != null) && (m._compile.call != null)) {
|
497 | n = this.policy.name;
|
498 | if (n.type === "Identifier") {
|
499 | varname = n.name;
|
500 | }
|
501 | m._compile.call(this, [varname]);
|
502 | }
|
503 | return this.pureExprNode(kit.call(kit.id("require"), [c]));
|
504 | };
|
505 |
|
506 | directive.ref = function(e) {
|
507 | var a, i, j, len, ref;
|
508 | ref = e["arguments"];
|
509 | for (j = 0, len = ref.length; j < len; j++) {
|
510 | a = ref[j];
|
511 | i = kit.getId(a);
|
512 | if (i == null) {
|
513 | throw kit.exprError('expected identifiers', a);
|
514 | }
|
515 | this.refs[i] = true;
|
516 | }
|
517 | return this.emptyNode();
|
518 | };
|
519 |
|
520 | }).call(this);
|