1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | "use strict";
|
45 |
|
46 | function characters(str) {
|
47 | return str.split("");
|
48 | }
|
49 |
|
50 | function member(name, array) {
|
51 | return array.indexOf(name) >= 0;
|
52 | }
|
53 |
|
54 | function find_if(func, array) {
|
55 | for (var i = array.length; --i >= 0;) if (func(array[i])) return array[i];
|
56 | }
|
57 |
|
58 | function repeat_string(str, i) {
|
59 | if (i <= 0) return "";
|
60 | if (i == 1) return str;
|
61 | var d = repeat_string(str, i >> 1);
|
62 | d += d;
|
63 | return i & 1 ? d + str : d;
|
64 | }
|
65 |
|
66 | function configure_error_stack(fn) {
|
67 | Object.defineProperty(fn.prototype, "stack", {
|
68 | get: function() {
|
69 | var err = new Error(this.message);
|
70 | err.name = this.name;
|
71 | try {
|
72 | throw err;
|
73 | } catch (e) {
|
74 | return e.stack;
|
75 | }
|
76 | }
|
77 | });
|
78 | }
|
79 |
|
80 | function DefaultsError(msg, defs) {
|
81 | this.message = msg;
|
82 | this.defs = defs;
|
83 | }
|
84 | DefaultsError.prototype = Object.create(Error.prototype);
|
85 | DefaultsError.prototype.constructor = DefaultsError;
|
86 | DefaultsError.prototype.name = "DefaultsError";
|
87 | configure_error_stack(DefaultsError);
|
88 |
|
89 | function defaults(args, defs, croak) {
|
90 | if (args === true) args = {};
|
91 | var ret = args || {};
|
92 | if (croak) for (var i in ret) if (HOP(ret, i) && !HOP(defs, i)) {
|
93 | throw new DefaultsError("`" + i + "` is not a supported option", defs);
|
94 | }
|
95 | for (var i in defs) if (HOP(defs, i)) {
|
96 | ret[i] = (args && HOP(args, i)) ? args[i] : defs[i];
|
97 | }
|
98 | return ret;
|
99 | }
|
100 |
|
101 | function merge(obj, ext) {
|
102 | var count = 0;
|
103 | for (var i in ext) if (HOP(ext, i)) {
|
104 | obj[i] = ext[i];
|
105 | count++;
|
106 | }
|
107 | return count;
|
108 | }
|
109 |
|
110 | function noop() {}
|
111 | function return_false() { return false; }
|
112 | function return_true() { return true; }
|
113 | function return_this() { return this; }
|
114 | function return_null() { return null; }
|
115 |
|
116 | var MAP = (function() {
|
117 | function MAP(a, f, backwards) {
|
118 | var ret = [], top = [], i;
|
119 | function doit() {
|
120 | var val = f(a[i], i);
|
121 | var is_last = val instanceof Last;
|
122 | if (is_last) val = val.v;
|
123 | if (val instanceof AtTop) {
|
124 | val = val.v;
|
125 | if (val instanceof Splice) {
|
126 | top.push.apply(top, backwards ? val.v.slice().reverse() : val.v);
|
127 | } else {
|
128 | top.push(val);
|
129 | }
|
130 | } else if (val !== skip) {
|
131 | if (val instanceof Splice) {
|
132 | ret.push.apply(ret, backwards ? val.v.slice().reverse() : val.v);
|
133 | } else {
|
134 | ret.push(val);
|
135 | }
|
136 | }
|
137 | return is_last;
|
138 | }
|
139 | if (Array.isArray(a)) {
|
140 | if (backwards) {
|
141 | for (i = a.length; --i >= 0;) if (doit()) break;
|
142 | ret.reverse();
|
143 | top.reverse();
|
144 | } else {
|
145 | for (i = 0; i < a.length; ++i) if (doit()) break;
|
146 | }
|
147 | } else {
|
148 | for (i in a) if (HOP(a, i)) if (doit()) break;
|
149 | }
|
150 | return top.concat(ret);
|
151 | }
|
152 | MAP.at_top = function(val) { return new AtTop(val) };
|
153 | MAP.splice = function(val) { return new Splice(val) };
|
154 | MAP.last = function(val) { return new Last(val) };
|
155 | var skip = MAP.skip = {};
|
156 | function AtTop(val) { this.v = val }
|
157 | function Splice(val) { this.v = val }
|
158 | function Last(val) { this.v = val }
|
159 | return MAP;
|
160 | })();
|
161 |
|
162 | function push_uniq(array, el) {
|
163 | if (array.indexOf(el) < 0) return array.push(el);
|
164 | }
|
165 |
|
166 | function string_template(text, props) {
|
167 | return text.replace(/\{(.+?)\}/g, function(str, p) {
|
168 | return props && props[p];
|
169 | });
|
170 | }
|
171 |
|
172 | function remove(array, el) {
|
173 | var index = array.indexOf(el);
|
174 | if (index >= 0) array.splice(index, 1);
|
175 | }
|
176 |
|
177 | function makePredicate(words) {
|
178 | if (!Array.isArray(words)) words = words.split(" ");
|
179 | var map = Object.create(null);
|
180 | words.forEach(function(word) {
|
181 | map[word] = true;
|
182 | });
|
183 | return map;
|
184 | }
|
185 |
|
186 | function all(array, predicate) {
|
187 | for (var i = array.length; --i >= 0;)
|
188 | if (!predicate(array[i]))
|
189 | return false;
|
190 | return true;
|
191 | }
|
192 |
|
193 | function Dictionary() {
|
194 | this._values = Object.create(null);
|
195 | this._size = 0;
|
196 | }
|
197 | Dictionary.prototype = {
|
198 | set: function(key, val) {
|
199 | if (!this.has(key)) ++this._size;
|
200 | this._values["$" + key] = val;
|
201 | return this;
|
202 | },
|
203 | add: function(key, val) {
|
204 | if (this.has(key)) {
|
205 | this.get(key).push(val);
|
206 | } else {
|
207 | this.set(key, [ val ]);
|
208 | }
|
209 | return this;
|
210 | },
|
211 | get: function(key) { return this._values["$" + key] },
|
212 | del: function(key) {
|
213 | if (this.has(key)) {
|
214 | --this._size;
|
215 | delete this._values["$" + key];
|
216 | }
|
217 | return this;
|
218 | },
|
219 | has: function(key) { return ("$" + key) in this._values },
|
220 | each: function(f) {
|
221 | for (var i in this._values)
|
222 | f(this._values[i], i.substr(1));
|
223 | },
|
224 | size: function() {
|
225 | return this._size;
|
226 | },
|
227 | map: function(f) {
|
228 | var ret = [];
|
229 | for (var i in this._values)
|
230 | ret.push(f(this._values[i], i.substr(1)));
|
231 | return ret;
|
232 | },
|
233 | clone: function() {
|
234 | var ret = new Dictionary();
|
235 | for (var i in this._values)
|
236 | ret._values[i] = this._values[i];
|
237 | ret._size = this._size;
|
238 | return ret;
|
239 | },
|
240 | toObject: function() { return this._values }
|
241 | };
|
242 | Dictionary.fromObject = function(obj) {
|
243 | var dict = new Dictionary();
|
244 | dict._size = merge(dict._values, obj);
|
245 | return dict;
|
246 | };
|
247 |
|
248 | function HOP(obj, prop) {
|
249 | return Object.prototype.hasOwnProperty.call(obj, prop);
|
250 | }
|
251 |
|
252 |
|
253 |
|
254 |
|
255 | function first_in_statement(stack) {
|
256 | var node = stack.parent(-1);
|
257 | for (var i = 0, p; p = stack.parent(i++); node = p) {
|
258 | if (p.TYPE == "Call") {
|
259 | if (p.expression === node) continue;
|
260 | } else if (p instanceof AST_Binary) {
|
261 | if (p.left === node) continue;
|
262 | } else if (p instanceof AST_Conditional) {
|
263 | if (p.condition === node) continue;
|
264 | } else if (p instanceof AST_PropAccess) {
|
265 | if (p.expression === node) continue;
|
266 | } else if (p instanceof AST_Sequence) {
|
267 | if (p.expressions[0] === node) continue;
|
268 | } else if (p instanceof AST_Statement) {
|
269 | return p.body === node;
|
270 | } else if (p instanceof AST_UnaryPostfix) {
|
271 | if (p.expression === node) continue;
|
272 | }
|
273 | return false;
|
274 | }
|
275 | }
|