UNPKG

nodulator

Version:

Complete NodeJS Framework for Restfull APIs

1,613 lines (1,611 loc) 157 kB
// Generated by LiveScript 1.4.0 var fold, ref$, nameFromPath, stripString, SourceNode, SourceMapGenerator, sn, snEmpty, snSafe, snRemoveLeft, snAutofill, Node, Negatable, Block, Atom, Literal, Var, Key, Index, Slice, Chain, Call, List, Obj, Prop, Arr, Yield, Unary, Binary, Assign, Import, In, Existence, Fun, Class, Super, Parens, Splat, Jump, Throw, Return, While, For, StepSlice, Try, Switch, Case, If, Label, Cascade, JS, Require, Util, Vars, CopyL, DECLS, UTILS, LEVEL_TOP, LEVEL_PAREN, LEVEL_LIST, LEVEL_COND, LEVEL_OP, LEVEL_CALL, PREC, TAB, ID, SIMPLENUM, slice$ = [].slice, toString$ = {}.toString; fold = require('prelude-ls').fold; ref$ = require('./util'), nameFromPath = ref$.nameFromPath, stripString = ref$.stripString; ref$ = require('source-map'), SourceNode = ref$.SourceNode, SourceMapGenerator = ref$.SourceMapGenerator; sn = function(node){ var parts, result, e; node == null && (node = {}); parts = slice$.call(arguments, 1); try { result = new SourceNode(node.line, node.column, null, parts); result.displayName = node.constructor.displayName; return result; } catch (e$) { e = e$; console.dir(parts); throw e; } }; snEmpty = function(node){ var i$, ref$, len$, child; if (node instanceof SourceNode) { for (i$ = 0, len$ = (ref$ = node.children).length; i$ < len$; ++i$) { child = ref$[i$]; if (!snEmpty(child)) { return false; } } return true; } else { return !node; } }; snSafe = function(code){ if (code instanceof SourceNode) { return code; } else { return code.toString(); } }; snRemoveLeft = function(node, count){ var i$, to$, i, child; for (i$ = 0, to$ = node.children.length; i$ < to$; ++i$) { i = i$; child = node.children[i]; if (child instanceof SourceNode) { count = snRemoveLeft(child, count); } else { child = child.toString(); node.children[i] = child.slice(count); count -= child.length; } if (count <= 0) { return 0; } } return count; }; snAutofill = function(node, parents){ var i$, len$, p, ref$, child; parents == null && (parents = []); if (node instanceof SourceNode) { if (node.line) { for (i$ = 0, len$ = parents.length; i$ < len$; ++i$) { p = parents[i$]; p.line = node.line; p.column = node.column; } parents.length = 0; } else { parents.push(node); } for (i$ = 0, len$ = (ref$ = node.children).length; i$ < len$; ++i$) { child = ref$[i$]; snAutofill(child, parents); } } return node; }; SourceNode.prototype.replace = function(){ var args; args = slice$.call(arguments); return new SourceNode(this.line, this.column, this.source, (function(){ var i$, x$, ref$, len$, results$ = []; for (i$ = 0, len$ = (ref$ = this.children).length; i$ < len$; ++i$) { x$ = ref$[i$]; results$.push(x$.replace.apply(x$, args)); } return results$; }.call(this)), this.name); }; SourceNode.prototype.setFile = function(filename){ var i$, ref$, len$, child, results$ = []; this.source = filename; for (i$ = 0, len$ = (ref$ = this.children).length; i$ < len$; ++i$) { child = ref$[i$]; if (child instanceof SourceNode) { results$.push(child.setFile(filename)); } } return results$; }; SourceNode.prototype.toStringWithSourceMap = function(){ var args, gen, genLine, genColumn, stack, code, debugOutput, debugIndent, debugIndentStr, genForNode; args = slice$.call(arguments); gen = (function(func, args, ctor) { ctor.prototype = func.prototype; var child = new ctor, result = func.apply(child, args), t; return (t = typeof result) == "object" || t == "function" ? result || child : child; })(SourceMapGenerator, args, function(){}); genLine = 1; genColumn = 0; stack = []; code = ''; debugOutput = ''; debugIndent = ''; debugIndentStr = ' '; genForNode = function(node){ var valid, i$, ref$, len$, child, cur, to$, i, c, results$ = []; if (node instanceof SourceNode) { debugOutput += debugIndent + node.displayName; valid = node.line && 'column' in node; if (valid) { stack.push(node); debugOutput += '!'; } debugOutput += " " + node.line + ":" + node.column + " " + genLine + ":" + genColumn + "\n"; debugIndent += debugIndentStr; for (i$ = 0, len$ = (ref$ = node.children).length; i$ < len$; ++i$) { child = ref$[i$]; genForNode(child); } debugIndent = debugIndent.slice(0, debugIndent.length - debugIndentStr.length); if (valid) { return stack.pop(); } } else { debugOutput += debugIndent + "" + JSON.stringify(node) + "\n"; code += node; cur = stack[stack.length - 1]; if (cur) { gen.addMapping({ source: cur.source, original: { line: cur.line, column: cur.column }, generated: { line: genLine, column: genColumn }, name: cur.name }); } for (i$ = 0, to$ = node.length; i$ < to$; ++i$) { i = i$; c = node.charAt(i); if (c === "\n") { genColumn = 0; ++genLine; if (cur) { results$.push(gen.addMapping({ source: cur.source, original: { line: cur.line, column: cur.column }, generated: { line: genLine, column: genColumn }, name: cur.name })); } } else { results$.push(++genColumn); } } return results$; } }; genForNode(this); return { code: code, map: gen, debug: debugOutput }; }; /* # Use this to track down places where a SourceNode is being converted into a string and causing the location to be lost tmp-to-string = SourceNode::to-string SourceNode::to-string = (...args) -> console.log("toString(): ", new Error().stack) tmp-to-string.apply this, args */ (Node = function(){ throw Error('unimplemented'); }).prototype = { compile: function(options, level){ var o, node, code, that, i$, len$, tmp; o = import$({}, options); if (level != null) { o.level = level; } node = this.unfoldSoak(o) || this; if (o.level && node.isStatement()) { return node.compileClosure(o); } code = (node.tab = o.indent, node).compileNode(o); if (that = node.temps) { for (i$ = 0, len$ = that.length; i$ < len$; ++i$) { tmp = that[i$]; o.scope.free(tmp); } } return code; }, compileClosure: function(o){ var that, fun, call, hasArgs, hasThis, out; if (that = this.getJump()) { that.carp('inconvertible statement'); } fun = Fun([], Block(this)); call = Call(); if (o.inGenerator) { fun.generator = true; } this.traverseChildren(function(it){ switch (it.value) { case 'this': hasThis = true; break; case 'arguments': hasArgs = it.value = 'args$'; } }); if (hasThis) { call.args.push(Literal('this')); call.method = '.call'; } if (hasArgs) { call.args.push(Literal('arguments')); fun.params.push(Var('args$')); } out = Parens(Chain((fun.wrapper = true, fun['void'] = this['void'], fun), [call]), true); if (o.inGenerator) { out = new Yield('yieldfrom', out); } return out.compile(o); }, compileBlock: function(o, node){ var code; if (!snEmpty(code = node != null ? node.compile(o, LEVEL_TOP) : void 8)) { return sn(null, "{\n", code, "\n" + this.tab + "}"); } else { return sn(node, '{}'); } }, cache: function(o, once, level){ var ref$, sub, ref; if (!this.isComplex()) { return [ref$ = level != null ? this.compile(o, level) : this, ref$]; } sub = Assign(ref = Var(o.scope.temporary()), this); if (level != null) { sub = sub.compile(o, level); if (once) { o.scope.free(ref.value); } return [sub, ref.value]; } if (once) { return [sub, (ref.temp = true, ref)]; } else { return [sub, ref, [ref.value]]; } }, compileLoopReference: function(o, name, ret){ var ref$, asn, tmp; if (this instanceof Var && o.scope.check(this.value) || this instanceof Unary && ((ref$ = this.op) === '+' || ref$ === '-') && (-1 / 0 < (ref$ = +this.it.value) && ref$ < 1 / 0) || this instanceof Literal && !this.isComplex()) { return [ref$ = this.compile(o), ref$]; } asn = Assign(Var(tmp = o.scope.temporary(name)), this); ret || (asn['void'] = true); return [tmp, asn.compile(o, ret ? LEVEL_CALL : LEVEL_PAREN)]; }, eachChild: function(fn){ var i$, ref$, len$, name, child, j$, len1$, i, node, that; for (i$ = 0, len$ = (ref$ = this.children).length; i$ < len$; ++i$) { name = ref$[i$]; if (child = this[name]) { if ('length' in child) { for (j$ = 0, len1$ = child.length; j$ < len1$; ++j$) { i = j$; node = child[j$]; if (that = fn(node, name, i)) { return that; } } } else { if ((that = fn(child, name)) != null) { return that; } } } } }, traverseChildren: function(fn, xscope){ var this$ = this; return this.eachChild(function(node, name, index){ var ref$; return (ref$ = fn(node, this$, name, index)) != null ? ref$ : node.traverseChildren(fn, xscope); }); }, anaphorize: function(){ var base, name, ref$; this.children = this.aTargets; if (this.eachChild(hasThat)) { if ((base = this)[name = this.aSource] instanceof Existence) { base = base[name]; name = 'it'; } if (base[name].value !== 'that') { base[name] = Assign(Var('that'), base[name]); } } function hasThat(it){ var that; return it.value === 'that' || ((that = it.aSource) ? (that = it[that]) ? hasThat(that) : void 8 : it.eachChild(hasThat)); } delete this.children; return ref$ = this[this.aSource], ref$.cond = true, ref$; }, carp: function(msg, type){ type == null && (type = SyntaxError); throw type(msg + " on line " + (this.line || this.traverseChildren(function(it){ return it.line; }))); }, delegate: function(names, fn){ var i$, len$; for (i$ = 0, len$ = names.length; i$ < len$; ++i$) { (fn$.call(this, names[i$])); } function fn$(name){ this[name] = function(it){ return fn.call(this, name, it); }; } }, children: [], terminator: ';', isComplex: YES, isStatement: NO, isAssignable: NO, isCallable: NO, isEmpty: NO, isArray: NO, isString: NO, isRegex: NO, isMatcher: function(){ return this.isString() || this.isRegex(); }, assigns: NO, ripName: VOID, unfoldSoak: VOID, unfoldAssign: VOID, unparen: THIS, unwrap: THIS, maybeKey: THIS, expandSlice: THIS, varName: String, getAccessors: VOID, getCall: VOID, getDefault: VOID, getJump: VOID, invert: function(){ return Unary('!', this, true); }, invertCheck: function(it){ if (it.inverted) { return this.invert(); } else { return this; } }, addElse: function($else){ this['else'] = $else; return this; }, makeReturn: function(ref, obj){ var items, kv, i, v; if (obj) { items = this instanceof Arr ? (this.items[0] == null || this.items[1] == null && this.carp('must specify both key and value for object comprehension'), this.items) : (kv = 'keyValue$', (function(){ var i$, ref$, len$, results$ = []; for (i$ = 0, len$ = (ref$ = [Assign(Var(kv), this), Var(kv)]).length; i$ < len$; ++i$) { i = i$; v = ref$[i$]; results$.push(Chain(v).add(Index(Literal(i)))); } return results$; }.call(this))); return Assign(Chain(Var(ref)).add(Index(items[0], '.', true)), items[1]); } else if (ref) { return Call.make(JS(ref + '.push'), [this]); } else { return Return(this); } }, show: String, toString: function(idt){ var tree, that; idt || (idt = ''); tree = '\n' + idt + this.constructor.displayName; if (that = this.show()) { tree += ' ' + that; } this.eachChild(function(it){ tree += it.toString(idt + TAB); }); return tree; }, stringify: function(space){ return JSON.stringify(this, null, space); }, toJSON: function(){ return import$({ type: this.constructor.displayName }, this); } }; exports.parse = function(json){ return exports.fromJSON(JSON.parse(json)); }; exports.fromJSON = (function(){ function fromJSON(it){ var that, node, key, val, i$, len$, v, results$ = []; if (!(it && typeof it === 'object')) { return it; } if (that = it.type) { node = clone$(exports[that].prototype); for (key in it) { val = it[key]; node[key] = fromJSON(val); } return node; } if (it.length != null) { for (i$ = 0, len$ = it.length; i$ < len$; ++i$) { v = it[i$]; results$.push(fromJSON(v)); } return results$; } else { return it; } } return fromJSON; }()); Negatable = { show: function(){ return this.negated && '!'; }, invert: function(){ this.negated = !this.negated; return this; } }; exports.Block = Block = (function(superclass){ var prototype = extend$((import$(Block, superclass).displayName = 'Block', Block), superclass).prototype, constructor = Block; function Block(body){ var this$ = this instanceof ctor$ ? this : new ctor$; body || (body = []); if ('length' in body) { this$.lines = body; } else { this$.lines = []; this$.add(body); } return this$; } function ctor$(){} ctor$.prototype = prototype; prototype.children = ['lines']; prototype.toJSON = function(){ delete this.back; return superclass.prototype.toJSON.call(this); }; prototype.add = function(it){ var that, ref$; it = it.unparen(); switch (false) { case !(that = this.back): that.add(it); break; case !(that = it.lines): (ref$ = this.lines).push.apply(ref$, that); break; default: this.lines.push(it); if (that = (ref$ = it.back, delete it.back, ref$)) { this.back = that; } } return this; }; prototype.prepend = function(){ var ref$; (ref$ = this.lines).splice.apply(ref$, [this.neck(), 0].concat(slice$.call(arguments))); return this; }; prototype.pipe = function(target, type){ var args; args = type === '|>' ? this.lines.pop() : target; if (toString$.call(args).slice(8, -1) !== 'Array') { args = [args]; } switch (type) { case '|>': this.lines.push(Call.make(target, args, { pipe: true })); break; case '<|': this.lines.push(Call.make(this.lines.pop(), args)); } return this; }; prototype.unwrap = function(){ if (this.lines.length === 1) { return this.lines[0]; } else { return this; } }; prototype.chomp = function(){ var lines, i, that; lines = this.lines; i = lines.length; while (that = lines[--i]) { if (!that.comment) { break; } } lines.length = i + 1; return this; }; prototype.neck = function(){ var pos, i$, ref$, len$, x; pos = 0; for (i$ = 0, len$ = (ref$ = this.lines).length; i$ < len$; ++i$) { x = ref$[i$]; if (!(x.comment || x instanceof Literal)) { break; } ++pos; } return pos; }; prototype.isComplex = function(){ var ref$; return this.lines.length > 1 || ((ref$ = this.lines[0]) != null ? ref$.isComplex() : void 8); }; prototype.delegate(['isCallable', 'isArray', 'isString', 'isRegex'], function(it){ var ref$, ref1$; return (ref$ = (ref1$ = this.lines)[ref1$.length - 1]) != null ? ref$[it]() : void 8; }); prototype.getJump = function(it){ var i$, ref$, len$, node, that; for (i$ = 0, len$ = (ref$ = this.lines).length; i$ < len$; ++i$) { node = ref$[i$]; if (that = node.getJump(it)) { return that; } } }; prototype.makeReturn = function(){ var that, ref$, key$, ref1$; this.chomp(); if (that = (ref1$ = ref$ = this.lines)[key$ = ref1$.length - 1] != null ? ref$[key$] = (ref$ = ref$[key$]).makeReturn.apply(ref$, arguments) : void 8) { if (that instanceof Return && !that.it) { --this.lines.length; } } return this; }; prototype.compile = function(o, level){ var tab, codes, i$, ref$, len$, node, code; level == null && (level = o.level); if (level) { return this.compileExpressions(o, level); } o.block = this; tab = o.indent; codes = []; for (i$ = 0, len$ = (ref$ = this.lines).length; i$ < len$; ++i$) { node = ref$[i$]; node = node.unfoldSoak(o) || node; if (snEmpty(code = (node.front = true, node).compile(o, level))) { continue; } codes.push(tab); codes.push(code); node.isStatement() || codes.push(node.terminator); codes.push('\n'); } codes.pop(); return sn.apply(null, [null].concat(slice$.call(codes))); }; prototype.compileRoot = function(options){ var o, saveTo, ref$, bare, prefix, ref1$, code, result; o = (import$({ level: LEVEL_TOP, scope: this.scope = Scope.root = new Scope }, options)); if (saveTo = (ref$ = o.saveScope, delete o.saveScope, ref$)) { o.scope = saveTo.savedScope || (saveTo.savedScope = o.scope); } delete o.filename; o.indent = (bare = (ref$ = o.bare, delete o.bare, ref$)) ? '' : TAB; if (/^\s*(?:[/#]|javascript:)/.test((ref$ = this.lines[0]) != null ? ref$.code : void 8)) { prefix = this.lines.shift().code + '\n'; } if ((ref1$ = o.eval, delete o.eval, ref1$) && this.chomp().lines.length) { if (bare) { this.lines.push(Parens(this.lines.pop())); } else { this.makeReturn(); } } code = [this.compileWithDeclarations(o)]; bare || (code = ["(function(){\n"].concat(slice$.call(code), ["\n}).call(this);\n"])); return result = sn.apply(null, [null, prefix || []].concat(slice$.call(code))); }; prototype.compileWithDeclarations = function(o){ var pre, i, rest, post, that; o.level = LEVEL_TOP; pre = []; if (i = this.neck()) { rest = this.lines.splice(i, 9e9); pre = [this.compile(o), "\n"]; this.lines = rest; } if (snEmpty(post = this.compile(o))) { return sn(this, pre[0] || []); } return sn.apply(null, [null].concat(slice$.call(pre), [(that = this.scope) ? that.emit(post, o.indent) : post])); }; prototype.compileExpressions = function(o, level){ var lines, i, that, code, last, i$, len$, node; lines = this.chomp().lines; i = -1; while (that = lines[++i]) { if (that.comment) { lines.splice(i--, 1); } } if (!lines.length) { lines.push(Literal('void')); } lines[0].front = this.front; lines[lines.length - 1]['void'] = this['void']; if (!lines[1]) { return lines[0].compile(o, level); } code = []; last = lines.pop(); for (i$ = 0, len$ = lines.length; i$ < len$; ++i$) { node = lines[i$]; code.push((node['void'] = true, node).compile(o, LEVEL_PAREN), ', '); } code.push(last.compile(o, LEVEL_PAREN)); if (level < LEVEL_LIST) { return sn.apply(null, [null].concat(slice$.call(code))); } else { return sn.apply(null, [null, "("].concat(slice$.call(code), [")"])); } }; return Block; }(Node)); Atom = (function(superclass){ var prototype = extend$((import$(Atom, superclass).displayName = 'Atom', Atom), superclass).prototype, constructor = Atom; prototype.show = function(){ return this.value; }; prototype.isComplex = NO; function Atom(){ Atom.superclass.apply(this, arguments); } return Atom; }(Node)); exports.Literal = Literal = (function(superclass){ var prototype = extend$((import$(Literal, superclass).displayName = 'Literal', Literal), superclass).prototype, constructor = Literal; function Literal(value){ var this$ = this instanceof ctor$ ? this : new ctor$; this$.value = value; if (value.js) { return JS(value + "", true); } if (value === 'super') { return new Super; } return this$; } function ctor$(){} ctor$.prototype = prototype; prototype.isEmpty = function(){ var ref$; return (ref$ = this.value) === 'void' || ref$ === 'null'; }; prototype.isCallable = function(){ var ref$; return (ref$ = this.value) === 'this' || ref$ === 'eval' || ref$ === '..'; }; prototype.isString = function(){ return 0 <= '\'"'.indexOf((this.value + "").charAt()); }; prototype.isRegex = function(){ return (this.value + "").charAt() === '/'; }; prototype.isComplex = function(){ return this.isRegex() || this.value === 'debugger'; }; prototype.isWhat = function(){ switch (false) { case !this.isEmpty(): return 'empty'; case !this.isCallable(): return 'callable'; case !this.isString(): return 'string'; case !this.isRegex(): return 'regex'; case !this.isComplex(): return 'complex'; default: } }; prototype.varName = function(){ if (/^\w+$/.test(this.value)) { return '$' + this.value; } else { return ''; } }; prototype.makeReturn = function(it){ if (!it && this.value === 'debugger') { return this; } else { return superclass.prototype.makeReturn.apply(this, arguments); } }; prototype.compile = function(o, level){ var val, ref$; level == null && (level = o.level); switch (val = this.value + "") { case 'this': return sn(this, ((ref$ = o.scope.fun) != null ? ref$.bound : void 8) || val); case 'void': if (!level) { return sn(this, ''); } val += ' 8'; // fallthrough case 'null': if (level === LEVEL_CALL) { this.carp('invalid use of ' + this.value); } break; case 'on': case 'yes': val = 'true'; break; case 'off': case 'no': val = 'false'; break; case '*': this.carp('stray star'); break; case '..': if (!(val = o.ref)) { this.carp('stray reference'); } this.cascadee || (val.erred = true); break; case 'debugger': if (level) { return sn(this, "(function(){ debugger; }())"); } } return sn(this, snSafe(val)); }; return Literal; }(Atom)); exports.Var = Var = (function(superclass){ var prototype = extend$((import$(Var, superclass).displayName = 'Var', Var), superclass).prototype, constructor = Var; function Var(value){ var this$ = this instanceof ctor$ ? this : new ctor$; this$.value = value; return this$; } function ctor$(){} ctor$.prototype = prototype; prototype.isAssignable = prototype.isCallable = YES; prototype.assigns = function(it){ return it === this.value; }; prototype.maybeKey = function(){ var ref$; return ref$ = Key(this.value), ref$.line = this.line, ref$; }; prototype.varName = prototype.show; prototype.compile = function(o){ return sn(this, this.temp ? o.scope.free(this.value) : this.value); }; return Var; }(Atom)); exports.Key = Key = (function(superclass){ var prototype = extend$((import$(Key, superclass).displayName = 'Key', Key), superclass).prototype, constructor = Key; function Key(name, reserved){ var this$ = this instanceof ctor$ ? this : new ctor$; this$.reserved = reserved || name.reserved; this$.name = '' + name; return this$; } function ctor$(){} ctor$.prototype = prototype; prototype.isComplex = NO; prototype.assigns = function(it){ return it === this.name; }; prototype.varName = function(){ var name; name = this.name; if (this.reserved || (name === 'arguments' || name === 'eval')) { return "$" + name; } else { return name; } }; prototype.show = function(){ if (this.reserved) { return "'" + this.name + "'"; } else { return this.name; } }; prototype.compile = function(){ return sn(this, this.show()); }; return Key; }(Node)); exports.Index = Index = (function(superclass){ var prototype = extend$((import$(Index, superclass).displayName = 'Index', Index), superclass).prototype, constructor = Index; function Index(key, symbol, init){ var k, this$ = this instanceof ctor$ ? this : new ctor$; symbol || (symbol = '.'); if (init && key instanceof Arr) { switch (key.items.length) { case 1: if (!((k = key.items[0]) instanceof Splat)) { key = Parens(k); } } } switch (symbol) { case '[]': this$.vivify = Arr; break; case '{}': this$.vivify = Obj; break; default: if ('=' === symbol.slice(-1)) { this$.assign = symbol.slice(1); } } this$.key = key; this$.symbol = symbol; return this$; } function ctor$(){} ctor$.prototype = prototype; prototype.children = ['key']; prototype.show = function(){ return [this.soak ? '?' : void 8] + this.symbol; }; prototype.isComplex = function(){ return this.key.isComplex(); }; prototype.varName = function(){ var ref$; return ((ref$ = this.key) instanceof Key || ref$ instanceof Literal) && this.key.varName(); }; prototype.compile = function(o){ var code; code = this.key.compile(o, LEVEL_PAREN); if (this.key instanceof Key && '\'' !== code.toString().charAt(0)) { return sn(this, ".", code); } else { return sn(this, "[", code, "]"); } }; return Index; }(Node)); exports.Slice = Slice = (function(superclass){ var prototype = extend$((import$(Slice, superclass).displayName = 'Slice', Slice), superclass).prototype, constructor = Slice; function Slice(arg$){ var this$ = this instanceof ctor$ ? this : new ctor$; this$.type = arg$.type, this$.target = arg$.target, this$.from = arg$.from, this$.to = arg$.to; this$.from == null && (this$.from = Literal(0)); if (this$.to && this$.type === 'to') { this$.to = Binary('+', this$.to, Literal('1')); } return this$; } function ctor$(){} ctor$.prototype = prototype; prototype.children = ['target', 'from', 'to']; prototype.show = function(){ return this.type; }; prototype.compileNode = function(o){ var args; if (this.to && this.type === 'to') { this.to = Binary('||', this.to, Literal('9e9')); } args = [this.target, this.from]; if (this.to) { args.push(this.to); } return Chain(Var(util('slice'))).add(Index(Key('call'), '.', true)).add(Call(args)).compile(o); }; return Slice; }(Node)); exports.Chain = Chain = (function(superclass){ var prototype = extend$((import$(Chain, superclass).displayName = 'Chain', Chain), superclass).prototype, constructor = Chain; function Chain(head, tails){ var this$ = this instanceof ctor$ ? this : new ctor$; if (!tails && head instanceof Chain) { return head; } this$.head = head; this$.tails = tails || []; return this$; } function ctor$(){} ctor$.prototype = prototype; prototype.children = ['head', 'tails']; prototype.add = function(it){ var last, ref$, index, ref1$, bi, that, ref2$, logics, call, f; if (this.tails.length) { last = (ref$ = this.tails)[ref$.length - 1]; if (last instanceof Call && ((ref$ = last.partialized) != null ? ref$.length : void 8) === 1 && it.args.length === 1) { index = last.partialized[0].head.value; delete last.partialized; last.args[index] = it.args[0]; return this; } } if (this.head instanceof Existence) { ref1$ = Chain(this.head.it), this.head = ref1$.head, this.tails = ref1$.tails; it.soak = true; } this.tails.push(it); bi = this.head instanceof Parens && this.head.it instanceof Binary && !this.head.it.partial ? this.head.it : this.head instanceof Binary && !this.head.partial ? this.head : void 8; if (this.head instanceof Super) { if (!this.head.called && it instanceof Call && !it.method) { it.method = '.call'; it.args.unshift(Literal('this')); this.head.called = true; } else if (!this.tails[1] && ((ref1$ = it.key) != null ? ref1$.name : void 8) === 'prototype') { this.head.sproto = true; } } else if (that = (ref2$ = it.vivify, delete it.vivify, ref2$)) { this.head = Assign(Chain(this.head, this.tails.splice(0, 9e9)), that(), '=', '||'); } else if (it instanceof Call && this.tails.length === 1 && bi && in$(bi.op, logics = ['&&', '||', 'xor'])) { call = it; f = function(x, key){ var y; y = x[key]; if (y instanceof Binary && in$(y.op, logics)) { f(y, 'first'); return f(y, 'second'); } else { return x[key] = Chain(y).autoCompare(call.args); } }; f(bi, 'first'); f(bi, 'second'); return bi; } return this; }; prototype.autoCompare = function(target){ var test; test = this.head; switch (false) { case !(test instanceof Literal): return Binary('===', test, target[0]); case !(test instanceof Unary && test.it instanceof Literal): return Binary('===', test, target[0]); case !(test instanceof Arr || test instanceof Obj): return Binary('====', test, target[0]); case !(test instanceof Var && test.value === '_'): return Literal('true'); default: return this.add(Call(target)) || []; } }; prototype.flipIt = function(){ this.flip = true; return this; }; prototype.unwrap = function(){ if (this.tails.length) { return this; } else { return this.head; } }; prototype.delegate(['getJump', 'assigns', 'isStatement', 'isString'], function(it, arg){ return !this.tails.length && this.head[it](arg); }); prototype.isComplex = function(){ return this.tails.length || this.head.isComplex(); }; prototype.isCallable = function(){ var that, ref$; if (that = (ref$ = this.tails)[ref$.length - 1]) { return !((ref$ = that.key) != null && ref$.items); } else { return this.head.isCallable(); } }; prototype.isArray = function(){ var that, ref$; if (that = (ref$ = this.tails)[ref$.length - 1]) { return that.key instanceof Arr; } else { return this.head.isArray(); } }; prototype.isRegex = function(){ return this.head.value === 'RegExp' && !this.tails[1] && this.tails[0] instanceof Call; }; prototype.isAssignable = function(){ var tail, ref$, i$, len$; if (!(tail = (ref$ = this.tails)[ref$.length - 1])) { return this.head.isAssignable(); } if (!(tail instanceof Index) || tail.key instanceof List || tail.symbol === '.~') { return false; } for (i$ = 0, len$ = (ref$ = this.tails).length; i$ < len$; ++i$) { tail = ref$[i$]; if (tail.assign) { return false; } } return true; }; prototype.isSimpleAccess = function(){ return this.tails.length === 1 && !this.head.isComplex() && !this.tails[0].isComplex(); }; prototype.makeReturn = function(){ var ref$; if (this.tails.length) { return superclass.prototype.makeReturn.apply(this, arguments); } else { return (ref$ = this.head).makeReturn.apply(ref$, arguments); } }; prototype.getCall = function(){ var tail, ref$; return (tail = (ref$ = this.tails)[ref$.length - 1]) instanceof Call && tail; }; prototype.varName = function(){ var ref$, ref1$; return (ref$ = (ref1$ = this.tails)[ref1$.length - 1]) != null ? ref$.varName() : void 8; }; prototype.cacheReference = function(o){ var name, ref$, base, ref, bref, nref; name = (ref$ = this.tails)[ref$.length - 1]; if (!this.isAssignable()) { return this.unwrap().cache(o, true); } if (this.tails.length < 2 && !this.head.isComplex() && !(name != null && name.isComplex())) { return [this, this]; } base = Chain(this.head, this.tails.slice(0, -1)); if (base.isComplex()) { ref = o.scope.temporary(); base = Chain(Assign(Var(ref), base)); bref = (ref$ = Var(ref), ref$.temp = true, ref$); } if (!name) { return [base, bref]; } if (name.isComplex()) { ref = o.scope.temporary('key'); name = Index(Assign(Var(ref), name.key)); nref = Index((ref$ = Var(ref), ref$.temp = true, ref$)); } return [base.add(name), Chain(bref || base.head, [nref || name])]; }; prototype.compileNode = function(o){ var head, tails, that, i$, len$, t, hasPartial, pre, rest, broken, partial, post, context, idt, func, base, news, ref$; if (this.flip) { util('flip'); util('curry'); } head = this.head, tails = this.tails; head.front = this.front; head.newed = this.newed; if (!tails.length) { return head.compile(o); } if (that = this.unfoldAssign(o)) { return that.compile(o); } for (i$ = 0, len$ = tails.length; i$ < len$; ++i$) { t = tails[i$]; if (t.partialized) { hasPartial = true; break; } } if (hasPartial) { util('slice'); pre = []; rest = []; for (i$ = 0, len$ = tails.length; i$ < len$; ++i$) { t = tails[i$]; broken = broken || t.partialized != null; if (broken) { rest.push(t); } else { pre.push(t); } } if (rest != null) { partial = rest[0], post = slice$.call(rest, 1); } this.tails = pre; context = pre.length ? Chain(head, slice$.call(pre, 0, -1)) : Literal('this'); return Chain(Chain(Var(util('partialize'))).add(Index(Key('apply'))).add(Call([context, Arr([this, Arr(partial.args), Arr(partial.partialized)])])), post).compile(o); } if (tails[0] instanceof Call && !head.isCallable()) { this.carp('invalid callee'); } this.expandSlice(o); this.expandBind(o); this.expandSplat(o); this.expandStar(o); if (this.splattedNewArgs) { idt = o.indent + TAB; func = Chain(this.head, tails.slice(0, -1)); return sn(null, "(function(func, args, ctor) {\n" + idt + "ctor.prototype = func.prototype;\n" + idt + "var child = new ctor, result = func.apply(child, args), t;\n" + idt + "return (t = typeof result) == \"object\" || t == \"function\" ? result || child : child;\n" + TAB + "})(", func.compile(o), ", ", this.splattedNewArgs, ", function(){})"); } if (!this.tails.length) { return this.head.compile(o); } base = [this.head.compile(o, LEVEL_CALL)]; news = []; rest = []; for (i$ = 0, len$ = (ref$ = this.tails).length; i$ < len$; ++i$) { t = ref$[i$]; if (t['new']) { news.push('new '); } rest.push(t.compile(o)); } if ('.' === rest.join("").charAt(0) && SIMPLENUM.test(base[0].toString())) { base.push(' '); } return sn.apply(null, [null].concat(slice$.call(news), slice$.call(base), slice$.call(rest))); }; prototype.unfoldSoak = function(o){ var that, ref$, i$, len$, i, node, ref1$, bust, test; if (that = this.head.unfoldSoak(o)) { (ref$ = that.then.tails).push.apply(ref$, this.tails); return that; } for (i$ = 0, len$ = (ref$ = this.tails).length; i$ < len$; ++i$) { i = i$; node = ref$[i$]; if (ref1$ = node.soak, delete node.soak, ref1$) { bust = Chain(this.head, this.tails.splice(0, i)); if (node.assign && !bust.isAssignable()) { node.carp('invalid accessign'); } if (i && (node.assign || node instanceof Call)) { ref1$ = bust.cacheReference(o), test = ref1$[0], bust = ref1$[1]; if (bust instanceof Chain) { (ref1$ = this.tails).unshift.apply(ref1$, bust.tails); bust = bust.head; } this.head = bust; } else { ref1$ = bust.unwrap().cache(o), test = ref1$[0], this.head = ref1$[1]; } test = node instanceof Call ? JS("typeof " + test.compile(o, LEVEL_OP) + " == 'function'") : Existence(test); return ref1$ = If(test, this), ref1$.soak = true, ref1$.cond = this.cond, ref1$['void'] = this['void'], ref1$; } } }; prototype.unfoldAssign = function(o){ var that, ref$, i$, len$, i, index, op, left, lefts, rites, j$, len1$, node, ref1$; if (that = this.head.unfoldAssign(o)) { (ref$ = that.right.tails).push.apply(ref$, this.tails); return that; } for (i$ = 0, len$ = (ref$ = this.tails).length; i$ < len$; ++i$) { i = i$; index = ref$[i$]; if (op = index.assign) { index.assign = ''; left = Chain(this.head, this.tails.splice(0, i)).expandSlice(o).unwrap(); if (left instanceof Arr) { lefts = left.items; rites = (this.head = Arr()).items; for (j$ = 0, len1$ = lefts.length; j$ < len1$; ++j$) { i = j$; node = lefts[j$]; ref1$ = Chain(node).cacheReference(o), rites[i] = ref1$[0], lefts[i] = ref1$[1]; } } else { ref1$ = Chain(left).cacheReference(o), left = ref1$[0], this.head = ref1$[1]; } if (op === '=') { op = ':='; } return ref1$ = Assign(left, this, op), ref1$.access = true, ref1$; } } }; prototype.expandSplat = function(o){ var tails, i, call, args, ctx, ref$; tails = this.tails; i = -1; while (call = tails[++i]) { if (!(args = call.args)) { continue; } ctx = call.method === '.call' && (args = args.concat()).shift(); if (!!snEmpty(args = Splat.compileArray(o, args, true))) { continue; } if (call['new']) { this.splattedNewArgs = args; } else { if (!ctx && tails[i - 1] instanceof Index) { ref$ = Chain(this.head, tails.splice(0, i - 1)).cache(o, true), this.head = ref$[0], ctx = ref$[1]; i = 0; } call.method = '.apply'; call.args = [ctx || Literal('null'), JS(args)]; } } }; prototype.expandBind = function(o){ var tails, i, that, obj, key, call; tails = this.tails; i = -1; while (that = tails[++i]) { if (that.symbol !== '.~') { continue; } that.symbol = ''; obj = Chain(this.head, tails.splice(0, i)).unwrap(); key = tails.shift().key; call = Call.make(Util('bind'), [obj, (key.reserved = true, key)]); this.head = this.newed ? Parens(call, true) : call; i = -1; } }; prototype.expandStar = function(o){ var tails, i, that, stars, ref$, sub, ref, temps, value, i$, len$, star; tails = this.tails; i = -1; while (that = tails[++i]) { if (that.args || that.stars || that.key instanceof Key) { continue; } stars = that.stars = []; that.eachChild(seek); if (!stars.length) { continue; } ref$ = Chain(this.head, tails.splice(0, i)).unwrap().cache(o), sub = ref$[0], ref = ref$[1], temps = ref$[2]; value = Chain(ref, [Index(Key('length'))]).compile(o); for (i$ = 0, len$ = stars.length; i$ < len$; ++i$) { star = stars[i$]; star.value = value; star.isAssignable = YES; } this.head = JS(sub.compile(o, LEVEL_CALL) + tails.shift().compile(o)); if (temps) { o.scope.free(temps[0]); } i = -1; } function seek(it){ if (it.value === '*') { stars.push(it); } else if (!(it instanceof Index)) { it.eachChild(seek); } } }; prototype.expandSlice = function(o, assign){ var tails, i, tail, ref$, x; tails = this.tails; i = -1; while (tail = tails[++i]) { if ((ref$ = tail.key) != null && ref$.items) { if (tails[i + 1] instanceof Call) { tail.carp('calling a slice'); } x = tails.splice(0, i + 1); x = x.pop().key.toSlice(o, Chain(this.head, x).unwrap(), tail.symbol, assign); this.head = (x.front = this.front, x); i = -1; } } return this; }; return Chain; }(Node)); exports.Call = Call = (function(superclass){ var prototype = extend$((import$(Call, superclass).displayName = 'Call', Call), superclass).prototype, constructor = Call; function Call(args){ var splat, i$, len$, i, a, ref$, this$ = this instanceof ctor$ ? this : new ctor$; args || (args = []); if (args.length === 1 && (splat = args[0]) instanceof Splat) { if (splat.filler) { this$.method = '.call'; args[0] = Literal('this'); args[1] = Splat(Literal('arguments')); } else if (splat.it instanceof Arr) { args = splat.it.items; } } else { for (i$ = 0, len$ = args.length; i$ < len$; ++i$) { i = i$; a = args[i$]; if (a.value === '_') { args[i] = Chain(Literal('void')); args[i].placeholder = true; ((ref$ = this$.partialized) != null ? ref$ : this$.partialized = []).push(Chain(Literal(i))); } } } this$.args = args; return this$; } function ctor$(){} ctor$.prototype = prototype; prototype.children = ['args']; prototype.show = function(){ return [this['new']] + [this.method] + [this.soak ? '?' : void 8]; }; prototype.compile = function(o){ var code, i$, ref$, len$, i, a; code = [sn(this, this.method || '', '(') + (this.pipe ? "\n" + o.indent : '')]; for (i$ = 0, len$ = (ref$ = this.args).length; i$ < len$; ++i$) { i = i$; a = ref$[i$]; code.push(i ? ', ' : '', a.compile(o, LEVEL_LIST)); } code.push(sn(this, ')')); return sn.apply(null, [null].concat(slice$.call(code))); }; Call.make = function(callee, args, opts){ var call; call = Call(args); if (opts) { import$(call, opts); } return Chain(callee).add(call); }; Call.block = function(fun, args, method){ var ref$, ref1$; return ref$ = Parens(Chain(fun, [(ref1$ = Call(args), ref1$.method = method, ref1$)]), true), ref$.calling = true, ref$; }; Call.back = function(params, node, bound, curried, hushed, generator){ var fun, ref$, args, index, i$, len$, a; fun = Fun(params, void 8, bound, curried, hushed, generator); if (node instanceof Label) { fun.name = node.label; fun.labeled = true; node = node.it; } if (!fun.hushed && (fun.hushed = node.op === '!')) { node = node.it; } if ((ref$ = node.getCall()) != null) { ref$.partialized = null; } args = (node.getCall() || (node = Chain(node).add(Call())).getCall()).args; index = 0; for (i$ = 0, len$ = args.length; i$ < len$; ++i$) { a = args[i$]; if (a.placeholder) { break; } ++index; } return node.back = (args[index] = fun).body, node; }; Call['let'] = function(args, body){ var params, res$, i$, len$, i, a, that, gotThis; res$ = []; for (i$ = 0, len$ = args.length; i$ < len$; ++i$) { i = i$; a = args[i$]; if (that = a.op === '=' && !a.logic && a.right) { args[i] = that; if (i === 0 && (gotThis = a.left.value === 'this')) { continue; } res$.push(a.left); } else { res$.push(Var(a.varName() || a.carp('invalid "let" argument'))); } } params = res$; gotThis || args.unshift(Literal('this')); return this.block(Fun(params, body), args, '.call'); }; return Call; }(Node)); List = (function(superclass){ var prototype = extend$((import$(List, superclass).displayName = 'List', List), superclass).prototype, constructor = List; prototype.children = ['items']; prototype.show = function(){ return this.name; }; prototype.named = function(name){ this.name = name; return this; }; prototype.isEmpty = function(){ return !this.items.length; }; prototype.assigns = function(it){ var i$, ref$, len$, node; for (i$ = 0, len$ = (ref$ = this.items).length; i$ < len$; ++i$) { node = ref$[i$]; if (node.assigns(it)) { return true; } } }; List.compile = function(o, items, deepEq){ var indent, level, code, i, that, target; switch (items.length) { case 0: return ''; case 1: return items[0].compile(o, LEVEL_LIST); } indent = o.indent, level = o.level; o.indent = indent + TAB; o.level = LEVEL_LIST; code = [items[i = 0].compile(o)]; while (that = items[++i]) { code.push(', '); target = that; if (deepEq) { if (target instanceof Var && target.value === '_') { target = Obj([Prop(Key('__placeholder__'), Literal(true))]); } else if (target instanceof Obj || target instanceof Arr) { target.deepEq = true; } } code.push(target.compile(o)); } if (~code.join("").indexOf('\n')) { code = ["\n" + o.indent].concat(slice$.call(code), ["\n" + indent]); } o.indent = indent; o.level = level; return sn.apply(null, [this].concat(slice$.call(code))); }; function List(){ List.superclass.apply(this, arguments); } return List; }(Node)); exports.Obj = Obj = (function(superclass){ var prototype = extend$((import$(Obj, superclass).displayName = 'Obj', Obj), superclass).prototype, constructor = Obj; function Obj(items){ var this$ = this instanceof ctor$ ? this : new ctor$; this$.items = items || []; return this$; } function ctor$(){} ctor$.prototype = prototype; prototype.asObj = THIS; prototype.toSlice = function(o, base, symbol, assign){ var items, ref$, ref, temps, i$, len$, i, node, name, chain, logic, key, val; items = this.items; if (items.length > 1) { ref$ = base.cache(o), base = ref$[0], ref = ref$[1], temps = ref$[2]; } else { ref = base; } for (i$ = 0, len$ = items.length; i$ < len$; ++i$) { i = i$; node = items[i$]; if (node.comment) { continue; } if (node instanceof Prop || node instanceof Splat) { node[name = (ref$ = node.children)[ref$.length - 1]] = chain = Chain(base, [Index(node[name].maybeKey())]); } else { if (logic = node.getDefault()) { node = node.first; } if (node instanceof Parens) { ref$ = node.cache(o, true), key = ref$[0], node = ref$[1]; if (assign) { ref$ = [node, key], key = ref$[0], node = ref$[1]; } key = Parens(key); } else { key = node; } val = chain = Chain(base, [Index(node.maybeKey(), symbol)]); if (logic) { val = (logic.first = val, logic); } items[i] = Prop(key, val); } base = ref; } chain || this.carp('empty slice'); if (temps) { (chain.head = Var(temps[0])).temp = true; } return this; }; prototype.compileNode = function(o){ var items, code, idt, dic, i$, len$, i, node, logic, rest, ref$, multi, key, val; items = this.items; if (!items.length) { return sn(this, this.front ? '({})' : '{}'); } code = []; idt = '\n' + (o.indent += TAB); dic = {}; for (i$ = 0, len$ = items.length; i$ < len$; ++i$) { i = i$; node = items[i$]; if (node.comment) { code.push(idt, node.compile(o)); continue; } if (logic = node.getDefault()) { node = node.first; } if (node instanceof Splat || (node.key || node) instanceof Parens) { rest = items.slice(i); break; } if (logic) { if (node instanceof Prop) { node.val = (logic.first = node.val, logic); } else { node = Prop(node, (logic.first = node, logic)); } } if (this.deepEq && node instanceof Prop) { if (node.val instanceof Var && node.val.value === '_') { node.val = Obj([Prop(Key('__placeholder__'), Literal(true))]); } else if ((ref$ = node.val) instanceof Obj || ref$ instanceof Arr) { node.val.deepEq = true; } } if (multi) { code.push(','); } else { multi = true; } code.push(idt); if (node instanceof Prop) { key = node.key, val = node.val; if (node.accessor) { code.push(node.compileAccessor(o, key = key.compile(o))); } else { val.ripName(key); code.push(key = key.compile(o), ": ", val.compile(o, LEVEL_LIST)); } } else { code.push(key = node.compile(o), ": ", key); } ID.test(key) || (key = Function("return " + key)()); if (!(dic[key + "."] ^= 1)) { node.carp("duplicate property \"" + key + "\""); } } if (code.join("")) { code.push('\n' + this.tab); } code = sn.apply(null, [null, sn(this, "{")].concat(slice$.call(code), [sn(this, "}")])); rest && (code = Import(JS(code), Obj(rest)).compile((o.indent = this.tab, o))); if (this.front && '{' === code.toString().charAt()) { return sn(null, "(", code, ")"); } else { return code;