|
| 1 | +// | Acorn.js tools |____________________________/ |
| 2 | +// | |
| 3 | +// | (C) Mozilla Corp |
| 4 | +// | licensed under MPL 2.0 http://www.mozilla.org/MPL/ |
| 5 | +// \____________________________________________/ |
| 6 | + |
| 7 | +define(function(require, exports, module){ |
| 8 | + "no tracegl" |
| 9 | + |
| 10 | + var acorn = require('./acorn') |
| 11 | + |
| 12 | + exports.dump = function(o, t, r){ |
| 13 | + t = t || '' |
| 14 | + var a = Array.isArray(o) |
| 15 | + var s = (a?'[':'{') |
| 16 | + for(var k in o)if(o.hasOwnProperty(k)){ |
| 17 | + |
| 18 | + if(k == 'parent' || k == 'tokens' || k=='start' || k=='end' || k=='token' || k=='loc') continue |
| 19 | + if(k == 'token'){ |
| 20 | + s += '\n'+t+'token: '+o[k].t |
| 21 | + continue |
| 22 | + } |
| 23 | + var v = o[k] |
| 24 | + s += '\n' + t + k+': ' |
| 25 | + if(typeof v == 'object') { |
| 26 | + s += exports.dump(v, t + ' ', r) |
| 27 | + } |
| 28 | + else s += v |
| 29 | + } |
| 30 | + s += '\n'+t.slice(1) + (a?']':'}') |
| 31 | + return s |
| 32 | + } |
| 33 | + |
| 34 | + // |
| 35 | + // AST walker |
| 36 | + // |
| 37 | + |
| 38 | + var walk = { |
| 39 | + Literal: {}, // 1 single node |
| 40 | + Identifier: {}, // 2 array of nodes |
| 41 | + Program: {body:2}, // 3 keyss structure |
| 42 | + ExpressionStatement: {expression:1}, // 4 value endpoint |
| 43 | + BreakStatement: {}, |
| 44 | + ContinueStatement: {}, |
| 45 | + DebuggerStatement: {}, |
| 46 | + DoWhileStatement: {body:1, test:1}, |
| 47 | + ReturnStatement: {argument:1}, |
| 48 | + SwitchStatement: {discriminant:1,cases:2}, |
| 49 | + SwitchCase: {consequent:2,test:1}, |
| 50 | + WhileStatement: {test:1, body:1}, |
| 51 | + WithStatement: {object:1,body:1}, |
| 52 | + EmptyStatement: {}, |
| 53 | + LabeledStatement: {body:1,label:4}, |
| 54 | + BlockStatement: {body:2}, |
| 55 | + ForStatement: {init:1,test:1,update:1,body:1}, |
| 56 | + ForInStatement: {left:1,right:1,body:1}, |
| 57 | + VariableDeclaration: {declarations:2}, |
| 58 | + VariableDeclarator: {id:4,init:1}, |
| 59 | + SequenceExpression: {expressions:2}, |
| 60 | + AssignmentExpression: {left:1,right:1}, |
| 61 | + ConditionalExpression:{test:1,consequent:1,alternate:1}, |
| 62 | + LogicalExpression: {left:1,right:1}, |
| 63 | + BinaryExpression: {left:1,right:1}, |
| 64 | + UpdateExpression: {argument:1}, |
| 65 | + UnaryExpression: {argument:1}, |
| 66 | + CallExpression: {callee:1,arguments:2}, |
| 67 | + ThisExpression: {}, |
| 68 | + ArrayExpression: {elements:2}, |
| 69 | + NewExpression: {callee:1,arguments:2}, |
| 70 | + FunctionDeclaration: {id:4,params:2,body:1}, |
| 71 | + FunctionExpression: {id:4,params:2,body:1}, |
| 72 | + ObjectExpression: {properties:3}, |
| 73 | + MemberExpression: {object:1,property:1}, |
| 74 | + IfStatement: {test:1,consequent:1,alternate:1}, |
| 75 | + ThrowStatement: {argument:1}, |
| 76 | + TryStatement: {block:1,handlers:2,finalizer:1}, |
| 77 | + CatchClause: {param:1,guard:1,body:1} |
| 78 | + } |
| 79 | + |
| 80 | + function walkDown(n, o, p, k){ |
| 81 | + if(!n) return |
| 82 | + var f = o[n.type] |
| 83 | + if(f){ |
| 84 | + if(f(n, p)) return |
| 85 | + } |
| 86 | + var w = walk[n.type] |
| 87 | + for(var k in w){ |
| 88 | + var t = w[k] // type |
| 89 | + var m = n[k] // node prop |
| 90 | + if(t == 2){ // array |
| 91 | + if(!Array.isArray(m))throw new Error("invalid type") |
| 92 | + for(var i = 0; i < m.length; i++){ |
| 93 | + walkDown(m[i], o, {up:p, sub:k, type:n.type, node:n, index:i} ) |
| 94 | + } |
| 95 | + } else if(t == 3){ // keys |
| 96 | + if(!Array.isArray(m))throw new Error("invalid type") |
| 97 | + for(var i = 0; i < m.length; i++){ |
| 98 | + walkDown(m[i].value, o, {up:p, sub:k, type:n.type, node:n, index:i, key:m[i].key} ) |
| 99 | + } |
| 100 | + } else { // single node or value |
| 101 | + if(m) walkDown(m, o, {up:p, sub:k, type:n.type, node:n}) |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + function walkUp(p, o){ |
| 107 | + while(p){ |
| 108 | + var f = o[p.node.type] |
| 109 | + if(f && f(p.node, p)) break |
| 110 | + p = p.up |
| 111 | + } |
| 112 | + } |
| 113 | + exports.walkDown = walkDown |
| 114 | + exports.walkUp = walkUp |
| 115 | + |
| 116 | + // |
| 117 | + // AST serializer |
| 118 | + // |
| 119 | + |
| 120 | + var sSep |
| 121 | + |
| 122 | + function sExp(e){ |
| 123 | + if(!e || !e.type) return '' |
| 124 | + return sTab[e.type](e) |
| 125 | + } |
| 126 | + |
| 127 | + function sBlk(b){ |
| 128 | + var s = '' |
| 129 | + for(var i = 0;i<b.length;i++) s += sExp(b[i]) + sSep |
| 130 | + return s |
| 131 | + } |
| 132 | + |
| 133 | + function sSeq(b){ |
| 134 | + var s = '' |
| 135 | + for(var i = 0;i<b.length;i++){ |
| 136 | + if(i) s += ', ' |
| 137 | + s += sExp(b[i]) |
| 138 | + } |
| 139 | + return s |
| 140 | + } |
| 141 | + |
| 142 | + var sTab = { |
| 143 | + Literal: function(n){ return n.raw }, |
| 144 | + Identifier: function(n){ return n.name }, |
| 145 | + Program: function(n){ return sBlk(n.body) }, |
| 146 | + ExpressionStatement: function(n){ return sExp(n.expression) }, |
| 147 | + BreakStatement: function(n){ return 'break' }, |
| 148 | + ContinueStatement: function(n){ return 'continue' }, |
| 149 | + DebuggerStatement: function(n){ return 'debugger' }, |
| 150 | + DoWhileStatement: function(n){ return 'do'+sExp(n.body)+sSep+'while('+sExp(n.test)+')' }, |
| 151 | + ReturnStatement: function(n){ return 'return '+sExp(n.argument) }, |
| 152 | + SwitchStatement: function(n){ return 'switch('+sExp(n.discriminant)+'){'+sBlk(n.cases)+'}' }, |
| 153 | + SwitchCase: function(n){ return 'case '+sExp(n.test)+':'+sSep+sBlk(n.consequent) }, |
| 154 | + WhileStatement: function(n){ return 'while('+sExp(n.test)+')'+sExp(n.body) }, |
| 155 | + WithStatement: function(n){ return 'with('+sExp(n.object)+')'+sExp(n.body) }, |
| 156 | + EmptyStatement: function(n){ return '' }, |
| 157 | + LabeledStatement: function(n){ return sExp(n.label) + ':' + sSep + sExp(n.body) }, |
| 158 | + BlockStatement: function(n){ return '{'+sSep+sBlk(n.body)+'}' }, |
| 159 | + ForStatement: function(n){ return 'for('+sExp(n.init)+';'+sExp(n.test)+';'+sExp(n.update)+')'+sExp(n.body) }, |
| 160 | + ForInStatement: function(n){ return 'for('+sExp(n.left)+' in '+sExp(n.right)+')'+sExp(n.body) }, |
| 161 | + VariableDeclarator: function(n){ return sExp(n.id)+' = ' +sExp(n.init) }, |
| 162 | + VariableDeclaration: function(n){ return 'var '+sSeq(n.declarations) }, |
| 163 | + SequenceExpression: function(n){ return sSeq(n.expressions) }, |
| 164 | + AssignmentExpression: function(n){ return sExp(n.left)+n.operator+sExp(n.right) }, |
| 165 | + ConditionalExpression:function(n){ return sExp(n.test)+'?'+sExp(n.consequent)+':'+sExp(n.alternate) }, |
| 166 | + LogicalExpression: function(n){ return sExp(n.left)+n.operator+sExp(n.right) }, |
| 167 | + BinaryExpression: function(n){ return sExp(n.left)+n.operator+sExp(n.right) }, |
| 168 | + UpdateExpression: function(n){ return n.prefix?n.operator+sExp(n.argument):sExp(n.argument)+n.operator }, |
| 169 | + UnaryExpression: function(n){ return n.prefix?n.operator+sExp(n.argument):sExp(n.argument)+n.operator }, |
| 170 | + CallExpression: function(n){ return sExp(n.callee)+'('+sSeq(n.arguments)+')' }, |
| 171 | + ThisExpression: function(n){ return 'this' }, |
| 172 | + ArrayExpression: function(n){ return '['+sSeq(n.elements)+']' }, |
| 173 | + NewExpression: function(n){ return 'new '+sExp(n.callee)+'('+sSeq(n.arguments)+')' }, |
| 174 | + FunctionDeclaration: function(n){ return 'function'+(n.id?' '+sExp(n.id):'')+'('+sSeq(n.params)+')'+sExp(n.body) }, |
| 175 | + FunctionExpression: function(n){ return 'function'+(n.id?' '+sExp(n.id):'')+'('+sSeq(n.params)+')'+sExp(n.body) }, |
| 176 | + ObjectExpression: function(n){ |
| 177 | + var s = '{' |
| 178 | + var b = n.properties |
| 179 | + for(var i = 0;i<b.length;i++){ |
| 180 | + if(i) s += ', ' |
| 181 | + s += sExp(b.key) + ':' + sExp(b.value) |
| 182 | + } |
| 183 | + s += '}' |
| 184 | + return s |
| 185 | + }, |
| 186 | + MemberExpression: function(n){ |
| 187 | + if(n.computed) return sExp(n.object)+'['+sExp(n.property)+']' |
| 188 | + return sExp(n.object)+'.'+sExp(n.property) |
| 189 | + }, |
| 190 | + IfStatement: function(n){ |
| 191 | + return 'if('+sExp(n.test)+')' + sExp(n.consequent) + sSep + |
| 192 | + (n.alternate ? 'else ' + sExp(n.alternate) + sSep : '') |
| 193 | + }, |
| 194 | + ThrowStatement: function(n){ return 'throw '+sExp(n.argument) }, |
| 195 | + TryStatement: function(n){ |
| 196 | + return 'try '+sExp(n.block)+sSep+sBlk(n.handlers)+sSep+ |
| 197 | + (n.finalizer? 'finally ' + sBlk(n.finalizer) : '') |
| 198 | + }, |
| 199 | + CatchClause: function(n){ |
| 200 | + return 'catch(' + sExp(n.param) + (n.guard?' if '+sExp(n.guard):')') + sExp(n.body) |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + function stringify(n, sep){ |
| 205 | + sSep = sep || '\n' |
| 206 | + return sExp(n) |
| 207 | + } |
| 208 | + |
| 209 | + exports.stringify = stringify |
| 210 | + |
| 211 | + var types = acorn.tokTypes |
| 212 | + |
| 213 | + function nodeProto(p){ |
| 214 | + |
| 215 | + // property getter type checking |
| 216 | + for(var k in types){ |
| 217 | + (function(k){ |
| 218 | + p.__defineGetter__(k, function(){ |
| 219 | + return this._t == types[k] |
| 220 | + }) |
| 221 | + })(k) |
| 222 | + } |
| 223 | + // other types |
| 224 | + p.__defineGetter__('isAssign', function(){ return this._t && this._t.isAssign }) |
| 225 | + p.__defineGetter__('isLoop', function(){ return this._t && this._t.isLoop }) |
| 226 | + p.__defineGetter__('prefix', function(){ return this._t && this._t.prefix }) |
| 227 | + p.__defineGetter__('beforeExpr', function(){ return this._t && this._t.beforeExpr }) |
| 228 | + p.__defineGetter__('beforeNewline', function(){ return this.w && this.w.match(/\n/) }) |
| 229 | + p.__defineGetter__('beforeEnd', function(){ return this.w && this.w.match(/\n/) || this.d.semi || this.d.braceR }) |
| 230 | + p.__defineGetter__('fnscope', function(){ return this.d.parenL ? this.d.d : this.d.d.d }) |
| 231 | + p.__defineGetter__('last', function(){ var t = this; while(t._d) t = t._d; return t }) |
| 232 | + p.__defineGetter__('astParent', function(){ var t = this; while(t._d) t = t._d; return t }) |
| 233 | + |
| 234 | + // walker |
| 235 | + p.walk = function(cb){ |
| 236 | + var n = this |
| 237 | + var p = n |
| 238 | + cb(n) |
| 239 | + n = n._c |
| 240 | + while(n && n != p){ |
| 241 | + cb(n) |
| 242 | + if(n._c) n = n._c |
| 243 | + else while(n != p){ |
| 244 | + if(n._d){ n = n._d; break } |
| 245 | + n = n._p |
| 246 | + } |
| 247 | + } |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + function Node(){ } |
| 252 | + nodeProto(Node.prototype) |
| 253 | + |
| 254 | + // acorn parse wrapper that also spits out a token tree |
| 255 | + exports.parse = function(inpt, opts) { |
| 256 | + var h = { |
| 257 | + finishToken:finishToken, |
| 258 | + initTokenState:initTokenState, |
| 259 | + tokTree:new Node() |
| 260 | + } |
| 261 | + h.tokTree.root = 1 |
| 262 | + h.tokTree.t = h.tokTree.w = '' |
| 263 | + |
| 264 | + if(opts && opts.compact) h.compact = 1 |
| 265 | + if(opts && opts.noclose) h.noclose = 1 |
| 266 | + |
| 267 | + var n = acorn.parse(inpt, opts, h) |
| 268 | + n.tokens = h.tokTree |
| 269 | + return n |
| 270 | + } |
| 271 | + |
| 272 | + function initTokenState(hack, tokPos, input){ |
| 273 | + if(tokPos != 0) hack.tokTree.w = input.slice(0, tokPos) |
| 274 | + } |
| 275 | + |
| 276 | + function finishToken(hack, type, val, input, tokStart, tokEnd, tokPos){ |
| 277 | + var tokTree = hack.tokTree |
| 278 | + var n |
| 279 | + if(type == types.eof) return |
| 280 | + if(type == types.regexp && tokTree._e && tokTree._e._t.binop == 10){ |
| 281 | + // verify this one |
| 282 | + n = tokTree._e, tokStart -= 1 |
| 283 | + } else if(hack.compact && tokTree._e && (type == types.name && tokTree._e._t == types.dot || type == types.dot && tokTree._e._t == types.name)){ |
| 284 | + n = tokTree._e |
| 285 | + n._t = type |
| 286 | + n.t += input.slice(tokStart, tokEnd) |
| 287 | + } else { |
| 288 | + var n = new Node() |
| 289 | + var t = tokTree |
| 290 | + if(t){ |
| 291 | + if(!t._c) t._e = t._c = n |
| 292 | + else t._e._d = n, n._u = t._e, t._e = n |
| 293 | + } |
| 294 | + n._p = t |
| 295 | + n._t = type |
| 296 | + n.t = input.slice(tokStart, tokEnd) |
| 297 | + } |
| 298 | + |
| 299 | + if(tokEnd != tokPos) n.w = input.slice(tokEnd, tokPos) |
| 300 | + else n.w = '' |
| 301 | + |
| 302 | + if(type == types.braceL || type == types.bracketL || type == types.parenL){ |
| 303 | + tokTree = n |
| 304 | + } |
| 305 | + else if(type == types.braceR || type == types.bracketR || type == types.parenR){ |
| 306 | + if(hack.noclose){ |
| 307 | + if(!tokTree._e._u) delete tokTree._c, delete tokTree._e |
| 308 | + else delete tokTree._e._u._d |
| 309 | + } |
| 310 | + if(tokTree._p) |
| 311 | + tokTree = tokTree._p |
| 312 | + } |
| 313 | + hack.tokTree = tokTree |
| 314 | + } |
| 315 | +}) |
0 commit comments