uglifier 0.1.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of uglifier might be problematic. Click here for more details.

@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
@@ -0,0 +1,3 @@
1
+ [submodule "vendor/uglifyjs"]
2
+ path = vendor/uglifyjs
3
+ url = http://github.com/mishoo/UglifyJS.git
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ gem "therubyracer", ">= 0.7.5"
7
+
8
+ # Add dependencies to develop your gem here.
9
+ # Include everything needed to run rake, tests, features, etc.
10
+ group :development do
11
+ gem "rspec", "~> 2.0.0"
12
+ gem "bundler", "~> 1.0.0"
13
+ gem "jeweler", "~> 1.5.0.pre5"
14
+ gem "rcov", ">= 0"
15
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Ville Lautanala
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,46 @@
1
+ = Uglifier
2
+
3
+ Ruby wrapper for UglifyJS[http://github.com/mishoo/UglifyJS] JavaScript compressor.
4
+
5
+ == Usage
6
+
7
+ require 'uglifier'
8
+
9
+ Uglifier.new.compile(File.read("source.js"))
10
+ # => js file minified
11
+
12
+ When initializing UglifyJS, you can tune the behavior of UglifyJS by passing options. For example, if you want top-level variable names to be mangled:
13
+
14
+ Uglifier.new(:toplevel => true).compile(source)
15
+
16
+ Defaults are
17
+
18
+ {
19
+ :mangle => true, # Mangle variables names
20
+ :toplevel => false, # Mangle top-level variable names
21
+ :squeeze => true, # Squeeze code resulting in smaller, but less-readable code
22
+ :seqs => true, # Reduce consecutive statements in blocks into single statement
23
+ :dead_code => true, # Remove dead code (e.g. after return)
24
+ :beautify => false, # Ouput indented code
25
+ :beautify_options => {
26
+ :indent_level => 4,
27
+ :indent_start => 0,
28
+ :quote_keys => false,
29
+ :space_colon => 0
30
+ }
31
+ }
32
+
33
+ == Contributing to uglifier
34
+
35
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
36
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
37
+ * Fork the project
38
+ * Start a feature/bugfix branch
39
+ * Commit and push until you are happy with your contribution
40
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
41
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
42
+
43
+ == Copyright
44
+
45
+ Copyright (c) 2010 Ville Lautanala. See LICENSE.txt for
46
+ further details.
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "uglifier"
16
+ gem.summary = %Q{Ruby wrapper for UglifyJS JavaScript compressor}
17
+ gem.email = "lautis@gmail.com"
18
+ gem.homepage = "http://github.com/lautis/uglifier"
19
+ gem.authors = ["Ville Lautanala"]
20
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
21
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
22
+ # spec.add_runtime_dependency 'jabber4r', '> 0.1'
23
+ # spec.add_development_dependency 'rspec', '> 1.2.3'
24
+ gem.add_development_dependency "therubyracer", ">=0.7.5"
25
+ gem.add_development_dependency "rspec", "~> 2.0.0"
26
+ gem.add_development_dependency "bundler", "~> 1.0.0"
27
+ gem.add_development_dependency "jeweler", "~> 1.5.0.pre5"
28
+ gem.add_development_dependency "rcov", ">= 0"
29
+ gem.files.include 'vendor/uglifyjs/lib/*'
30
+ end
31
+ Jeweler::RubygemsDotOrgTasks.new
32
+
33
+ require 'rspec/core'
34
+ require 'rspec/core/rake_task'
35
+ RSpec::Core::RakeTask.new(:spec) do |spec|
36
+ spec.pattern = FileList['spec/**/*_spec.rb']
37
+ end
38
+
39
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
40
+ spec.pattern = 'spec/**/*_spec.rb'
41
+ spec.rcov = true
42
+ end
43
+
44
+ task :default => :spec
45
+
46
+ require 'rake/rdoctask'
47
+ Rake::RDocTask.new do |rdoc|
48
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "uglifier #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,79 @@
1
+ require "v8"
2
+
3
+ class Uglifier
4
+ # Raised when compilation fails
5
+ class Error < StandardError; end
6
+
7
+ DEFAULTS = {
8
+ :mangle => true, # Mangle variables names
9
+ :toplevel => false, # Mangle top-level variable names
10
+ :squeeze => true, # Squeeze code resulting in smaller, but less-readable code
11
+ :seqs => true, # Reduce consecutive statements in blocks into single statement
12
+ :dead_code => true, # Remove dead code (e.g. after return)
13
+ :beautify => false, # Ouput indented code
14
+ :beautify_options => {
15
+ :indent_level => 4,
16
+ :indent_start => 0,
17
+ :quote_keys => false,
18
+ :space_colon => 0
19
+ }
20
+ }
21
+
22
+ def initialize(options = {})
23
+ @options = DEFAULTS.merge options
24
+ end
25
+
26
+ def compile(source)
27
+ V8::Context.new do |cxt|
28
+ initialize_v8(cxt)
29
+ begin
30
+ return generate_code(cxt, ast(cxt, source))
31
+ rescue Exception => e
32
+ raise Error.new(e.message)
33
+ end
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def generate_code(cxt, ast)
40
+ cxt["gen_code"].call(ast, @options[:beautify] && @options[:beautify_options])
41
+ end
42
+
43
+ def ast(cxt, source)
44
+ squeeze(cxt, mangle(cxt, cxt["parse"].call(source)))
45
+ end
46
+
47
+ def mangle(cxt, ast)
48
+ cxt["ast_mangle"].call(ast, @options[:toplevel])
49
+ end
50
+
51
+ def squeeze(cxt, ast)
52
+ cxt["ast_squeeze"].call(ast, {
53
+ "make_seqs" => @options[:seqs],
54
+ "dead_code" => @options[:dead_code]
55
+ })
56
+ end
57
+
58
+ def initialize_v8(cxt)
59
+ cxt["process"] = { :version => "v1" }
60
+ exports = {
61
+ "sys" => {
62
+ :debug => lambda { |m| puts m }
63
+ },
64
+ "./parse-js" => load_file(cxt, "vendor/uglifyjs/lib/parse-js.js")
65
+ }
66
+
67
+ cxt["require"] = lambda do |file|
68
+ exports[file]
69
+ end
70
+
71
+ load_file(cxt, "vendor/uglifyjs/lib/process.js")
72
+ end
73
+
74
+ def load_file(cxt, file)
75
+ cxt["exports"] = {}
76
+ cxt.load(File.join(File.dirname(__FILE__), "..", file))
77
+ cxt["exports"]
78
+ end
79
+ end
@@ -0,0 +1,10 @@
1
+ require 'uglifier'
2
+ require 'rspec'
3
+
4
+ # Requires supporting files with custom matchers and macros, etc,
5
+ # in ./support/ and its subdirectories.
6
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
7
+
8
+ RSpec.configure do |config|
9
+
10
+ end
@@ -0,0 +1,18 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Uglifier" do
4
+ it "minifies JS" do
5
+ source = File.read("vendor/uglifyjs/lib/parse-js.js")
6
+ minified = Uglifier.new.compile(source)
7
+ minified.length.should < source.length
8
+ lambda {
9
+ Uglifier.new.compile(minified)
10
+ }.should_not raise_error
11
+ end
12
+
13
+ it "throws an exception when compilation fails" do
14
+ lambda {
15
+ Uglifier.new.compile(")(")
16
+ }.should raise_error(Uglifier::Error)
17
+ end
18
+ end
@@ -0,0 +1,1220 @@
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
+ <mihai.bazon@gmail.com>
25
+ http://mihai.bazon.net/blog
26
+
27
+ Distributed under the same terms as the original code (ZLIB license):
28
+
29
+ Copyright 2010 (c) Mihai Bazon <mihai.bazon@gmail.com>
30
+ Based on parse-js (http://marijn.haverbeke.nl/parse-js/).
31
+
32
+ This software is provided 'as-is', without any express or implied
33
+ warranty. In no event will the authors be held liable for any
34
+ damages arising from the use of this software.
35
+
36
+ Permission is granted to anyone to use this software for any
37
+ purpose, including commercial applications, and to alter it and
38
+ redistribute it freely, subject to the following restrictions:
39
+
40
+ 1. The origin of this software must not be misrepresented; you must
41
+ not claim that you wrote the original software. If you use this
42
+ software in a product, an acknowledgment in the product
43
+ documentation would be appreciated but is not required.
44
+
45
+ 2. Altered source versions must be plainly marked as such, and must
46
+ not be misrepresented as being the original software.
47
+
48
+ 3. This notice may not be removed or altered from any source
49
+ distribution.
50
+
51
+ ***********************************************************************/
52
+
53
+ /* -----[ Tokenizer (constants) ]----- */
54
+
55
+ var KEYWORDS = array_to_hash([
56
+ "break",
57
+ "case",
58
+ "catch",
59
+ "const",
60
+ "continue",
61
+ "default",
62
+ "delete",
63
+ "do",
64
+ "else",
65
+ "finally",
66
+ "for",
67
+ "function",
68
+ "if",
69
+ "in",
70
+ "instanceof",
71
+ "new",
72
+ "return",
73
+ "switch",
74
+ "throw",
75
+ "try",
76
+ "typeof",
77
+ "var",
78
+ "void",
79
+ "while",
80
+ "with"
81
+ ]);
82
+
83
+ var RESERVED_WORDS = array_to_hash([
84
+ "abstract",
85
+ "boolean",
86
+ "byte",
87
+ "char",
88
+ "class",
89
+ "debugger",
90
+ "double",
91
+ "enum",
92
+ "export",
93
+ "extends",
94
+ "final",
95
+ "float",
96
+ "goto",
97
+ "implements",
98
+ "import",
99
+ "int",
100
+ "interface",
101
+ "long",
102
+ "native",
103
+ "package",
104
+ "private",
105
+ "protected",
106
+ "public",
107
+ "short",
108
+ "static",
109
+ "super",
110
+ "synchronized",
111
+ "throws",
112
+ "transient",
113
+ "volatile"
114
+ ]);
115
+
116
+ var KEYWORDS_BEFORE_EXPRESSION = array_to_hash([
117
+ "return",
118
+ "new",
119
+ "delete",
120
+ "throw",
121
+ "else"
122
+ ]);
123
+
124
+ var KEYWORDS_ATOM = array_to_hash([
125
+ "false",
126
+ "null",
127
+ "true",
128
+ "undefined"
129
+ ]);
130
+
131
+ var OPERATOR_CHARS = array_to_hash(characters("+-*&%=<>!?|~^"));
132
+
133
+ var RE_HEX_NUMBER = /^0x[0-9a-f]+$/i;
134
+ var RE_OCT_NUMBER = /^0[0-7]+$/;
135
+ var RE_DEC_NUMBER = /^\d*\.?\d*(?:e[+-]?\d*(?:\d\.?|\.?\d)\d*)?$/i;
136
+
137
+ var OPERATORS = array_to_hash([
138
+ "in",
139
+ "instanceof",
140
+ "typeof",
141
+ "new",
142
+ "void",
143
+ "delete",
144
+ "++",
145
+ "--",
146
+ "+",
147
+ "-",
148
+ "!",
149
+ "~",
150
+ "&",
151
+ "|",
152
+ "^",
153
+ "*",
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
+ var WHITESPACE_CHARS = array_to_hash(characters(" \n\r\t"));
187
+
188
+ var PUNC_BEFORE_EXPRESSION = array_to_hash(characters("[{}(,.;:"));
189
+
190
+ var PUNC_CHARS = array_to_hash(characters("[]{}(),;:"));
191
+
192
+ var REGEXP_MODIFIERS = array_to_hash(characters("gmsiy"));
193
+
194
+ /* -----[ Tokenizer ]----- */
195
+
196
+ function is_alphanumeric_char(ch) {
197
+ ch = ch.charCodeAt(0);
198
+ return (ch >= 48 && ch <= 57) ||
199
+ (ch >= 65 && ch <= 90) ||
200
+ (ch >= 97 && ch <= 122);
201
+ };
202
+
203
+ function is_identifier_char(ch) {
204
+ return is_alphanumeric_char(ch) || ch == "$" || ch == "_";
205
+ };
206
+
207
+ function is_digit(ch) {
208
+ ch = ch.charCodeAt(0);
209
+ return ch >= 48 && ch <= 57;
210
+ };
211
+
212
+ function parse_js_number(num) {
213
+ if (RE_HEX_NUMBER.test(num)) {
214
+ return parseInt(num.substr(2), 16);
215
+ } else if (RE_OCT_NUMBER.test(num)) {
216
+ return parseInt(num.substr(1), 8);
217
+ } else if (RE_DEC_NUMBER.test(num)) {
218
+ return parseFloat(num);
219
+ }
220
+ };
221
+
222
+ function JS_Parse_Error(message, line, col, pos) {
223
+ this.message = message;
224
+ this.line = line;
225
+ this.col = col;
226
+ this.pos = pos;
227
+ try {
228
+ ({})();
229
+ } catch(ex) {
230
+ this.stack = ex.stack;
231
+ };
232
+ };
233
+
234
+ JS_Parse_Error.prototype.toString = function() {
235
+ return this.message + " (line: " + this.line + ", col: " + this.col + ", pos: " + this.pos + ")" + "\n\n" + this.stack;
236
+ };
237
+
238
+ function js_error(message, line, col, pos) {
239
+ throw new JS_Parse_Error(message, line, col, pos);
240
+ };
241
+
242
+ function is_token(token, type, val) {
243
+ return token.type == type && (val == null || token.value == val);
244
+ };
245
+
246
+ var EX_EOF = {};
247
+
248
+ function tokenizer($TEXT, skip_comments) {
249
+
250
+ var S = {
251
+ text : $TEXT.replace(/\r\n?|[\n\u2028\u2029]/g, "\n").replace(/^\uFEFF/, ''),
252
+ pos : 0,
253
+ tokpos : 0,
254
+ line : 0,
255
+ tokline : 0,
256
+ col : 0,
257
+ tokcol : 0,
258
+ newline_before : false,
259
+ regex_allowed : false
260
+ };
261
+
262
+ function peek() { return S.text.charAt(S.pos); };
263
+
264
+ function next(signal_eof) {
265
+ var ch = S.text.charAt(S.pos++);
266
+ if (signal_eof && !ch)
267
+ throw EX_EOF;
268
+ if (ch == "\n") {
269
+ S.newline_before = true;
270
+ ++S.line;
271
+ S.col = 0;
272
+ } else {
273
+ ++S.col;
274
+ }
275
+ return ch;
276
+ };
277
+
278
+ function eof() {
279
+ return !S.peek();
280
+ };
281
+
282
+ function find(what, signal_eof) {
283
+ var pos = S.text.indexOf(what, S.pos);
284
+ if (signal_eof && pos == -1) throw EX_EOF;
285
+ return pos;
286
+ };
287
+
288
+ function start_token() {
289
+ S.tokline = S.line;
290
+ S.tokcol = S.col;
291
+ S.tokpos = S.pos;
292
+ };
293
+
294
+ function token(type, value) {
295
+ S.regex_allowed = ((type == "operator" && !HOP(UNARY_POSTFIX, value)) ||
296
+ (type == "keyword" && HOP(KEYWORDS_BEFORE_EXPRESSION, value)) ||
297
+ (type == "punc" && HOP(PUNC_BEFORE_EXPRESSION, value)));
298
+ var ret = {
299
+ type : type,
300
+ value : value,
301
+ line : S.tokline,
302
+ col : S.tokcol,
303
+ pos : S.tokpos,
304
+ nlb : S.newline_before
305
+ };
306
+ S.newline_before = false;
307
+ return ret;
308
+ };
309
+
310
+ function skip_whitespace() {
311
+ while (HOP(WHITESPACE_CHARS, peek()))
312
+ next();
313
+ };
314
+
315
+ function read_while(pred) {
316
+ var ret = "", ch = peek(), i = 0;
317
+ while (ch && pred(ch, i++)) {
318
+ ret += next();
319
+ ch = peek();
320
+ }
321
+ return ret;
322
+ };
323
+
324
+ function parse_error(err) {
325
+ js_error(err, S.tokline, S.tokcol, S.tokpos);
326
+ };
327
+
328
+ function read_num(prefix) {
329
+ var has_e = false, after_e = false, has_x = false;
330
+ var num = read_while(function(ch, i){
331
+ if (ch == "x" || ch == "X") {
332
+ if (has_x) return false;
333
+ return has_x = true;
334
+ }
335
+ if (!has_x && (ch == "E" || ch == "e")) {
336
+ if (has_e) return false;
337
+ return has_e = after_e = true;
338
+ }
339
+ if (ch == "-") {
340
+ if (after_e || (i == 0 && !prefix)) return true;
341
+ return false;
342
+ }
343
+ if (ch == "+") return after_e;
344
+ after_e = false;
345
+ return is_alphanumeric_char(ch) || ch == ".";
346
+ });
347
+ if (prefix)
348
+ num = prefix + num;
349
+ var valid = parse_js_number(num);
350
+ if (!isNaN(valid)) {
351
+ return token("num", valid);
352
+ } else {
353
+ parse_error("Invalid syntax: " + num);
354
+ }
355
+ };
356
+
357
+ function read_escaped_char() {
358
+ var ch = next(true);
359
+ switch (ch) {
360
+ case "n" : return "\n";
361
+ case "r" : return "\r";
362
+ case "t" : return "\t";
363
+ case "b" : return "\b";
364
+ case "v" : return "\v";
365
+ case "f" : return "\f";
366
+ case "0" : return "\0";
367
+ case "x" : return String.fromCharCode(hex_bytes(2));
368
+ case "u" : return String.fromCharCode(hex_bytes(4));
369
+ default : return ch;
370
+ }
371
+ };
372
+
373
+ function hex_bytes(n) {
374
+ var num = 0;
375
+ for (; n > 0; --n) {
376
+ var digit = parseInt(next(true), 16);
377
+ if (isNaN(digit))
378
+ parse_error("Invalid hex-character pattern in string");
379
+ num = (num << 4) | digit;
380
+ }
381
+ return num;
382
+ };
383
+
384
+ function read_string() {
385
+ return with_eof_error("Unterminated string constant", function(){
386
+ var quote = next(), ret = "";
387
+ for (;;) {
388
+ var ch = next(true);
389
+ if (ch == "\\") ch = read_escaped_char();
390
+ else if (ch == quote) break;
391
+ ret += ch;
392
+ }
393
+ return token("string", ret);
394
+ });
395
+ };
396
+
397
+ function read_line_comment() {
398
+ next();
399
+ var i = find("\n"), ret;
400
+ if (i == -1) {
401
+ ret = S.text.substr(S.pos);
402
+ S.pos = S.text.length;
403
+ } else {
404
+ ret = S.text.substring(S.pos, i);
405
+ S.pos = i;
406
+ }
407
+ return token("comment1", ret);
408
+ };
409
+
410
+ function read_multiline_comment() {
411
+ next();
412
+ return with_eof_error("Unterminated multiline comment", function(){
413
+ var i = find("*/", true),
414
+ text = S.text.substring(S.pos, i),
415
+ tok = token("comment2", text);
416
+ S.pos = i + 2;
417
+ S.newline_before = text.indexOf("\n") >= 0;
418
+ return tok;
419
+ });
420
+ };
421
+
422
+ function read_regexp() {
423
+ return with_eof_error("Unterminated regular expression", function(){
424
+ var prev_backslash = false, regexp = "", ch, in_class = false;
425
+ while ((ch = next(true))) if (prev_backslash) {
426
+ regexp += "\\" + ch;
427
+ prev_backslash = false;
428
+ } else if (ch == "[") {
429
+ in_class = true;
430
+ regexp += ch;
431
+ } else if (ch == "]" && in_class) {
432
+ in_class = false;
433
+ regexp += ch;
434
+ } else if (ch == "/" && !in_class) {
435
+ break;
436
+ } else if (ch == "\\") {
437
+ prev_backslash = true;
438
+ } else {
439
+ regexp += ch;
440
+ }
441
+ var mods = read_while(function(ch){
442
+ return HOP(REGEXP_MODIFIERS, ch);
443
+ });
444
+ return token("regexp", [ regexp, mods ]);
445
+ });
446
+ };
447
+
448
+ function read_operator(prefix) {
449
+ function grow(op) {
450
+ var bigger = op + peek();
451
+ if (HOP(OPERATORS, bigger)) {
452
+ next();
453
+ return grow(bigger);
454
+ } else {
455
+ return op;
456
+ }
457
+ };
458
+ return token("operator", grow(prefix || next()));
459
+ };
460
+
461
+ var handle_slash = skip_comments ? function() {
462
+ next();
463
+ switch (peek()) {
464
+ case "/": read_line_comment(); return next_token();
465
+ case "*": read_multiline_comment(); return next_token();
466
+ }
467
+ return S.regex_allowed ? read_regexp() : read_operator("/");
468
+ } : function() {
469
+ next();
470
+ switch (peek()) {
471
+ case "/": return read_line_comment();
472
+ case "*": return read_multiline_comment();
473
+ }
474
+ return S.regex_allowed ? read_regexp() : read_operator("/");
475
+ };
476
+
477
+ function handle_dot() {
478
+ next();
479
+ return is_digit(peek())
480
+ ? read_num(".")
481
+ : token("punc", ".");
482
+ };
483
+
484
+ function read_word() {
485
+ var word = read_while(is_identifier_char);
486
+ return !HOP(KEYWORDS, word)
487
+ ? token("name", word)
488
+ : HOP(OPERATORS, word)
489
+ ? token("operator", word)
490
+ : HOP(KEYWORDS_ATOM, word)
491
+ ? token("atom", word)
492
+ : token("keyword", word);
493
+ };
494
+
495
+ function with_eof_error(eof_error, cont) {
496
+ try {
497
+ return cont();
498
+ } catch(ex) {
499
+ if (ex === EX_EOF) parse_error(eof_error);
500
+ else throw ex;
501
+ }
502
+ };
503
+
504
+ function next_token(force_regexp) {
505
+ if (force_regexp)
506
+ return read_regexp();
507
+ skip_whitespace();
508
+ start_token();
509
+ var ch = peek();
510
+ if (!ch) return token("eof");
511
+ if (is_digit(ch)) return read_num();
512
+ if (ch == '"' || ch == "'") return read_string();
513
+ if (HOP(PUNC_CHARS, ch)) return token("punc", next());
514
+ if (ch == ".") return handle_dot();
515
+ if (ch == "/") return handle_slash();
516
+ if (HOP(OPERATOR_CHARS, ch)) return read_operator();
517
+ if (is_identifier_char(ch)) return read_word();
518
+ parse_error("Unexpected character '" + ch + "'");
519
+ };
520
+
521
+ next_token.context = function(nc) {
522
+ if (nc) S = nc;
523
+ return S;
524
+ };
525
+
526
+ return next_token;
527
+
528
+ };
529
+
530
+ /* -----[ Parser (constants) ]----- */
531
+
532
+ var UNARY_PREFIX = array_to_hash([
533
+ "typeof",
534
+ "void",
535
+ "delete",
536
+ "--",
537
+ "++",
538
+ "!",
539
+ "~",
540
+ "-",
541
+ "+"
542
+ ]);
543
+
544
+ var UNARY_POSTFIX = array_to_hash([ "--", "++" ]);
545
+
546
+ var ASSIGNMENT = (function(a, ret, i){
547
+ while (i < a.length) {
548
+ ret[a[i]] = a[i].substr(0, a[i].length - 1);
549
+ i++;
550
+ }
551
+ return ret;
552
+ })(
553
+ ["+=", "-=", "/=", "*=", "%=", ">>=", "<<=", ">>>=", "~=", "%=", "|=", "^=", "&="],
554
+ { "=": true },
555
+ 0
556
+ );
557
+
558
+ var PRECEDENCE = (function(a, ret){
559
+ for (var i = 0, n = 1; i < a.length; ++i, ++n) {
560
+ var b = a[i];
561
+ for (var j = 0; j < b.length; ++j) {
562
+ ret[b[j]] = n;
563
+ }
564
+ }
565
+ return ret;
566
+ })(
567
+ [
568
+ ["||"],
569
+ ["&&"],
570
+ ["|"],
571
+ ["^"],
572
+ ["&"],
573
+ ["==", "===", "!=", "!=="],
574
+ ["<", ">", "<=", ">=", "in", "instanceof"],
575
+ [">>", "<<", ">>>"],
576
+ ["+", "-"],
577
+ ["*", "/", "%"]
578
+ ],
579
+ {}
580
+ );
581
+
582
+ var STATEMENTS_WITH_LABELS = array_to_hash([ "for", "do", "while", "switch" ]);
583
+
584
+ var ATOMIC_START_TOKEN = array_to_hash([ "atom", "num", "string", "regexp", "name" ]);
585
+
586
+ /* -----[ Parser ]----- */
587
+
588
+ function NodeWithToken(str, start, end) {
589
+ this.name = str;
590
+ this.start = start;
591
+ this.end = end;
592
+ };
593
+
594
+ NodeWithToken.prototype.toString = function() { return this.name; };
595
+
596
+ function parse($TEXT, strict_mode, embed_tokens) {
597
+
598
+ var S = {
599
+ input: tokenizer($TEXT, true),
600
+ token: null,
601
+ prev: null,
602
+ peeked: null,
603
+ in_function: 0,
604
+ in_loop: 0,
605
+ labels: []
606
+ };
607
+
608
+ S.token = next();
609
+
610
+ function is(type, value) {
611
+ return is_token(S.token, type, value);
612
+ };
613
+
614
+ function peek() { return S.peeked || (S.peeked = S.input()); };
615
+
616
+ function next() {
617
+ S.prev = S.token;
618
+ if (S.peeked) {
619
+ S.token = S.peeked;
620
+ S.peeked = null;
621
+ } else {
622
+ S.token = S.input();
623
+ }
624
+ return S.token;
625
+ };
626
+
627
+ function prev() {
628
+ return S.prev;
629
+ };
630
+
631
+ function croak(msg, line, col, pos) {
632
+ var ctx = S.input.context();
633
+ js_error(msg,
634
+ line != null ? line : ctx.tokline,
635
+ col != null ? col : ctx.tokcol,
636
+ pos != null ? pos : ctx.tokpos);
637
+ };
638
+
639
+ function token_error(token, msg) {
640
+ croak(msg, token.line, token.col);
641
+ };
642
+
643
+ function unexpected(token) {
644
+ if (token == null)
645
+ token = S.token;
646
+ token_error(token, "Unexpected token: " + token.type + " (" + token.value + ")");
647
+ };
648
+
649
+ function expect_token(type, val) {
650
+ if (is(type, val)) {
651
+ return next();
652
+ }
653
+ token_error(S.token, "Unexpected token " + S.token.type + ", expected " + type);
654
+ };
655
+
656
+ function expect(punc) { return expect_token("punc", punc); };
657
+
658
+ function can_insert_semicolon() {
659
+ return !strict_mode && (
660
+ S.token.nlb || is("eof") || is("punc", "}")
661
+ );
662
+ };
663
+
664
+ function semicolon() {
665
+ if (is("punc", ";")) next();
666
+ else if (!can_insert_semicolon()) unexpected();
667
+ };
668
+
669
+ function as() {
670
+ return slice(arguments);
671
+ };
672
+
673
+ function parenthesised() {
674
+ expect("(");
675
+ var ex = expression();
676
+ expect(")");
677
+ return ex;
678
+ };
679
+
680
+ function add_tokens(str, start, end) {
681
+ return new NodeWithToken(str, start, end);
682
+ };
683
+
684
+ var statement = embed_tokens ? function() {
685
+ var start = S.token;
686
+ var stmt = $statement();
687
+ stmt[0] = add_tokens(stmt[0], start, prev());
688
+ return stmt;
689
+ } : $statement;
690
+
691
+ function $statement() {
692
+ if (is("operator", "/")) {
693
+ S.peeked = null;
694
+ S.token = S.input(true); // force regexp
695
+ }
696
+ switch (S.token.type) {
697
+ case "num":
698
+ case "string":
699
+ case "regexp":
700
+ case "operator":
701
+ case "atom":
702
+ return simple_statement();
703
+
704
+ case "name":
705
+ return is_token(peek(), "punc", ":")
706
+ ? labeled_statement(prog1(S.token.value, next, next))
707
+ : simple_statement();
708
+
709
+ case "punc":
710
+ switch (S.token.value) {
711
+ case "{":
712
+ return as("block", block_());
713
+ case "[":
714
+ case "(":
715
+ return simple_statement();
716
+ case ";":
717
+ next();
718
+ return as("block");
719
+ default:
720
+ unexpected();
721
+ }
722
+
723
+ case "keyword":
724
+ switch (prog1(S.token.value, next)) {
725
+ case "break":
726
+ return break_cont("break");
727
+
728
+ case "continue":
729
+ return break_cont("continue");
730
+
731
+ case "debugger":
732
+ semicolon();
733
+ return as("debugger");
734
+
735
+ case "do":
736
+ return (function(body){
737
+ expect_token("keyword", "while");
738
+ return as("do", prog1(parenthesised, semicolon), body);
739
+ })(in_loop(statement));
740
+
741
+ case "for":
742
+ return for_();
743
+
744
+ case "function":
745
+ return function_(true);
746
+
747
+ case "if":
748
+ return if_();
749
+
750
+ case "return":
751
+ if (S.in_function == 0)
752
+ croak("'return' outside of function");
753
+ return as("return",
754
+ is("punc", ";")
755
+ ? (next(), null)
756
+ : can_insert_semicolon()
757
+ ? null
758
+ : prog1(expression, semicolon));
759
+
760
+ case "switch":
761
+ return as("switch", parenthesised(), switch_block_());
762
+
763
+ case "throw":
764
+ return as("throw", prog1(expression, semicolon));
765
+
766
+ case "try":
767
+ return try_();
768
+
769
+ case "var":
770
+ return prog1(var_, semicolon);
771
+
772
+ case "const":
773
+ return prog1(const_, semicolon);
774
+
775
+ case "while":
776
+ return as("while", parenthesised(), in_loop(statement));
777
+
778
+ case "with":
779
+ return as("with", parenthesised(), statement());
780
+
781
+ default:
782
+ unexpected();
783
+ }
784
+ }
785
+ };
786
+
787
+ function labeled_statement(label) {
788
+ S.labels.push(label);
789
+ var start = S.token, stat = statement();
790
+ if (strict_mode && !HOP(STATEMENTS_WITH_LABELS, stat[0]))
791
+ unexpected(start);
792
+ S.labels.pop();
793
+ return as("label", label, stat);
794
+ };
795
+
796
+ function simple_statement() {
797
+ return as("stat", prog1(expression, semicolon));
798
+ };
799
+
800
+ function break_cont(type) {
801
+ var name = is("name") ? S.token.value : null;
802
+ if (name != null) {
803
+ next();
804
+ if (!member(name, S.labels))
805
+ croak("Label " + name + " without matching loop or statement");
806
+ }
807
+ else if (S.in_loop == 0)
808
+ croak(type + " not inside a loop or switch");
809
+ semicolon();
810
+ return as(type, name);
811
+ };
812
+
813
+ function for_() {
814
+ expect("(");
815
+ var has_var = is("keyword", "var");
816
+ if (has_var)
817
+ next();
818
+ if (is("name") && is_token(peek(), "operator", "in")) {
819
+ // for (i in foo)
820
+ var name = S.token.value;
821
+ next(); next();
822
+ var obj = expression();
823
+ expect(")");
824
+ return as("for-in", has_var, name, obj, in_loop(statement));
825
+ } else {
826
+ // classic for
827
+ var init = is("punc", ";") ? null : has_var ? var_() : expression();
828
+ expect(";");
829
+ var test = is("punc", ";") ? null : expression();
830
+ expect(";");
831
+ var step = is("punc", ")") ? null : expression();
832
+ expect(")");
833
+ return as("for", init, test, step, in_loop(statement));
834
+ }
835
+ };
836
+
837
+ function function_(in_statement) {
838
+ var name = is("name") ? prog1(S.token.value, next) : null;
839
+ if (in_statement && !name)
840
+ unexpected();
841
+ expect("(");
842
+ return as(in_statement ? "defun" : "function",
843
+ name,
844
+ // arguments
845
+ (function(first, a){
846
+ while (!is("punc", ")")) {
847
+ if (first) first = false; else expect(",");
848
+ if (!is("name")) unexpected();
849
+ a.push(S.token.value);
850
+ next();
851
+ }
852
+ next();
853
+ return a;
854
+ })(true, []),
855
+ // body
856
+ (function(){
857
+ ++S.in_function;
858
+ var loop = S.in_loop;
859
+ S.in_loop = 0;
860
+ var a = block_();
861
+ --S.in_function;
862
+ S.in_loop = loop;
863
+ return a;
864
+ })());
865
+ };
866
+
867
+ function if_() {
868
+ var cond = parenthesised(), body = statement(), belse;
869
+ if (is("keyword", "else")) {
870
+ next();
871
+ belse = statement();
872
+ }
873
+ return as("if", cond, body, belse);
874
+ };
875
+
876
+ function block_() {
877
+ expect("{");
878
+ var a = [];
879
+ while (!is("punc", "}")) {
880
+ if (is("eof")) unexpected();
881
+ a.push(statement());
882
+ }
883
+ next();
884
+ return a;
885
+ };
886
+
887
+ var switch_block_ = curry(in_loop, function(){
888
+ expect("{");
889
+ var a = [], cur = null;
890
+ while (!is("punc", "}")) {
891
+ if (is("eof")) unexpected();
892
+ if (is("keyword", "case")) {
893
+ next();
894
+ cur = [];
895
+ a.push([ expression(), cur ]);
896
+ expect(":");
897
+ }
898
+ else if (is("keyword", "default")) {
899
+ next();
900
+ expect(":");
901
+ cur = [];
902
+ a.push([ null, cur ]);
903
+ }
904
+ else {
905
+ if (!cur) unexpected();
906
+ cur.push(statement());
907
+ }
908
+ }
909
+ next();
910
+ return a;
911
+ });
912
+
913
+ function try_() {
914
+ var body = block_(), bcatch, bfinally;
915
+ if (is("keyword", "catch")) {
916
+ next();
917
+ expect("(");
918
+ if (!is("name"))
919
+ croak("Name expected");
920
+ var name = S.token.value;
921
+ next();
922
+ expect(")");
923
+ bcatch = [ name, block_() ];
924
+ }
925
+ if (is("keyword", "finally")) {
926
+ next();
927
+ bfinally = block_();
928
+ }
929
+ if (!bcatch && !bfinally)
930
+ croak("Missing catch/finally blocks");
931
+ return as("try", body, bcatch, bfinally);
932
+ };
933
+
934
+ function vardefs() {
935
+ var a = [];
936
+ for (;;) {
937
+ if (!is("name"))
938
+ unexpected();
939
+ var name = S.token.value;
940
+ next();
941
+ if (is("operator", "=")) {
942
+ next();
943
+ a.push([ name, expression(false) ]);
944
+ } else {
945
+ a.push([ name ]);
946
+ }
947
+ if (!is("punc", ","))
948
+ break;
949
+ next();
950
+ }
951
+ return a;
952
+ };
953
+
954
+ function var_() {
955
+ return as("var", vardefs());
956
+ };
957
+
958
+ function const_() {
959
+ return as("const", vardefs());
960
+ };
961
+
962
+ function new_() {
963
+ var newexp = expr_atom(false), args;
964
+ if (is("punc", "(")) {
965
+ next();
966
+ args = expr_list(")");
967
+ } else {
968
+ args = [];
969
+ }
970
+ return subscripts(as("new", newexp, args), true);
971
+ };
972
+
973
+ function expr_atom(allow_calls) {
974
+ if (is("operator", "new")) {
975
+ next();
976
+ return new_();
977
+ }
978
+ if (is("operator") && HOP(UNARY_PREFIX, S.token.value)) {
979
+ return make_unary("unary-prefix",
980
+ prog1(S.token.value, next),
981
+ expr_atom(allow_calls));
982
+ }
983
+ if (is("punc")) {
984
+ switch (S.token.value) {
985
+ case "(":
986
+ next();
987
+ return subscripts(prog1(expression, curry(expect, ")")), allow_calls);
988
+ case "[":
989
+ next();
990
+ return subscripts(array_(), allow_calls);
991
+ case "{":
992
+ next();
993
+ return subscripts(object_(), allow_calls);
994
+ }
995
+ unexpected();
996
+ }
997
+ if (is("keyword", "function")) {
998
+ next();
999
+ return subscripts(function_(false), allow_calls);
1000
+ }
1001
+ if (HOP(ATOMIC_START_TOKEN, S.token.type)) {
1002
+ var atom = S.token.type == "regexp"
1003
+ ? as("regexp", S.token.value[0], S.token.value[1])
1004
+ : as(S.token.type, S.token.value);
1005
+ return subscripts(prog1(atom, next), allow_calls);
1006
+ }
1007
+ unexpected();
1008
+ };
1009
+
1010
+ function expr_list(closing, allow_trailing_comma) {
1011
+ var first = true, a = [];
1012
+ while (!is("punc", closing)) {
1013
+ if (first) first = false; else expect(",");
1014
+ if (allow_trailing_comma && is("punc", closing))
1015
+ break;
1016
+ a.push(expression(false));
1017
+ }
1018
+ next();
1019
+ return a;
1020
+ };
1021
+
1022
+ function array_() {
1023
+ return as("array", expr_list("]", !strict_mode));
1024
+ };
1025
+
1026
+ function object_() {
1027
+ var first = true, a = [];
1028
+ while (!is("punc", "}")) {
1029
+ if (first) first = false; else expect(",");
1030
+ if (!strict_mode && is("punc", "}"))
1031
+ // allow trailing comma
1032
+ break;
1033
+ var name = as_property_name();
1034
+ expect(":");
1035
+ var value = expression(false);
1036
+ a.push([ name, value ]);
1037
+ }
1038
+ next();
1039
+ return as("object", a);
1040
+ };
1041
+
1042
+ function as_property_name() {
1043
+ switch (S.token.type) {
1044
+ case "num":
1045
+ case "string":
1046
+ return prog1(S.token.value, next);
1047
+ }
1048
+ return as_name();
1049
+ };
1050
+
1051
+ function as_name() {
1052
+ switch (S.token.type) {
1053
+ case "name":
1054
+ case "operator":
1055
+ case "keyword":
1056
+ case "atom":
1057
+ return prog1(S.token.value, next);
1058
+ default:
1059
+ unexpected();
1060
+ }
1061
+ };
1062
+
1063
+ function subscripts(expr, allow_calls) {
1064
+ if (is("punc", ".")) {
1065
+ next();
1066
+ return subscripts(as("dot", expr, as_name()), allow_calls);
1067
+ }
1068
+ if (is("punc", "[")) {
1069
+ next();
1070
+ return subscripts(as("sub", expr, prog1(expression, curry(expect, "]"))), allow_calls);
1071
+ }
1072
+ if (allow_calls && is("punc", "(")) {
1073
+ next();
1074
+ return subscripts(as("call", expr, expr_list(")")), true);
1075
+ }
1076
+ if (allow_calls && is("operator") && HOP(UNARY_POSTFIX, S.token.value)) {
1077
+ return prog1(curry(make_unary, "unary-postfix", S.token.value, expr),
1078
+ next);
1079
+ }
1080
+ return expr;
1081
+ };
1082
+
1083
+ function make_unary(tag, op, expr) {
1084
+ if ((op == "++" || op == "--") && !is_assignable(expr))
1085
+ croak("Invalid use of " + op + " operator");
1086
+ return as(tag, op, expr);
1087
+ };
1088
+
1089
+ function expr_op(left, min_prec) {
1090
+ var op = is("operator") ? S.token.value : null;
1091
+ var prec = op != null ? PRECEDENCE[op] : null;
1092
+ if (prec != null && prec > min_prec) {
1093
+ next();
1094
+ var right = expr_op(expr_atom(true), prec);
1095
+ return expr_op(as("binary", op, left, right), min_prec);
1096
+ }
1097
+ return left;
1098
+ };
1099
+
1100
+ function expr_ops() {
1101
+ return expr_op(expr_atom(true), 0);
1102
+ };
1103
+
1104
+ function maybe_conditional() {
1105
+ var expr = expr_ops();
1106
+ if (is("operator", "?")) {
1107
+ next();
1108
+ var yes = expression(false);
1109
+ expect(":");
1110
+ return as("conditional", expr, yes, expression(false));
1111
+ }
1112
+ return expr;
1113
+ };
1114
+
1115
+ function is_assignable(expr) {
1116
+ switch (expr[0]) {
1117
+ case "dot":
1118
+ case "sub":
1119
+ return true;
1120
+ case "name":
1121
+ return expr[1] != "this";
1122
+ }
1123
+ };
1124
+
1125
+ function maybe_assign() {
1126
+ var left = maybe_conditional(), val = S.token.value;
1127
+ if (is("operator") && HOP(ASSIGNMENT, val)) {
1128
+ if (is_assignable(left)) {
1129
+ next();
1130
+ return as("assign", ASSIGNMENT[val], left, maybe_assign());
1131
+ }
1132
+ croak("Invalid assignment");
1133
+ }
1134
+ return left;
1135
+ };
1136
+
1137
+ function expression(commas) {
1138
+ if (arguments.length == 0)
1139
+ commas = true;
1140
+ var expr = maybe_assign();
1141
+ if (commas && is("punc", ",")) {
1142
+ next();
1143
+ return as("seq", expr, expression());
1144
+ }
1145
+ return expr;
1146
+ };
1147
+
1148
+ function in_loop(cont) {
1149
+ try {
1150
+ ++S.in_loop;
1151
+ return cont();
1152
+ } finally {
1153
+ --S.in_loop;
1154
+ }
1155
+ };
1156
+
1157
+ return as("toplevel", (function(a){
1158
+ while (!is("eof"))
1159
+ a.push(statement());
1160
+ return a;
1161
+ })([]));
1162
+
1163
+ };
1164
+
1165
+ /* -----[ Utilities ]----- */
1166
+
1167
+ function curry(f) {
1168
+ var args = slice(arguments, 1);
1169
+ return function() { return f.apply(this, args.concat(slice(arguments))); };
1170
+ };
1171
+
1172
+ function prog1(ret) {
1173
+ if (ret instanceof Function)
1174
+ ret = ret();
1175
+ for (var i = 1, n = arguments.length; --n > 0; ++i)
1176
+ arguments[i]();
1177
+ return ret;
1178
+ };
1179
+
1180
+ function array_to_hash(a) {
1181
+ var ret = {};
1182
+ for (var i = 0; i < a.length; ++i)
1183
+ ret[a[i]] = true;
1184
+ return ret;
1185
+ };
1186
+
1187
+ function slice(a, start) {
1188
+ return Array.prototype.slice.call(a, start == null ? 0 : start);
1189
+ };
1190
+
1191
+ function characters(str) {
1192
+ return str.split("");
1193
+ };
1194
+
1195
+ function member(name, array) {
1196
+ for (var i = array.length; --i >= 0;)
1197
+ if (array[i] === name)
1198
+ return true;
1199
+ return false;
1200
+ };
1201
+
1202
+ function HOP(obj, prop) {
1203
+ return Object.prototype.hasOwnProperty.call(obj, prop);
1204
+ };
1205
+
1206
+ /* -----[ Exports ]----- */
1207
+
1208
+ exports.tokenizer = tokenizer;
1209
+ exports.parse = parse;
1210
+ exports.slice = slice;
1211
+ exports.curry = curry;
1212
+ exports.member = member;
1213
+ exports.array_to_hash = array_to_hash;
1214
+ exports.PRECEDENCE = PRECEDENCE;
1215
+ exports.KEYWORDS_ATOM = KEYWORDS_ATOM;
1216
+ exports.RESERVED_WORDS = RESERVED_WORDS;
1217
+ exports.KEYWORDS = KEYWORDS;
1218
+ exports.ATOMIC_START_TOKEN = ATOMIC_START_TOKEN;
1219
+ exports.OPERATORS = OPERATORS;
1220
+ exports.is_alphanumeric_char = is_alphanumeric_char;