jcov 1.0.1 → 1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/bin/jcov +1 -0
- data/examples/jasmine/javascripts/mean.js +15 -0
- data/features/coverage.feature +8 -13
- data/features/html_report.feature +3 -3
- data/features/instrumentation.feature +310 -0
- data/features/javascript_interface.feature +36 -0
- data/features/run.feature +16 -4
- data/jcov.gemspec +1 -2
- data/lib/jcov.rb +5 -1
- data/lib/jcov/commands.rb +7 -7
- data/lib/jcov/configuration.rb +9 -3
- data/lib/jcov/context.rb +17 -0
- data/lib/jcov/context/instrumentation_context.rb +31 -0
- data/lib/jcov/context/run_context.rb +44 -0
- data/lib/jcov/coverage.rb +55 -130
- data/lib/jcov/errors.rb +6 -0
- data/lib/jcov/js/parser.js +44 -0
- data/lib/jcov/loader.rb +92 -0
- data/lib/jcov/reporter/console_reporter.rb +3 -7
- data/lib/jcov/reporter/html_reporter.rb +1 -1
- data/lib/jcov/runner.rb +22 -62
- data/lib/jcov/version.rb +1 -1
- data/vendor/acorn.js +1711 -0
- data/vendor/walk.js +305 -0
- metadata +13 -19
data/lib/jcov/loader.rb
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
module JCov
|
2
|
+
|
3
|
+
class Loader
|
4
|
+
|
5
|
+
attr_accessor :coverable_files
|
6
|
+
|
7
|
+
def initialize(coverable_files, options={})
|
8
|
+
@options = options
|
9
|
+
@coverable_files = coverable_files
|
10
|
+
@file_cache = {}
|
11
|
+
|
12
|
+
create_parser
|
13
|
+
end
|
14
|
+
|
15
|
+
def load(file)
|
16
|
+
if @file_cache[file]
|
17
|
+
# reuse previously loaded file
|
18
|
+
content = @file_cache[file]
|
19
|
+
else
|
20
|
+
content = File.read(file)
|
21
|
+
|
22
|
+
# is this a file we need to instrument?
|
23
|
+
if coverable_files.include? file
|
24
|
+
|
25
|
+
# run it through the js parser to get coverage data
|
26
|
+
calculate_coverage_data file, content
|
27
|
+
|
28
|
+
# update the content with the coverage instrumentations
|
29
|
+
content = instrument_script(content, file)
|
30
|
+
|
31
|
+
# cache the file if it's reloaded
|
32
|
+
@file_cache[file] = content
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
content
|
37
|
+
end
|
38
|
+
|
39
|
+
def coverage_data
|
40
|
+
if @coverage_data.nil?
|
41
|
+
# set up coverage data structure
|
42
|
+
@coverage_data = {}
|
43
|
+
coverable_files.each {|file| @coverage_data[file] = {} }
|
44
|
+
end
|
45
|
+
@coverage_data
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def create_parser
|
51
|
+
context = JCov::Context::InstrumentationContext.new(coverage_data)
|
52
|
+
@parser = context.create
|
53
|
+
|
54
|
+
@parser.load(File.expand_path('../js/parser.js', __FILE__))
|
55
|
+
@parser.load(File.expand_path('../../../vendor/acorn.js', __FILE__))
|
56
|
+
@parser.load(File.expand_path('../../../vendor/walk.js', __FILE__))
|
57
|
+
end
|
58
|
+
|
59
|
+
def instrument_script(content, filename)
|
60
|
+
lines = coverage_data[filename] || {}
|
61
|
+
line_number = 0
|
62
|
+
output = ""
|
63
|
+
|
64
|
+
StringIO.new(content).each_line do |line|
|
65
|
+
line_number += 1
|
66
|
+
if !lines[line_number].nil? # nil values are ones set to be ignored by ignoreLine
|
67
|
+
column = lines[line_number]
|
68
|
+
output << line[0...column]
|
69
|
+
output << "_coverage_tick('#{filename}', #{line_number});"
|
70
|
+
output << line[column..-1]
|
71
|
+
lines[line_number] = 0 # reset to zero for counting
|
72
|
+
else
|
73
|
+
output << line
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
puts output if @options[:dump]
|
78
|
+
|
79
|
+
output
|
80
|
+
end
|
81
|
+
|
82
|
+
def calculate_coverage_data(filename, content)
|
83
|
+
@parser['filename'] = filename
|
84
|
+
@parser['code'] = content
|
85
|
+
@parser.eval("JCov.calculateCoverageData(code, filename);")
|
86
|
+
rescue V8::Error => e
|
87
|
+
message = e.message.split(' at').first
|
88
|
+
raise JCov::ParseError.new("#{message} in #{filename}")
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
@@ -15,7 +15,7 @@ module JCov::Reporter
|
|
15
15
|
# report a warning message if we're not checking any files for coverage
|
16
16
|
puts "No files were checked for coverage. Maybe your ignore list in #{config.filename} is too inclusive?"
|
17
17
|
elsif report_coverage?
|
18
|
-
report_file_coverage if
|
18
|
+
report_file_coverage if config.report
|
19
19
|
report_total_coverage
|
20
20
|
report_threshold_errors if config.threshold
|
21
21
|
passed?
|
@@ -30,13 +30,9 @@ module JCov::Reporter
|
|
30
30
|
@coverage_runner.config
|
31
31
|
end
|
32
32
|
|
33
|
-
def options
|
34
|
-
@coverage_runner.options
|
35
|
-
end
|
36
|
-
|
37
33
|
# no_coverage.nil? because no_coverage gets set to false when set because it's prefixed as 'no'
|
38
34
|
def report_coverage?
|
39
|
-
|
35
|
+
config.report || config.no_coverage.nil? && config.coverage && config.test.nil? && config.args.empty?
|
40
36
|
end
|
41
37
|
|
42
38
|
# passes if any are true:
|
@@ -80,7 +76,7 @@ module JCov::Reporter
|
|
80
76
|
percent = 100
|
81
77
|
coverage_string = "(EMPTY)"
|
82
78
|
end
|
83
|
-
if
|
79
|
+
if config.test.nil? || percent > 0 # only show ran files if we're doing a focused test
|
84
80
|
printf "%-#{filename_length}s %-10s %3s%\n", file, coverage_string, percent
|
85
81
|
end
|
86
82
|
end
|
data/lib/jcov/runner.rb
CHANGED
@@ -1,27 +1,23 @@
|
|
1
1
|
module JCov
|
2
2
|
|
3
3
|
class Runner
|
4
|
-
attr_reader :context
|
5
4
|
attr_reader :config
|
6
|
-
attr_reader :
|
5
|
+
attr_reader :coverage
|
7
6
|
|
8
|
-
def initialize(config
|
7
|
+
def initialize(config)
|
9
8
|
@config = config
|
10
|
-
@options = options
|
11
|
-
|
12
|
-
setup_context
|
13
9
|
end
|
14
10
|
|
11
|
+
# which tests shall we run?
|
15
12
|
def tests
|
16
|
-
# which tests shall we run?
|
17
13
|
if @tests.nil?
|
18
|
-
if
|
19
|
-
@tests =
|
14
|
+
if config.args.size > 0
|
15
|
+
@tests = config.args
|
20
16
|
else
|
21
17
|
@tests = Dir.glob(File.join(config.test_directory, "**", "*.js"))
|
22
18
|
end
|
23
|
-
if
|
24
|
-
@tests = @tests.grep(/#{
|
19
|
+
if config.test # limit to a single or group of tests
|
20
|
+
@tests = @tests.grep(/#{config.test}/)
|
25
21
|
end
|
26
22
|
# remove the runner if it's in there
|
27
23
|
@tests.delete(config.test_runner)
|
@@ -30,69 +26,33 @@ module JCov
|
|
30
26
|
@tests
|
31
27
|
end
|
32
28
|
|
33
|
-
def print s
|
34
|
-
Kernel.print s
|
35
|
-
end
|
36
|
-
|
37
|
-
def println *s
|
38
|
-
Kernel.puts s
|
39
|
-
end
|
40
|
-
|
41
|
-
def load file
|
42
|
-
content = File.read(file)
|
43
|
-
|
44
|
-
context.eval(content, file)
|
45
|
-
end
|
46
|
-
|
47
|
-
# for JSpec
|
48
|
-
def readFile file
|
49
|
-
File.read(file)
|
50
|
-
end
|
51
|
-
|
52
|
-
# so we can do our dotted line output
|
53
|
-
def putc char
|
54
|
-
$stdout.putc(char);
|
55
|
-
$stdout.flush();
|
56
|
-
end
|
57
|
-
|
58
29
|
def failure_count
|
59
|
-
context.eval(config.error_field)
|
30
|
+
@context.eval(config.error_field)
|
60
31
|
end
|
61
32
|
|
62
33
|
def run
|
63
|
-
|
34
|
+
setup
|
35
|
+
|
36
|
+
@context.load(config.test_runner)
|
64
37
|
|
65
38
|
failure_count
|
66
39
|
end
|
67
40
|
|
68
41
|
private
|
69
42
|
|
70
|
-
def
|
71
|
-
#
|
72
|
-
@
|
73
|
-
|
74
|
-
# create the jcov context object
|
75
|
-
@context["JCov"] = {}
|
76
|
-
|
77
|
-
setup_object_proxies
|
78
|
-
setup_method_proxies
|
79
|
-
end
|
43
|
+
def setup
|
44
|
+
# for coverage reporting
|
45
|
+
@coverage = JCov::Coverage.new(config)
|
80
46
|
|
81
|
-
|
82
|
-
|
83
|
-
context
|
84
|
-
context['JCov']['options'] = options.__hash__
|
85
|
-
end
|
47
|
+
# v8 context for running
|
48
|
+
run_context = JCov::Context::RunContext.new(@coverage.loader)
|
49
|
+
@context = run_context.create
|
86
50
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
readFile
|
93
|
-
putc}.each do |method|
|
94
|
-
context[method] = self.method(method)
|
95
|
-
end
|
51
|
+
# create the jcov context object
|
52
|
+
@context['JCov'] = {}
|
53
|
+
@context['JCov']['tests'] = tests
|
54
|
+
@context['JCov']['config'] = config
|
55
|
+
@context['JCov']['options'] = config
|
96
56
|
end
|
97
57
|
|
98
58
|
end
|
data/lib/jcov/version.rb
CHANGED
data/vendor/acorn.js
ADDED
@@ -0,0 +1,1711 @@
|
|
1
|
+
// Acorn is a tiny, fast JavaScript parser written in JavaScript.
|
2
|
+
//
|
3
|
+
// Acorn was written by Marijn Haverbeke and released under an MIT
|
4
|
+
// license. The Unicode regexps (for identifiers and whitespace) were
|
5
|
+
// taken from [Esprima](http://esprima.org) by Ariya Hidayat.
|
6
|
+
//
|
7
|
+
// Git repositories for Acorn are available at
|
8
|
+
//
|
9
|
+
// http://marijnhaverbeke.nl/git/acorn
|
10
|
+
// https://github.com/marijnh/acorn.git
|
11
|
+
//
|
12
|
+
// Please use the [github bug tracker][ghbt] to report issues.
|
13
|
+
//
|
14
|
+
// [ghbt]: https://github.com/marijnh/acorn/issues
|
15
|
+
//
|
16
|
+
// This file defines the main parser interface. The library also comes
|
17
|
+
// with a [error-tolerant parser][dammit] and an
|
18
|
+
// [abstract syntax tree walker][walk], defined in other files.
|
19
|
+
//
|
20
|
+
// [dammit]: acorn_loose.js
|
21
|
+
// [walk]: util/walk.js
|
22
|
+
|
23
|
+
(function(exports) {
|
24
|
+
"use strict";
|
25
|
+
|
26
|
+
exports.version = "0.1.01";
|
27
|
+
|
28
|
+
// The main exported interface (under `self.acorn` when in the
|
29
|
+
// browser) is a `parse` function that takes a code string and
|
30
|
+
// returns an abstract syntax tree as specified by [Mozilla parser
|
31
|
+
// API][api], with the caveat that the SpiderMonkey-specific syntax
|
32
|
+
// (`let`, `yield`, inline XML, etc) is not recognized.
|
33
|
+
//
|
34
|
+
// [api]: https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API
|
35
|
+
|
36
|
+
var options, input, inputLen, sourceFile;
|
37
|
+
|
38
|
+
exports.parse = function(inpt, opts) {
|
39
|
+
input = String(inpt); inputLen = input.length;
|
40
|
+
setOptions(opts);
|
41
|
+
initTokenState();
|
42
|
+
return parseTopLevel(options.program);
|
43
|
+
};
|
44
|
+
|
45
|
+
// A second optional argument can be given to further configure
|
46
|
+
// the parser process. These options are recognized:
|
47
|
+
|
48
|
+
var defaultOptions = exports.defaultOptions = {
|
49
|
+
// `ecmaVersion` indicates the ECMAScript version to parse. Must
|
50
|
+
// be either 3 or 5. This
|
51
|
+
// influences support for strict mode, the set of reserved words, and
|
52
|
+
// support for getters and setter.
|
53
|
+
ecmaVersion: 5,
|
54
|
+
// Turn on `strictSemicolons` to prevent the parser from doing
|
55
|
+
// automatic semicolon insertion.
|
56
|
+
strictSemicolons: false,
|
57
|
+
// When `allowTrailingCommas` is false, the parser will not allow
|
58
|
+
// trailing commas in array and object literals.
|
59
|
+
allowTrailingCommas: true,
|
60
|
+
// By default, reserved words are not enforced. Enable
|
61
|
+
// `forbidReserved` to enforce them.
|
62
|
+
forbidReserved: false,
|
63
|
+
// When `locations` is on, `loc` properties holding objects with
|
64
|
+
// `start` and `end` properties in `{line, column}` form (with
|
65
|
+
// line being 1-based and column 0-based) will be attached to the
|
66
|
+
// nodes.
|
67
|
+
locations: false,
|
68
|
+
// A function can be passed as `onComment` option, which will
|
69
|
+
// cause Acorn to call that function with `(block, text, start,
|
70
|
+
// end)` parameters whenever a comment is skipped. `block` is a
|
71
|
+
// boolean indicating whether this is a block (`/* */`) comment,
|
72
|
+
// `text` is the content of the comment, and `start` and `end` are
|
73
|
+
// character offsets that denote the start and end of the comment.
|
74
|
+
// When the `locations` option is on, two more parameters are
|
75
|
+
// passed, the full `{line, column}` locations of the start and
|
76
|
+
// end of the comments.
|
77
|
+
onComment: null,
|
78
|
+
// Nodes have their start and end characters offsets recorded in
|
79
|
+
// `start` and `end` properties (directly on the node, rather than
|
80
|
+
// the `loc` object, which holds line/column data. To also add a
|
81
|
+
// [semi-standardized][range] `range` property holding a `[start,
|
82
|
+
// end]` array with the same numbers, set the `ranges` option to
|
83
|
+
// `true`.
|
84
|
+
//
|
85
|
+
// [range]: https://bugzilla.mozilla.org/show_bug.cgi?id=745678
|
86
|
+
ranges: false,
|
87
|
+
// It is possible to parse multiple files into a single AST by
|
88
|
+
// passing the tree produced by parsing the first file as
|
89
|
+
// `program` option in subsequent parses. This will add the
|
90
|
+
// toplevel forms of the parsed file to the `Program` (top) node
|
91
|
+
// of an existing parse tree.
|
92
|
+
program: null,
|
93
|
+
// When `location` is on, you can pass this to record the source
|
94
|
+
// file in every node's `loc` object.
|
95
|
+
sourceFile: null
|
96
|
+
};
|
97
|
+
|
98
|
+
function setOptions(opts) {
|
99
|
+
options = opts || {};
|
100
|
+
for (var opt in defaultOptions) if (!options.hasOwnProperty(opt))
|
101
|
+
options[opt] = defaultOptions[opt];
|
102
|
+
sourceFile = options.sourceFile || null;
|
103
|
+
}
|
104
|
+
|
105
|
+
// The `getLineInfo` function is mostly useful when the
|
106
|
+
// `locations` option is off (for performance reasons) and you
|
107
|
+
// want to find the line/column position for a given character
|
108
|
+
// offset. `input` should be the code string that the offset refers
|
109
|
+
// into.
|
110
|
+
|
111
|
+
var getLineInfo = exports.getLineInfo = function(input, offset) {
|
112
|
+
for (var line = 1, cur = 0;;) {
|
113
|
+
lineBreak.lastIndex = cur;
|
114
|
+
var match = lineBreak.exec(input);
|
115
|
+
if (match && match.index < offset) {
|
116
|
+
++line;
|
117
|
+
cur = match.index + match[0].length;
|
118
|
+
} else break;
|
119
|
+
}
|
120
|
+
return {line: line, column: offset - cur};
|
121
|
+
};
|
122
|
+
|
123
|
+
// Acorn is organized as a tokenizer and a recursive-descent parser.
|
124
|
+
// The `tokenize` export provides an interface to the tokenizer.
|
125
|
+
// Because the tokenizer is optimized for being efficiently used by
|
126
|
+
// the Acorn parser itself, this interface is somewhat crude and not
|
127
|
+
// very modular. Performing another parse or call to `tokenize` will
|
128
|
+
// reset the internal state, and invalidate existing tokenizers.
|
129
|
+
|
130
|
+
exports.tokenize = function(inpt, opts) {
|
131
|
+
input = String(inpt); inputLen = input.length;
|
132
|
+
setOptions(opts);
|
133
|
+
initTokenState();
|
134
|
+
|
135
|
+
var t = {};
|
136
|
+
function getToken(forceRegexp) {
|
137
|
+
readToken(forceRegexp);
|
138
|
+
t.start = tokStart; t.end = tokEnd;
|
139
|
+
t.startLoc = tokStartLoc; t.endLoc = tokEndLoc;
|
140
|
+
t.type = tokType; t.value = tokVal;
|
141
|
+
return t;
|
142
|
+
}
|
143
|
+
getToken.jumpTo = function(pos, reAllowed) {
|
144
|
+
tokPos = pos;
|
145
|
+
if (options.locations) {
|
146
|
+
tokCurLine = tokLineStart = lineBreak.lastIndex = 0;
|
147
|
+
var match;
|
148
|
+
while ((match = lineBreak.exec(input)) && match.index < pos) {
|
149
|
+
++tokCurLine;
|
150
|
+
tokLineStart = match.index + match[0].length;
|
151
|
+
}
|
152
|
+
}
|
153
|
+
var ch = input.charAt(pos - 1);
|
154
|
+
tokRegexpAllowed = reAllowed;
|
155
|
+
skipSpace();
|
156
|
+
};
|
157
|
+
return getToken;
|
158
|
+
};
|
159
|
+
|
160
|
+
// State is kept in (closure-)global variables. We already saw the
|
161
|
+
// `options`, `input`, and `inputLen` variables above.
|
162
|
+
|
163
|
+
// The current position of the tokenizer in the input.
|
164
|
+
|
165
|
+
var tokPos;
|
166
|
+
|
167
|
+
// The start and end offsets of the current token.
|
168
|
+
|
169
|
+
var tokStart, tokEnd;
|
170
|
+
|
171
|
+
// When `options.locations` is true, these hold objects
|
172
|
+
// containing the tokens start and end line/column pairs.
|
173
|
+
|
174
|
+
var tokStartLoc, tokEndLoc;
|
175
|
+
|
176
|
+
// The type and value of the current token. Token types are objects,
|
177
|
+
// named by variables against which they can be compared, and
|
178
|
+
// holding properties that describe them (indicating, for example,
|
179
|
+
// the precedence of an infix operator, and the original name of a
|
180
|
+
// keyword token). The kind of value that's held in `tokVal` depends
|
181
|
+
// on the type of the token. For literals, it is the literal value,
|
182
|
+
// for operators, the operator name, and so on.
|
183
|
+
|
184
|
+
var tokType, tokVal;
|
185
|
+
|
186
|
+
// Interal state for the tokenizer. To distinguish between division
|
187
|
+
// operators and regular expressions, it remembers whether the last
|
188
|
+
// token was one that is allowed to be followed by an expression.
|
189
|
+
// (If it is, a slash is probably a regexp, if it isn't it's a
|
190
|
+
// division operator. See the `parseStatement` function for a
|
191
|
+
// caveat.)
|
192
|
+
|
193
|
+
var tokRegexpAllowed;
|
194
|
+
|
195
|
+
// When `options.locations` is true, these are used to keep
|
196
|
+
// track of the current line, and know when a new line has been
|
197
|
+
// entered.
|
198
|
+
|
199
|
+
var tokCurLine, tokLineStart;
|
200
|
+
|
201
|
+
// These store the position of the previous token, which is useful
|
202
|
+
// when finishing a node and assigning its `end` position.
|
203
|
+
|
204
|
+
var lastStart, lastEnd, lastEndLoc;
|
205
|
+
|
206
|
+
// This is the parser's state. `inFunction` is used to reject
|
207
|
+
// `return` statements outside of functions, `labels` to verify that
|
208
|
+
// `break` and `continue` have somewhere to jump to, and `strict`
|
209
|
+
// indicates whether strict mode is on.
|
210
|
+
|
211
|
+
var inFunction, labels, strict;
|
212
|
+
|
213
|
+
// This function is used to raise exceptions on parse errors. It
|
214
|
+
// takes an offset integer (into the current `input`) to indicate
|
215
|
+
// the location of the error, attaches the position to the end
|
216
|
+
// of the error message, and then raises a `SyntaxError` with that
|
217
|
+
// message.
|
218
|
+
|
219
|
+
function raise(pos, message) {
|
220
|
+
var loc = getLineInfo(input, pos);
|
221
|
+
message += " (" + loc.line + ":" + loc.column + ")";
|
222
|
+
var err = new SyntaxError(message);
|
223
|
+
err.pos = pos; err.loc = loc; err.raisedAt = tokPos;
|
224
|
+
throw err;
|
225
|
+
}
|
226
|
+
|
227
|
+
// ## Token types
|
228
|
+
|
229
|
+
// The assignment of fine-grained, information-carrying type objects
|
230
|
+
// allows the tokenizer to store the information it has about a
|
231
|
+
// token in a way that is very cheap for the parser to look up.
|
232
|
+
|
233
|
+
// All token type variables start with an underscore, to make them
|
234
|
+
// easy to recognize.
|
235
|
+
|
236
|
+
// These are the general types. The `type` property is only used to
|
237
|
+
// make them recognizeable when debugging.
|
238
|
+
|
239
|
+
var _num = {type: "num"}, _regexp = {type: "regexp"}, _string = {type: "string"};
|
240
|
+
var _name = {type: "name"}, _eof = {type: "eof"};
|
241
|
+
|
242
|
+
// Keyword tokens. The `keyword` property (also used in keyword-like
|
243
|
+
// operators) indicates that the token originated from an
|
244
|
+
// identifier-like word, which is used when parsing property names.
|
245
|
+
//
|
246
|
+
// The `beforeExpr` property is used to disambiguate between regular
|
247
|
+
// expressions and divisions. It is set on all token types that can
|
248
|
+
// be followed by an expression (thus, a slash after them would be a
|
249
|
+
// regular expression).
|
250
|
+
//
|
251
|
+
// `isLoop` marks a keyword as starting a loop, which is important
|
252
|
+
// to know when parsing a label, in order to allow or disallow
|
253
|
+
// continue jumps to that label.
|
254
|
+
|
255
|
+
var _break = {keyword: "break"}, _case = {keyword: "case", beforeExpr: true}, _catch = {keyword: "catch"};
|
256
|
+
var _continue = {keyword: "continue"}, _debugger = {keyword: "debugger"}, _default = {keyword: "default"};
|
257
|
+
var _do = {keyword: "do", isLoop: true}, _else = {keyword: "else", beforeExpr: true};
|
258
|
+
var _finally = {keyword: "finally"}, _for = {keyword: "for", isLoop: true}, _function = {keyword: "function"};
|
259
|
+
var _if = {keyword: "if"}, _return = {keyword: "return", beforeExpr: true}, _switch = {keyword: "switch"};
|
260
|
+
var _throw = {keyword: "throw", beforeExpr: true}, _try = {keyword: "try"}, _var = {keyword: "var"};
|
261
|
+
var _while = {keyword: "while", isLoop: true}, _with = {keyword: "with"}, _new = {keyword: "new", beforeExpr: true};
|
262
|
+
var _this = {keyword: "this"};
|
263
|
+
|
264
|
+
// The keywords that denote values.
|
265
|
+
|
266
|
+
var _null = {keyword: "null", atomValue: null}, _true = {keyword: "true", atomValue: true};
|
267
|
+
var _false = {keyword: "false", atomValue: false};
|
268
|
+
|
269
|
+
// Some keywords are treated as regular operators. `in` sometimes
|
270
|
+
// (when parsing `for`) needs to be tested against specifically, so
|
271
|
+
// we assign a variable name to it for quick comparing.
|
272
|
+
|
273
|
+
var _in = {keyword: "in", binop: 7, beforeExpr: true};
|
274
|
+
|
275
|
+
// Map keyword names to token types.
|
276
|
+
|
277
|
+
var keywordTypes = {"break": _break, "case": _case, "catch": _catch,
|
278
|
+
"continue": _continue, "debugger": _debugger, "default": _default,
|
279
|
+
"do": _do, "else": _else, "finally": _finally, "for": _for,
|
280
|
+
"function": _function, "if": _if, "return": _return, "switch": _switch,
|
281
|
+
"throw": _throw, "try": _try, "var": _var, "while": _while, "with": _with,
|
282
|
+
"null": _null, "true": _true, "false": _false, "new": _new, "in": _in,
|
283
|
+
"instanceof": {keyword: "instanceof", binop: 7, beforeExpr: true}, "this": _this,
|
284
|
+
"typeof": {keyword: "typeof", prefix: true, beforeExpr: true},
|
285
|
+
"void": {keyword: "void", prefix: true, beforeExpr: true},
|
286
|
+
"delete": {keyword: "delete", prefix: true, beforeExpr: true}};
|
287
|
+
|
288
|
+
// Punctuation token types. Again, the `type` property is purely for debugging.
|
289
|
+
|
290
|
+
var _bracketL = {type: "[", beforeExpr: true}, _bracketR = {type: "]"}, _braceL = {type: "{", beforeExpr: true};
|
291
|
+
var _braceR = {type: "}"}, _parenL = {type: "(", beforeExpr: true}, _parenR = {type: ")"};
|
292
|
+
var _comma = {type: ",", beforeExpr: true}, _semi = {type: ";", beforeExpr: true};
|
293
|
+
var _colon = {type: ":", beforeExpr: true}, _dot = {type: "."}, _question = {type: "?", beforeExpr: true};
|
294
|
+
|
295
|
+
// Operators. These carry several kinds of properties to help the
|
296
|
+
// parser use them properly (the presence of these properties is
|
297
|
+
// what categorizes them as operators).
|
298
|
+
//
|
299
|
+
// `binop`, when present, specifies that this operator is a binary
|
300
|
+
// operator, and will refer to its precedence.
|
301
|
+
//
|
302
|
+
// `prefix` and `postfix` mark the operator as a prefix or postfix
|
303
|
+
// unary operator. `isUpdate` specifies that the node produced by
|
304
|
+
// the operator should be of type UpdateExpression rather than
|
305
|
+
// simply UnaryExpression (`++` and `--`).
|
306
|
+
//
|
307
|
+
// `isAssign` marks all of `=`, `+=`, `-=` etcetera, which act as
|
308
|
+
// binary operators with a very low precedence, that should result
|
309
|
+
// in AssignmentExpression nodes.
|
310
|
+
|
311
|
+
var _slash = {binop: 10, beforeExpr: true}, _eq = {isAssign: true, beforeExpr: true};
|
312
|
+
var _assign = {isAssign: true, beforeExpr: true}, _plusmin = {binop: 9, prefix: true, beforeExpr: true};
|
313
|
+
var _incdec = {postfix: true, prefix: true, isUpdate: true}, _prefix = {prefix: true, beforeExpr: true};
|
314
|
+
var _bin1 = {binop: 1, beforeExpr: true}, _bin2 = {binop: 2, beforeExpr: true};
|
315
|
+
var _bin3 = {binop: 3, beforeExpr: true}, _bin4 = {binop: 4, beforeExpr: true};
|
316
|
+
var _bin5 = {binop: 5, beforeExpr: true}, _bin6 = {binop: 6, beforeExpr: true};
|
317
|
+
var _bin7 = {binop: 7, beforeExpr: true}, _bin8 = {binop: 8, beforeExpr: true};
|
318
|
+
var _bin10 = {binop: 10, beforeExpr: true};
|
319
|
+
|
320
|
+
// Provide access to the token types for external users of the
|
321
|
+
// tokenizer.
|
322
|
+
|
323
|
+
exports.tokTypes = {bracketL: _bracketL, bracketR: _bracketR, braceL: _braceL, braceR: _braceR,
|
324
|
+
parenL: _parenL, parenR: _parenR, comma: _comma, semi: _semi, colon: _colon,
|
325
|
+
dot: _dot, question: _question, slash: _slash, eq: _eq, name: _name, eof: _eof,
|
326
|
+
num: _num, regexp: _regexp, string: _string};
|
327
|
+
for (var kw in keywordTypes) exports.tokTypes[kw] = keywordTypes[kw];
|
328
|
+
|
329
|
+
// This is a trick taken from Esprima. It turns out that, on
|
330
|
+
// non-Chrome browsers, to check whether a string is in a set, a
|
331
|
+
// predicate containing a big ugly `switch` statement is faster than
|
332
|
+
// a regular expression, and on Chrome the two are about on par.
|
333
|
+
// This function uses `eval` (non-lexical) to produce such a
|
334
|
+
// predicate from a space-separated string of words.
|
335
|
+
//
|
336
|
+
// It starts by sorting the words by length.
|
337
|
+
|
338
|
+
function makePredicate(words) {
|
339
|
+
words = words.split(" ");
|
340
|
+
var f = "", cats = [];
|
341
|
+
out: for (var i = 0; i < words.length; ++i) {
|
342
|
+
for (var j = 0; j < cats.length; ++j)
|
343
|
+
if (cats[j][0].length == words[i].length) {
|
344
|
+
cats[j].push(words[i]);
|
345
|
+
continue out;
|
346
|
+
}
|
347
|
+
cats.push([words[i]]);
|
348
|
+
}
|
349
|
+
function compareTo(arr) {
|
350
|
+
if (arr.length == 1) return f += "return str === " + JSON.stringify(arr[0]) + ";";
|
351
|
+
f += "switch(str){";
|
352
|
+
for (var i = 0; i < arr.length; ++i) f += "case " + JSON.stringify(arr[i]) + ":";
|
353
|
+
f += "return true}return false;";
|
354
|
+
}
|
355
|
+
|
356
|
+
// When there are more than three length categories, an outer
|
357
|
+
// switch first dispatches on the lengths, to save on comparisons.
|
358
|
+
|
359
|
+
if (cats.length > 3) {
|
360
|
+
cats.sort(function(a, b) {return b.length - a.length;});
|
361
|
+
f += "switch(str.length){";
|
362
|
+
for (var i = 0; i < cats.length; ++i) {
|
363
|
+
var cat = cats[i];
|
364
|
+
f += "case " + cat[0].length + ":";
|
365
|
+
compareTo(cat);
|
366
|
+
}
|
367
|
+
f += "}";
|
368
|
+
|
369
|
+
// Otherwise, simply generate a flat `switch` statement.
|
370
|
+
|
371
|
+
} else {
|
372
|
+
compareTo(words);
|
373
|
+
}
|
374
|
+
return new Function("str", f);
|
375
|
+
}
|
376
|
+
|
377
|
+
// The ECMAScript 3 reserved word list.
|
378
|
+
|
379
|
+
var isReservedWord3 = makePredicate("abstract boolean byte char class double enum export extends final float goto implements import int interface long native package private protected public short static super synchronized throws transient volatile");
|
380
|
+
|
381
|
+
// ECMAScript 5 reserved words.
|
382
|
+
|
383
|
+
var isReservedWord5 = makePredicate("class enum extends super const export import");
|
384
|
+
|
385
|
+
// The additional reserved words in strict mode.
|
386
|
+
|
387
|
+
var isStrictReservedWord = makePredicate("implements interface let package private protected public static yield");
|
388
|
+
|
389
|
+
// The forbidden variable names in strict mode.
|
390
|
+
|
391
|
+
var isStrictBadIdWord = makePredicate("eval arguments");
|
392
|
+
|
393
|
+
// And the keywords.
|
394
|
+
|
395
|
+
var isKeyword = makePredicate("break case catch continue debugger default do else finally for function if return switch throw try var while with null true false instanceof typeof void delete new in this");
|
396
|
+
|
397
|
+
// ## Character categories
|
398
|
+
|
399
|
+
// Big ugly regular expressions that match characters in the
|
400
|
+
// whitespace, identifier, and identifier-start categories. These
|
401
|
+
// are only applied when a character is found to actually have a
|
402
|
+
// code point above 128.
|
403
|
+
|
404
|
+
var nonASCIIwhitespace = /[\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]/;
|
405
|
+
var nonASCIIidentifierStartChars = "\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u0527\u0531-\u0556\u0559\u0561-\u0587\u05d0-\u05ea\u05f0-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u08a0\u08a2-\u08ac\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0977\u0979-\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\u0cf1\u0cf2\u0d05-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\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-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\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-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f0\u1700-\u170c\u170e-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1877\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191c\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19c1-\u19c7\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4b\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1ce9-\u1cec\u1cee-\u1cf1\u1cf5\u1cf6\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-\u209c\u2102\u2107\u210a-\u2113\u2115\u2119-\u211d\u2124\u2126\u2128\u212a-\u212d\u212f-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2c2e\u2c30-\u2c5e\u2c60-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u2e2f\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309d-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312d\u3131-\u318e\u31a0-\u31ba\u31f0-\u31ff\u3400-\u4db5\u4e00-\u9fcc\ua000-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua697\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua78e\ua790-\ua793\ua7a0-\ua7aa\ua7f8-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa80-\uaaaf\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uabc0-\uabe2\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\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";
|
406
|
+
var nonASCIIidentifierChars = "\u0371-\u0374\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u0620-\u0649\u0672-\u06d3\u06e7-\u06e8\u06fb-\u06fc\u0730-\u074a\u0800-\u0814\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0840-\u0857\u08e4-\u08fe\u0900-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962-\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09d7\u09df-\u09e0\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2-\u0ae3\u0ae6-\u0aef\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b56\u0b57\u0b5f-\u0b60\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c01-\u0c03\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62-\u0c63\u0c66-\u0c6f\u0c82\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2-\u0ce3\u0ce6-\u0cef\u0d02\u0d03\u0d46-\u0d48\u0d57\u0d62-\u0d63\u0d66-\u0d6f\u0d82\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0df2\u0df3\u0e34-\u0e3a\u0e40-\u0e45\u0e50-\u0e59\u0eb4-\u0eb9\u0ec8-\u0ecd\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f41-\u0f47\u0f71-\u0f84\u0f86-\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u1000-\u1029\u1040-\u1049\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u170e-\u1710\u1720-\u1730\u1740-\u1750\u1772\u1773\u1780-\u17b2\u17dd\u17e0-\u17e9\u180b-\u180d\u1810-\u1819\u1920-\u192b\u1930-\u193b\u1951-\u196d\u19b0-\u19c0\u19c8-\u19c9\u19d0-\u19d9\u1a00-\u1a15\u1a20-\u1a53\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1b46-\u1b4b\u1b50-\u1b59\u1b6b-\u1b73\u1bb0-\u1bb9\u1be6-\u1bf3\u1c00-\u1c22\u1c40-\u1c49\u1c5b-\u1c7d\u1cd0-\u1cd2\u1d00-\u1dbe\u1e01-\u1f15\u200c\u200d\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2d81-\u2d96\u2de0-\u2dff\u3021-\u3028\u3099\u309a\ua640-\ua66d\ua674-\ua67d\ua69f\ua6f0-\ua6f1\ua7f8-\ua800\ua806\ua80b\ua823-\ua827\ua880-\ua881\ua8b4-\ua8c4\ua8d0-\ua8d9\ua8f3-\ua8f7\ua900-\ua909\ua926-\ua92d\ua930-\ua945\ua980-\ua983\ua9b3-\ua9c0\uaa00-\uaa27\uaa40-\uaa41\uaa4c-\uaa4d\uaa50-\uaa59\uaa7b\uaae0-\uaae9\uaaf2-\uaaf3\uabc0-\uabe1\uabec\uabed\uabf0-\uabf9\ufb20-\ufb28\ufe00-\ufe0f\ufe20-\ufe26\ufe33\ufe34\ufe4d-\ufe4f\uff10-\uff19\uff3f";
|
407
|
+
var nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
|
408
|
+
var nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
|
409
|
+
|
410
|
+
// Whether a single character denotes a newline.
|
411
|
+
|
412
|
+
var newline = /[\n\r\u2028\u2029]/;
|
413
|
+
|
414
|
+
// Matches a whole line break (where CRLF is considered a single
|
415
|
+
// line break). Used to count lines.
|
416
|
+
|
417
|
+
var lineBreak = /\r\n|[\n\r\u2028\u2029]/g;
|
418
|
+
|
419
|
+
// Test whether a given character code starts an identifier.
|
420
|
+
|
421
|
+
function isIdentifierStart(code) {
|
422
|
+
if (code < 65) return code === 36;
|
423
|
+
if (code < 91) return true;
|
424
|
+
if (code < 97) return code === 95;
|
425
|
+
if (code < 123)return true;
|
426
|
+
return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code));
|
427
|
+
}
|
428
|
+
|
429
|
+
// Test whether a given character is part of an identifier.
|
430
|
+
|
431
|
+
function isIdentifierChar(code) {
|
432
|
+
if (code < 48) return code === 36;
|
433
|
+
if (code < 58) return true;
|
434
|
+
if (code < 65) return false;
|
435
|
+
if (code < 91) return true;
|
436
|
+
if (code < 97) return code === 95;
|
437
|
+
if (code < 123)return true;
|
438
|
+
return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code));
|
439
|
+
}
|
440
|
+
|
441
|
+
// ## Tokenizer
|
442
|
+
|
443
|
+
// These are used when `options.locations` is on, for the
|
444
|
+
// `tokStartLoc` and `tokEndLoc` properties.
|
445
|
+
|
446
|
+
function line_loc_t() {
|
447
|
+
this.line = tokCurLine;
|
448
|
+
this.column = tokPos - tokLineStart;
|
449
|
+
}
|
450
|
+
|
451
|
+
// Reset the token state. Used at the start of a parse.
|
452
|
+
|
453
|
+
function initTokenState() {
|
454
|
+
tokCurLine = 1;
|
455
|
+
tokPos = tokLineStart = 0;
|
456
|
+
tokRegexpAllowed = true;
|
457
|
+
skipSpace();
|
458
|
+
}
|
459
|
+
|
460
|
+
// Called at the end of every token. Sets `tokEnd`, `tokVal`, and
|
461
|
+
// `tokRegexpAllowed`, and skips the space after the token, so that
|
462
|
+
// the next one's `tokStart` will point at the right position.
|
463
|
+
|
464
|
+
function finishToken(type, val) {
|
465
|
+
tokEnd = tokPos;
|
466
|
+
if (options.locations) tokEndLoc = new line_loc_t;
|
467
|
+
tokType = type;
|
468
|
+
skipSpace();
|
469
|
+
tokVal = val;
|
470
|
+
tokRegexpAllowed = type.beforeExpr;
|
471
|
+
}
|
472
|
+
|
473
|
+
function skipBlockComment() {
|
474
|
+
var startLoc = options.onComment && options.locations && new line_loc_t;
|
475
|
+
var start = tokPos, end = input.indexOf("*/", tokPos += 2);
|
476
|
+
if (end === -1) raise(tokPos - 2, "Unterminated comment");
|
477
|
+
tokPos = end + 2;
|
478
|
+
if (options.locations) {
|
479
|
+
lineBreak.lastIndex = start;
|
480
|
+
var match;
|
481
|
+
while ((match = lineBreak.exec(input)) && match.index < tokPos) {
|
482
|
+
++tokCurLine;
|
483
|
+
tokLineStart = match.index + match[0].length;
|
484
|
+
}
|
485
|
+
}
|
486
|
+
if (options.onComment)
|
487
|
+
options.onComment(true, input.slice(start + 2, end), start, tokPos,
|
488
|
+
startLoc, options.locations && new line_loc_t);
|
489
|
+
}
|
490
|
+
|
491
|
+
function skipLineComment() {
|
492
|
+
var start = tokPos;
|
493
|
+
var startLoc = options.onComment && options.locations && new line_loc_t;
|
494
|
+
var ch = input.charCodeAt(tokPos+=2);
|
495
|
+
while (tokPos < inputLen && ch !== 10 && ch !== 13 && ch !== 8232 && ch !== 8329) {
|
496
|
+
++tokPos;
|
497
|
+
ch = input.charCodeAt(tokPos);
|
498
|
+
}
|
499
|
+
if (options.onComment)
|
500
|
+
options.onComment(false, input.slice(start + 2, tokPos), start, tokPos,
|
501
|
+
startLoc, options.locations && new line_loc_t);
|
502
|
+
}
|
503
|
+
|
504
|
+
// Called at the start of the parse and after every token. Skips
|
505
|
+
// whitespace and comments, and.
|
506
|
+
|
507
|
+
function skipSpace() {
|
508
|
+
while (tokPos < inputLen) {
|
509
|
+
var ch = input.charCodeAt(tokPos);
|
510
|
+
if (ch === 32) { // ' '
|
511
|
+
++tokPos;
|
512
|
+
} else if(ch === 13) {
|
513
|
+
++tokPos;
|
514
|
+
var next = input.charCodeAt(tokPos);
|
515
|
+
if(next === 10) {
|
516
|
+
++tokPos;
|
517
|
+
}
|
518
|
+
if(options.locations) {
|
519
|
+
++tokCurLine;
|
520
|
+
tokLineStart = tokPos;
|
521
|
+
}
|
522
|
+
} else if (ch === 10) {
|
523
|
+
++tokPos;
|
524
|
+
++tokCurLine;
|
525
|
+
tokLineStart = tokPos;
|
526
|
+
} else if(ch < 14 && ch > 8) {
|
527
|
+
++tokPos;
|
528
|
+
} else if (ch === 47) { // '/'
|
529
|
+
var next = input.charCodeAt(tokPos+1);
|
530
|
+
if (next === 42) { // '*'
|
531
|
+
skipBlockComment();
|
532
|
+
} else if (next === 47) { // '/'
|
533
|
+
skipLineComment();
|
534
|
+
} else break;
|
535
|
+
} else if ((ch < 14 && ch > 8) || ch === 32 || ch === 160) { // ' ', '\xa0'
|
536
|
+
++tokPos;
|
537
|
+
} else if (ch >= 5760 && nonASCIIwhitespace.test(String.fromCharCode(ch))) {
|
538
|
+
++tokPos;
|
539
|
+
} else {
|
540
|
+
break;
|
541
|
+
}
|
542
|
+
}
|
543
|
+
}
|
544
|
+
|
545
|
+
// ### Token reading
|
546
|
+
|
547
|
+
// This is the function that is called to fetch the next token. It
|
548
|
+
// is somewhat obscure, because it works in character codes rather
|
549
|
+
// than characters, and because operator parsing has been inlined
|
550
|
+
// into it.
|
551
|
+
//
|
552
|
+
// All in the name of speed.
|
553
|
+
//
|
554
|
+
// The `forceRegexp` parameter is used in the one case where the
|
555
|
+
// `tokRegexpAllowed` trick does not work. See `parseStatement`.
|
556
|
+
|
557
|
+
function readToken_dot() {
|
558
|
+
var next = input.charCodeAt(tokPos+1);
|
559
|
+
if (next >= 48 && next <= 57) return readNumber(true);
|
560
|
+
++tokPos;
|
561
|
+
return finishToken(_dot);
|
562
|
+
}
|
563
|
+
|
564
|
+
function readToken_slash() { // '/'
|
565
|
+
var next = input.charCodeAt(tokPos+1);
|
566
|
+
if (tokRegexpAllowed) {++tokPos; return readRegexp();}
|
567
|
+
if (next === 61) return finishOp(_assign, 2);
|
568
|
+
return finishOp(_slash, 1);
|
569
|
+
}
|
570
|
+
|
571
|
+
function readToken_mult_modulo() { // '%*'
|
572
|
+
var next = input.charCodeAt(tokPos+1);
|
573
|
+
if (next === 61) return finishOp(_assign, 2);
|
574
|
+
return finishOp(_bin10, 1);
|
575
|
+
}
|
576
|
+
|
577
|
+
function readToken_pipe_amp(code) { // '|&'
|
578
|
+
var next = input.charCodeAt(tokPos+1);
|
579
|
+
if (next === code) return finishOp(code === 124 ? _bin1 : _bin2, 2);
|
580
|
+
if (next === 61) return finishOp(_assign, 2);
|
581
|
+
return finishOp(code === 124 ? _bin3 : _bin5, 1);
|
582
|
+
}
|
583
|
+
|
584
|
+
function readToken_caret() { // '^'
|
585
|
+
var next = input.charCodeAt(tokPos+1);
|
586
|
+
if (next === 61) return finishOp(_assign, 2);
|
587
|
+
return finishOp(_bin4, 1);
|
588
|
+
}
|
589
|
+
|
590
|
+
function readToken_plus_min(code) { // '+-'
|
591
|
+
var next = input.charCodeAt(tokPos+1);
|
592
|
+
if (next === code) return finishOp(_incdec, 2);
|
593
|
+
if (next === 61) return finishOp(_assign, 2);
|
594
|
+
return finishOp(_plusmin, 1);
|
595
|
+
}
|
596
|
+
|
597
|
+
function readToken_lt_gt(code) { // '<>'
|
598
|
+
var next = input.charCodeAt(tokPos+1);
|
599
|
+
var size = 1;
|
600
|
+
if (next === code) {
|
601
|
+
size = code === 62 && input.charCodeAt(tokPos+2) === 62 ? 3 : 2;
|
602
|
+
if (input.charCodeAt(tokPos + size) === 61) return finishOp(_assign, size + 1);
|
603
|
+
return finishOp(_bin8, size);
|
604
|
+
}
|
605
|
+
if (next === 61)
|
606
|
+
size = input.charCodeAt(tokPos+2) === 61 ? 3 : 2;
|
607
|
+
return finishOp(_bin7, size);
|
608
|
+
}
|
609
|
+
|
610
|
+
function readToken_eq_excl(code) { // '=!'
|
611
|
+
var next = input.charCodeAt(tokPos+1);
|
612
|
+
if (next === 61) return finishOp(_bin6, input.charCodeAt(tokPos+2) === 61 ? 3 : 2);
|
613
|
+
return finishOp(code === 61 ? _eq : _prefix, 1);
|
614
|
+
}
|
615
|
+
|
616
|
+
function getTokenFromCode(code) {
|
617
|
+
switch(code) {
|
618
|
+
// The interpretation of a dot depends on whether it is followed
|
619
|
+
// by a digit.
|
620
|
+
case 46: // '.'
|
621
|
+
return readToken_dot();
|
622
|
+
|
623
|
+
// Punctuation tokens.
|
624
|
+
case 40: ++tokPos; return finishToken(_parenL);
|
625
|
+
case 41: ++tokPos; return finishToken(_parenR);
|
626
|
+
case 59: ++tokPos; return finishToken(_semi);
|
627
|
+
case 44: ++tokPos; return finishToken(_comma);
|
628
|
+
case 91: ++tokPos; return finishToken(_bracketL);
|
629
|
+
case 93: ++tokPos; return finishToken(_bracketR);
|
630
|
+
case 123: ++tokPos; return finishToken(_braceL);
|
631
|
+
case 125: ++tokPos; return finishToken(_braceR);
|
632
|
+
case 58: ++tokPos; return finishToken(_colon);
|
633
|
+
case 63: ++tokPos; return finishToken(_question);
|
634
|
+
|
635
|
+
// '0x' is a hexadecimal number.
|
636
|
+
case 48: // '0'
|
637
|
+
var next = input.charCodeAt(tokPos+1);
|
638
|
+
if (next === 120 || next === 88) return readHexNumber();
|
639
|
+
// Anything else beginning with a digit is an integer, octal
|
640
|
+
// number, or float.
|
641
|
+
case 49: case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57: // 1-9
|
642
|
+
return readNumber(false);
|
643
|
+
|
644
|
+
// Quotes produce strings.
|
645
|
+
case 34: case 39: // '"', "'"
|
646
|
+
return readString(code);
|
647
|
+
|
648
|
+
// Operators are parsed inline in tiny state machines. '=' (61) is
|
649
|
+
// often referred to. `finishOp` simply skips the amount of
|
650
|
+
// characters it is given as second argument, and returns a token
|
651
|
+
// of the type given by its first argument.
|
652
|
+
|
653
|
+
case 47: // '/'
|
654
|
+
return readToken_slash(code);
|
655
|
+
|
656
|
+
case 37: case 42: // '%*'
|
657
|
+
return readToken_mult_modulo();
|
658
|
+
|
659
|
+
case 124: case 38: // '|&'
|
660
|
+
return readToken_pipe_amp(code);
|
661
|
+
|
662
|
+
case 94: // '^'
|
663
|
+
return readToken_caret();
|
664
|
+
|
665
|
+
case 43: case 45: // '+-'
|
666
|
+
return readToken_plus_min(code);
|
667
|
+
|
668
|
+
case 60: case 62: // '<>'
|
669
|
+
return readToken_lt_gt(code);
|
670
|
+
|
671
|
+
case 61: case 33: // '=!'
|
672
|
+
return readToken_eq_excl(code);
|
673
|
+
|
674
|
+
case 126: // '~'
|
675
|
+
return finishOp(_prefix, 1);
|
676
|
+
}
|
677
|
+
|
678
|
+
return false;
|
679
|
+
}
|
680
|
+
|
681
|
+
function readToken(forceRegexp) {
|
682
|
+
if (!forceRegexp) tokStart = tokPos;
|
683
|
+
else tokPos = tokStart + 1;
|
684
|
+
if (options.locations) tokStartLoc = new line_loc_t;
|
685
|
+
if (forceRegexp) return readRegexp();
|
686
|
+
if (tokPos >= inputLen) return finishToken(_eof);
|
687
|
+
|
688
|
+
var code = input.charCodeAt(tokPos);
|
689
|
+
// Identifier or keyword. '\uXXXX' sequences are allowed in
|
690
|
+
// identifiers, so '\' also dispatches to that.
|
691
|
+
if (isIdentifierStart(code) || code === 92 /* '\' */) return readWord();
|
692
|
+
|
693
|
+
var tok = getTokenFromCode(code);
|
694
|
+
|
695
|
+
if (tok === false) {
|
696
|
+
// If we are here, we either found a non-ASCII identifier
|
697
|
+
// character, or something that's entirely disallowed.
|
698
|
+
var ch = String.fromCharCode(code);
|
699
|
+
if (ch === "\\" || nonASCIIidentifierStart.test(ch)) return readWord();
|
700
|
+
raise(tokPos, "Unexpected character '" + ch + "'");
|
701
|
+
}
|
702
|
+
return tok;
|
703
|
+
}
|
704
|
+
|
705
|
+
function finishOp(type, size) {
|
706
|
+
var str = input.slice(tokPos, tokPos + size);
|
707
|
+
tokPos += size;
|
708
|
+
finishToken(type, str);
|
709
|
+
}
|
710
|
+
|
711
|
+
// Parse a regular expression. Some context-awareness is necessary,
|
712
|
+
// since a '/' inside a '[]' set does not end the expression.
|
713
|
+
|
714
|
+
function readRegexp() {
|
715
|
+
var content = "", escaped, inClass, start = tokPos;
|
716
|
+
for (;;) {
|
717
|
+
if (tokPos >= inputLen) raise(start, "Unterminated regular expression");
|
718
|
+
var ch = input.charAt(tokPos);
|
719
|
+
if (newline.test(ch)) raise(start, "Unterminated regular expression");
|
720
|
+
if (!escaped) {
|
721
|
+
if (ch === "[") inClass = true;
|
722
|
+
else if (ch === "]" && inClass) inClass = false;
|
723
|
+
else if (ch === "/" && !inClass) break;
|
724
|
+
escaped = ch === "\\";
|
725
|
+
} else escaped = false;
|
726
|
+
++tokPos;
|
727
|
+
}
|
728
|
+
var content = input.slice(start, tokPos);
|
729
|
+
++tokPos;
|
730
|
+
// Need to use `readWord1` because '\uXXXX' sequences are allowed
|
731
|
+
// here (don't ask).
|
732
|
+
var mods = readWord1();
|
733
|
+
if (mods && !/^[gmsiy]*$/.test(mods)) raise(start, "Invalid regexp flag");
|
734
|
+
return finishToken(_regexp, new RegExp(content, mods));
|
735
|
+
}
|
736
|
+
|
737
|
+
// Read an integer in the given radix. Return null if zero digits
|
738
|
+
// were read, the integer value otherwise. When `len` is given, this
|
739
|
+
// will return `null` unless the integer has exactly `len` digits.
|
740
|
+
|
741
|
+
function readInt(radix, len) {
|
742
|
+
var start = tokPos, total = 0;
|
743
|
+
for (var i = 0, e = len == null ? Infinity : len; i < e; ++i) {
|
744
|
+
var code = input.charCodeAt(tokPos), val;
|
745
|
+
if (code >= 97) val = code - 97 + 10; // a
|
746
|
+
else if (code >= 65) val = code - 65 + 10; // A
|
747
|
+
else if (code >= 48 && code <= 57) val = code - 48; // 0-9
|
748
|
+
else val = Infinity;
|
749
|
+
if (val >= radix) break;
|
750
|
+
++tokPos;
|
751
|
+
total = total * radix + val;
|
752
|
+
}
|
753
|
+
if (tokPos === start || len != null && tokPos - start !== len) return null;
|
754
|
+
|
755
|
+
return total;
|
756
|
+
}
|
757
|
+
|
758
|
+
function readHexNumber() {
|
759
|
+
tokPos += 2; // 0x
|
760
|
+
var val = readInt(16);
|
761
|
+
if (val == null) raise(tokStart + 2, "Expected hexadecimal number");
|
762
|
+
if (isIdentifierStart(input.charCodeAt(tokPos))) raise(tokPos, "Identifier directly after number");
|
763
|
+
return finishToken(_num, val);
|
764
|
+
}
|
765
|
+
|
766
|
+
// Read an integer, octal integer, or floating-point number.
|
767
|
+
|
768
|
+
function readNumber(startsWithDot) {
|
769
|
+
var start = tokPos, isFloat = false, octal = input.charCodeAt(tokPos) === 48;
|
770
|
+
if (!startsWithDot && readInt(10) === null) raise(start, "Invalid number");
|
771
|
+
if (input.charCodeAt(tokPos) === 46) {
|
772
|
+
++tokPos;
|
773
|
+
readInt(10);
|
774
|
+
isFloat = true;
|
775
|
+
}
|
776
|
+
var next = input.charCodeAt(tokPos);
|
777
|
+
if (next === 69 || next === 101) { // 'eE'
|
778
|
+
next = input.charCodeAt(++tokPos);
|
779
|
+
if (next === 43 || next === 45) ++tokPos; // '+-'
|
780
|
+
if (readInt(10) === null) raise(start, "Invalid number")
|
781
|
+
isFloat = true;
|
782
|
+
}
|
783
|
+
if (isIdentifierStart(input.charCodeAt(tokPos))) raise(tokPos, "Identifier directly after number");
|
784
|
+
|
785
|
+
var str = input.slice(start, tokPos), val;
|
786
|
+
if (isFloat) val = parseFloat(str);
|
787
|
+
else if (!octal || str.length === 1) val = parseInt(str, 10);
|
788
|
+
else if (/[89]/.test(str) || strict) raise(start, "Invalid number");
|
789
|
+
else val = parseInt(str, 8);
|
790
|
+
return finishToken(_num, val);
|
791
|
+
}
|
792
|
+
|
793
|
+
// Read a string value, interpreting backslash-escapes.
|
794
|
+
|
795
|
+
function readString(quote) {
|
796
|
+
tokPos++;
|
797
|
+
var out = "";
|
798
|
+
for (;;) {
|
799
|
+
if (tokPos >= inputLen) raise(tokStart, "Unterminated string constant");
|
800
|
+
var ch = input.charCodeAt(tokPos);
|
801
|
+
if (ch === quote) {
|
802
|
+
++tokPos;
|
803
|
+
return finishToken(_string, out);
|
804
|
+
}
|
805
|
+
if (ch === 92) { // '\'
|
806
|
+
ch = input.charCodeAt(++tokPos);
|
807
|
+
var octal = /^[0-7]+/.exec(input.slice(tokPos, tokPos + 3));
|
808
|
+
if (octal) octal = octal[0];
|
809
|
+
while (octal && parseInt(octal, 8) > 255) octal = octal.slice(0, octal.length - 1);
|
810
|
+
if (octal === "0") octal = null;
|
811
|
+
++tokPos;
|
812
|
+
if (octal) {
|
813
|
+
if (strict) raise(tokPos - 2, "Octal literal in strict mode");
|
814
|
+
out += String.fromCharCode(parseInt(octal, 8));
|
815
|
+
tokPos += octal.length - 1;
|
816
|
+
} else {
|
817
|
+
switch (ch) {
|
818
|
+
case 110: out += "\n"; break; // 'n' -> '\n'
|
819
|
+
case 114: out += "\r"; break; // 'r' -> '\r'
|
820
|
+
case 120: out += String.fromCharCode(readHexChar(2)); break; // 'x'
|
821
|
+
case 117: out += String.fromCharCode(readHexChar(4)); break; // 'u'
|
822
|
+
case 85: out += String.fromCharCode(readHexChar(8)); break; // 'U'
|
823
|
+
case 116: out += "\t"; break; // 't' -> '\t'
|
824
|
+
case 98: out += "\b"; break; // 'b' -> '\b'
|
825
|
+
case 118: out += "\v"; break; // 'v' -> '\u000b'
|
826
|
+
case 102: out += "\f"; break; // 'f' -> '\f'
|
827
|
+
case 48: out += "\0"; break; // 0 -> '\0'
|
828
|
+
case 13: if (input.charCodeAt(tokPos) === 10) ++tokPos; // '\r\n'
|
829
|
+
case 10: // ' \n'
|
830
|
+
if (options.locations) { tokLineStart = tokPos; ++tokCurLine; }
|
831
|
+
break;
|
832
|
+
default: out += String.fromCharCode(ch); break;
|
833
|
+
}
|
834
|
+
}
|
835
|
+
} else {
|
836
|
+
if (ch === 13 || ch === 10 || ch === 8232 || ch === 8329) raise(tokStart, "Unterminated string constant");
|
837
|
+
out += String.fromCharCode(ch); // '\'
|
838
|
+
++tokPos;
|
839
|
+
}
|
840
|
+
}
|
841
|
+
}
|
842
|
+
|
843
|
+
// Used to read character escape sequences ('\x', '\u', '\U').
|
844
|
+
|
845
|
+
function readHexChar(len) {
|
846
|
+
var n = readInt(16, len);
|
847
|
+
if (n === null) raise(tokStart, "Bad character escape sequence");
|
848
|
+
return n;
|
849
|
+
}
|
850
|
+
|
851
|
+
// Used to signal to callers of `readWord1` whether the word
|
852
|
+
// contained any escape sequences. This is needed because words with
|
853
|
+
// escape sequences must not be interpreted as keywords.
|
854
|
+
|
855
|
+
var containsEsc;
|
856
|
+
|
857
|
+
// Read an identifier, and return it as a string. Sets `containsEsc`
|
858
|
+
// to whether the word contained a '\u' escape.
|
859
|
+
//
|
860
|
+
// Only builds up the word character-by-character when it actually
|
861
|
+
// containeds an escape, as a micro-optimization.
|
862
|
+
|
863
|
+
function readWord1() {
|
864
|
+
containsEsc = false;
|
865
|
+
var word, first = true, start = tokPos;
|
866
|
+
for (;;) {
|
867
|
+
var ch = input.charCodeAt(tokPos);
|
868
|
+
if (isIdentifierChar(ch)) {
|
869
|
+
if (containsEsc) word += input.charAt(tokPos);
|
870
|
+
++tokPos;
|
871
|
+
} else if (ch === 92) { // "\"
|
872
|
+
if (!containsEsc) word = input.slice(start, tokPos);
|
873
|
+
containsEsc = true;
|
874
|
+
if (input.charCodeAt(++tokPos) != 117) // "u"
|
875
|
+
raise(tokPos, "Expecting Unicode escape sequence \\uXXXX");
|
876
|
+
++tokPos;
|
877
|
+
var esc = readHexChar(4);
|
878
|
+
var escStr = String.fromCharCode(esc);
|
879
|
+
if (!escStr) raise(tokPos - 1, "Invalid Unicode escape");
|
880
|
+
if (!(first ? isIdentifierStart(esc) : isIdentifierChar(esc)))
|
881
|
+
raise(tokPos - 4, "Invalid Unicode escape");
|
882
|
+
word += escStr;
|
883
|
+
} else {
|
884
|
+
break;
|
885
|
+
}
|
886
|
+
first = false;
|
887
|
+
}
|
888
|
+
return containsEsc ? word : input.slice(start, tokPos);
|
889
|
+
}
|
890
|
+
|
891
|
+
// Read an identifier or keyword token. Will check for reserved
|
892
|
+
// words when necessary.
|
893
|
+
|
894
|
+
function readWord() {
|
895
|
+
var word = readWord1();
|
896
|
+
var type = _name;
|
897
|
+
if (!containsEsc) {
|
898
|
+
if (isKeyword(word)) type = keywordTypes[word];
|
899
|
+
else if (options.forbidReserved &&
|
900
|
+
(options.ecmaVersion === 3 ? isReservedWord3 : isReservedWord5)(word) ||
|
901
|
+
strict && isStrictReservedWord(word))
|
902
|
+
raise(tokStart, "The keyword '" + word + "' is reserved");
|
903
|
+
}
|
904
|
+
return finishToken(type, word);
|
905
|
+
}
|
906
|
+
|
907
|
+
// ## Parser
|
908
|
+
|
909
|
+
// A recursive descent parser operates by defining functions for all
|
910
|
+
// syntactic elements, and recursively calling those, each function
|
911
|
+
// advancing the input stream and returning an AST node. Precedence
|
912
|
+
// of constructs (for example, the fact that `!x[1]` means `!(x[1])`
|
913
|
+
// instead of `(!x)[1]` is handled by the fact that the parser
|
914
|
+
// function that parses unary prefix operators is called first, and
|
915
|
+
// in turn calls the function that parses `[]` subscripts — that
|
916
|
+
// way, it'll receive the node for `x[1]` already parsed, and wraps
|
917
|
+
// *that* in the unary operator node.
|
918
|
+
//
|
919
|
+
// Acorn uses an [operator precedence parser][opp] to handle binary
|
920
|
+
// operator precedence, because it is much more compact than using
|
921
|
+
// the technique outlined above, which uses different, nesting
|
922
|
+
// functions to specify precedence, for all of the ten binary
|
923
|
+
// precedence levels that JavaScript defines.
|
924
|
+
//
|
925
|
+
// [opp]: http://en.wikipedia.org/wiki/Operator-precedence_parser
|
926
|
+
|
927
|
+
// ### Parser utilities
|
928
|
+
|
929
|
+
// Continue to the next token.
|
930
|
+
|
931
|
+
function next() {
|
932
|
+
lastStart = tokStart;
|
933
|
+
lastEnd = tokEnd;
|
934
|
+
lastEndLoc = tokEndLoc;
|
935
|
+
readToken();
|
936
|
+
}
|
937
|
+
|
938
|
+
// Enter strict mode. Re-reads the next token to please pedantic
|
939
|
+
// tests ("use strict"; 010; -- should fail).
|
940
|
+
|
941
|
+
function setStrict(strct) {
|
942
|
+
strict = strct;
|
943
|
+
tokPos = lastEnd;
|
944
|
+
skipSpace();
|
945
|
+
readToken();
|
946
|
+
}
|
947
|
+
|
948
|
+
// Start an AST node, attaching a start offset.
|
949
|
+
|
950
|
+
function node_t() {
|
951
|
+
this.type = null;
|
952
|
+
this.start = tokStart;
|
953
|
+
this.end = null;
|
954
|
+
}
|
955
|
+
|
956
|
+
function node_loc_t() {
|
957
|
+
this.start = tokStartLoc;
|
958
|
+
this.end = null;
|
959
|
+
if (sourceFile !== null) this.source = sourceFile;
|
960
|
+
}
|
961
|
+
|
962
|
+
function startNode() {
|
963
|
+
var node = new node_t();
|
964
|
+
if (options.locations)
|
965
|
+
node.loc = new node_loc_t();
|
966
|
+
if (options.ranges)
|
967
|
+
node.range = [tokStart, 0];
|
968
|
+
return node;
|
969
|
+
}
|
970
|
+
|
971
|
+
// Start a node whose start offset information should be based on
|
972
|
+
// the start of another node. For example, a binary operator node is
|
973
|
+
// only started after its left-hand side has already been parsed.
|
974
|
+
|
975
|
+
function startNodeFrom(other) {
|
976
|
+
var node = new node_t();
|
977
|
+
node.start = other.start;
|
978
|
+
if (options.locations) {
|
979
|
+
node.loc = new node_loc_t();
|
980
|
+
node.loc.start = other.loc.start;
|
981
|
+
}
|
982
|
+
if (options.ranges)
|
983
|
+
node.range = [other.range[0], 0];
|
984
|
+
|
985
|
+
return node;
|
986
|
+
}
|
987
|
+
|
988
|
+
// Finish an AST node, adding `type` and `end` properties.
|
989
|
+
|
990
|
+
function finishNode(node, type) {
|
991
|
+
node.type = type;
|
992
|
+
node.end = lastEnd;
|
993
|
+
if (options.locations)
|
994
|
+
node.loc.end = lastEndLoc;
|
995
|
+
if (options.ranges)
|
996
|
+
node.range[1] = lastEnd;
|
997
|
+
return node;
|
998
|
+
}
|
999
|
+
|
1000
|
+
// Test whether a statement node is the string literal `"use strict"`.
|
1001
|
+
|
1002
|
+
function isUseStrict(stmt) {
|
1003
|
+
return options.ecmaVersion >= 5 && stmt.type === "ExpressionStatement" &&
|
1004
|
+
stmt.expression.type === "Literal" && stmt.expression.value === "use strict";
|
1005
|
+
}
|
1006
|
+
|
1007
|
+
// Predicate that tests whether the next token is of the given
|
1008
|
+
// type, and if yes, consumes it as a side effect.
|
1009
|
+
|
1010
|
+
function eat(type) {
|
1011
|
+
if (tokType === type) {
|
1012
|
+
next();
|
1013
|
+
return true;
|
1014
|
+
}
|
1015
|
+
}
|
1016
|
+
|
1017
|
+
// Test whether a semicolon can be inserted at the current position.
|
1018
|
+
|
1019
|
+
function canInsertSemicolon() {
|
1020
|
+
return !options.strictSemicolons &&
|
1021
|
+
(tokType === _eof || tokType === _braceR || newline.test(input.slice(lastEnd, tokStart)));
|
1022
|
+
}
|
1023
|
+
|
1024
|
+
// Consume a semicolon, or, failing that, see if we are allowed to
|
1025
|
+
// pretend that there is a semicolon at this position.
|
1026
|
+
|
1027
|
+
function semicolon() {
|
1028
|
+
if (!eat(_semi) && !canInsertSemicolon()) unexpected();
|
1029
|
+
}
|
1030
|
+
|
1031
|
+
// Expect a token of a given type. If found, consume it, otherwise,
|
1032
|
+
// raise an unexpected token error.
|
1033
|
+
|
1034
|
+
function expect(type) {
|
1035
|
+
if (tokType === type) next();
|
1036
|
+
else unexpected();
|
1037
|
+
}
|
1038
|
+
|
1039
|
+
// Raise an unexpected token error.
|
1040
|
+
|
1041
|
+
function unexpected() {
|
1042
|
+
raise(tokStart, "Unexpected token");
|
1043
|
+
}
|
1044
|
+
|
1045
|
+
// Verify that a node is an lval — something that can be assigned
|
1046
|
+
// to.
|
1047
|
+
|
1048
|
+
function checkLVal(expr) {
|
1049
|
+
if (expr.type !== "Identifier" && expr.type !== "MemberExpression")
|
1050
|
+
raise(expr.start, "Assigning to rvalue");
|
1051
|
+
if (strict && expr.type === "Identifier" && isStrictBadIdWord(expr.name))
|
1052
|
+
raise(expr.start, "Assigning to " + expr.name + " in strict mode");
|
1053
|
+
}
|
1054
|
+
|
1055
|
+
// ### Statement parsing
|
1056
|
+
|
1057
|
+
// Parse a program. Initializes the parser, reads any number of
|
1058
|
+
// statements, and wraps them in a Program node. Optionally takes a
|
1059
|
+
// `program` argument. If present, the statements will be appended
|
1060
|
+
// to its body instead of creating a new node.
|
1061
|
+
|
1062
|
+
function parseTopLevel(program) {
|
1063
|
+
lastStart = lastEnd = tokPos;
|
1064
|
+
if (options.locations) lastEndLoc = new line_loc_t;
|
1065
|
+
inFunction = strict = null;
|
1066
|
+
labels = [];
|
1067
|
+
readToken();
|
1068
|
+
|
1069
|
+
var node = program || startNode(), first = true;
|
1070
|
+
if (!program) node.body = [];
|
1071
|
+
while (tokType !== _eof) {
|
1072
|
+
var stmt = parseStatement();
|
1073
|
+
node.body.push(stmt);
|
1074
|
+
if (first && isUseStrict(stmt)) setStrict(true);
|
1075
|
+
first = false;
|
1076
|
+
}
|
1077
|
+
return finishNode(node, "Program");
|
1078
|
+
}
|
1079
|
+
|
1080
|
+
var loopLabel = {kind: "loop"}, switchLabel = {kind: "switch"};
|
1081
|
+
|
1082
|
+
// Parse a single statement.
|
1083
|
+
//
|
1084
|
+
// If expecting a statement and finding a slash operator, parse a
|
1085
|
+
// regular expression literal. This is to handle cases like
|
1086
|
+
// `if (foo) /blah/.exec(foo);`, where looking at the previous token
|
1087
|
+
// does not help.
|
1088
|
+
|
1089
|
+
function parseStatement() {
|
1090
|
+
if (tokType === _slash)
|
1091
|
+
readToken(true);
|
1092
|
+
|
1093
|
+
var starttype = tokType, node = startNode();
|
1094
|
+
|
1095
|
+
// Most types of statements are recognized by the keyword they
|
1096
|
+
// start with. Many are trivial to parse, some require a bit of
|
1097
|
+
// complexity.
|
1098
|
+
|
1099
|
+
switch (starttype) {
|
1100
|
+
case _break: case _continue:
|
1101
|
+
next();
|
1102
|
+
var isBreak = starttype === _break;
|
1103
|
+
if (eat(_semi) || canInsertSemicolon()) node.label = null;
|
1104
|
+
else if (tokType !== _name) unexpected();
|
1105
|
+
else {
|
1106
|
+
node.label = parseIdent();
|
1107
|
+
semicolon();
|
1108
|
+
}
|
1109
|
+
|
1110
|
+
// Verify that there is an actual destination to break or
|
1111
|
+
// continue to.
|
1112
|
+
for (var i = 0; i < labels.length; ++i) {
|
1113
|
+
var lab = labels[i];
|
1114
|
+
if (node.label == null || lab.name === node.label.name) {
|
1115
|
+
if (lab.kind != null && (isBreak || lab.kind === "loop")) break;
|
1116
|
+
if (node.label && isBreak) break;
|
1117
|
+
}
|
1118
|
+
}
|
1119
|
+
if (i === labels.length) raise(node.start, "Unsyntactic " + starttype.keyword);
|
1120
|
+
return finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");
|
1121
|
+
|
1122
|
+
case _debugger:
|
1123
|
+
next();
|
1124
|
+
semicolon();
|
1125
|
+
return finishNode(node, "DebuggerStatement");
|
1126
|
+
|
1127
|
+
case _do:
|
1128
|
+
next();
|
1129
|
+
labels.push(loopLabel);
|
1130
|
+
node.body = parseStatement();
|
1131
|
+
labels.pop();
|
1132
|
+
expect(_while);
|
1133
|
+
node.test = parseParenExpression();
|
1134
|
+
semicolon();
|
1135
|
+
return finishNode(node, "DoWhileStatement");
|
1136
|
+
|
1137
|
+
// Disambiguating between a `for` and a `for`/`in` loop is
|
1138
|
+
// non-trivial. Basically, we have to parse the init `var`
|
1139
|
+
// statement or expression, disallowing the `in` operator (see
|
1140
|
+
// the second parameter to `parseExpression`), and then check
|
1141
|
+
// whether the next token is `in`. When there is no init part
|
1142
|
+
// (semicolon immediately after the opening parenthesis), it is
|
1143
|
+
// a regular `for` loop.
|
1144
|
+
|
1145
|
+
case _for:
|
1146
|
+
next();
|
1147
|
+
labels.push(loopLabel);
|
1148
|
+
expect(_parenL);
|
1149
|
+
if (tokType === _semi) return parseFor(node, null);
|
1150
|
+
if (tokType === _var) {
|
1151
|
+
var init = startNode();
|
1152
|
+
next();
|
1153
|
+
parseVar(init, true);
|
1154
|
+
if (init.declarations.length === 1 && eat(_in))
|
1155
|
+
return parseForIn(node, init);
|
1156
|
+
return parseFor(node, init);
|
1157
|
+
}
|
1158
|
+
var init = parseExpression(false, true);
|
1159
|
+
if (eat(_in)) {checkLVal(init); return parseForIn(node, init);}
|
1160
|
+
return parseFor(node, init);
|
1161
|
+
|
1162
|
+
case _function:
|
1163
|
+
next();
|
1164
|
+
return parseFunction(node, true);
|
1165
|
+
|
1166
|
+
case _if:
|
1167
|
+
next();
|
1168
|
+
node.test = parseParenExpression();
|
1169
|
+
node.consequent = parseStatement();
|
1170
|
+
node.alternate = eat(_else) ? parseStatement() : null;
|
1171
|
+
return finishNode(node, "IfStatement");
|
1172
|
+
|
1173
|
+
case _return:
|
1174
|
+
if (!inFunction) raise(tokStart, "'return' outside of function");
|
1175
|
+
next();
|
1176
|
+
|
1177
|
+
// In `return` (and `break`/`continue`), the keywords with
|
1178
|
+
// optional arguments, we eagerly look for a semicolon or the
|
1179
|
+
// possibility to insert one.
|
1180
|
+
|
1181
|
+
if (eat(_semi) || canInsertSemicolon()) node.argument = null;
|
1182
|
+
else { node.argument = parseExpression(); semicolon(); }
|
1183
|
+
return finishNode(node, "ReturnStatement");
|
1184
|
+
|
1185
|
+
case _switch:
|
1186
|
+
next();
|
1187
|
+
node.discriminant = parseParenExpression();
|
1188
|
+
node.cases = [];
|
1189
|
+
expect(_braceL);
|
1190
|
+
labels.push(switchLabel);
|
1191
|
+
|
1192
|
+
// Statements under must be grouped (by label) in SwitchCase
|
1193
|
+
// nodes. `cur` is used to keep the node that we are currently
|
1194
|
+
// adding statements to.
|
1195
|
+
|
1196
|
+
for (var cur, sawDefault; tokType != _braceR;) {
|
1197
|
+
if (tokType === _case || tokType === _default) {
|
1198
|
+
var isCase = tokType === _case;
|
1199
|
+
if (cur) finishNode(cur, "SwitchCase");
|
1200
|
+
node.cases.push(cur = startNode());
|
1201
|
+
cur.consequent = [];
|
1202
|
+
next();
|
1203
|
+
if (isCase) cur.test = parseExpression();
|
1204
|
+
else {
|
1205
|
+
if (sawDefault) raise(lastStart, "Multiple default clauses"); sawDefault = true;
|
1206
|
+
cur.test = null;
|
1207
|
+
}
|
1208
|
+
expect(_colon);
|
1209
|
+
} else {
|
1210
|
+
if (!cur) unexpected();
|
1211
|
+
cur.consequent.push(parseStatement());
|
1212
|
+
}
|
1213
|
+
}
|
1214
|
+
if (cur) finishNode(cur, "SwitchCase");
|
1215
|
+
next(); // Closing brace
|
1216
|
+
labels.pop();
|
1217
|
+
return finishNode(node, "SwitchStatement");
|
1218
|
+
|
1219
|
+
case _throw:
|
1220
|
+
next();
|
1221
|
+
if (newline.test(input.slice(lastEnd, tokStart)))
|
1222
|
+
raise(lastEnd, "Illegal newline after throw");
|
1223
|
+
node.argument = parseExpression();
|
1224
|
+
semicolon();
|
1225
|
+
return finishNode(node, "ThrowStatement");
|
1226
|
+
|
1227
|
+
case _try:
|
1228
|
+
next();
|
1229
|
+
node.block = parseBlock();
|
1230
|
+
node.handlers = [];
|
1231
|
+
while (tokType === _catch) {
|
1232
|
+
var clause = startNode();
|
1233
|
+
next();
|
1234
|
+
expect(_parenL);
|
1235
|
+
clause.param = parseIdent();
|
1236
|
+
if (strict && isStrictBadIdWord(clause.param.name))
|
1237
|
+
raise(clause.param.start, "Binding " + clause.param.name + " in strict mode");
|
1238
|
+
expect(_parenR);
|
1239
|
+
clause.guard = null;
|
1240
|
+
clause.body = parseBlock();
|
1241
|
+
node.handlers.push(finishNode(clause, "CatchClause"));
|
1242
|
+
}
|
1243
|
+
node.finalizer = eat(_finally) ? parseBlock() : null;
|
1244
|
+
if (!node.handlers.length && !node.finalizer)
|
1245
|
+
raise(node.start, "Missing catch or finally clause");
|
1246
|
+
return finishNode(node, "TryStatement");
|
1247
|
+
|
1248
|
+
case _var:
|
1249
|
+
next();
|
1250
|
+
node = parseVar(node);
|
1251
|
+
semicolon();
|
1252
|
+
return node;
|
1253
|
+
|
1254
|
+
case _while:
|
1255
|
+
next();
|
1256
|
+
node.test = parseParenExpression();
|
1257
|
+
labels.push(loopLabel);
|
1258
|
+
node.body = parseStatement();
|
1259
|
+
labels.pop();
|
1260
|
+
return finishNode(node, "WhileStatement");
|
1261
|
+
|
1262
|
+
case _with:
|
1263
|
+
if (strict) raise(tokStart, "'with' in strict mode");
|
1264
|
+
next();
|
1265
|
+
node.object = parseParenExpression();
|
1266
|
+
node.body = parseStatement();
|
1267
|
+
return finishNode(node, "WithStatement");
|
1268
|
+
|
1269
|
+
case _braceL:
|
1270
|
+
return parseBlock();
|
1271
|
+
|
1272
|
+
case _semi:
|
1273
|
+
next();
|
1274
|
+
return finishNode(node, "EmptyStatement");
|
1275
|
+
|
1276
|
+
// If the statement does not start with a statement keyword or a
|
1277
|
+
// brace, it's an ExpressionStatement or LabeledStatement. We
|
1278
|
+
// simply start parsing an expression, and afterwards, if the
|
1279
|
+
// next token is a colon and the expression was a simple
|
1280
|
+
// Identifier node, we switch to interpreting it as a label.
|
1281
|
+
|
1282
|
+
default:
|
1283
|
+
var maybeName = tokVal, expr = parseExpression();
|
1284
|
+
if (starttype === _name && expr.type === "Identifier" && eat(_colon)) {
|
1285
|
+
for (var i = 0; i < labels.length; ++i)
|
1286
|
+
if (labels[i].name === maybeName) raise(expr.start, "Label '" + maybeName + "' is already declared");
|
1287
|
+
var kind = tokType.isLoop ? "loop" : tokType === _switch ? "switch" : null;
|
1288
|
+
labels.push({name: maybeName, kind: kind});
|
1289
|
+
node.body = parseStatement();
|
1290
|
+
labels.pop();
|
1291
|
+
node.label = expr;
|
1292
|
+
return finishNode(node, "LabeledStatement");
|
1293
|
+
} else {
|
1294
|
+
node.expression = expr;
|
1295
|
+
semicolon();
|
1296
|
+
return finishNode(node, "ExpressionStatement");
|
1297
|
+
}
|
1298
|
+
}
|
1299
|
+
}
|
1300
|
+
|
1301
|
+
// Used for constructs like `switch` and `if` that insist on
|
1302
|
+
// parentheses around their expression.
|
1303
|
+
|
1304
|
+
function parseParenExpression() {
|
1305
|
+
expect(_parenL);
|
1306
|
+
var val = parseExpression();
|
1307
|
+
expect(_parenR);
|
1308
|
+
return val;
|
1309
|
+
}
|
1310
|
+
|
1311
|
+
// Parse a semicolon-enclosed block of statements, handling `"use
|
1312
|
+
// strict"` declarations when `allowStrict` is true (used for
|
1313
|
+
// function bodies).
|
1314
|
+
|
1315
|
+
function parseBlock(allowStrict) {
|
1316
|
+
var node = startNode(), first = true, strict = false, oldStrict;
|
1317
|
+
node.body = [];
|
1318
|
+
expect(_braceL);
|
1319
|
+
while (!eat(_braceR)) {
|
1320
|
+
var stmt = parseStatement();
|
1321
|
+
node.body.push(stmt);
|
1322
|
+
if (first && isUseStrict(stmt)) {
|
1323
|
+
oldStrict = strict;
|
1324
|
+
setStrict(strict = true);
|
1325
|
+
}
|
1326
|
+
first = false
|
1327
|
+
}
|
1328
|
+
if (strict && !oldStrict) setStrict(false);
|
1329
|
+
return finishNode(node, "BlockStatement");
|
1330
|
+
}
|
1331
|
+
|
1332
|
+
// Parse a regular `for` loop. The disambiguation code in
|
1333
|
+
// `parseStatement` will already have parsed the init statement or
|
1334
|
+
// expression.
|
1335
|
+
|
1336
|
+
function parseFor(node, init) {
|
1337
|
+
node.init = init;
|
1338
|
+
expect(_semi);
|
1339
|
+
node.test = tokType === _semi ? null : parseExpression();
|
1340
|
+
expect(_semi);
|
1341
|
+
node.update = tokType === _parenR ? null : parseExpression();
|
1342
|
+
expect(_parenR);
|
1343
|
+
node.body = parseStatement();
|
1344
|
+
labels.pop();
|
1345
|
+
return finishNode(node, "ForStatement");
|
1346
|
+
}
|
1347
|
+
|
1348
|
+
// Parse a `for`/`in` loop.
|
1349
|
+
|
1350
|
+
function parseForIn(node, init) {
|
1351
|
+
node.left = init;
|
1352
|
+
node.right = parseExpression();
|
1353
|
+
expect(_parenR);
|
1354
|
+
node.body = parseStatement();
|
1355
|
+
labels.pop();
|
1356
|
+
return finishNode(node, "ForInStatement");
|
1357
|
+
}
|
1358
|
+
|
1359
|
+
// Parse a list of variable declarations.
|
1360
|
+
|
1361
|
+
function parseVar(node, noIn) {
|
1362
|
+
node.declarations = [];
|
1363
|
+
node.kind = "var";
|
1364
|
+
for (;;) {
|
1365
|
+
var decl = startNode();
|
1366
|
+
decl.id = parseIdent();
|
1367
|
+
if (strict && isStrictBadIdWord(decl.id.name))
|
1368
|
+
raise(decl.id.start, "Binding " + decl.id.name + " in strict mode");
|
1369
|
+
decl.init = eat(_eq) ? parseExpression(true, noIn) : null;
|
1370
|
+
node.declarations.push(finishNode(decl, "VariableDeclarator"));
|
1371
|
+
if (!eat(_comma)) break;
|
1372
|
+
}
|
1373
|
+
return finishNode(node, "VariableDeclaration");
|
1374
|
+
}
|
1375
|
+
|
1376
|
+
// ### Expression parsing
|
1377
|
+
|
1378
|
+
// These nest, from the most general expression type at the top to
|
1379
|
+
// 'atomic', nondivisible expression types at the bottom. Most of
|
1380
|
+
// the functions will simply let the function(s) below them parse,
|
1381
|
+
// and, *if* the syntactic construct they handle is present, wrap
|
1382
|
+
// the AST node that the inner parser gave them in another node.
|
1383
|
+
|
1384
|
+
// Parse a full expression. The arguments are used to forbid comma
|
1385
|
+
// sequences (in argument lists, array literals, or object literals)
|
1386
|
+
// or the `in` operator (in for loops initalization expressions).
|
1387
|
+
|
1388
|
+
function parseExpression(noComma, noIn) {
|
1389
|
+
var expr = parseMaybeAssign(noIn);
|
1390
|
+
if (!noComma && tokType === _comma) {
|
1391
|
+
var node = startNodeFrom(expr);
|
1392
|
+
node.expressions = [expr];
|
1393
|
+
while (eat(_comma)) node.expressions.push(parseMaybeAssign(noIn));
|
1394
|
+
return finishNode(node, "SequenceExpression");
|
1395
|
+
}
|
1396
|
+
return expr;
|
1397
|
+
}
|
1398
|
+
|
1399
|
+
// Parse an assignment expression. This includes applications of
|
1400
|
+
// operators like `+=`.
|
1401
|
+
|
1402
|
+
function parseMaybeAssign(noIn) {
|
1403
|
+
var left = parseMaybeConditional(noIn);
|
1404
|
+
if (tokType.isAssign) {
|
1405
|
+
var node = startNodeFrom(left);
|
1406
|
+
node.operator = tokVal;
|
1407
|
+
node.left = left;
|
1408
|
+
next();
|
1409
|
+
node.right = parseMaybeAssign(noIn);
|
1410
|
+
checkLVal(left);
|
1411
|
+
return finishNode(node, "AssignmentExpression");
|
1412
|
+
}
|
1413
|
+
return left;
|
1414
|
+
}
|
1415
|
+
|
1416
|
+
// Parse a ternary conditional (`?:`) operator.
|
1417
|
+
|
1418
|
+
function parseMaybeConditional(noIn) {
|
1419
|
+
var expr = parseExprOps(noIn);
|
1420
|
+
if (eat(_question)) {
|
1421
|
+
var node = startNodeFrom(expr);
|
1422
|
+
node.test = expr;
|
1423
|
+
node.consequent = parseExpression(true);
|
1424
|
+
expect(_colon);
|
1425
|
+
node.alternate = parseExpression(true, noIn);
|
1426
|
+
return finishNode(node, "ConditionalExpression");
|
1427
|
+
}
|
1428
|
+
return expr;
|
1429
|
+
}
|
1430
|
+
|
1431
|
+
// Start the precedence parser.
|
1432
|
+
|
1433
|
+
function parseExprOps(noIn) {
|
1434
|
+
return parseExprOp(parseMaybeUnary(noIn), -1, noIn);
|
1435
|
+
}
|
1436
|
+
|
1437
|
+
// Parse binary operators with the operator precedence parsing
|
1438
|
+
// algorithm. `left` is the left-hand side of the operator.
|
1439
|
+
// `minPrec` provides context that allows the function to stop and
|
1440
|
+
// defer further parser to one of its callers when it encounters an
|
1441
|
+
// operator that has a lower precedence than the set it is parsing.
|
1442
|
+
|
1443
|
+
function parseExprOp(left, minPrec, noIn) {
|
1444
|
+
var prec = tokType.binop;
|
1445
|
+
if (prec != null && (!noIn || tokType !== _in)) {
|
1446
|
+
if (prec > minPrec) {
|
1447
|
+
var node = startNodeFrom(left);
|
1448
|
+
node.left = left;
|
1449
|
+
node.operator = tokVal;
|
1450
|
+
next();
|
1451
|
+
node.right = parseExprOp(parseMaybeUnary(noIn), prec, noIn);
|
1452
|
+
var node = finishNode(node, /&&|\|\|/.test(node.operator) ? "LogicalExpression" : "BinaryExpression");
|
1453
|
+
return parseExprOp(node, minPrec, noIn);
|
1454
|
+
}
|
1455
|
+
}
|
1456
|
+
return left;
|
1457
|
+
}
|
1458
|
+
|
1459
|
+
// Parse unary operators, both prefix and postfix.
|
1460
|
+
|
1461
|
+
function parseMaybeUnary(noIn) {
|
1462
|
+
if (tokType.prefix) {
|
1463
|
+
var node = startNode(), update = tokType.isUpdate;
|
1464
|
+
node.operator = tokVal;
|
1465
|
+
node.prefix = true;
|
1466
|
+
next();
|
1467
|
+
node.argument = parseMaybeUnary(noIn);
|
1468
|
+
if (update) checkLVal(node.argument);
|
1469
|
+
else if (strict && node.operator === "delete" &&
|
1470
|
+
node.argument.type === "Identifier")
|
1471
|
+
raise(node.start, "Deleting local variable in strict mode");
|
1472
|
+
return finishNode(node, update ? "UpdateExpression" : "UnaryExpression");
|
1473
|
+
}
|
1474
|
+
var expr = parseExprSubscripts();
|
1475
|
+
while (tokType.postfix && !canInsertSemicolon()) {
|
1476
|
+
var node = startNodeFrom(expr);
|
1477
|
+
node.operator = tokVal;
|
1478
|
+
node.prefix = false;
|
1479
|
+
node.argument = expr;
|
1480
|
+
checkLVal(expr);
|
1481
|
+
next();
|
1482
|
+
expr = finishNode(node, "UpdateExpression");
|
1483
|
+
}
|
1484
|
+
return expr;
|
1485
|
+
}
|
1486
|
+
|
1487
|
+
// Parse call, dot, and `[]`-subscript expressions.
|
1488
|
+
|
1489
|
+
function parseExprSubscripts() {
|
1490
|
+
return parseSubscripts(parseExprAtom());
|
1491
|
+
}
|
1492
|
+
|
1493
|
+
function parseSubscripts(base, noCalls) {
|
1494
|
+
if (eat(_dot)) {
|
1495
|
+
var node = startNodeFrom(base);
|
1496
|
+
node.object = base;
|
1497
|
+
node.property = parseIdent(true);
|
1498
|
+
node.computed = false;
|
1499
|
+
return parseSubscripts(finishNode(node, "MemberExpression"), noCalls);
|
1500
|
+
} else if (eat(_bracketL)) {
|
1501
|
+
var node = startNodeFrom(base);
|
1502
|
+
node.object = base;
|
1503
|
+
node.property = parseExpression();
|
1504
|
+
node.computed = true;
|
1505
|
+
expect(_bracketR);
|
1506
|
+
return parseSubscripts(finishNode(node, "MemberExpression"), noCalls);
|
1507
|
+
} else if (!noCalls && eat(_parenL)) {
|
1508
|
+
var node = startNodeFrom(base);
|
1509
|
+
node.callee = base;
|
1510
|
+
node.arguments = parseExprList(_parenR, false);
|
1511
|
+
return parseSubscripts(finishNode(node, "CallExpression"), noCalls);
|
1512
|
+
} else return base;
|
1513
|
+
}
|
1514
|
+
|
1515
|
+
// Parse an atomic expression — either a single token that is an
|
1516
|
+
// expression, an expression started by a keyword like `function` or
|
1517
|
+
// `new`, or an expression wrapped in punctuation like `()`, `[]`,
|
1518
|
+
// or `{}`.
|
1519
|
+
|
1520
|
+
function parseExprAtom() {
|
1521
|
+
switch (tokType) {
|
1522
|
+
case _this:
|
1523
|
+
var node = startNode();
|
1524
|
+
next();
|
1525
|
+
return finishNode(node, "ThisExpression");
|
1526
|
+
case _name:
|
1527
|
+
return parseIdent();
|
1528
|
+
case _num: case _string: case _regexp:
|
1529
|
+
var node = startNode();
|
1530
|
+
node.value = tokVal;
|
1531
|
+
node.raw = input.slice(tokStart, tokEnd);
|
1532
|
+
next();
|
1533
|
+
return finishNode(node, "Literal");
|
1534
|
+
|
1535
|
+
case _null: case _true: case _false:
|
1536
|
+
var node = startNode();
|
1537
|
+
node.value = tokType.atomValue;
|
1538
|
+
node.raw = tokType.keyword
|
1539
|
+
next();
|
1540
|
+
return finishNode(node, "Literal");
|
1541
|
+
|
1542
|
+
case _parenL:
|
1543
|
+
var tokStartLoc1 = tokStartLoc, tokStart1 = tokStart;
|
1544
|
+
next();
|
1545
|
+
var val = parseExpression();
|
1546
|
+
val.start = tokStart1;
|
1547
|
+
val.end = tokEnd;
|
1548
|
+
if (options.locations) {
|
1549
|
+
val.loc.start = tokStartLoc1;
|
1550
|
+
val.loc.end = tokEndLoc;
|
1551
|
+
}
|
1552
|
+
if (options.ranges)
|
1553
|
+
val.range = [tokStart1, tokEnd];
|
1554
|
+
expect(_parenR);
|
1555
|
+
return val;
|
1556
|
+
|
1557
|
+
case _bracketL:
|
1558
|
+
var node = startNode();
|
1559
|
+
next();
|
1560
|
+
node.elements = parseExprList(_bracketR, true, true);
|
1561
|
+
return finishNode(node, "ArrayExpression");
|
1562
|
+
|
1563
|
+
case _braceL:
|
1564
|
+
return parseObj();
|
1565
|
+
|
1566
|
+
case _function:
|
1567
|
+
var node = startNode();
|
1568
|
+
next();
|
1569
|
+
return parseFunction(node, false);
|
1570
|
+
|
1571
|
+
case _new:
|
1572
|
+
return parseNew();
|
1573
|
+
|
1574
|
+
default:
|
1575
|
+
unexpected();
|
1576
|
+
}
|
1577
|
+
}
|
1578
|
+
|
1579
|
+
// New's precedence is slightly tricky. It must allow its argument
|
1580
|
+
// to be a `[]` or dot subscript expression, but not a call — at
|
1581
|
+
// least, not without wrapping it in parentheses. Thus, it uses the
|
1582
|
+
|
1583
|
+
function parseNew() {
|
1584
|
+
var node = startNode();
|
1585
|
+
next();
|
1586
|
+
node.callee = parseSubscripts(parseExprAtom(), true);
|
1587
|
+
if (eat(_parenL)) node.arguments = parseExprList(_parenR, false);
|
1588
|
+
else node.arguments = [];
|
1589
|
+
return finishNode(node, "NewExpression");
|
1590
|
+
}
|
1591
|
+
|
1592
|
+
// Parse an object literal.
|
1593
|
+
|
1594
|
+
function parseObj() {
|
1595
|
+
var node = startNode(), first = true, sawGetSet = false;
|
1596
|
+
node.properties = [];
|
1597
|
+
next();
|
1598
|
+
while (!eat(_braceR)) {
|
1599
|
+
if (!first) {
|
1600
|
+
expect(_comma);
|
1601
|
+
if (options.allowTrailingCommas && eat(_braceR)) break;
|
1602
|
+
} else first = false;
|
1603
|
+
|
1604
|
+
var prop = {key: parsePropertyName()}, isGetSet = false, kind;
|
1605
|
+
if (eat(_colon)) {
|
1606
|
+
prop.value = parseExpression(true);
|
1607
|
+
kind = prop.kind = "init";
|
1608
|
+
} else if (options.ecmaVersion >= 5 && prop.key.type === "Identifier" &&
|
1609
|
+
(prop.key.name === "get" || prop.key.name === "set")) {
|
1610
|
+
isGetSet = sawGetSet = true;
|
1611
|
+
kind = prop.kind = prop.key.name;
|
1612
|
+
prop.key = parsePropertyName();
|
1613
|
+
if (tokType !== _parenL) unexpected();
|
1614
|
+
prop.value = parseFunction(startNode(), false);
|
1615
|
+
} else unexpected();
|
1616
|
+
|
1617
|
+
// getters and setters are not allowed to clash — either with
|
1618
|
+
// each other or with an init property — and in strict mode,
|
1619
|
+
// init properties are also not allowed to be repeated.
|
1620
|
+
|
1621
|
+
if (prop.key.type === "Identifier" && (strict || sawGetSet)) {
|
1622
|
+
for (var i = 0; i < node.properties.length; ++i) {
|
1623
|
+
var other = node.properties[i];
|
1624
|
+
if (other.key.name === prop.key.name) {
|
1625
|
+
var conflict = kind == other.kind || isGetSet && other.kind === "init" ||
|
1626
|
+
kind === "init" && (other.kind === "get" || other.kind === "set");
|
1627
|
+
if (conflict && !strict && kind === "init" && other.kind === "init") conflict = false;
|
1628
|
+
if (conflict) raise(prop.key.start, "Redefinition of property");
|
1629
|
+
}
|
1630
|
+
}
|
1631
|
+
}
|
1632
|
+
node.properties.push(prop);
|
1633
|
+
}
|
1634
|
+
return finishNode(node, "ObjectExpression");
|
1635
|
+
}
|
1636
|
+
|
1637
|
+
function parsePropertyName() {
|
1638
|
+
if (tokType === _num || tokType === _string) return parseExprAtom();
|
1639
|
+
return parseIdent(true);
|
1640
|
+
}
|
1641
|
+
|
1642
|
+
// Parse a function declaration or literal (depending on the
|
1643
|
+
// `isStatement` parameter).
|
1644
|
+
|
1645
|
+
function parseFunction(node, isStatement) {
|
1646
|
+
if (tokType === _name) node.id = parseIdent();
|
1647
|
+
else if (isStatement) unexpected();
|
1648
|
+
else node.id = null;
|
1649
|
+
node.params = [];
|
1650
|
+
var first = true;
|
1651
|
+
expect(_parenL);
|
1652
|
+
while (!eat(_parenR)) {
|
1653
|
+
if (!first) expect(_comma); else first = false;
|
1654
|
+
node.params.push(parseIdent());
|
1655
|
+
}
|
1656
|
+
|
1657
|
+
// Start a new scope with regard to labels and the `inFunction`
|
1658
|
+
// flag (restore them to their old value afterwards).
|
1659
|
+
var oldInFunc = inFunction, oldLabels = labels;
|
1660
|
+
inFunction = true; labels = [];
|
1661
|
+
node.body = parseBlock(true);
|
1662
|
+
inFunction = oldInFunc; labels = oldLabels;
|
1663
|
+
|
1664
|
+
// If this is a strict mode function, verify that argument names
|
1665
|
+
// are not repeated, and it does not try to bind the words `eval`
|
1666
|
+
// or `arguments`.
|
1667
|
+
if (strict || node.body.body.length && isUseStrict(node.body.body[0])) {
|
1668
|
+
for (var i = node.id ? -1 : 0; i < node.params.length; ++i) {
|
1669
|
+
var id = i < 0 ? node.id : node.params[i];
|
1670
|
+
if (isStrictReservedWord(id.name) || isStrictBadIdWord(id.name))
|
1671
|
+
raise(id.start, "Defining '" + id.name + "' in strict mode");
|
1672
|
+
if (i >= 0) for (var j = 0; j < i; ++j) if (id.name === node.params[j].name)
|
1673
|
+
raise(id.start, "Argument name clash in strict mode");
|
1674
|
+
}
|
1675
|
+
}
|
1676
|
+
|
1677
|
+
return finishNode(node, isStatement ? "FunctionDeclaration" : "FunctionExpression");
|
1678
|
+
}
|
1679
|
+
|
1680
|
+
// Parses a comma-separated list of expressions, and returns them as
|
1681
|
+
// an array. `close` is the token type that ends the list, and
|
1682
|
+
// `allowEmpty` can be turned on to allow subsequent commas with
|
1683
|
+
// nothing in between them to be parsed as `null` (which is needed
|
1684
|
+
// for array literals).
|
1685
|
+
|
1686
|
+
function parseExprList(close, allowTrailingComma, allowEmpty) {
|
1687
|
+
var elts = [], first = true;
|
1688
|
+
while (!eat(close)) {
|
1689
|
+
if (!first) {
|
1690
|
+
expect(_comma);
|
1691
|
+
if (allowTrailingComma && options.allowTrailingCommas && eat(close)) break;
|
1692
|
+
} else first = false;
|
1693
|
+
|
1694
|
+
if (allowEmpty && tokType === _comma) elts.push(null);
|
1695
|
+
else elts.push(parseExpression(true));
|
1696
|
+
}
|
1697
|
+
return elts;
|
1698
|
+
}
|
1699
|
+
|
1700
|
+
// Parse the next token as an identifier. If `liberal` is true (used
|
1701
|
+
// when parsing properties), it will also convert keywords into
|
1702
|
+
// identifiers.
|
1703
|
+
|
1704
|
+
function parseIdent(liberal) {
|
1705
|
+
var node = startNode();
|
1706
|
+
node.name = tokType === _name ? tokVal : (liberal && !options.forbidReserved && tokType.keyword) || unexpected();
|
1707
|
+
next();
|
1708
|
+
return finishNode(node, "Identifier");
|
1709
|
+
}
|
1710
|
+
|
1711
|
+
})(typeof exports === "undefined" ? (self.acorn = {}) : exports);
|