├── .gitignore ├── README.md ├── file ├── default.css ├── jscex.min.js ├── functions.js ├── jscex-builderbase.min.js ├── jscex-async-powerpack.min.js ├── jscex-async.min.js ├── love.js ├── jscex-jit.js └── jscex-parser.js └── index.html /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 给女朋友的小惊喜 2 | 3 | 需要修改的部分是index.html文件部分内容。 4 | 5 | 现已发现日期设置成“喜欢你”当月会出现问题。 6 | 7 | **我不得不提醒一句,前来fork的哥们儿,这不是我的原创** 8 | 9 | 我是在某个纯前端设计网站上面看到的,然后觉得很有意思,就把仓库搞了过来。随机被舍友发现了这个页面,他就拿去用了。本身他女友也挺喜欢的。然而万万没想到的是,她女朋友刷短视频。短视频里面有一个哥们儿对着电脑也在写代码,然后画面呈现的就是这个仓库的页面。奉劝各位,还是不要说是自己原创的,真要被逮住了可麻烦得很。 10 | -------------------------------------------------------------------------------- /file/default.css: -------------------------------------------------------------------------------- 1 | body{margin:0;padding:0;background:#ffe;font-size:14px;font-family:'微软雅黑','宋体',sans-serif;color:#231F20;overflow:auto} 2 | a {color:#000;font-size:14px;} 3 | #main{width:100%;} 4 | #wrap{position:relative;margin:0 auto;width:1100px;height:680px;margin-top:10px;} 5 | #text{width:272px; 6 | height:560px; 7 | left:188px; 8 | top:80px;position:absolute; 9 | } 10 | #code{display:none;font-size:16px;} 11 | #clock-box {position:absolute;left:60px;top:550px;font-size:28px;display:none;} 12 | #clock-box a {font-size:28px;text-decoration:none;} 13 | #clock{margin-left:48px;} 14 | #clock .digit {font-size:64px;} 15 | #canvas{margin:0 auto;width:1100px;height:680px;} 16 | #error{margin:0 auto;text-align:center;margin-top:60px;display:none;} 17 | .hand{cursor:pointer;} 18 | .say{margin-left:5px;} 19 | .space{margin-right:150px;} 20 | -------------------------------------------------------------------------------- /file/jscex.min.js: -------------------------------------------------------------------------------- 1 | (function(){var b={DEBUG:1,INFO:2,WARN:3,ERROR:4},d=function(){this.level=b.WARN};d.prototype={log:function(a){try{console.log(a)}catch(b){}},debug:function(a){this.level<=b.DEBUG&&this.log(a)},info:function(a){this.level<=b.INFO&&this.log(a)},warn:function(a){this.level<=b.WARN&&this.log(a)},error:function(a){this.level<=b.ERROR&&this.log(a)}};var e=function(a){var b=[],c;for(c in a)b.push(c);return b},c=function(a){a._forInKeys=e;a.Logging={Logger:d,Level:b};a.logger=new d;a.modules={};a.binders= 2 | {};a.builders={}},f=typeof define==="function"&&!define.amd,g=typeof require==="function"&&typeof define==="function"&&define.amd;typeof require==="function"&&typeof module!=="undefined"&&module.exports?c(module.exports):f?define("jscex",function(a,b,d){c(d.exports)}):g?define("jscex",function(){var a={};c(a);return a}):(typeof Jscex=="undefined"&&(Jscex={}),c(Jscex))})(); 3 | -------------------------------------------------------------------------------- /file/functions.js: -------------------------------------------------------------------------------- 1 | /* 2 | * http://love.hackerzhou.me 3 | */ 4 | 5 | // variables 6 | var $win = $(window); 7 | var clientWidth = $win.width(); 8 | var clientHeight = $win.height(); 9 | 10 | $(window).resize(function() { 11 | var newWidth = $win.width(); 12 | var newHeight = $win.height(); 13 | if (newWidth != clientWidth && newHeight != clientHeight) { 14 | location.replace(location); 15 | } 16 | }); 17 | 18 | (function($) { 19 | $.fn.typewriter = function() { 20 | this.each(function() { 21 | var $ele = $(this), str = $ele.html(), progress = 0; 22 | $ele.html(''); 23 | var timer = setInterval(function() { 24 | var current = str.substr(progress, 1); 25 | if (current == '<') { 26 | progress = str.indexOf('>', progress) + 1; 27 | } else { 28 | progress++; 29 | } 30 | $ele.html(str.substring(0, progress) + (progress & 1 ? '_' : '')); 31 | if (progress >= str.length) { 32 | clearInterval(timer); 33 | } 34 | }, 75); 35 | }); 36 | return this; 37 | }; 38 | })(jQuery); 39 | 40 | function timeElapse(date){ 41 | var current = Date(); 42 | var seconds = (Date.parse(current) - Date.parse(date)) / 1000; 43 | var days = Math.floor(seconds / (3600 * 24)); 44 | seconds = seconds % (3600 * 24); 45 | var hours = Math.floor(seconds / 3600); 46 | if (hours < 10) { 47 | hours = "0" + hours; 48 | } 49 | seconds = seconds % 3600; 50 | var minutes = Math.floor(seconds / 60); 51 | if (minutes < 10) { 52 | minutes = "0" + minutes; 53 | } 54 | seconds = seconds % 60; 55 | if (seconds < 10) { 56 | seconds = "0" + seconds; 57 | } 58 | var result = "第 " + days + "" + hours + " 小时 " + minutes + " 分钟 " + seconds + " 秒"; 59 | $("#clock").html(result); 60 | } 61 | -------------------------------------------------------------------------------- /file/jscex-builderbase.min.js: -------------------------------------------------------------------------------- 1 | (function(){var j=function(){};j.prototype={Loop:function(b,c,a,d){return{next:function(e,i){var f=function(b){a.next(e,function(a,e){if(a=="normal"||a=="continue")g(b);else if(a=="throw"||a=="return")i(a,e);else if(a=="break")i("normal");else throw Error('Invalid type for "Loop": '+a);})},g=function(a){try{c&&!a&&c.call(e),!b||b.call(e)?f(!1):i("normal")}catch(d){i("throw",d)}};d?f(!0):g(!0)}}},Delay:function(b){return{next:function(c,a){try{b.call(c).next(c,a)}catch(d){a("throw",d)}}}},Combine:function(b, 2 | c){return{next:function(a,d){b.next(a,function(b,i,f){if(b=="normal")try{c.next(a,d)}catch(g){d("throw",g)}else d(b,i,f)})}}},Return:function(b){return{next:function(c,a){a("return",b)}}},Normal:function(){return{next:function(b,c){c("normal")}}},Break:function(){return{next:function(b,c){c("break")}}},Continue:function(){return{next:function(b,c){c("continue")}}},Throw:function(b){return{next:function(c,a){a("throw",b)}}},Try:function(b,c,a){return{next:function(d,e){b.next(d,function(b,f,g){if(b!= 3 | "throw"||!c)a?a.next(d,function(a,c,d){a=="normal"?e(b,f,g):e(a,c,d)}):e(b,f,g);else if(c){var h;try{h=c.call(d,f)}catch(j){a?a.next(d,function(a,b,c){a=="normal"?e("throw",j):e(a,b,c)}):e("throw",j)}h&&h.next(d,function(b,c,f){b=="throw"?a?a.next(d,function(a,d,g){a=="normal"?e(b,c,f):e(a,d,g)}):e(b,c,f):a?a.next(d,function(a,d,g){a=="normal"?e(b,c,f):e(a,d,g)}):e(b,c,f)})}else a.next(d,function(a,c,d){a=="normal"?e(b,f,g):e(a,c,d)})})}}}};var h=function(b){if(!b.modules)b.modules={};if(!b.modules.builderbase)b.modules.builderbase= 4 | !0,b.BuilderBase=j},k=typeof define==="function"&&!define.amd,l=typeof require==="function"&&typeof define==="function"&&define.amd;if(typeof require==="function"&&typeof module!=="undefined"&&module.exports)module.exports.init=h;else if(k)define("jscex-builderbase",function(b,c,a){a.exports.init=h});else if(l)define("jscex-builderbase",function(){return{init:h}});else{if(typeof Jscex==="undefined")throw Error('Missing the root object, please load "jscex" module first.');h(Jscex)}})(); 5 | -------------------------------------------------------------------------------- /file/jscex-async-powerpack.min.js: -------------------------------------------------------------------------------- 1 | (function(){var m=function(j){if(j.length<=1)return null;for(var f=[],h=1;h=0&&this._handlers.splice(a,1))},cancel:function(){if(!this.isCancellationRequested){this.isCancellationRequested=!0;var a=this._handlers;delete this._handlers;for(var f=0;f=0&&c.splice(i,1)}}}};e.create=function(a){return new e(a)};e.isTask=l;var h=function(){};h.prototype={Start:function(a,b){return e.create(function(c){b.next(a,function(a,b){if(a=="normal"||a=="return")c.complete("success",b);else if(a=="throw")c.complete("failure",b);else throw Error("Unsupported type: "+a);})})},Bind:function(a,b){return{next:function(c,e){var d= 6 | function(a){if(a.error)e("throw",a.error);else{var d;try{d=b.call(c,a.result)}catch(h){e("throw",h);return}d.next(c,e)}};a.status=="ready"?(a.addEventListener("complete",d),a.start()):a.status=="running"?a.addEventListener("complete",d):d(a)}}}};for(var g in b.BuilderBase.prototype)h.prototype[g]=b.BuilderBase.prototype[g];if(!b.Async)b.Async={};g=b.Async;g.CancellationToken=d;g.CanceledError=k;g.Task=e;g.AsyncBuilder=h;if(!b.builders)b.builders={};b.binders.async="$await";b.builders.async=new h; 7 | b.modules.async=!0}},m=typeof define==="function"&&!define.amd,n=typeof require==="function"&&typeof define==="function"&&define.amd;if(typeof require==="function"&&typeof module!=="undefined"&&module.exports)module.exports.init=function(b){if(!b.modules.builderbase){if(typeof __dirname==="string")try{require.paths.unshift(__dirname)}catch(d){try{module.paths.unshift(__dirname)}catch(e){}}require("jscex-builderbase").init(b)}j(b)};else if(m)define("jscex-async",["jscex-builderbase"],function(b,d, 8 | e){e.exports.init=function(d){d.modules.builderbase||b("jscex-builderbase").init(d);j(d)}});else if(n)define("jscex-async",["jscex-builderbase"],function(b){return{init:function(d){d.modules.builderbase||b.init(d);j(d)}}});else{if(typeof Jscex==="undefined")throw Error('Missing the root object, please load "jscex" module first.');if(!Jscex.modules.builderbase)throw Error('Missing essential components, please initialize "builderbase" module first.');j(Jscex)}})(); 9 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 网页标题 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 16 | 17 | 18 |
19 |
本页面采用HTML5编辑,目前您的浏览器无法显示,请换成谷歌(Chrome)或者火狐(Firefox)浏览器,或者其他游览器的最新版本。
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 | 56 | 57 | 213 | -------------------------------------------------------------------------------- /file/love.js: -------------------------------------------------------------------------------- 1 | (function(window){ 2 | 3 | function random(min, max) { 4 | return min + Math.floor(Math.random() * (max - min + 1)); 5 | } 6 | 7 | function bezier(cp, t) { 8 | var p1 = cp[0].mul((1 - t) * (1 - t)); 9 | var p2 = cp[1].mul(2 * t * (1 - t)); 10 | var p3 = cp[2].mul(t * t); 11 | return p1.add(p2).add(p3); 12 | } 13 | 14 | function inheart(x, y, r) { 15 | // x^2+(y-(x^2)^(1/3))^2 = 1 16 | // http://www.wolframalpha.com/input/?i=x%5E2%2B%28y-%28x%5E2%29%5E%281%2F3%29%29%5E2+%3D+1 17 | var z = ((x / r) * (x / r) + (y / r) * (y / r) - 1) * ((x / r) * (x / r) + (y / r) * (y / r) - 1) * ((x / r) * (x / r) + (y / r) * (y / r) - 1) - (x / r) * (x / r) * (y / r) * (y / r) * (y / r); 18 | return z < 0; 19 | } 20 | 21 | Point = function(x, y) { 22 | this.x = x || 0; 23 | this.y = y || 0; 24 | } 25 | Point.prototype = { 26 | clone: function() { 27 | return new Point(this.x, this.y); 28 | }, 29 | add: function(o) { 30 | p = this.clone(); 31 | p.x += o.x; 32 | p.y += o.y; 33 | return p; 34 | }, 35 | sub: function(o) { 36 | p = this.clone(); 37 | p.x -= o.x; 38 | p.y -= o.y; 39 | return p; 40 | }, 41 | div: function(n) { 42 | p = this.clone(); 43 | p.x /= n; 44 | p.y /= n; 45 | return p; 46 | }, 47 | mul: function(n) { 48 | p = this.clone(); 49 | p.x *= n; 50 | p.y *= n; 51 | return p; 52 | } 53 | } 54 | 55 | Heart = function() { 56 | // x = 16 sin^3 t 57 | // y = 13 cos t - 5 cos 2t - 2 cos 3t - cos 4t 58 | // http://www.wolframalpha.com/input/?i=x+%3D+16+sin%5E3+t%2C+y+%3D+(13+cos+t+-+5+cos+2t+-+2+cos+3t+-+cos+4t) 59 | var points = [], x, y, t; 60 | for (var i = 10; i < 30; i += 0.2) { 61 | t = i / Math.PI; 62 | x = 16 * Math.pow(Math.sin(t), 3); 63 | y = 13 * Math.cos(t) - 5 * Math.cos(2 * t) - 2 * Math.cos(3 * t) - Math.cos(4 * t); 64 | points.push(new Point(x, y)); 65 | } 66 | this.points = points; 67 | this.length = points.length; 68 | } 69 | Heart.prototype = { 70 | get: function(i, scale) { 71 | return this.points[i].mul(scale || 1); 72 | } 73 | } 74 | 75 | Seed = function(tree, point, scale, color) { 76 | this.tree = tree; 77 | 78 | var scale = scale || 1 79 | var color = color || '#FF0000'; 80 | 81 | this.heart = { 82 | point : point, 83 | scale : scale, 84 | color : color, 85 | figure : new Heart(), 86 | } 87 | 88 | this.cirle = { 89 | point : point, 90 | scale : scale, 91 | color : color, 92 | radius : 5, 93 | } 94 | } 95 | Seed.prototype = { 96 | draw: function() { 97 | this.drawHeart(); 98 | this.drawText(); 99 | }, 100 | addPosition: function(x, y) { 101 | this.cirle.point = this.cirle.point.add(new Point(x, y)); 102 | }, 103 | canMove: function() { 104 | return this.cirle.point.y < (this.tree.height + 20); 105 | }, 106 | move: function(x, y) { 107 | this.clear(); 108 | this.drawCirle(); 109 | this.addPosition(x, y); 110 | }, 111 | canScale: function() { 112 | return this.heart.scale > 0.2; 113 | }, 114 | setHeartScale: function(scale) { 115 | this.heart.scale *= scale; 116 | }, 117 | scale: function(scale) { 118 | this.clear(); 119 | this.drawCirle(); 120 | this.drawHeart(); 121 | this.setHeartScale(scale); 122 | }, 123 | drawHeart: function() { 124 | var ctx = this.tree.ctx, heart = this.heart; 125 | var point = heart.point, color = heart.color, 126 | scale = heart.scale; 127 | ctx.save(); 128 | ctx.fillStyle = color; 129 | ctx.translate(point.x, point.y); 130 | ctx.beginPath(); 131 | ctx.moveTo(0, 0); 132 | for (var i = 0; i < heart.figure.length; i++) { 133 | var p = heart.figure.get(i, scale); 134 | ctx.lineTo(p.x, -p.y); 135 | } 136 | ctx.closePath(); 137 | ctx.fill(); 138 | ctx.restore(); 139 | }, 140 | drawCirle: function() { 141 | var ctx = this.tree.ctx, cirle = this.cirle; 142 | var point = cirle.point, color = cirle.color, 143 | scale = cirle.scale, radius = cirle.radius; 144 | ctx.save(); 145 | ctx.fillStyle = color; 146 | ctx.translate(point.x, point.y); 147 | ctx.scale(scale, scale); 148 | ctx.beginPath(); 149 | ctx.moveTo(0, 0); 150 | ctx.arc(0, 0, radius, 0, 2 * Math.PI); 151 | ctx.closePath(); 152 | ctx.fill(); 153 | ctx.restore(); 154 | }, 155 | drawText: function() { 156 | var ctx = this.tree.ctx, heart = this.heart; 157 | var point = heart.point, color = heart.color, 158 | scale = heart.scale; 159 | ctx.save(); 160 | ctx.strokeStyle = color; 161 | ctx.fillStyle = color; 162 | ctx.translate(point.x, point.y); 163 | ctx.scale(scale, scale); 164 | ctx.moveTo(0, 0); 165 | ctx.lineTo(15, 15); 166 | ctx.lineTo(60, 15); 167 | ctx.stroke(); 168 | 169 | ctx.moveTo(0, 0); 170 | ctx.scale(0.75, 0.75); 171 | ctx.font = "12px 微软雅黑,Verdana"; // 字号肿么没有用? (ˉ(∞)ˉ) 172 | ctx.fillText("Come Baby", 23, 10); 173 | ctx.restore(); 174 | }, 175 | clear: function() { 176 | var ctx = this.tree.ctx, cirle = this.cirle; 177 | var point = cirle.point, scale = cirle.scale, radius = 26; 178 | var w = h = (radius * scale); 179 | ctx.clearRect(point.x - w, point.y - h, 4 * w, 4 * h); 180 | }, 181 | hover: function(x, y) { 182 | var ctx = this.tree.ctx; 183 | var pixel = ctx.getImageData(x, y, 1, 1); 184 | return pixel.data[3] == 255 185 | } 186 | } 187 | 188 | Footer = function(tree, width, height, speed) { 189 | this.tree = tree; 190 | this.point = new Point(tree.seed.heart.point.x, tree.height - height / 2); 191 | this.width = width; 192 | this.height = height; 193 | this.speed = speed || 2; 194 | this.length = 0; 195 | } 196 | Footer.prototype = { 197 | draw: function() { 198 | var ctx = this.tree.ctx, point = this.point; 199 | var len = this.length / 2; 200 | 201 | ctx.save(); 202 | ctx.strokeStyle = 'rgb(35, 31, 32)'; 203 | ctx.lineWidth = this.height; 204 | ctx.lineCap = 'round'; 205 | ctx.lineJoin = 'round'; 206 | ctx.translate(point.x, point.y); 207 | ctx.beginPath(); 208 | ctx.moveTo(0, 0); 209 | ctx.lineTo(len, 0); 210 | ctx.lineTo(-len, 0); 211 | ctx.stroke(); 212 | ctx.restore(); 213 | 214 | if (this.length < this.width) { 215 | this.length += this.speed; 216 | } 217 | } 218 | } 219 | 220 | Tree = function(canvas, width, height, opt) { 221 | this.canvas = canvas; 222 | this.ctx = canvas.getContext('2d'); 223 | this.width = width; 224 | this.height = height; 225 | this.opt = opt || {}; 226 | 227 | this.record = {}; 228 | 229 | this.initSeed(); 230 | this.initFooter(); 231 | this.initBranch(); 232 | this.initBloom(); 233 | } 234 | Tree.prototype = { 235 | initSeed: function() { 236 | var seed = this.opt.seed || {}; 237 | var x = seed.x || this.width / 2; 238 | var y = seed.y || this.height / 2; 239 | var point = new Point(x, y); 240 | var color = seed.color || '#FF0000'; 241 | var scale = seed.scale || 1; 242 | 243 | this.seed = new Seed(this, point, scale, color); 244 | }, 245 | 246 | initFooter: function() { 247 | var footer = this.opt.footer || {}; 248 | var width = footer.width || this.width; 249 | var height = footer.height || 5; 250 | var speed = footer.speed || 2; 251 | this.footer = new Footer(this, width, height, speed); 252 | }, 253 | 254 | initBranch: function() { 255 | var branchs = this.opt.branch || [] 256 | this.branchs = []; 257 | this.addBranchs(branchs); 258 | }, 259 | 260 | initBloom: function() { 261 | var bloom = this.opt.bloom || {}; 262 | var cache = [], 263 | num = bloom.num || 500, 264 | width = bloom.width || this.width, 265 | height = bloom.height || this.height, 266 | figure = this.seed.heart.figure; 267 | var r = 240, x, y; 268 | for (var i = 0; i < num; i++) { 269 | cache.push(this.createBloom(width, height, r, figure)); 270 | } 271 | this.blooms = []; 272 | this.bloomsCache = cache; 273 | }, 274 | 275 | toDataURL: function(type) { 276 | return this.canvas.toDataURL(type); 277 | }, 278 | 279 | draw: function(k) { 280 | var s = this, ctx = s.ctx; 281 | var rec = s.record[k]; 282 | if (!rec) { 283 | return ; 284 | } 285 | var point = rec.point, 286 | image = rec.image; 287 | 288 | ctx.save(); 289 | ctx.putImageData(image, point.x, point.y); 290 | ctx.restore(); 291 | }, 292 | 293 | addBranch: function(branch) { 294 | this.branchs.push(branch); 295 | }, 296 | 297 | addBranchs: function(branchs){ 298 | var s = this, b, p1, p2, p3, r, l, c; 299 | for (var i = 0; i < branchs.length; i++) { 300 | b = branchs[i]; 301 | p1 = new Point(b[0], b[1]); 302 | p2 = new Point(b[2], b[3]); 303 | p3 = new Point(b[4], b[5]); 304 | r = b[6]; 305 | l = b[7]; 306 | c = b[8] 307 | s.addBranch(new Branch(s, p1, p2, p3, r, l, c)); 308 | } 309 | }, 310 | 311 | removeBranch: function(branch) { 312 | var branchs = this.branchs; 313 | for (var i = 0; i < branchs.length; i++) { 314 | if (branchs[i] === branch) { 315 | branchs.splice(i, 1); 316 | } 317 | } 318 | }, 319 | 320 | canGrow: function() { 321 | return !!this.branchs.length; 322 | }, 323 | grow: function() { 324 | var branchs = this.branchs; 325 | for (var i = 0; i < branchs.length; i++) { 326 | var branch = branchs[i]; 327 | if (branch) { 328 | branch.grow(); 329 | } 330 | } 331 | }, 332 | 333 | addBloom: function (bloom) { 334 | this.blooms.push(bloom); 335 | }, 336 | 337 | removeBloom: function (bloom) { 338 | var blooms = this.blooms; 339 | for (var i = 0; i < blooms.length; i++) { 340 | if (blooms[i] === bloom) { 341 | blooms.splice(i, 1); 342 | } 343 | } 344 | }, 345 | 346 | createBloom: function(width, height, radius, figure, color, alpha, angle, scale, place, speed) { 347 | var x, y; 348 | while (true) { 349 | x = random(20, width - 20); 350 | y = random(20, height - 20); 351 | if (inheart(x - width / 2, height - (height - 40) / 2 - y, radius)) { 352 | return new Bloom(this, new Point(x, y), figure, color, alpha, angle, scale, place, speed); 353 | } 354 | } 355 | }, 356 | 357 | canFlower: function() { 358 | return !!this.blooms.length; 359 | }, 360 | flower: function(num) { 361 | var s = this, blooms = s.bloomsCache.splice(0, num); 362 | for (var i = 0; i < blooms.length; i++) { 363 | s.addBloom(blooms[i]); 364 | } 365 | blooms = s.blooms; 366 | for (var j = 0; j < blooms.length; j++) { 367 | blooms[j].flower(); 368 | } 369 | }, 370 | 371 | snapshot: function(k, x, y, width, height) { 372 | var ctx = this.ctx; 373 | var image = ctx.getImageData(x, y, width, height); 374 | this.record[k] = { 375 | image: image, 376 | point: new Point(x, y), 377 | width: width, 378 | height: height 379 | } 380 | }, 381 | setSpeed: function(k, speed) { 382 | this.record[k || "move"].speed = speed; 383 | }, 384 | move: function(k, x, y) { 385 | var s = this, ctx = s.ctx; 386 | var rec = s.record[k || "move"]; 387 | var point = rec.point, 388 | image = rec.image, 389 | speed = rec.speed || 10, 390 | width = rec.width, 391 | height = rec.height; 392 | 393 | i = point.x + speed < x ? point.x + speed : x; 394 | j = point.y + speed < y ? point.y + speed : y; 395 | 396 | ctx.save(); 397 | ctx.clearRect(point.x, point.y, width, height); 398 | ctx.putImageData(image, i, j); 399 | ctx.restore(); 400 | 401 | rec.point = new Point(i, j); 402 | rec.speed = speed * 0.95; 403 | 404 | if (rec.speed < 2) { 405 | rec.speed = 2; 406 | } 407 | return i < x || j < y; 408 | }, 409 | 410 | jump: function() { 411 | var s = this, blooms = s.blooms; 412 | if (blooms.length) { 413 | for (var i = 0; i < blooms.length; i++) { 414 | blooms[i].jump(); 415 | } 416 | } 417 | if ((blooms.length && blooms.length < 3) || !blooms.length) { 418 | var bloom = this.opt.bloom || {}, 419 | width = bloom.width || this.width, 420 | height = bloom.height || this.height, 421 | figure = this.seed.heart.figure; 422 | var r = 240, x, y; 423 | for (var i = 0; i < random(1,2); i++) { 424 | blooms.push(this.createBloom(width / 2 + width, height, r, figure, null, 1, null, 1, new Point(random(-100,600), 720), random(200,300))); 425 | } 426 | } 427 | } 428 | } 429 | 430 | Branch = function(tree, point1, point2, point3, radius, length, branchs) { 431 | this.tree = tree; 432 | this.point1 = point1; 433 | this.point2 = point2; 434 | this.point3 = point3; 435 | this.radius = radius; 436 | this.length = length || 100; 437 | this.len = 0; 438 | this.t = 1 / (this.length - 1); 439 | this.branchs = branchs || []; 440 | } 441 | 442 | Branch.prototype = { 443 | grow: function() { 444 | var s = this, p; 445 | if (s.len <= s.length) { 446 | p = bezier([s.point1, s.point2, s.point3], s.len * s.t); 447 | s.draw(p); 448 | s.len += 1; 449 | s.radius *= 0.97; 450 | } else { 451 | s.tree.removeBranch(s); 452 | s.tree.addBranchs(s.branchs); 453 | } 454 | }, 455 | draw: function(p) { 456 | var s = this; 457 | var ctx = s.tree.ctx; 458 | ctx.save(); 459 | ctx.beginPath(); 460 | ctx.fillStyle = 'rgb(35, 31, 32)'; 461 | ctx.shadowColor = 'rgb(35, 31, 32)'; 462 | ctx.shadowBlur = 2; 463 | ctx.moveTo(p.x, p.y); 464 | ctx.arc(p.x, p.y, s.radius, 0, 2 * Math.PI); 465 | ctx.closePath(); 466 | ctx.fill(); 467 | ctx.restore(); 468 | } 469 | } 470 | 471 | Bloom = function(tree, point, figure, color, alpha, angle, scale, place, speed) { 472 | this.tree = tree; 473 | this.point = point; 474 | this.color = color || 'rgb(255,' + random(0, 255) + ',' + random(0, 255) + ')'; 475 | this.alpha = alpha || random(0.3, 1); 476 | this.angle = angle || random(0, 360); 477 | this.scale = scale || 0.1; 478 | this.place = place; 479 | this.speed = speed; 480 | 481 | this.figure = figure; 482 | } 483 | Bloom.prototype = { 484 | setFigure: function(figure) { 485 | this.figure = figure; 486 | }, 487 | flower: function() { 488 | var s = this; 489 | s.draw(); 490 | s.scale += 0.1; 491 | if (s.scale > 1) { 492 | s.tree.removeBloom(s); 493 | } 494 | }, 495 | draw: function() { 496 | var s = this, ctx = s.tree.ctx, figure = s.figure; 497 | 498 | ctx.save(); 499 | ctx.fillStyle = s.color; 500 | ctx.globalAlpha = s.alpha; 501 | ctx.translate(s.point.x, s.point.y); 502 | ctx.scale(s.scale, s.scale); 503 | ctx.rotate(s.angle); 504 | ctx.beginPath(); 505 | ctx.moveTo(0, 0); 506 | for (var i = 0; i < figure.length; i++) { 507 | var p = figure.get(i); 508 | ctx.lineTo(p.x, -p.y); 509 | } 510 | ctx.closePath(); 511 | ctx.fill(); 512 | ctx.restore(); 513 | }, 514 | jump: function() { 515 | var s = this, height = s.tree.height; 516 | 517 | if (s.point.x < -20 || s.point.y > height + 20) { 518 | s.tree.removeBloom(s); 519 | } else { 520 | s.draw(); 521 | s.point = s.place.sub(s.point).div(s.speed).add(s.point); 522 | s.angle += 0.05; 523 | s.speed -= 1; 524 | } 525 | } 526 | } 527 | 528 | window.random = random; 529 | window.bezier = bezier; 530 | window.Point = Point; 531 | window.Tree = Tree; 532 | 533 | })(window); 534 | -------------------------------------------------------------------------------- /file/jscex-jit.js: -------------------------------------------------------------------------------- 1 | (function () { 2 | 3 | var codeGenerator = (typeof eval("(function () {})") == "function") ? 4 | function (code) { return code; } : 5 | function (code) { return "false || " + code; }; 6 | 7 | // support string type only. 8 | var stringify = (typeof JSON !== "undefined" && JSON.stringify) ? 9 | function (s) { return JSON.stringify(s); } : 10 | (function () { 11 | // Implementation comes from JSON2 (http://www.json.org/js.html) 12 | 13 | var escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g; 14 | 15 | var meta = { // table of character substitutions 16 | '\b': '\\b', 17 | '\t': '\\t', 18 | '\n': '\\n', 19 | '\f': '\\f', 20 | '\r': '\\r', 21 | '"' : '\\"', 22 | '\\': '\\\\' 23 | } 24 | 25 | return function (s) { 26 | // If the string contains no control characters, no quote characters, and no 27 | // backslash characters, then we can safely slap some quotes around it. 28 | // Otherwise we must also replace the offending characters with safe escape 29 | // sequences. 30 | 31 | escapable.lastIndex = 0; 32 | return escapable.test(s) ? '"' + s.replace(escapable, function (a) { 33 | var c = meta[a]; 34 | return typeof c === 's' ? c : 35 | '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4); 36 | }) + '"' : '"' + s + '"'; 37 | }; 38 | })(); 39 | 40 | // seed defined in global 41 | if (typeof __jscex__tempVarSeed === "undefined") { 42 | __jscex__tempVarSeed = 0; 43 | } 44 | 45 | var init = function (root) { 46 | 47 | if (root.modules["jit"]) { 48 | return; 49 | } 50 | 51 | function JscexTreeGenerator(binder) { 52 | this._binder = binder; 53 | this._root = null; 54 | } 55 | JscexTreeGenerator.prototype = { 56 | 57 | generate: function (ast) { 58 | 59 | var params = ast[2], statements = ast[3]; 60 | 61 | this._root = { type: "delay", stmts: [] }; 62 | 63 | this._visitStatements(statements, this._root.stmts); 64 | 65 | return this._root; 66 | }, 67 | 68 | _getBindInfo: function (stmt) { 69 | 70 | var type = stmt[0]; 71 | if (type == "stat") { 72 | var expr = stmt[1]; 73 | if (expr[0] == "call") { 74 | var callee = expr[1]; 75 | if (callee[0] == "name" && callee[1] == this._binder && expr[2].length == 1) { 76 | return { 77 | expression: expr[2][0], 78 | argName: "", 79 | assignee: null 80 | }; 81 | } 82 | } else if (expr[0] == "assign") { 83 | var assignee = expr[2]; 84 | expr = expr[3]; 85 | if (expr[0] == "call") { 86 | var callee = expr[1]; 87 | if (callee[0] == "name" && callee[1] == this._binder && expr[2].length == 1) { 88 | return { 89 | expression: expr[2][0], 90 | argName: "$$_result_$$", 91 | assignee: assignee 92 | }; 93 | } 94 | } 95 | } 96 | } else if (type == "var") { 97 | var defs = stmt[1]; 98 | if (defs.length == 1) { 99 | var item = defs[0]; 100 | var name = item[0]; 101 | var expr = item[1]; 102 | if (expr && expr[0] == "call") { 103 | var callee = expr[1]; 104 | if (callee[0] == "name" && callee[1] == this._binder && expr[2].length == 1) { 105 | return { 106 | expression: expr[2][0], 107 | argName: name, 108 | assignee: null 109 | }; 110 | } 111 | } 112 | } 113 | } else if (type == "return") { 114 | var expr = stmt[1]; 115 | if (expr && expr[0] == "call") { 116 | var callee = expr[1]; 117 | if (callee[0] == "name" && callee[1] == this._binder && expr[2].length == 1) { 118 | return { 119 | expression: expr[2][0], 120 | argName: "$$_result_$$", 121 | assignee: "return" 122 | }; 123 | } 124 | } 125 | } 126 | 127 | return null; 128 | }, 129 | 130 | _visitStatements: function (statements, stmts, index) { 131 | if (arguments.length <= 2) index = 0; 132 | 133 | if (index >= statements.length) { 134 | stmts.push({ type: "normal" }); 135 | return this; 136 | } 137 | 138 | var currStmt = statements[index]; 139 | var bindInfo = this._getBindInfo(currStmt); 140 | 141 | if (bindInfo) { 142 | var bindStmt = { type: "bind", info: bindInfo }; 143 | stmts.push(bindStmt); 144 | 145 | if (bindInfo.assignee != "return") { 146 | bindStmt.stmts = []; 147 | this._visitStatements(statements, bindStmt.stmts, index + 1); 148 | } 149 | 150 | } else { 151 | var type = currStmt[0]; 152 | if (type == "return" || type == "break" || type == "continue" || type == "throw") { 153 | 154 | stmts.push({ type: type, stmt: currStmt }); 155 | 156 | } else if (type == "if" || type == "try" || type == "for" || type == "do" 157 | || type == "while" || type == "switch" || type == "for-in") { 158 | 159 | var newStmt = this._visit(currStmt); 160 | 161 | if (newStmt.type == "raw") { 162 | stmts.push(newStmt); 163 | this._visitStatements(statements, stmts, index + 1); 164 | } else { 165 | var isLast = (index == statements.length - 1); 166 | if (isLast) { 167 | stmts.push(newStmt); 168 | } else { 169 | 170 | var combineStmt = { 171 | type: "combine", 172 | first: { type: "delay", stmts: [newStmt] }, 173 | second: { type: "delay", stmts: [] } 174 | }; 175 | stmts.push(combineStmt); 176 | 177 | this._visitStatements(statements, combineStmt.second.stmts, index + 1); 178 | } 179 | } 180 | 181 | } else { 182 | 183 | stmts.push({ type: "raw", stmt: currStmt }); 184 | 185 | this._visitStatements(statements, stmts, index + 1); 186 | } 187 | } 188 | 189 | return this; 190 | }, 191 | 192 | _visit: function (ast) { 193 | 194 | var type = ast[0]; 195 | 196 | function throwUnsupportedError() { 197 | throw new Error('"' + type + '" is not currently supported.'); 198 | } 199 | 200 | var visitor = this._visitors[type]; 201 | 202 | if (visitor) { 203 | return visitor.call(this, ast); 204 | } else { 205 | throwUnsupportedError(); 206 | } 207 | }, 208 | 209 | _visitBody: function (ast, stmts) { 210 | if (ast[0] == "block") { 211 | this._visitStatements(ast[1], stmts); 212 | } else { 213 | this._visitStatements([ast], stmts); 214 | } 215 | }, 216 | 217 | _noBinding: function (stmts) { 218 | switch (stmts[stmts.length - 1].type) { 219 | case "normal": 220 | case "return": 221 | case "break": 222 | case "throw": 223 | case "continue": 224 | return true; 225 | } 226 | 227 | return false; 228 | }, 229 | 230 | _collectCaseStatements: function (cases, index) { 231 | var res = []; 232 | 233 | for (var i = index; i < cases.length; i++) { 234 | var rawStmts = cases[i][1]; 235 | for (var j = 0; j < rawStmts.length; j++) { 236 | if (rawStmts[j][0] == "break") { 237 | return res 238 | } 239 | 240 | res.push(rawStmts[j]); 241 | } 242 | } 243 | 244 | return res; 245 | }, 246 | 247 | _visitors: { 248 | 249 | "for": function (ast) { 250 | 251 | var bodyStmts = []; 252 | var body = ast[4]; 253 | this._visitBody(body, bodyStmts); 254 | 255 | if (this._noBinding(bodyStmts)) { 256 | return { type: "raw", stmt: ast }; 257 | } 258 | 259 | var delayStmt = { type: "delay", stmts: [] }; 260 | 261 | var setup = ast[1]; 262 | if (setup) { 263 | delayStmt.stmts.push({ type: "raw", stmt: setup }); 264 | } 265 | 266 | var loopStmt = { type: "loop", bodyFirst: false, bodyStmt: { type: "delay", stmts: bodyStmts } }; 267 | delayStmt.stmts.push(loopStmt); 268 | 269 | var condition = ast[2]; 270 | if (condition) { 271 | loopStmt.condition = condition; 272 | } 273 | 274 | var update = ast[3]; 275 | if (update) { 276 | loopStmt.update = update; 277 | } 278 | 279 | return delayStmt; 280 | }, 281 | 282 | "for-in": function (ast) { 283 | 284 | var body = ast[4]; 285 | 286 | var bodyStmts = []; 287 | this._visitBody(body, bodyStmts); 288 | 289 | if (this._noBinding(bodyStmts)) { 290 | return { type: "raw", stmt: ast }; 291 | } 292 | 293 | var id = (__jscex__tempVarSeed++); 294 | var keysVar = "$$_keys_$$_" + id; 295 | var indexVar = "$$_index_$$_" + id; 296 | // var memVar = "$$_mem_$$_" + id; 297 | 298 | var delayStmt = { type: "delay", stmts: [] }; 299 | 300 | // var members = Jscex._forInKeys(obj); 301 | var keysAst = root.parse("var " + keysVar + " = Jscex._forInKeys(obj);")[1][0]; 302 | keysAst[1][0][1][2][0] = ast[3]; // replace obj with real AST; 303 | delayStmt.stmts.push({ type: "raw", stmt: keysAst }); 304 | 305 | /* 306 | // var members = []; 307 | delayStmt.stmts.push({ 308 | type: "raw", 309 | stmt: uglifyJS.parse("var " + membersVar + " = [];")[1][0] 310 | }); 311 | 312 | // for (var mem in obj) members.push(mem); 313 | var keysAst = uglifyJS.parse("for (var " + memVar +" in obj) " + membersVar + ".push(" + memVar + ");")[1][0]; 314 | keysAst[3] = ast[3]; // replace the "obj" with real AST. 315 | delayStmt.stmts.push({ type : "raw", stmt: keysAst}); 316 | */ 317 | 318 | // var index = 0; 319 | delayStmt.stmts.push({ 320 | type: "raw", 321 | stmt: root.parse("var " + indexVar + " = 0;")[1][0] 322 | }); 323 | 324 | // index < members.length 325 | var condition = root.parse(indexVar + " < " + keysVar + ".length")[1][0][1]; 326 | 327 | // index++ 328 | var update = root.parse(indexVar + "++")[1][0][1]; 329 | 330 | var loopStmt = { 331 | type: "loop", 332 | bodyFirst: false, 333 | update: update, 334 | condition: condition, 335 | bodyStmt: { type: "delay", stmts: [] } 336 | }; 337 | delayStmt.stmts.push(loopStmt); 338 | 339 | var varName = ast[2][1]; // ast[2] == ["name", m] 340 | if (ast[1][0] == "var") { 341 | loopStmt.bodyStmt.stmts.push({ 342 | type: "raw", 343 | stmt: root.parse("var " + varName + " = " + keysVar + "[" + indexVar + "];")[1][0] 344 | }); 345 | } else { 346 | loopStmt.bodyStmt.stmts.push({ 347 | type: "raw", 348 | stmt: root.parse(varName + " = " + keysVar + "[" + indexVar + "];")[1][0] 349 | }); 350 | } 351 | 352 | this._visitBody(body, loopStmt.bodyStmt.stmts); 353 | 354 | return delayStmt; 355 | }, 356 | 357 | "while": function (ast) { 358 | 359 | var bodyStmts = []; 360 | var body = ast[2]; 361 | this._visitBody(body, bodyStmts); 362 | 363 | if (this._noBinding(bodyStmts)) { 364 | return { type: "raw", stmt: ast } 365 | } 366 | 367 | var loopStmt = { type: "loop", bodyFirst: false, bodyStmt: { type: "delay", stmts: bodyStmts } }; 368 | 369 | var condition = ast[1]; 370 | loopStmt.condition = condition; 371 | 372 | return loopStmt; 373 | }, 374 | 375 | "do": function (ast) { 376 | 377 | var bodyStmts = []; 378 | var body = ast[2]; 379 | this._visitBody(body, bodyStmts); 380 | 381 | if (this._noBinding(bodyStmts)) { 382 | return { type: "raw", stmt: ast }; 383 | } 384 | 385 | var loopStmt = { type: "loop", bodyFirst: true, bodyStmt: { type: "delay", stmts: bodyStmts } }; 386 | 387 | var condition = ast[1]; 388 | loopStmt.condition = condition; 389 | 390 | return loopStmt; 391 | }, 392 | 393 | "switch": function (ast) { 394 | var noBinding = true; 395 | 396 | var switchStmt = { type: "switch", item: ast[1], caseStmts: [] }; 397 | 398 | var cases = ast[2]; 399 | for (var i = 0; i < cases.length; i++) { 400 | var caseStmt = { item: cases[i][0], stmts: [] }; 401 | switchStmt.caseStmts.push(caseStmt); 402 | 403 | var statements = this._collectCaseStatements(cases, i); 404 | this._visitStatements(statements, caseStmt.stmts); 405 | noBinding = noBinding && this._noBinding(caseStmt.stmts); 406 | } 407 | 408 | if (noBinding) { 409 | return { type: "raw", stmt: ast }; 410 | } else { 411 | return switchStmt; 412 | } 413 | }, 414 | 415 | "if": function (ast) { 416 | 417 | var noBinding = true; 418 | 419 | var ifStmt = { type: "if", conditionStmts: [] }; 420 | 421 | var currAst = ast; 422 | while (true) { 423 | var condition = currAst[1]; 424 | var condStmt = { cond: condition, stmts: [] }; 425 | ifStmt.conditionStmts.push(condStmt); 426 | 427 | var thenPart = currAst[2]; 428 | this._visitBody(thenPart, condStmt.stmts); 429 | 430 | noBinding = noBinding && this._noBinding(condStmt.stmts); 431 | 432 | var elsePart = currAst[3]; 433 | if (elsePart && elsePart[0] == "if") { 434 | currAst = elsePart; 435 | } else { 436 | break; 437 | } 438 | } 439 | 440 | var elsePart = currAst[3]; 441 | if (elsePart) { 442 | ifStmt.elseStmts = []; 443 | 444 | this._visitBody(elsePart, ifStmt.elseStmts); 445 | 446 | noBinding = noBinding && this._noBinding(ifStmt.elseStmts); 447 | } 448 | 449 | if (noBinding) { 450 | return { type: "raw", stmt: ast }; 451 | } else { 452 | return ifStmt; 453 | } 454 | }, 455 | 456 | "try": function (ast, stmts) { 457 | 458 | var bodyStmts = []; 459 | var bodyStatements = ast[1]; 460 | this._visitStatements(bodyStatements, bodyStmts); 461 | 462 | var noBinding = this._noBinding(bodyStmts) 463 | 464 | var tryStmt = { type: "try", bodyStmt: { type: "delay", stmts: bodyStmts } }; 465 | 466 | var catchClause = ast[2]; 467 | if (catchClause) { 468 | var exVar = catchClause[0]; 469 | tryStmt.exVar = exVar; 470 | tryStmt.catchStmts = []; 471 | 472 | this._visitStatements(catchClause[1], tryStmt.catchStmts); 473 | 474 | noBinding = noBinding && this._noBinding(tryStmt.catchStmts); 475 | } 476 | 477 | var finallyStatements = ast[3]; 478 | if (finallyStatements) { 479 | tryStmt.finallyStmt = { type: "delay", stmts: [] }; 480 | 481 | this._visitStatements(finallyStatements, tryStmt.finallyStmt.stmts); 482 | 483 | noBinding = noBinding && this._noBinding(tryStmt.finallyStmt.stmts); 484 | } 485 | 486 | if (noBinding) { 487 | return { type: "raw", stmt: ast }; 488 | } else { 489 | return tryStmt; 490 | } 491 | } 492 | } 493 | } 494 | 495 | function CodeGenerator(builderName, binder, indent) { 496 | this._builderName = builderName; 497 | this._binder = binder; 498 | this._normalMode = false; 499 | this._indent = indent; 500 | this._indentLevel = 0; 501 | this._builderVar = "$$_builder_$$_" + (__jscex__tempVarSeed++); 502 | } 503 | CodeGenerator.prototype = { 504 | _write: function (s) { 505 | this._buffer.push(s); 506 | return this; 507 | }, 508 | 509 | _writeLine: function (s) { 510 | this._write(s)._write("\n"); 511 | return this; 512 | }, 513 | 514 | _writeIndents: function () { 515 | for (var i = 0; i < this._indent; i++) { 516 | this._write(" "); 517 | } 518 | 519 | for (var i = 0; i < this._indentLevel; i++) { 520 | this._write(" "); 521 | } 522 | return this; 523 | }, 524 | 525 | generate: function (params, jscexAst) { 526 | this._buffer = []; 527 | 528 | this._writeLine("(function (" + params.join(", ") + ") {"); 529 | this._indentLevel++; 530 | 531 | this._writeIndents() 532 | ._writeLine("var " + this._builderVar + " = Jscex.builders[" + stringify(this._builderName) + "];"); 533 | 534 | this._writeIndents() 535 | ._writeLine("return " + this._builderVar + ".Start(this,"); 536 | this._indentLevel++; 537 | 538 | this._pos = { }; 539 | 540 | this._writeIndents() 541 | ._visitJscex(jscexAst) 542 | ._writeLine(); 543 | this._indentLevel--; 544 | 545 | this._writeIndents() 546 | ._writeLine(");"); 547 | this._indentLevel--; 548 | 549 | this._writeIndents() 550 | ._write("})"); 551 | 552 | return this._buffer.join(""); 553 | }, 554 | 555 | _visitJscex: function (ast) { 556 | this._jscexVisitors[ast.type].call(this, ast); 557 | return this; 558 | }, 559 | 560 | _visitRaw: function (ast) { 561 | var type = ast[0]; 562 | 563 | function throwUnsupportedError() { 564 | throw new Error('"' + type + '" is not currently supported.'); 565 | } 566 | 567 | var visitor = this._rawVisitors[type]; 568 | 569 | if (visitor) { 570 | visitor.call(this, ast); 571 | } else { 572 | throwUnsupportedError(); 573 | } 574 | 575 | return this; 576 | }, 577 | 578 | _visitJscexStatements: function (statements) { 579 | for (var i = 0; i < statements.length; i++) { 580 | var stmt = statements[i]; 581 | 582 | if (stmt.type == "raw" || stmt.type == "if" || stmt.type == "switch") { 583 | this._writeIndents() 584 | ._visitJscex(stmt)._writeLine(); 585 | } else if (stmt.type == "delay") { 586 | this._visitJscexStatements(stmt.stmts); 587 | } else { 588 | this._writeIndents() 589 | ._write("return ")._visitJscex(stmt)._writeLine(";"); 590 | } 591 | } 592 | }, 593 | 594 | _visitRawStatements: function (statements) { 595 | for (var i = 0; i < statements.length; i++) { 596 | var s = statements[i]; 597 | 598 | this._writeIndents() 599 | ._visitRaw(s)._writeLine(); 600 | 601 | switch (s[0]) { 602 | case "break": 603 | case "return": 604 | case "continue": 605 | case "throw": 606 | return; 607 | } 608 | } 609 | }, 610 | 611 | _visitRawBody: function (body) { 612 | if (body[0] == "block") { 613 | this._visitRaw(body); 614 | } else { 615 | this._writeLine(); 616 | this._indentLevel++; 617 | 618 | this._writeIndents() 619 | ._visitRaw(body); 620 | this._indentLevel--; 621 | } 622 | 623 | return this; 624 | }, 625 | 626 | _visitRawFunction: function (ast) { 627 | var funcName = ast[1] || ""; 628 | var args = ast[2]; 629 | var statements = ast[3]; 630 | 631 | this._writeLine("function " + funcName + "(" + args.join(", ") + ") {") 632 | this._indentLevel++; 633 | 634 | var currInFunction = this._pos.inFunction; 635 | this._pos.inFunction = true; 636 | 637 | this._visitRawStatements(statements); 638 | this._indentLevel--; 639 | 640 | this._pos.inFunction = currInFunction; 641 | 642 | this._writeIndents() 643 | ._write("}"); 644 | }, 645 | 646 | _jscexVisitors: { 647 | "delay": function (ast) { 648 | if (ast.stmts.length == 1) { 649 | var subStmt = ast.stmts[0]; 650 | switch (subStmt.type) { 651 | case "delay": 652 | case "combine": 653 | case "normal": 654 | case "break": 655 | case "continue": 656 | case "loop": 657 | case "try": 658 | this._visitJscex(subStmt); 659 | return; 660 | case "return": 661 | if (!subStmt.stmt[1]) { 662 | this._visitJscex(subStmt); 663 | return; 664 | } 665 | } 666 | } 667 | 668 | this._writeLine(this._builderVar + ".Delay(function () {"); 669 | this._indentLevel++; 670 | 671 | this._visitJscexStatements(ast.stmts); 672 | this._indentLevel--; 673 | 674 | this._writeIndents() 675 | ._write("})"); 676 | }, 677 | 678 | "combine": function (ast) { 679 | this._writeLine(this._builderVar + ".Combine("); 680 | this._indentLevel++; 681 | 682 | this._writeIndents() 683 | ._visitJscex(ast.first)._writeLine(","); 684 | this._writeIndents() 685 | ._visitJscex(ast.second)._writeLine(); 686 | this._indentLevel--; 687 | 688 | this._writeIndents() 689 | ._write(")"); 690 | }, 691 | 692 | "loop": function (ast) { 693 | this._writeLine(this._builderVar + ".Loop("); 694 | this._indentLevel++; 695 | 696 | if (ast.condition) { 697 | this._writeIndents() 698 | ._writeLine("function () {"); 699 | this._indentLevel++; 700 | 701 | this._writeIndents() 702 | ._write("return ")._visitRaw(ast.condition)._writeLine(";"); 703 | this._indentLevel--; 704 | 705 | this._writeIndents() 706 | ._writeLine("},"); 707 | } else { 708 | this._writeIndents()._writeLine("null,"); 709 | } 710 | 711 | if (ast.update) { 712 | this._writeIndents() 713 | ._writeLine("function () {"); 714 | this._indentLevel++; 715 | 716 | this._writeIndents() 717 | ._visitRaw(ast.update)._writeLine(";"); 718 | this._indentLevel--; 719 | 720 | this._writeIndents() 721 | ._writeLine("},"); 722 | } else { 723 | this._writeIndents()._writeLine("null,"); 724 | } 725 | 726 | this._writeIndents() 727 | ._visitJscex(ast.bodyStmt)._writeLine(","); 728 | 729 | this._writeIndents() 730 | ._writeLine(ast.bodyFirst); 731 | this._indentLevel--; 732 | 733 | this._writeIndents() 734 | ._write(")"); 735 | }, 736 | 737 | "raw": function (ast) { 738 | this._visitRaw(ast.stmt); 739 | }, 740 | 741 | "bind": function (ast) { 742 | var info = ast.info; 743 | this._write(this._builderVar + ".Bind(")._visitRaw(info.expression)._writeLine(", function (" + info.argName + ") {"); 744 | this._indentLevel++; 745 | 746 | if (info.assignee == "return") { 747 | this._writeIndents() 748 | ._writeLine("return " + this._builderVar + ".Return(" + info.argName + ");"); 749 | } else { 750 | if (info.assignee) { 751 | this._writeIndents() 752 | ._visitRaw(info.assignee)._writeLine(" = " + info.argName + ";"); 753 | } 754 | 755 | this._visitJscexStatements(ast.stmts); 756 | } 757 | this._indentLevel--; 758 | 759 | this._writeIndents() 760 | ._write("})"); 761 | }, 762 | 763 | "if": function (ast) { 764 | 765 | for (var i = 0; i < ast.conditionStmts.length; i++) { 766 | var stmt = ast.conditionStmts[i]; 767 | 768 | this._write("if (")._visitRaw(stmt.cond)._writeLine(") {"); 769 | this._indentLevel++; 770 | 771 | this._visitJscexStatements(stmt.stmts); 772 | this._indentLevel--; 773 | 774 | this._writeIndents() 775 | ._write("} else "); 776 | } 777 | 778 | this._writeLine("{"); 779 | this._indentLevel++; 780 | 781 | if (ast.elseStmts) { 782 | this._visitJscexStatements(ast.elseStmts); 783 | } else { 784 | this._writeIndents() 785 | ._writeLine("return " + this._builderVar + ".Normal();"); 786 | } 787 | 788 | this._indentLevel--; 789 | 790 | this._writeIndents() 791 | ._write("}"); 792 | }, 793 | 794 | "switch": function (ast) { 795 | this._write("switch (")._visitRaw(ast.item)._writeLine(") {"); 796 | this._indentLevel++; 797 | 798 | for (var i = 0; i < ast.caseStmts.length; i++) { 799 | var caseStmt = ast.caseStmts[i]; 800 | 801 | if (caseStmt.item) { 802 | this._writeIndents() 803 | ._write("case ")._visitRaw(caseStmt.item)._writeLine(":"); 804 | } else { 805 | this._writeIndents()._writeLine("default:"); 806 | } 807 | this._indentLevel++; 808 | 809 | this._visitJscexStatements(caseStmt.stmts); 810 | this._indentLevel--; 811 | } 812 | 813 | this._writeIndents() 814 | ._write("}"); 815 | }, 816 | 817 | "try": function (ast) { 818 | this._writeLine(this._builderVar + ".Try("); 819 | this._indentLevel++; 820 | 821 | this._writeIndents() 822 | ._visitJscex(ast.bodyStmt)._writeLine(","); 823 | 824 | if (ast.catchStmts) { 825 | this._writeIndents() 826 | ._writeLine("function (" + ast.exVar + ") {"); 827 | this._indentLevel++; 828 | 829 | this._visitJscexStatements(ast.catchStmts); 830 | this._indentLevel--; 831 | 832 | this._writeIndents() 833 | ._writeLine("},"); 834 | } else { 835 | this._writeIndents() 836 | ._writeLine("null,"); 837 | } 838 | 839 | if (ast.finallyStmt) { 840 | this._writeIndents() 841 | ._visitJscex(ast.finallyStmt)._writeLine(); 842 | } else { 843 | this._writeIndents() 844 | ._writeLine("null"); 845 | } 846 | this._indentLevel--; 847 | 848 | this._writeIndents() 849 | ._write(")"); 850 | }, 851 | 852 | "normal": function (ast) { 853 | this._write(this._builderVar + ".Normal()"); 854 | }, 855 | 856 | "throw": function (ast) { 857 | this._write(this._builderVar + ".Throw(")._visitRaw(ast.stmt[1])._write(")"); 858 | }, 859 | 860 | "break": function (ast) { 861 | this._write(this._builderVar + ".Break()"); 862 | }, 863 | 864 | "continue": function (ast) { 865 | this._write(this._builderVar + ".Continue()"); 866 | }, 867 | 868 | "return": function (ast) { 869 | this._write(this._builderVar + ".Return("); 870 | if (ast.stmt[1]) this._visitRaw(ast.stmt[1]); 871 | this._write(")"); 872 | } 873 | }, 874 | 875 | _rawVisitors: { 876 | "var": function (ast) { 877 | this._write("var "); 878 | 879 | var items = ast[1]; 880 | for (var i = 0; i < items.length; i++) { 881 | this._write(items[i][0]); 882 | if (items[i].length > 1) { 883 | this._write(" = ")._visitRaw(items[i][1]); 884 | } 885 | if (i < items.length - 1) this._write(", "); 886 | } 887 | 888 | this._write(";"); 889 | }, 890 | 891 | "seq": function (ast) { 892 | for (var i = 1; i < ast.length; i++) { 893 | this._visitRaw(ast[i]); 894 | if (i < ast.length - 1) this._write(", "); 895 | } 896 | }, 897 | 898 | "binary": function (ast) { 899 | var op = ast[1], left = ast[2], right = ast[3]; 900 | 901 | function needBracket(item) { 902 | var type = item[0]; 903 | return !(type == "num" || type == "name" || type == "dot"); 904 | } 905 | 906 | if (needBracket(left)) { 907 | this._write("(")._visitRaw(left)._write(") "); 908 | } else { 909 | this._visitRaw(left)._write(" "); 910 | } 911 | 912 | this._write(op); 913 | 914 | if (needBracket(right)) { 915 | this._write(" (")._visitRaw(right)._write(")"); 916 | } else { 917 | this._write(" ")._visitRaw(right); 918 | } 919 | }, 920 | 921 | "sub": function (ast) { 922 | var prop = ast[1], index = ast[2]; 923 | 924 | function needBracket() { 925 | return !(prop[0] == "name") 926 | } 927 | 928 | if (needBracket()) { 929 | this._write("(")._visitRaw(prop)._write(")[")._visitRaw(index)._write("]"); 930 | } else { 931 | this._visitRaw(prop)._write("[")._visitRaw(index)._write("]"); 932 | } 933 | }, 934 | 935 | "unary-postfix": function (ast) { 936 | var op = ast[1]; 937 | var item = ast[2]; 938 | this._visitRaw(item)._write(op); 939 | }, 940 | 941 | "unary-prefix": function (ast) { 942 | var op = ast[1]; 943 | var item = ast[2]; 944 | this._write(op); 945 | if (op == "typeof") { 946 | this._write("(")._visitRaw(item)._write(")"); 947 | } else { 948 | this._visitRaw(item); 949 | } 950 | }, 951 | 952 | "assign": function (ast) { 953 | var op = ast[1]; 954 | var name = ast[2]; 955 | var value = ast[3]; 956 | 957 | this._visitRaw(name); 958 | if ((typeof op) == "string") { 959 | this._write(" " + op + "= "); 960 | } else { 961 | this._write(" = "); 962 | } 963 | this._visitRaw(value); 964 | }, 965 | 966 | "stat": function (ast) { 967 | this._visitRaw(ast[1])._write(";"); 968 | }, 969 | 970 | "dot": function (ast) { 971 | function needBracket() { 972 | var leftOp = ast[1][0]; 973 | return !(leftOp == "dot" || leftOp == "name"); 974 | } 975 | 976 | if (needBracket()) { 977 | this._write("(")._visitRaw(ast[1])._write(").")._write(ast[2]); 978 | } else { 979 | this._visitRaw(ast[1])._write(".")._write(ast[2]); 980 | } 981 | }, 982 | 983 | "new": function (ast) { 984 | var ctor = ast[1]; 985 | 986 | this._write("new ")._visitRaw(ctor)._write("("); 987 | 988 | var args = ast[2]; 989 | for (var i = 0, len = args.length; i < len; i++) { 990 | this._visitRaw(args[i]); 991 | if (i < len - 1) this._write(", "); 992 | } 993 | 994 | this._write(")"); 995 | }, 996 | 997 | "call": function (ast) { 998 | 999 | if (_isJscexPattern(ast)) { 1000 | var indent = this._indent + this._indentLevel * 4; 1001 | var newCode = _compileJscexPattern(ast, indent); 1002 | this._write(newCode); 1003 | } else { 1004 | 1005 | var invalidBind = (ast[1][0] == "name") && (ast[1][1] == this._binder); 1006 | if (invalidBind) { 1007 | this._pos = { inFunction: true }; 1008 | this._buffer = []; 1009 | } 1010 | 1011 | this._visitRaw(ast[1])._write("("); 1012 | 1013 | var args = ast[2]; 1014 | for (var i = 0; i < args.length; i++) { 1015 | this._visitRaw(args[i]); 1016 | if (i < args.length - 1) this._write(", "); 1017 | } 1018 | 1019 | this._write(")"); 1020 | 1021 | if (invalidBind) { 1022 | throw ("Invalid bind operation: " + this._buffer.join("")); 1023 | } 1024 | } 1025 | }, 1026 | 1027 | "name": function (ast) { 1028 | this._write(ast[1]); 1029 | }, 1030 | 1031 | "object": function (ast) { 1032 | var items = ast[1]; 1033 | if (items.length <= 0) { 1034 | this._write("{ }"); 1035 | } else { 1036 | this._writeLine("{"); 1037 | this._indentLevel++; 1038 | 1039 | for (var i = 0; i < items.length; i++) { 1040 | this._writeIndents() 1041 | ._write(stringify(items[i][0]) + ": ") 1042 | ._visitRaw(items[i][1]); 1043 | 1044 | if (i < items.length - 1) { 1045 | this._writeLine(","); 1046 | } else { 1047 | this._writeLine(""); 1048 | } 1049 | } 1050 | 1051 | this._indentLevel--; 1052 | this._writeIndents()._write("}"); 1053 | } 1054 | }, 1055 | 1056 | "array": function (ast) { 1057 | this._write("["); 1058 | 1059 | var items = ast[1]; 1060 | for (var i = 0; i < items.length; i++) { 1061 | this._visitRaw(items[i]); 1062 | if (i < items.length - 1) this._write(", "); 1063 | } 1064 | 1065 | this._write("]"); 1066 | }, 1067 | 1068 | "num": function (ast) { 1069 | this._write(ast[1]); 1070 | }, 1071 | 1072 | "regexp": function (ast) { 1073 | this._write("/" + ast[1] + "/" + ast[2]); 1074 | }, 1075 | 1076 | "string": function (ast) { 1077 | this._write(stringify(ast[1])); 1078 | }, 1079 | 1080 | "function": function (ast) { 1081 | this._visitRawFunction(ast); 1082 | }, 1083 | 1084 | "defun": function (ast) { 1085 | this._visitRawFunction(ast); 1086 | }, 1087 | 1088 | "return": function (ast) { 1089 | if (this._pos.inFunction) { 1090 | this._write("return"); 1091 | var value = ast[1]; 1092 | if (value) this._write(" ")._visitRaw(value); 1093 | this._write(";"); 1094 | } else { 1095 | this._write("return ")._visitJscex({ type: "return", stmt: ast })._write(";"); 1096 | } 1097 | }, 1098 | 1099 | "for": function (ast) { 1100 | this._write("for ("); 1101 | 1102 | var setup = ast[1]; 1103 | if (setup) { 1104 | this._visitRaw(setup); 1105 | if (setup[0] != "var") { 1106 | this._write("; "); 1107 | } else { 1108 | this._write(" "); 1109 | } 1110 | } else { 1111 | this._write("; "); 1112 | } 1113 | 1114 | var condition = ast[2]; 1115 | if (condition) this._visitRaw(condition); 1116 | this._write("; "); 1117 | 1118 | var update = ast[3]; 1119 | if (update) this._visitRaw(update); 1120 | this._write(") "); 1121 | 1122 | var currInLoop = this._pos.inLoop; 1123 | this._pos.inLoop = true; 1124 | 1125 | var body = ast[4]; 1126 | this._visitRawBody(body); 1127 | 1128 | this._pos.inLoop = currInLoop; 1129 | }, 1130 | 1131 | "for-in": function (ast) { 1132 | this._write("for ("); 1133 | 1134 | var declare = ast[1]; 1135 | if (declare[0] == "var") { // declare == ["var", [["m"]]] 1136 | this._write("var " + declare[1][0][0]); 1137 | } else { 1138 | this._visitRaw(declare); 1139 | } 1140 | 1141 | this._write(" in ")._visitRaw(ast[3])._write(") "); 1142 | 1143 | var body = ast[4]; 1144 | this._visitRawBody(body); 1145 | }, 1146 | 1147 | "block": function (ast) { 1148 | this._writeLine("{") 1149 | this._indentLevel++; 1150 | 1151 | this._visitRawStatements(ast[1]); 1152 | this._indentLevel--; 1153 | 1154 | this._writeIndents() 1155 | ._write("}"); 1156 | }, 1157 | 1158 | "while": function (ast) { 1159 | var condition = ast[1]; 1160 | var body = ast[2]; 1161 | 1162 | var currInLoop = this._pos.inLoop 1163 | this._pos.inLoop = true; 1164 | 1165 | this._write("while (")._visitRaw(condition)._write(") ")._visitRawBody(body); 1166 | 1167 | this._pos.inLoop = currInLoop; 1168 | }, 1169 | 1170 | "do": function (ast) { 1171 | var condition = ast[1]; 1172 | var body = ast[2]; 1173 | 1174 | var currInLoop = this._pos.inLoop; 1175 | this._pos.inLoop = true; 1176 | 1177 | this._write("do ")._visitRawBody(body); 1178 | 1179 | this._pos.inLoop = currInLoop; 1180 | 1181 | if (body[0] == "block") { 1182 | this._write(" "); 1183 | } else { 1184 | this._writeLine()._writeIndents(); 1185 | } 1186 | 1187 | this._write("while (")._visitRaw(condition)._write(");"); 1188 | }, 1189 | 1190 | "if": function (ast) { 1191 | var condition = ast[1]; 1192 | var thenPart = ast[2]; 1193 | 1194 | this._write("if (")._visitRaw(condition)._write(") ")._visitRawBody(thenPart); 1195 | 1196 | var elsePart = ast[3]; 1197 | if (elsePart) { 1198 | if (thenPart[0] == "block") { 1199 | this._write(" "); 1200 | } else { 1201 | this._writeLine("") 1202 | ._writeIndents(); 1203 | } 1204 | 1205 | if (elsePart[0] == "if") { 1206 | this._write("else ")._visitRaw(elsePart); 1207 | } else { 1208 | this._write("else ")._visitRawBody(elsePart); 1209 | } 1210 | } 1211 | }, 1212 | 1213 | "break": function (ast) { 1214 | if (this._pos.inLoop || this._pos.inSwitch) { 1215 | this._write("break;"); 1216 | } else { 1217 | this._write("return ")._visitJscex({ type: "break", stmt: ast })._write(";"); 1218 | } 1219 | }, 1220 | 1221 | "continue": function (ast) { 1222 | if (this._pos.inLoop) { 1223 | this._write("continue;"); 1224 | } else { 1225 | this._write("return ")._visitJscex({ type: "continue", stmt: ast })._write(";"); 1226 | } 1227 | }, 1228 | 1229 | "throw": function (ast) { 1230 | var pos = this._pos; 1231 | if (pos.inTry || pos.inFunction) { 1232 | this._write("throw ")._visitRaw(ast[1])._write(";"); 1233 | } else { 1234 | this._write("return ")._visitJscex({ type: "throw", stmt: ast })._write(";"); 1235 | } 1236 | }, 1237 | 1238 | "conditional": function (ast) { 1239 | this._write("(")._visitRaw(ast[1])._write(") ? (")._visitRaw(ast[2])._write(") : (")._visitRaw(ast[3])._write(")"); 1240 | }, 1241 | 1242 | "try": function (ast) { 1243 | 1244 | this._writeLine("try {"); 1245 | this._indentLevel++; 1246 | 1247 | var currInTry = this._pos.inTry; 1248 | this._pos.inTry = true; 1249 | 1250 | this._visitRawStatements(ast[1]); 1251 | this._indentLevel--; 1252 | 1253 | this._pos.inTry = currInTry; 1254 | 1255 | var catchClause = ast[2]; 1256 | var finallyStatements = ast[3]; 1257 | 1258 | if (catchClause) { 1259 | this._writeIndents() 1260 | ._writeLine("} catch (" + catchClause[0] + ") {") 1261 | this._indentLevel++; 1262 | 1263 | this._visitRawStatements(catchClause[1]); 1264 | this._indentLevel--; 1265 | } 1266 | 1267 | if (finallyStatements) { 1268 | this._writeIndents() 1269 | ._writeLine("} finally {"); 1270 | this._indentLevel++; 1271 | 1272 | this._visitRawStatements(finallyStatements); 1273 | this._indentLevel--; 1274 | } 1275 | 1276 | this._writeIndents() 1277 | ._write("}"); 1278 | }, 1279 | 1280 | "switch": function (ast) { 1281 | this._write("switch (")._visitRaw(ast[1])._writeLine(") {"); 1282 | this._indentLevel++; 1283 | 1284 | var currInSwitch = this._pos.inSwitch; 1285 | this._pos.inSwitch = true; 1286 | 1287 | var cases = ast[2]; 1288 | for (var i = 0; i < cases.length; i++) { 1289 | var c = cases[i]; 1290 | this._writeIndents(); 1291 | 1292 | if (c[0]) { 1293 | this._write("case ")._visitRaw(c[0])._writeLine(":"); 1294 | } else { 1295 | this._writeLine("default:"); 1296 | } 1297 | this._indentLevel++; 1298 | 1299 | this._visitRawStatements(c[1]); 1300 | this._indentLevel--; 1301 | } 1302 | this._indentLevel--; 1303 | 1304 | this._pos.inSwitch = currInSwitch; 1305 | 1306 | this._writeIndents() 1307 | ._write("}"); 1308 | } 1309 | } 1310 | } 1311 | 1312 | function _isJscexPattern(ast) { 1313 | if (ast[0] != "call") return false; 1314 | 1315 | var evalName = ast[1]; 1316 | if (evalName[0] != "name" || evalName[1] != "eval") return false; 1317 | 1318 | var compileCall = ast[2][0]; 1319 | if (!compileCall || compileCall[0] != "call") return false; 1320 | 1321 | var compileMethod = compileCall[1]; 1322 | if (!compileMethod || compileMethod[0] != "dot" || compileMethod[2] != "compile") return false; 1323 | 1324 | var jscexName = compileMethod[1]; 1325 | if (!jscexName || jscexName[0] != "name" || jscexName[1] != "Jscex") return false; 1326 | 1327 | var builder = compileCall[2][0]; 1328 | if (!builder || builder[0] != "string") return false; 1329 | 1330 | var func = compileCall[2][1]; 1331 | if (!func || func[0] != "function") return false; 1332 | 1333 | return true; 1334 | } 1335 | 1336 | function _compileJscexPattern(ast, indent) { 1337 | 1338 | var builderName = ast[2][0][2][0][1]; 1339 | var funcAst = ast[2][0][2][1]; 1340 | var binder = root.binders[builderName]; 1341 | 1342 | var jscexTreeGenerator = new JscexTreeGenerator(binder); 1343 | var jscexAst = jscexTreeGenerator.generate(funcAst); 1344 | 1345 | var codeGenerator = new CodeGenerator(builderName, binder, indent); 1346 | var newCode = codeGenerator.generate(funcAst[2], jscexAst); 1347 | 1348 | return newCode; 1349 | } 1350 | 1351 | function compile(builderName, func) { 1352 | 1353 | var funcCode = func.toString(); 1354 | var evalCode = "eval(Jscex.compile(" + stringify(builderName) + ", " + funcCode + "))" 1355 | var evalCodeAst = root.parse(evalCode); 1356 | 1357 | // [ "toplevel", [ [ "stat", [ "call", ... ] ] ] ] 1358 | var evalAst = evalCodeAst[1][0][1]; 1359 | var newCode = _compileJscexPattern(evalAst, 0); 1360 | 1361 | root.logger.debug(funcCode + "\n\n>>>\n\n" + newCode); 1362 | 1363 | return codeGenerator(newCode); 1364 | }; 1365 | 1366 | root.compile = compile; 1367 | 1368 | root.modules["jit"] = true; 1369 | } 1370 | 1371 | var isCommonJS = (typeof require !== "undefined" && typeof module !== "undefined" && module.exports); 1372 | var isAmd = (typeof define !== "undefined" && define.amd); 1373 | 1374 | if (isCommonJS) { 1375 | module.exports.init = function (root) { 1376 | if (!root.modules["parser"]) { 1377 | require("./jscex-parser").init(root); 1378 | }; 1379 | 1380 | init(root); 1381 | } 1382 | } else if (isAmd) { 1383 | define("jscex-jit", ["jscex-parser"], function (parser) { 1384 | return { 1385 | init: function (root) { 1386 | if (!root.modules["parser"]) { 1387 | parser.init(root); 1388 | } 1389 | 1390 | init(root); 1391 | } 1392 | }; 1393 | }); 1394 | } else { 1395 | if (typeof Jscex === "undefined") { 1396 | throw new Error('Missing root object, please load "jscex" module first.'); 1397 | } 1398 | 1399 | if (!Jscex.modules["parser"]) { 1400 | throw new Error('Missing essential components, please initialize "parser" module first.'); 1401 | } 1402 | 1403 | init(Jscex); 1404 | } 1405 | 1406 | })(); 1407 | -------------------------------------------------------------------------------- /file/jscex-parser.js: -------------------------------------------------------------------------------- 1 | /*********************************************************************** 2 | 3 | A JavaScript tokenizer / parser / beautifier / compressor. 4 | 5 | This version is suitable for Node.js. With minimal changes (the 6 | exports stuff) it should work on any JS platform. 7 | 8 | This file contains the tokenizer/parser. It is a port to JavaScript 9 | of parse-js [1], a JavaScript parser library written in Common Lisp 10 | by Marijn Haverbeke. Thank you Marijn! 11 | 12 | [1] http://marijn.haverbeke.nl/parse-js/ 13 | 14 | Exported functions: 15 | 16 | - tokenizer(code) -- returns a function. Call the returned 17 | function to fetch the next token. 18 | 19 | - parse(code) -- returns an AST of the given JavaScript code. 20 | 21 | -------------------------------- (C) --------------------------------- 22 | 23 | Author: Mihai Bazon 24 | 25 | http://mihai.bazon.net/blog 26 | 27 | Distributed under the BSD license: 28 | 29 | Copyright 2010 (c) Mihai Bazon 30 | Based on parse-js (http://marijn.haverbeke.nl/parse-js/). 31 | 32 | Redistribution and use in source and binary forms, with or without 33 | modification, are permitted provided that the following conditions 34 | are met: 35 | 36 | * Redistributions of source code must retain the above 37 | copyright notice, this list of conditions and the following 38 | disclaimer. 39 | 40 | * Redistributions in binary form must reproduce the above 41 | copyright notice, this list of conditions and the following 42 | disclaimer in the documentation and/or other materials 43 | provided with the distribution. 44 | 45 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY 46 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 47 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 48 | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE 49 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 50 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 51 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 52 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 53 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 54 | TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 55 | THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 56 | SUCH DAMAGE. 57 | 58 | ***********************************************************************/ 59 | 60 | (function () { 61 | 62 | /* -----[ Tokenizer (constants) ]----- */ 63 | 64 | var KEYWORDS = array_to_hash([ 65 | "break", 66 | "case", 67 | "catch", 68 | "const", 69 | "continue", 70 | "default", 71 | "delete", 72 | "do", 73 | "else", 74 | "finally", 75 | "for", 76 | "function", 77 | "if", 78 | "in", 79 | "instanceof", 80 | "new", 81 | "return", 82 | "switch", 83 | "throw", 84 | "try", 85 | "typeof", 86 | "var", 87 | "void", 88 | "while", 89 | "with" 90 | ]); 91 | 92 | var RESERVED_WORDS = array_to_hash([ 93 | "abstract", 94 | "boolean", 95 | "byte", 96 | "char", 97 | "class", 98 | "debugger", 99 | "double", 100 | "enum", 101 | "export", 102 | "extends", 103 | "final", 104 | "float", 105 | "goto", 106 | "implements", 107 | "import", 108 | "int", 109 | "interface", 110 | "long", 111 | "native", 112 | "package", 113 | "private", 114 | "protected", 115 | "public", 116 | "short", 117 | "static", 118 | "super", 119 | "synchronized", 120 | "throws", 121 | "transient", 122 | "volatile" 123 | ]); 124 | 125 | var KEYWORDS_BEFORE_EXPRESSION = array_to_hash([ 126 | "return", 127 | "new", 128 | "delete", 129 | "throw", 130 | "else", 131 | "case" 132 | ]); 133 | 134 | var KEYWORDS_ATOM = array_to_hash([ 135 | "false", 136 | "null", 137 | "true", 138 | "undefined" 139 | ]); 140 | 141 | var OPERATOR_CHARS = array_to_hash(characters("+-*&%=<>!?|~^")); 142 | 143 | var RE_HEX_NUMBER = /^0x[0-9a-f]+$/i; 144 | var RE_OCT_NUMBER = /^0[0-7]+$/; 145 | var RE_DEC_NUMBER = /^\d*\.?\d*(?:e[+-]?\d*(?:\d\.?|\.?\d)\d*)?$/i; 146 | 147 | var OPERATORS = array_to_hash([ 148 | "in", 149 | "instanceof", 150 | "typeof", 151 | "new", 152 | "void", 153 | "delete", 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 | var WHITESPACE_CHARS = array_to_hash(characters(" \n\r\t\u200b")); 195 | 196 | var PUNC_BEFORE_EXPRESSION = array_to_hash(characters("[{}(,.;:")); 197 | 198 | var PUNC_CHARS = array_to_hash(characters("[]{}(),;:")); 199 | 200 | var REGEXP_MODIFIERS = array_to_hash(characters("gmsiy")); 201 | 202 | /* -----[ Tokenizer ]----- */ 203 | 204 | // regexps adapted from http://xregexp.com/plugins/#unicode 205 | var UNICODE = { 206 | letter: new RegExp("[\\u0041-\\u005A\\u0061-\\u007A\\u00AA\\u00B5\\u00BA\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02C1\\u02C6-\\u02D1\\u02E0-\\u02E4\\u02EC\\u02EE\\u0370-\\u0374\\u0376\\u0377\\u037A-\\u037D\\u0386\\u0388-\\u038A\\u038C\\u038E-\\u03A1\\u03A3-\\u03F5\\u03F7-\\u0481\\u048A-\\u0523\\u0531-\\u0556\\u0559\\u0561-\\u0587\\u05D0-\\u05EA\\u05F0-\\u05F2\\u0621-\\u064A\\u066E\\u066F\\u0671-\\u06D3\\u06D5\\u06E5\\u06E6\\u06EE\\u06EF\\u06FA-\\u06FC\\u06FF\\u0710\\u0712-\\u072F\\u074D-\\u07A5\\u07B1\\u07CA-\\u07EA\\u07F4\\u07F5\\u07FA\\u0904-\\u0939\\u093D\\u0950\\u0958-\\u0961\\u0971\\u0972\\u097B-\\u097F\\u0985-\\u098C\\u098F\\u0990\\u0993-\\u09A8\\u09AA-\\u09B0\\u09B2\\u09B6-\\u09B9\\u09BD\\u09CE\\u09DC\\u09DD\\u09DF-\\u09E1\\u09F0\\u09F1\\u0A05-\\u0A0A\\u0A0F\\u0A10\\u0A13-\\u0A28\\u0A2A-\\u0A30\\u0A32\\u0A33\\u0A35\\u0A36\\u0A38\\u0A39\\u0A59-\\u0A5C\\u0A5E\\u0A72-\\u0A74\\u0A85-\\u0A8D\\u0A8F-\\u0A91\\u0A93-\\u0AA8\\u0AAA-\\u0AB0\\u0AB2\\u0AB3\\u0AB5-\\u0AB9\\u0ABD\\u0AD0\\u0AE0\\u0AE1\\u0B05-\\u0B0C\\u0B0F\\u0B10\\u0B13-\\u0B28\\u0B2A-\\u0B30\\u0B32\\u0B33\\u0B35-\\u0B39\\u0B3D\\u0B5C\\u0B5D\\u0B5F-\\u0B61\\u0B71\\u0B83\\u0B85-\\u0B8A\\u0B8E-\\u0B90\\u0B92-\\u0B95\\u0B99\\u0B9A\\u0B9C\\u0B9E\\u0B9F\\u0BA3\\u0BA4\\u0BA8-\\u0BAA\\u0BAE-\\u0BB9\\u0BD0\\u0C05-\\u0C0C\\u0C0E-\\u0C10\\u0C12-\\u0C28\\u0C2A-\\u0C33\\u0C35-\\u0C39\\u0C3D\\u0C58\\u0C59\\u0C60\\u0C61\\u0C85-\\u0C8C\\u0C8E-\\u0C90\\u0C92-\\u0CA8\\u0CAA-\\u0CB3\\u0CB5-\\u0CB9\\u0CBD\\u0CDE\\u0CE0\\u0CE1\\u0D05-\\u0D0C\\u0D0E-\\u0D10\\u0D12-\\u0D28\\u0D2A-\\u0D39\\u0D3D\\u0D60\\u0D61\\u0D7A-\\u0D7F\\u0D85-\\u0D96\\u0D9A-\\u0DB1\\u0DB3-\\u0DBB\\u0DBD\\u0DC0-\\u0DC6\\u0E01-\\u0E30\\u0E32\\u0E33\\u0E40-\\u0E46\\u0E81\\u0E82\\u0E84\\u0E87\\u0E88\\u0E8A\\u0E8D\\u0E94-\\u0E97\\u0E99-\\u0E9F\\u0EA1-\\u0EA3\\u0EA5\\u0EA7\\u0EAA\\u0EAB\\u0EAD-\\u0EB0\\u0EB2\\u0EB3\\u0EBD\\u0EC0-\\u0EC4\\u0EC6\\u0EDC\\u0EDD\\u0F00\\u0F40-\\u0F47\\u0F49-\\u0F6C\\u0F88-\\u0F8B\\u1000-\\u102A\\u103F\\u1050-\\u1055\\u105A-\\u105D\\u1061\\u1065\\u1066\\u106E-\\u1070\\u1075-\\u1081\\u108E\\u10A0-\\u10C5\\u10D0-\\u10FA\\u10FC\\u1100-\\u1159\\u115F-\\u11A2\\u11A8-\\u11F9\\u1200-\\u1248\\u124A-\\u124D\\u1250-\\u1256\\u1258\\u125A-\\u125D\\u1260-\\u1288\\u128A-\\u128D\\u1290-\\u12B0\\u12B2-\\u12B5\\u12B8-\\u12BE\\u12C0\\u12C2-\\u12C5\\u12C8-\\u12D6\\u12D8-\\u1310\\u1312-\\u1315\\u1318-\\u135A\\u1380-\\u138F\\u13A0-\\u13F4\\u1401-\\u166C\\u166F-\\u1676\\u1681-\\u169A\\u16A0-\\u16EA\\u1700-\\u170C\\u170E-\\u1711\\u1720-\\u1731\\u1740-\\u1751\\u1760-\\u176C\\u176E-\\u1770\\u1780-\\u17B3\\u17D7\\u17DC\\u1820-\\u1877\\u1880-\\u18A8\\u18AA\\u1900-\\u191C\\u1950-\\u196D\\u1970-\\u1974\\u1980-\\u19A9\\u19C1-\\u19C7\\u1A00-\\u1A16\\u1B05-\\u1B33\\u1B45-\\u1B4B\\u1B83-\\u1BA0\\u1BAE\\u1BAF\\u1C00-\\u1C23\\u1C4D-\\u1C4F\\u1C5A-\\u1C7D\\u1D00-\\u1DBF\\u1E00-\\u1F15\\u1F18-\\u1F1D\\u1F20-\\u1F45\\u1F48-\\u1F4D\\u1F50-\\u1F57\\u1F59\\u1F5B\\u1F5D\\u1F5F-\\u1F7D\\u1F80-\\u1FB4\\u1FB6-\\u1FBC\\u1FBE\\u1FC2-\\u1FC4\\u1FC6-\\u1FCC\\u1FD0-\\u1FD3\\u1FD6-\\u1FDB\\u1FE0-\\u1FEC\\u1FF2-\\u1FF4\\u1FF6-\\u1FFC\\u2071\\u207F\\u2090-\\u2094\\u2102\\u2107\\u210A-\\u2113\\u2115\\u2119-\\u211D\\u2124\\u2126\\u2128\\u212A-\\u212D\\u212F-\\u2139\\u213C-\\u213F\\u2145-\\u2149\\u214E\\u2183\\u2184\\u2C00-\\u2C2E\\u2C30-\\u2C5E\\u2C60-\\u2C6F\\u2C71-\\u2C7D\\u2C80-\\u2CE4\\u2D00-\\u2D25\\u2D30-\\u2D65\\u2D6F\\u2D80-\\u2D96\\u2DA0-\\u2DA6\\u2DA8-\\u2DAE\\u2DB0-\\u2DB6\\u2DB8-\\u2DBE\\u2DC0-\\u2DC6\\u2DC8-\\u2DCE\\u2DD0-\\u2DD6\\u2DD8-\\u2DDE\\u2E2F\\u3005\\u3006\\u3031-\\u3035\\u303B\\u303C\\u3041-\\u3096\\u309D-\\u309F\\u30A1-\\u30FA\\u30FC-\\u30FF\\u3105-\\u312D\\u3131-\\u318E\\u31A0-\\u31B7\\u31F0-\\u31FF\\u3400\\u4DB5\\u4E00\\u9FC3\\uA000-\\uA48C\\uA500-\\uA60C\\uA610-\\uA61F\\uA62A\\uA62B\\uA640-\\uA65F\\uA662-\\uA66E\\uA67F-\\uA697\\uA717-\\uA71F\\uA722-\\uA788\\uA78B\\uA78C\\uA7FB-\\uA801\\uA803-\\uA805\\uA807-\\uA80A\\uA80C-\\uA822\\uA840-\\uA873\\uA882-\\uA8B3\\uA90A-\\uA925\\uA930-\\uA946\\uAA00-\\uAA28\\uAA40-\\uAA42\\uAA44-\\uAA4B\\uAC00\\uD7A3\\uF900-\\uFA2D\\uFA30-\\uFA6A\\uFA70-\\uFAD9\\uFB00-\\uFB06\\uFB13-\\uFB17\\uFB1D\\uFB1F-\\uFB28\\uFB2A-\\uFB36\\uFB38-\\uFB3C\\uFB3E\\uFB40\\uFB41\\uFB43\\uFB44\\uFB46-\\uFBB1\\uFBD3-\\uFD3D\\uFD50-\\uFD8F\\uFD92-\\uFDC7\\uFDF0-\\uFDFB\\uFE70-\\uFE74\\uFE76-\\uFEFC\\uFF21-\\uFF3A\\uFF41-\\uFF5A\\uFF66-\\uFFBE\\uFFC2-\\uFFC7\\uFFCA-\\uFFCF\\uFFD2-\\uFFD7\\uFFDA-\\uFFDC]"), 207 | non_spacing_mark: new RegExp("[\\u0300-\\u036F\\u0483-\\u0487\\u0591-\\u05BD\\u05BF\\u05C1\\u05C2\\u05C4\\u05C5\\u05C7\\u0610-\\u061A\\u064B-\\u065E\\u0670\\u06D6-\\u06DC\\u06DF-\\u06E4\\u06E7\\u06E8\\u06EA-\\u06ED\\u0711\\u0730-\\u074A\\u07A6-\\u07B0\\u07EB-\\u07F3\\u0816-\\u0819\\u081B-\\u0823\\u0825-\\u0827\\u0829-\\u082D\\u0900-\\u0902\\u093C\\u0941-\\u0948\\u094D\\u0951-\\u0955\\u0962\\u0963\\u0981\\u09BC\\u09C1-\\u09C4\\u09CD\\u09E2\\u09E3\\u0A01\\u0A02\\u0A3C\\u0A41\\u0A42\\u0A47\\u0A48\\u0A4B-\\u0A4D\\u0A51\\u0A70\\u0A71\\u0A75\\u0A81\\u0A82\\u0ABC\\u0AC1-\\u0AC5\\u0AC7\\u0AC8\\u0ACD\\u0AE2\\u0AE3\\u0B01\\u0B3C\\u0B3F\\u0B41-\\u0B44\\u0B4D\\u0B56\\u0B62\\u0B63\\u0B82\\u0BC0\\u0BCD\\u0C3E-\\u0C40\\u0C46-\\u0C48\\u0C4A-\\u0C4D\\u0C55\\u0C56\\u0C62\\u0C63\\u0CBC\\u0CBF\\u0CC6\\u0CCC\\u0CCD\\u0CE2\\u0CE3\\u0D41-\\u0D44\\u0D4D\\u0D62\\u0D63\\u0DCA\\u0DD2-\\u0DD4\\u0DD6\\u0E31\\u0E34-\\u0E3A\\u0E47-\\u0E4E\\u0EB1\\u0EB4-\\u0EB9\\u0EBB\\u0EBC\\u0EC8-\\u0ECD\\u0F18\\u0F19\\u0F35\\u0F37\\u0F39\\u0F71-\\u0F7E\\u0F80-\\u0F84\\u0F86\\u0F87\\u0F90-\\u0F97\\u0F99-\\u0FBC\\u0FC6\\u102D-\\u1030\\u1032-\\u1037\\u1039\\u103A\\u103D\\u103E\\u1058\\u1059\\u105E-\\u1060\\u1071-\\u1074\\u1082\\u1085\\u1086\\u108D\\u109D\\u135F\\u1712-\\u1714\\u1732-\\u1734\\u1752\\u1753\\u1772\\u1773\\u17B7-\\u17BD\\u17C6\\u17C9-\\u17D3\\u17DD\\u180B-\\u180D\\u18A9\\u1920-\\u1922\\u1927\\u1928\\u1932\\u1939-\\u193B\\u1A17\\u1A18\\u1A56\\u1A58-\\u1A5E\\u1A60\\u1A62\\u1A65-\\u1A6C\\u1A73-\\u1A7C\\u1A7F\\u1B00-\\u1B03\\u1B34\\u1B36-\\u1B3A\\u1B3C\\u1B42\\u1B6B-\\u1B73\\u1B80\\u1B81\\u1BA2-\\u1BA5\\u1BA8\\u1BA9\\u1C2C-\\u1C33\\u1C36\\u1C37\\u1CD0-\\u1CD2\\u1CD4-\\u1CE0\\u1CE2-\\u1CE8\\u1CED\\u1DC0-\\u1DE6\\u1DFD-\\u1DFF\\u20D0-\\u20DC\\u20E1\\u20E5-\\u20F0\\u2CEF-\\u2CF1\\u2DE0-\\u2DFF\\u302A-\\u302F\\u3099\\u309A\\uA66F\\uA67C\\uA67D\\uA6F0\\uA6F1\\uA802\\uA806\\uA80B\\uA825\\uA826\\uA8C4\\uA8E0-\\uA8F1\\uA926-\\uA92D\\uA947-\\uA951\\uA980-\\uA982\\uA9B3\\uA9B6-\\uA9B9\\uA9BC\\uAA29-\\uAA2E\\uAA31\\uAA32\\uAA35\\uAA36\\uAA43\\uAA4C\\uAAB0\\uAAB2-\\uAAB4\\uAAB7\\uAAB8\\uAABE\\uAABF\\uAAC1\\uABE5\\uABE8\\uABED\\uFB1E\\uFE00-\\uFE0F\\uFE20-\\uFE26]"), 208 | space_combining_mark: new RegExp("[\\u0903\\u093E-\\u0940\\u0949-\\u094C\\u094E\\u0982\\u0983\\u09BE-\\u09C0\\u09C7\\u09C8\\u09CB\\u09CC\\u09D7\\u0A03\\u0A3E-\\u0A40\\u0A83\\u0ABE-\\u0AC0\\u0AC9\\u0ACB\\u0ACC\\u0B02\\u0B03\\u0B3E\\u0B40\\u0B47\\u0B48\\u0B4B\\u0B4C\\u0B57\\u0BBE\\u0BBF\\u0BC1\\u0BC2\\u0BC6-\\u0BC8\\u0BCA-\\u0BCC\\u0BD7\\u0C01-\\u0C03\\u0C41-\\u0C44\\u0C82\\u0C83\\u0CBE\\u0CC0-\\u0CC4\\u0CC7\\u0CC8\\u0CCA\\u0CCB\\u0CD5\\u0CD6\\u0D02\\u0D03\\u0D3E-\\u0D40\\u0D46-\\u0D48\\u0D4A-\\u0D4C\\u0D57\\u0D82\\u0D83\\u0DCF-\\u0DD1\\u0DD8-\\u0DDF\\u0DF2\\u0DF3\\u0F3E\\u0F3F\\u0F7F\\u102B\\u102C\\u1031\\u1038\\u103B\\u103C\\u1056\\u1057\\u1062-\\u1064\\u1067-\\u106D\\u1083\\u1084\\u1087-\\u108C\\u108F\\u109A-\\u109C\\u17B6\\u17BE-\\u17C5\\u17C7\\u17C8\\u1923-\\u1926\\u1929-\\u192B\\u1930\\u1931\\u1933-\\u1938\\u19B0-\\u19C0\\u19C8\\u19C9\\u1A19-\\u1A1B\\u1A55\\u1A57\\u1A61\\u1A63\\u1A64\\u1A6D-\\u1A72\\u1B04\\u1B35\\u1B3B\\u1B3D-\\u1B41\\u1B43\\u1B44\\u1B82\\u1BA1\\u1BA6\\u1BA7\\u1BAA\\u1C24-\\u1C2B\\u1C34\\u1C35\\u1CE1\\u1CF2\\uA823\\uA824\\uA827\\uA880\\uA881\\uA8B4-\\uA8C3\\uA952\\uA953\\uA983\\uA9B4\\uA9B5\\uA9BA\\uA9BB\\uA9BD-\\uA9C0\\uAA2F\\uAA30\\uAA33\\uAA34\\uAA4D\\uAA7B\\uABE3\\uABE4\\uABE6\\uABE7\\uABE9\\uABEA\\uABEC]"), 209 | connector_punctuation: new RegExp("[\\u005F\\u203F\\u2040\\u2054\\uFE33\\uFE34\\uFE4D-\\uFE4F\\uFF3F]") 210 | }; 211 | 212 | function is_letter(ch) { 213 | return UNICODE.letter.test(ch); 214 | }; 215 | 216 | function is_digit(ch) { 217 | ch = ch.charCodeAt(0); 218 | return ch >= 48 && ch <= 57; //XXX: find out if "UnicodeDigit" means something else than 0..9 219 | }; 220 | 221 | function is_alphanumeric_char(ch) { 222 | return is_digit(ch) || is_letter(ch); 223 | }; 224 | 225 | function is_unicode_combining_mark(ch) { 226 | return UNICODE.non_spacing_mark.test(ch) || UNICODE.space_combining_mark.test(ch); 227 | }; 228 | 229 | function is_unicode_connector_punctuation(ch) { 230 | return UNICODE.connector_punctuation.test(ch); 231 | }; 232 | 233 | function is_identifier_start(ch) { 234 | return ch == "$" || ch == "_" || is_letter(ch); 235 | }; 236 | 237 | function is_identifier_char(ch) { 238 | return is_identifier_start(ch) 239 | || is_unicode_combining_mark(ch) 240 | || is_digit(ch) 241 | || is_unicode_connector_punctuation(ch) 242 | || ch == "\u200c" // zero-width non-joiner 243 | || ch == "\u200d" // zero-width joiner (in my ECMA-262 PDF, this is also 200c) 244 | ; 245 | }; 246 | 247 | function parse_js_number(num) { 248 | if (RE_HEX_NUMBER.test(num)) { 249 | return parseInt(num.substr(2), 16); 250 | } else if (RE_OCT_NUMBER.test(num)) { 251 | return parseInt(num.substr(1), 8); 252 | } else if (RE_DEC_NUMBER.test(num)) { 253 | return parseFloat(num); 254 | } 255 | }; 256 | 257 | function JS_Parse_Error(message, line, col, pos) { 258 | this.message = message; 259 | this.line = line; 260 | this.col = col; 261 | this.pos = pos; 262 | try { 263 | ({})(); 264 | } catch(ex) { 265 | this.stack = ex.stack; 266 | }; 267 | }; 268 | 269 | JS_Parse_Error.prototype.toString = function() { 270 | return this.message + " (line: " + this.line + ", col: " + this.col + ", pos: " + this.pos + ")" + "\n\n" + this.stack; 271 | }; 272 | 273 | function js_error(message, line, col, pos) { 274 | throw new JS_Parse_Error(message, line, col, pos); 275 | }; 276 | 277 | function is_token(token, type, val) { 278 | return token.type == type && (val == null || token.value == val); 279 | }; 280 | 281 | var EX_EOF = {}; 282 | 283 | function tokenizer($TEXT) { 284 | 285 | var S = { 286 | text : $TEXT.replace(/\r\n?|[\n\u2028\u2029]/g, "\n").replace(/^\uFEFF/, ''), 287 | pos : 0, 288 | tokpos : 0, 289 | line : 0, 290 | tokline : 0, 291 | col : 0, 292 | tokcol : 0, 293 | newline_before : false, 294 | regex_allowed : false, 295 | comments_before : [] 296 | }; 297 | 298 | function peek() { return S.text.charAt(S.pos); }; 299 | 300 | function next(signal_eof) { 301 | var ch = S.text.charAt(S.pos++); 302 | if (signal_eof && !ch) 303 | throw EX_EOF; 304 | if (ch == "\n") { 305 | S.newline_before = true; 306 | ++S.line; 307 | S.col = 0; 308 | } else { 309 | ++S.col; 310 | } 311 | return ch; 312 | }; 313 | 314 | function eof() { 315 | return !S.peek(); 316 | }; 317 | 318 | function find(what, signal_eof) { 319 | var pos = S.text.indexOf(what, S.pos); 320 | if (signal_eof && pos == -1) throw EX_EOF; 321 | return pos; 322 | }; 323 | 324 | function start_token() { 325 | S.tokline = S.line; 326 | S.tokcol = S.col; 327 | S.tokpos = S.pos; 328 | }; 329 | 330 | function token(type, value, is_comment) { 331 | S.regex_allowed = ((type == "operator" && !HOP(UNARY_POSTFIX, value)) || 332 | (type == "keyword" && HOP(KEYWORDS_BEFORE_EXPRESSION, value)) || 333 | (type == "punc" && HOP(PUNC_BEFORE_EXPRESSION, value))); 334 | var ret = { 335 | type : type, 336 | value : value, 337 | line : S.tokline, 338 | col : S.tokcol, 339 | pos : S.tokpos, 340 | nlb : S.newline_before 341 | }; 342 | if (!is_comment) { 343 | ret.comments_before = S.comments_before; 344 | S.comments_before = []; 345 | } 346 | S.newline_before = false; 347 | return ret; 348 | }; 349 | 350 | function skip_whitespace() { 351 | while (HOP(WHITESPACE_CHARS, peek())) 352 | next(); 353 | }; 354 | 355 | function read_while(pred) { 356 | var ret = "", ch = peek(), i = 0; 357 | while (ch && pred(ch, i++)) { 358 | ret += next(); 359 | ch = peek(); 360 | } 361 | return ret; 362 | }; 363 | 364 | function parse_error(err) { 365 | js_error(err, S.tokline, S.tokcol, S.tokpos); 366 | }; 367 | 368 | function read_num(prefix) { 369 | var has_e = false, after_e = false, has_x = false, has_dot = prefix == "."; 370 | var num = read_while(function(ch, i){ 371 | if (ch == "x" || ch == "X") { 372 | if (has_x) return false; 373 | return has_x = true; 374 | } 375 | if (!has_x && (ch == "E" || ch == "e")) { 376 | if (has_e) return false; 377 | return has_e = after_e = true; 378 | } 379 | if (ch == "-") { 380 | if (after_e || (i == 0 && !prefix)) return true; 381 | return false; 382 | } 383 | if (ch == "+") return after_e; 384 | after_e = false; 385 | if (ch == ".") { 386 | if (!has_dot && !has_x) 387 | return has_dot = true; 388 | return false; 389 | } 390 | return is_alphanumeric_char(ch); 391 | }); 392 | if (prefix) 393 | num = prefix + num; 394 | var valid = parse_js_number(num); 395 | if (!isNaN(valid)) { 396 | return token("num", valid); 397 | } else { 398 | parse_error("Invalid syntax: " + num); 399 | } 400 | }; 401 | 402 | function read_escaped_char() { 403 | var ch = next(true); 404 | switch (ch) { 405 | case "n" : return "\n"; 406 | case "r" : return "\r"; 407 | case "t" : return "\t"; 408 | case "b" : return "\b"; 409 | case "v" : return "\v"; 410 | case "f" : return "\f"; 411 | case "0" : return "\0"; 412 | case "x" : return String.fromCharCode(hex_bytes(2)); 413 | case "u" : return String.fromCharCode(hex_bytes(4)); 414 | default : return ch; 415 | } 416 | }; 417 | 418 | function hex_bytes(n) { 419 | var num = 0; 420 | for (; n > 0; --n) { 421 | var digit = parseInt(next(true), 16); 422 | if (isNaN(digit)) 423 | parse_error("Invalid hex-character pattern in string"); 424 | num = (num << 4) | digit; 425 | } 426 | return num; 427 | }; 428 | 429 | function read_string() { 430 | return with_eof_error("Unterminated string constant", function(){ 431 | var quote = next(), ret = ""; 432 | for (;;) { 433 | var ch = next(true); 434 | if (ch == "\\") ch = read_escaped_char(); 435 | else if (ch == quote) break; 436 | ret += ch; 437 | } 438 | return token("string", ret); 439 | }); 440 | }; 441 | 442 | function read_line_comment() { 443 | next(); 444 | var i = find("\n"), ret; 445 | if (i == -1) { 446 | ret = S.text.substr(S.pos); 447 | S.pos = S.text.length; 448 | } else { 449 | ret = S.text.substring(S.pos, i); 450 | S.pos = i; 451 | } 452 | return token("comment1", ret, true); 453 | }; 454 | 455 | function read_multiline_comment() { 456 | next(); 457 | return with_eof_error("Unterminated multiline comment", function(){ 458 | var i = find("*/", true), 459 | text = S.text.substring(S.pos, i), 460 | tok = token("comment2", text, true); 461 | S.pos = i + 2; 462 | S.line += text.split("\n").length - 1; 463 | S.newline_before = text.indexOf("\n") >= 0; 464 | 465 | // https://github.com/mishoo/UglifyJS/issues/#issue/100 466 | if (/^@cc_on/i.test(text)) { 467 | warn("WARNING: at line " + S.line); 468 | warn("*** Found \"conditional comment\": " + text); 469 | warn("*** UglifyJS DISCARDS ALL COMMENTS. This means your code might no longer work properly in Internet Explorer."); 470 | } 471 | 472 | return tok; 473 | }); 474 | }; 475 | 476 | function read_name() { 477 | var backslash = false, name = "", ch; 478 | while ((ch = peek()) != null) { 479 | if (!backslash) { 480 | if (ch == "\\") backslash = true, next(); 481 | else if (is_identifier_char(ch)) name += next(); 482 | else break; 483 | } 484 | else { 485 | if (ch != "u") parse_error("Expecting UnicodeEscapeSequence -- uXXXX"); 486 | ch = read_escaped_char(); 487 | if (!is_identifier_char(ch)) parse_error("Unicode char: " + ch.charCodeAt(0) + " is not valid in identifier"); 488 | name += ch; 489 | backslash = false; 490 | } 491 | } 492 | return name; 493 | }; 494 | 495 | function read_regexp() { 496 | return with_eof_error("Unterminated regular expression", function(){ 497 | var prev_backslash = false, regexp = "", ch, in_class = false; 498 | while ((ch = next(true))) if (prev_backslash) { 499 | regexp += "\\" + ch; 500 | prev_backslash = false; 501 | } else if (ch == "[") { 502 | in_class = true; 503 | regexp += ch; 504 | } else if (ch == "]" && in_class) { 505 | in_class = false; 506 | regexp += ch; 507 | } else if (ch == "/" && !in_class) { 508 | break; 509 | } else if (ch == "\\") { 510 | prev_backslash = true; 511 | } else { 512 | regexp += ch; 513 | } 514 | var mods = read_name(); 515 | return token("regexp", [ regexp, mods ]); 516 | }); 517 | }; 518 | 519 | function read_operator(prefix) { 520 | function grow(op) { 521 | if (!peek()) return op; 522 | var bigger = op + peek(); 523 | if (HOP(OPERATORS, bigger)) { 524 | next(); 525 | return grow(bigger); 526 | } else { 527 | return op; 528 | } 529 | }; 530 | return token("operator", grow(prefix || next())); 531 | }; 532 | 533 | function handle_slash() { 534 | next(); 535 | var regex_allowed = S.regex_allowed; 536 | switch (peek()) { 537 | case "/": 538 | S.comments_before.push(read_line_comment()); 539 | S.regex_allowed = regex_allowed; 540 | return next_token(); 541 | case "*": 542 | S.comments_before.push(read_multiline_comment()); 543 | S.regex_allowed = regex_allowed; 544 | return next_token(); 545 | } 546 | return S.regex_allowed ? read_regexp() : read_operator("/"); 547 | }; 548 | 549 | function handle_dot() { 550 | next(); 551 | return is_digit(peek()) 552 | ? read_num(".") 553 | : token("punc", "."); 554 | }; 555 | 556 | function read_word() { 557 | var word = read_name(); 558 | return !HOP(KEYWORDS, word) 559 | ? token("name", word) 560 | : HOP(OPERATORS, word) 561 | ? token("operator", word) 562 | : HOP(KEYWORDS_ATOM, word) 563 | ? token("atom", word) 564 | : token("keyword", word); 565 | }; 566 | 567 | function with_eof_error(eof_error, cont) { 568 | try { 569 | return cont(); 570 | } catch(ex) { 571 | if (ex === EX_EOF) parse_error(eof_error); 572 | else throw ex; 573 | } 574 | }; 575 | 576 | function next_token(force_regexp) { 577 | if (force_regexp) 578 | return read_regexp(); 579 | skip_whitespace(); 580 | start_token(); 581 | var ch = peek(); 582 | if (!ch) return token("eof"); 583 | if (is_digit(ch)) return read_num(); 584 | if (ch == '"' || ch == "'") return read_string(); 585 | if (HOP(PUNC_CHARS, ch)) return token("punc", next()); 586 | if (ch == ".") return handle_dot(); 587 | if (ch == "/") return handle_slash(); 588 | if (HOP(OPERATOR_CHARS, ch)) return read_operator(); 589 | if (ch == "\\" || is_identifier_start(ch)) return read_word(); 590 | parse_error("Unexpected character '" + ch + "'"); 591 | }; 592 | 593 | next_token.context = function(nc) { 594 | if (nc) S = nc; 595 | return S; 596 | }; 597 | 598 | return next_token; 599 | 600 | }; 601 | 602 | /* -----[ Parser (constants) ]----- */ 603 | 604 | var UNARY_PREFIX = array_to_hash([ 605 | "typeof", 606 | "void", 607 | "delete", 608 | "--", 609 | "++", 610 | "!", 611 | "~", 612 | "-", 613 | "+" 614 | ]); 615 | 616 | var UNARY_POSTFIX = array_to_hash([ "--", "++" ]); 617 | 618 | var ASSIGNMENT = (function(a, ret, i){ 619 | while (i < a.length) { 620 | ret[a[i]] = a[i].substr(0, a[i].length - 1); 621 | i++; 622 | } 623 | return ret; 624 | })( 625 | ["+=", "-=", "/=", "*=", "%=", ">>=", "<<=", ">>>=", "|=", "^=", "&="], 626 | { "=": true }, 627 | 0 628 | ); 629 | 630 | var PRECEDENCE = (function(a, ret){ 631 | for (var i = 0, n = 1; i < a.length; ++i, ++n) { 632 | var b = a[i]; 633 | for (var j = 0; j < b.length; ++j) { 634 | ret[b[j]] = n; 635 | } 636 | } 637 | return ret; 638 | })( 639 | [ 640 | ["||"], 641 | ["&&"], 642 | ["|"], 643 | ["^"], 644 | ["&"], 645 | ["==", "===", "!=", "!=="], 646 | ["<", ">", "<=", ">=", "in", "instanceof"], 647 | [">>", "<<", ">>>"], 648 | ["+", "-"], 649 | ["*", "/", "%"] 650 | ], 651 | {} 652 | ); 653 | 654 | var STATEMENTS_WITH_LABELS = array_to_hash([ "for", "do", "while", "switch" ]); 655 | 656 | var ATOMIC_START_TOKEN = array_to_hash([ "atom", "num", "string", "regexp", "name" ]); 657 | 658 | /* -----[ Parser ]----- */ 659 | 660 | function NodeWithToken(str, start, end) { 661 | this.name = str; 662 | this.start = start; 663 | this.end = end; 664 | }; 665 | 666 | NodeWithToken.prototype.toString = function() { return this.name; }; 667 | 668 | function parse($TEXT, exigent_mode, embed_tokens) { 669 | 670 | var S = { 671 | input : typeof $TEXT == "string" ? tokenizer($TEXT, true) : $TEXT, 672 | token : null, 673 | prev : null, 674 | peeked : null, 675 | in_function : 0, 676 | in_loop : 0, 677 | labels : [] 678 | }; 679 | 680 | S.token = next(); 681 | 682 | function is(type, value) { 683 | return is_token(S.token, type, value); 684 | }; 685 | 686 | function peek() { return S.peeked || (S.peeked = S.input()); }; 687 | 688 | function next() { 689 | S.prev = S.token; 690 | if (S.peeked) { 691 | S.token = S.peeked; 692 | S.peeked = null; 693 | } else { 694 | S.token = S.input(); 695 | } 696 | return S.token; 697 | }; 698 | 699 | function prev() { 700 | return S.prev; 701 | }; 702 | 703 | function croak(msg, line, col, pos) { 704 | var ctx = S.input.context(); 705 | js_error(msg, 706 | line != null ? line : ctx.tokline, 707 | col != null ? col : ctx.tokcol, 708 | pos != null ? pos : ctx.tokpos); 709 | }; 710 | 711 | function token_error(token, msg) { 712 | croak(msg, token.line, token.col); 713 | }; 714 | 715 | function unexpected(token) { 716 | if (token == null) 717 | token = S.token; 718 | token_error(token, "Unexpected token: " + token.type + " (" + token.value + ")"); 719 | }; 720 | 721 | function expect_token(type, val) { 722 | if (is(type, val)) { 723 | return next(); 724 | } 725 | token_error(S.token, "Unexpected token " + S.token.type + ", expected " + type); 726 | }; 727 | 728 | function expect(punc) { return expect_token("punc", punc); }; 729 | 730 | function can_insert_semicolon() { 731 | return !exigent_mode && ( 732 | S.token.nlb || is("eof") || is("punc", "}") 733 | ); 734 | }; 735 | 736 | function semicolon() { 737 | if (is("punc", ";")) next(); 738 | else if (!can_insert_semicolon()) unexpected(); 739 | }; 740 | 741 | function as() { 742 | return slice(arguments); 743 | }; 744 | 745 | function parenthesised() { 746 | expect("("); 747 | var ex = expression(); 748 | expect(")"); 749 | return ex; 750 | }; 751 | 752 | function add_tokens(str, start, end) { 753 | return str instanceof NodeWithToken ? str : new NodeWithToken(str, start, end); 754 | }; 755 | 756 | var statement = embed_tokens ? function() { 757 | var start = S.token; 758 | var ast = $statement.apply(this, arguments); 759 | ast[0] = add_tokens(ast[0], start, prev()); 760 | return ast; 761 | } : $statement; 762 | 763 | function $statement() { 764 | if (is("operator", "/")) { 765 | S.peeked = null; 766 | S.token = S.input(true); // force regexp 767 | } 768 | switch (S.token.type) { 769 | case "num": 770 | case "string": 771 | case "regexp": 772 | case "operator": 773 | case "atom": 774 | return simple_statement(); 775 | 776 | case "name": 777 | return is_token(peek(), "punc", ":") 778 | ? labeled_statement(prog1(S.token.value, next, next)) 779 | : simple_statement(); 780 | 781 | case "punc": 782 | switch (S.token.value) { 783 | case "{": 784 | return as("block", block_()); 785 | case "[": 786 | case "(": 787 | return simple_statement(); 788 | case ";": 789 | next(); 790 | return as("block"); 791 | default: 792 | unexpected(); 793 | } 794 | 795 | case "keyword": 796 | switch (prog1(S.token.value, next)) { 797 | case "break": 798 | return break_cont("break"); 799 | 800 | case "continue": 801 | return break_cont("continue"); 802 | 803 | case "debugger": 804 | semicolon(); 805 | return as("debugger"); 806 | 807 | case "do": 808 | return (function(body){ 809 | expect_token("keyword", "while"); 810 | return as("do", prog1(parenthesised, semicolon), body); 811 | })(in_loop(statement)); 812 | 813 | case "for": 814 | return for_(); 815 | 816 | case "function": 817 | return function_(true); 818 | 819 | case "if": 820 | return if_(); 821 | 822 | case "return": 823 | if (S.in_function == 0) 824 | croak("'return' outside of function"); 825 | return as("return", 826 | is("punc", ";") 827 | ? (next(), null) 828 | : can_insert_semicolon() 829 | ? null 830 | : prog1(expression, semicolon)); 831 | 832 | case "switch": 833 | return as("switch", parenthesised(), switch_block_()); 834 | 835 | case "throw": 836 | return as("throw", prog1(expression, semicolon)); 837 | 838 | case "try": 839 | return try_(); 840 | 841 | case "var": 842 | return prog1(var_, semicolon); 843 | 844 | case "const": 845 | return prog1(const_, semicolon); 846 | 847 | case "while": 848 | return as("while", parenthesised(), in_loop(statement)); 849 | 850 | case "with": 851 | return as("with", parenthesised(), statement()); 852 | 853 | default: 854 | unexpected(); 855 | } 856 | } 857 | }; 858 | 859 | function labeled_statement(label) { 860 | S.labels.push(label); 861 | var start = S.token, stat = statement(); 862 | if (exigent_mode && !HOP(STATEMENTS_WITH_LABELS, stat[0])) 863 | unexpected(start); 864 | S.labels.pop(); 865 | return as("label", label, stat); 866 | }; 867 | 868 | function simple_statement() { 869 | return as("stat", prog1(expression, semicolon)); 870 | }; 871 | 872 | function break_cont(type) { 873 | var name = is("name") ? S.token.value : null; 874 | if (name != null) { 875 | next(); 876 | if (!member(name, S.labels)) 877 | croak("Label " + name + " without matching loop or statement"); 878 | } 879 | else if (S.in_loop == 0) 880 | croak(type + " not inside a loop or switch"); 881 | semicolon(); 882 | return as(type, name); 883 | }; 884 | 885 | function for_() { 886 | expect("("); 887 | var init = null; 888 | if (!is("punc", ";")) { 889 | init = is("keyword", "var") 890 | ? (next(), var_(true)) 891 | : expression(true, true); 892 | if (is("operator", "in")) 893 | return for_in(init); 894 | } 895 | return regular_for(init); 896 | }; 897 | 898 | function regular_for(init) { 899 | expect(";"); 900 | var test = is("punc", ";") ? null : expression(); 901 | expect(";"); 902 | var step = is("punc", ")") ? null : expression(); 903 | expect(")"); 904 | return as("for", init, test, step, in_loop(statement)); 905 | }; 906 | 907 | function for_in(init) { 908 | var lhs = init[0] == "var" ? as("name", init[1][0]) : init; 909 | next(); 910 | var obj = expression(); 911 | expect(")"); 912 | return as("for-in", init, lhs, obj, in_loop(statement)); 913 | }; 914 | 915 | var function_ = embed_tokens ? function() { 916 | var start = prev(); 917 | var ast = $function_.apply(this, arguments); 918 | ast[0] = add_tokens(ast[0], start, prev()); 919 | return ast; 920 | } : $function_; 921 | 922 | function $function_(in_statement) { 923 | var name = is("name") ? prog1(S.token.value, next) : null; 924 | if (in_statement && !name) 925 | unexpected(); 926 | expect("("); 927 | return as(in_statement ? "defun" : "function", 928 | name, 929 | // arguments 930 | (function(first, a){ 931 | while (!is("punc", ")")) { 932 | if (first) first = false; else expect(","); 933 | if (!is("name")) unexpected(); 934 | a.push(S.token.value); 935 | next(); 936 | } 937 | next(); 938 | return a; 939 | })(true, []), 940 | // body 941 | (function(){ 942 | ++S.in_function; 943 | var loop = S.in_loop; 944 | S.in_loop = 0; 945 | var a = block_(); 946 | --S.in_function; 947 | S.in_loop = loop; 948 | return a; 949 | })()); 950 | }; 951 | 952 | function if_() { 953 | var cond = parenthesised(), body = statement(), belse; 954 | if (is("keyword", "else")) { 955 | next(); 956 | belse = statement(); 957 | } 958 | return as("if", cond, body, belse); 959 | }; 960 | 961 | function block_() { 962 | expect("{"); 963 | var a = []; 964 | while (!is("punc", "}")) { 965 | if (is("eof")) unexpected(); 966 | a.push(statement()); 967 | } 968 | next(); 969 | return a; 970 | }; 971 | 972 | var switch_block_ = curry(in_loop, function(){ 973 | expect("{"); 974 | var a = [], cur = null; 975 | while (!is("punc", "}")) { 976 | if (is("eof")) unexpected(); 977 | if (is("keyword", "case")) { 978 | next(); 979 | cur = []; 980 | a.push([ expression(), cur ]); 981 | expect(":"); 982 | } 983 | else if (is("keyword", "default")) { 984 | next(); 985 | expect(":"); 986 | cur = []; 987 | a.push([ null, cur ]); 988 | } 989 | else { 990 | if (!cur) unexpected(); 991 | cur.push(statement()); 992 | } 993 | } 994 | next(); 995 | return a; 996 | }); 997 | 998 | function try_() { 999 | var body = block_(), bcatch, bfinally; 1000 | if (is("keyword", "catch")) { 1001 | next(); 1002 | expect("("); 1003 | if (!is("name")) 1004 | croak("Name expected"); 1005 | var name = S.token.value; 1006 | next(); 1007 | expect(")"); 1008 | bcatch = [ name, block_() ]; 1009 | } 1010 | if (is("keyword", "finally")) { 1011 | next(); 1012 | bfinally = block_(); 1013 | } 1014 | if (!bcatch && !bfinally) 1015 | croak("Missing catch/finally blocks"); 1016 | return as("try", body, bcatch, bfinally); 1017 | }; 1018 | 1019 | function vardefs(no_in) { 1020 | var a = []; 1021 | for (;;) { 1022 | if (!is("name")) 1023 | unexpected(); 1024 | var name = S.token.value; 1025 | next(); 1026 | if (is("operator", "=")) { 1027 | next(); 1028 | a.push([ name, expression(false, no_in) ]); 1029 | } else { 1030 | a.push([ name ]); 1031 | } 1032 | if (!is("punc", ",")) 1033 | break; 1034 | next(); 1035 | } 1036 | return a; 1037 | }; 1038 | 1039 | function var_(no_in) { 1040 | return as("var", vardefs(no_in)); 1041 | }; 1042 | 1043 | function const_() { 1044 | return as("const", vardefs()); 1045 | }; 1046 | 1047 | function new_() { 1048 | var newexp = expr_atom(false), args; 1049 | if (is("punc", "(")) { 1050 | next(); 1051 | args = expr_list(")"); 1052 | } else { 1053 | args = []; 1054 | } 1055 | return subscripts(as("new", newexp, args), true); 1056 | }; 1057 | 1058 | function expr_atom(allow_calls) { 1059 | if (is("operator", "new")) { 1060 | next(); 1061 | return new_(); 1062 | } 1063 | if (is("operator") && HOP(UNARY_PREFIX, S.token.value)) { 1064 | return make_unary("unary-prefix", 1065 | prog1(S.token.value, next), 1066 | expr_atom(allow_calls)); 1067 | } 1068 | if (is("punc")) { 1069 | switch (S.token.value) { 1070 | case "(": 1071 | next(); 1072 | return subscripts(prog1(expression, curry(expect, ")")), allow_calls); 1073 | case "[": 1074 | next(); 1075 | return subscripts(array_(), allow_calls); 1076 | case "{": 1077 | next(); 1078 | return subscripts(object_(), allow_calls); 1079 | } 1080 | unexpected(); 1081 | } 1082 | if (is("keyword", "function")) { 1083 | next(); 1084 | return subscripts(function_(false), allow_calls); 1085 | } 1086 | if (HOP(ATOMIC_START_TOKEN, S.token.type)) { 1087 | var atom = S.token.type == "regexp" 1088 | ? as("regexp", S.token.value[0], S.token.value[1]) 1089 | : as(S.token.type, S.token.value); 1090 | return subscripts(prog1(atom, next), allow_calls); 1091 | } 1092 | unexpected(); 1093 | }; 1094 | 1095 | function expr_list(closing, allow_trailing_comma, allow_empty) { 1096 | var first = true, a = []; 1097 | while (!is("punc", closing)) { 1098 | if (first) first = false; else expect(","); 1099 | if (allow_trailing_comma && is("punc", closing)) break; 1100 | if (is("punc", ",") && allow_empty) { 1101 | a.push([ "atom", "undefined" ]); 1102 | } else { 1103 | a.push(expression(false)); 1104 | } 1105 | } 1106 | next(); 1107 | return a; 1108 | }; 1109 | 1110 | function array_() { 1111 | return as("array", expr_list("]", !exigent_mode, true)); 1112 | }; 1113 | 1114 | function object_() { 1115 | var first = true, a = []; 1116 | while (!is("punc", "}")) { 1117 | if (first) first = false; else expect(","); 1118 | if (!exigent_mode && is("punc", "}")) 1119 | // allow trailing comma 1120 | break; 1121 | var type = S.token.type; 1122 | var name = as_property_name(); 1123 | if (type == "name" && (name == "get" || name == "set") && !is("punc", ":")) { 1124 | a.push([ as_name(), function_(false), name ]); 1125 | } else { 1126 | expect(":"); 1127 | a.push([ name, expression(false) ]); 1128 | } 1129 | } 1130 | next(); 1131 | return as("object", a); 1132 | }; 1133 | 1134 | function as_property_name() { 1135 | switch (S.token.type) { 1136 | case "num": 1137 | case "string": 1138 | return prog1(S.token.value, next); 1139 | } 1140 | return as_name(); 1141 | }; 1142 | 1143 | function as_name() { 1144 | switch (S.token.type) { 1145 | case "name": 1146 | case "operator": 1147 | case "keyword": 1148 | case "atom": 1149 | return prog1(S.token.value, next); 1150 | default: 1151 | unexpected(); 1152 | } 1153 | }; 1154 | 1155 | function subscripts(expr, allow_calls) { 1156 | if (is("punc", ".")) { 1157 | next(); 1158 | return subscripts(as("dot", expr, as_name()), allow_calls); 1159 | } 1160 | if (is("punc", "[")) { 1161 | next(); 1162 | return subscripts(as("sub", expr, prog1(expression, curry(expect, "]"))), allow_calls); 1163 | } 1164 | if (allow_calls && is("punc", "(")) { 1165 | next(); 1166 | return subscripts(as("call", expr, expr_list(")")), true); 1167 | } 1168 | if (allow_calls && is("operator") && HOP(UNARY_POSTFIX, S.token.value)) { 1169 | return prog1(curry(make_unary, "unary-postfix", S.token.value, expr), 1170 | next); 1171 | } 1172 | return expr; 1173 | }; 1174 | 1175 | function make_unary(tag, op, expr) { 1176 | if ((op == "++" || op == "--") && !is_assignable(expr)) 1177 | croak("Invalid use of " + op + " operator"); 1178 | return as(tag, op, expr); 1179 | }; 1180 | 1181 | function expr_op(left, min_prec, no_in) { 1182 | var op = is("operator") ? S.token.value : null; 1183 | if (op && op == "in" && no_in) op = null; 1184 | var prec = op != null ? PRECEDENCE[op] : null; 1185 | if (prec != null && prec > min_prec) { 1186 | next(); 1187 | var right = expr_op(expr_atom(true), prec, no_in); 1188 | return expr_op(as("binary", op, left, right), min_prec, no_in); 1189 | } 1190 | return left; 1191 | }; 1192 | 1193 | function expr_ops(no_in) { 1194 | return expr_op(expr_atom(true), 0, no_in); 1195 | }; 1196 | 1197 | function maybe_conditional(no_in) { 1198 | var expr = expr_ops(no_in); 1199 | if (is("operator", "?")) { 1200 | next(); 1201 | var yes = expression(false); 1202 | expect(":"); 1203 | return as("conditional", expr, yes, expression(false, no_in)); 1204 | } 1205 | return expr; 1206 | }; 1207 | 1208 | function is_assignable(expr) { 1209 | if (!exigent_mode) return true; 1210 | switch (expr[0]) { 1211 | case "dot": 1212 | case "sub": 1213 | case "new": 1214 | case "call": 1215 | return true; 1216 | case "name": 1217 | return expr[1] != "this"; 1218 | } 1219 | }; 1220 | 1221 | function maybe_assign(no_in) { 1222 | var left = maybe_conditional(no_in), val = S.token.value; 1223 | if (is("operator") && HOP(ASSIGNMENT, val)) { 1224 | if (is_assignable(left)) { 1225 | next(); 1226 | return as("assign", ASSIGNMENT[val], left, maybe_assign(no_in)); 1227 | } 1228 | croak("Invalid assignment"); 1229 | } 1230 | return left; 1231 | }; 1232 | 1233 | function expression(commas, no_in) { 1234 | if (arguments.length == 0) 1235 | commas = true; 1236 | var expr = maybe_assign(no_in); 1237 | if (commas && is("punc", ",")) { 1238 | next(); 1239 | return as("seq", expr, expression(true, no_in)); 1240 | } 1241 | return expr; 1242 | }; 1243 | 1244 | function in_loop(cont) { 1245 | try { 1246 | ++S.in_loop; 1247 | return cont(); 1248 | } finally { 1249 | --S.in_loop; 1250 | } 1251 | }; 1252 | 1253 | return as("toplevel", (function(a){ 1254 | while (!is("eof")) 1255 | a.push(statement()); 1256 | return a; 1257 | })([])); 1258 | 1259 | }; 1260 | 1261 | /* -----[ Utilities ]----- */ 1262 | 1263 | function curry(f) { 1264 | var args = slice(arguments, 1); 1265 | return function() { return f.apply(this, args.concat(slice(arguments))); }; 1266 | }; 1267 | 1268 | function prog1(ret) { 1269 | if (ret instanceof Function) 1270 | ret = ret(); 1271 | for (var i = 1, n = arguments.length; --n > 0; ++i) 1272 | arguments[i](); 1273 | return ret; 1274 | }; 1275 | 1276 | function array_to_hash(a) { 1277 | var ret = {}; 1278 | for (var i = 0; i < a.length; ++i) 1279 | ret[a[i]] = true; 1280 | return ret; 1281 | }; 1282 | 1283 | function slice(a, start) { 1284 | return Array.prototype.slice.call(a, start == null ? 0 : start); 1285 | }; 1286 | 1287 | function characters(str) { 1288 | return str.split(""); 1289 | }; 1290 | 1291 | function member(name, array) { 1292 | for (var i = array.length; --i >= 0;) 1293 | if (array[i] === name) 1294 | return true; 1295 | return false; 1296 | }; 1297 | 1298 | function HOP(obj, prop) { 1299 | return Object.prototype.hasOwnProperty.call(obj, prop); 1300 | }; 1301 | 1302 | var warn = function() {}; 1303 | 1304 | /* -----[ Exports ]----- */ 1305 | 1306 | var init = function (root) { 1307 | if (root.modules["parser"]) { 1308 | return; 1309 | } 1310 | 1311 | root.parse = parse; 1312 | 1313 | root.modules["parser"] = true; 1314 | } 1315 | 1316 | var isCommonJS = (typeof require !== "undefined" && typeof module !== "undefined" && module.exports); 1317 | var isAmd = (typeof define !== "undefined" && define.amd); 1318 | 1319 | if (isCommonJS) { 1320 | module.exports.init = init; 1321 | } else if (isAmd) { 1322 | define("jscex-parser", function () { 1323 | return { init: init }; 1324 | }); 1325 | } else { 1326 | if (typeof Jscex === "undefined") { 1327 | throw new Error('Missing root object, please load "jscex" module first.'); 1328 | } 1329 | 1330 | init(Jscex); 1331 | } 1332 | 1333 | /* 1334 | scope.tokenizer = tokenizer; 1335 | scope.parse = parse; 1336 | scope.slice = slice; 1337 | scope.curry = curry; 1338 | scope.member = member; 1339 | scope.array_to_hash = array_to_hash; 1340 | scope.PRECEDENCE = PRECEDENCE; 1341 | scope.KEYWORDS_ATOM = KEYWORDS_ATOM; 1342 | scope.RESERVED_WORDS = RESERVED_WORDS; 1343 | scope.KEYWORDS = KEYWORDS; 1344 | scope.ATOMIC_START_TOKEN = ATOMIC_START_TOKEN; 1345 | scope.OPERATORS = OPERATORS; 1346 | scope.is_alphanumeric_char = is_alphanumeric_char; 1347 | scope.set_logger = function (logger) { 1348 | warn = logger; 1349 | }; 1350 | */ 1351 | 1352 | })(); --------------------------------------------------------------------------------