|
Lines 16-437
Link Here
|
| 16 |
'javascript/lru', |
16 |
'javascript/lru', |
| 17 |
"javascript/util", |
17 |
"javascript/util", |
| 18 |
"acorn/dist/acorn", |
18 |
"acorn/dist/acorn", |
| 19 |
"acorn/dist/acorn_loose" |
19 |
"acorn/dist/acorn_loose", |
| 20 |
], function(Deferred, Objects, LRU, Util, acorn, acorn_loose) { |
20 |
"javascript/orionAcorn", |
| 21 |
var registry, |
21 |
], function(Deferred, Objects, LRU, Util, acorn, acorn_loose, OrionAcorn) { |
| 22 |
dependencies, |
22 |
var registry; |
| 23 |
environments, |
|
|
| 24 |
comments, |
| 25 |
tokens, |
| 26 |
leadingCommentsIndex, |
| 27 |
trailingCommentsIndex, |
| 28 |
needReset, |
| 29 |
currentOptions, |
| 30 |
options, |
| 31 |
error; |
| 32 |
|
| 33 |
/** |
| 34 |
* @name onToken |
| 35 |
* @description Function called when recording a token |
| 36 |
* @param token the given token to record |
| 37 |
*/ |
| 38 |
function onToken(token) { |
| 39 |
var type = "Punctuator"; |
| 40 |
var label = token.type.label; |
| 41 |
var eof = false; |
| 42 |
var value = token.value; |
| 43 |
switch(label) { |
| 44 |
case "num" : |
| 45 |
//num: new TokenType("num", startsExpr), |
| 46 |
type = "Numeric"; |
| 47 |
break; |
| 48 |
case "regexp" : |
| 49 |
//regexp: new TokenType("regexp", startsExpr), |
| 50 |
type = "RegularExpression"; |
| 51 |
token.value = "/" + value.pattern + "/" + (value.flags ? value.flags : ""); |
| 52 |
break; |
| 53 |
case "string" : |
| 54 |
//string: new TokenType("string", startsExpr), |
| 55 |
type = "String"; |
| 56 |
break; |
| 57 |
case "name" : |
| 58 |
// name: new TokenType("name", startsExpr), |
| 59 |
type = "Identifier"; |
| 60 |
break; |
| 61 |
case "eof" : |
| 62 |
//eof: new TokenType("eof) |
| 63 |
eof = true; |
| 64 |
break; |
| 65 |
default: |
| 66 |
var keyword = token.type.keyword; |
| 67 |
if (keyword) { |
| 68 |
switch(keyword) { |
| 69 |
case "null" : |
| 70 |
type = "Null"; |
| 71 |
break; |
| 72 |
case "true" : |
| 73 |
case "false" : |
| 74 |
type = "Boolean"; |
| 75 |
break; |
| 76 |
default: |
| 77 |
type = "Keyword"; |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
if (!eof) { |
| 82 |
if (typeof value === "undefined") { |
| 83 |
token.value = label; |
| 84 |
} |
| 85 |
token.type = type; |
| 86 |
token.index = tokens.length; |
| 87 |
tokens.push(token); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* @name onComment |
| 93 |
* @description function called when a comment is recorded |
| 94 |
* @param block a boolean to indicate if this is a block comment (true) or a line comment (false) |
| 95 |
* @param text the given comment contents |
| 96 |
* @param start the given start position |
| 97 |
* @param end the given end position |
| 98 |
* @param startLoc the given start location |
| 99 |
* @param endLoc the given end location |
| 100 |
*/ |
| 101 |
function onComment(block, text, start, end, startLoc, endLoc) { |
| 102 |
var comment = { |
| 103 |
type: block ? 'Block' : 'Line', |
| 104 |
value: text, |
| 105 |
start: start, |
| 106 |
end: end |
| 107 |
}; |
| 108 |
if (currentOptions.locations) { |
| 109 |
comment.loc = { |
| 110 |
start: startLoc, |
| 111 |
end: endLoc, |
| 112 |
sourceFile: currentOptions.sourceFile |
| 113 |
}; |
| 114 |
} |
| 115 |
if (currentOptions.ranges) { |
| 116 |
comment.range = [start, end]; |
| 117 |
} |
| 118 |
comments.push(comment); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Define an acorn plugin to record the comments even if there are syntax errors (incomplete block comments), |
| 123 |
* it linked comments and nodes (leadingComments and trailingComments) and it records environments and dependencies |
| 124 |
*/ |
| 125 |
function acornPlugin (instance, opts) { |
| 126 |
if (!opts) { |
| 127 |
return; |
| 128 |
} |
| 129 |
/** |
| 130 |
* Returns a deep copy of the given obj |
| 131 |
*/ |
| 132 |
function deepCopy(obj) { |
| 133 |
var ret = {}, key, val; |
| 134 |
for (key in obj) { |
| 135 |
if (obj.hasOwnProperty(key)) { |
| 136 |
val = obj[key]; |
| 137 |
if (typeof val === 'object' && val !== null) { |
| 138 |
ret[key] = deepCopy(val); |
| 139 |
} else { |
| 140 |
ret[key] = val; |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
return ret; |
| 145 |
} |
| 146 |
|
| 147 |
instance.extend("raise", function(nextMethod) { |
| 148 |
return function (pos, message) { |
| 149 |
try { |
| 150 |
return nextMethod.call(this, pos, message); |
| 151 |
} catch(err) { |
| 152 |
if (err instanceof SyntaxError) { |
| 153 |
if (this.pos === this.input.length) { |
| 154 |
err.type = Util.ErrorTypes.EndOfInput; |
| 155 |
} else { |
| 156 |
err.type = Util.ErrorTypes.Unexpected; |
| 157 |
} |
| 158 |
if (needReset) { |
| 159 |
// we only reset tokens once. We don't want to reset them again when the syntax error is thrown during acorn_loose parsing |
| 160 |
reset(); |
| 161 |
needReset = false; |
| 162 |
} |
| 163 |
} |
| 164 |
error = err; |
| 165 |
err.index = pos; |
| 166 |
throw err; |
| 167 |
} |
| 168 |
}; |
| 169 |
}); |
| 170 |
instance.extend("startNode", function(nextMethod) { |
| 171 |
return function () { |
| 172 |
var node = nextMethod.call(this); |
| 173 |
// attach leading comments |
| 174 |
var max = comments.length; |
| 175 |
if (max !== leadingCommentsIndex) { |
| 176 |
// we got new comments since the last node |
| 177 |
var i = leadingCommentsIndex; |
| 178 |
loop: for (; i< max; i++) { |
| 179 |
var comment = comments[i]; |
| 180 |
if (node.range[0] >= comment.range[1]) { |
| 181 |
// attach the comment to the node |
| 182 |
if (!node.leadingComments) { |
| 183 |
node.leadingComments = []; |
| 184 |
} |
| 185 |
node.leadingComments.push(deepCopy(comments[i])); |
| 186 |
} else { |
| 187 |
break loop; |
| 188 |
} |
| 189 |
} |
| 190 |
leadingCommentsIndex = i; |
| 191 |
} |
| 192 |
return node; |
| 193 |
}; |
| 194 |
}); |
| 195 |
instance.extend("finishNode", function(nextMethod) { |
| 196 |
/** |
| 197 |
* @description Collects the dependencies from call expressions and new expressions |
| 198 |
* @param {Node} callee The named callee node |
| 199 |
* @param {Array.<Node>} args The list of arguments for the expression |
| 200 |
* ORION |
| 201 |
*/ |
| 202 |
function collectDeps(callee, args, extra) { |
| 203 |
if(extra.deps) { |
| 204 |
var len = args.length; |
| 205 |
if(callee.name === 'importScripts') { |
| 206 |
addArrayDeps(args, extra); //importScripts('foo', 'bar'...) |
| 207 |
} else if(callee.name === 'Worker') { |
| 208 |
addDep(args[0], extra); |
| 209 |
} else if(callee.name === 'require') { |
| 210 |
var _a = args[0]; |
| 211 |
if(_a.type === "ArrayExpression") { |
| 212 |
extra.envs.node = true; |
| 213 |
addArrayDeps(_a.elements, extra); //require([foo]) |
| 214 |
} else if(_a.type === "Literal") { |
| 215 |
extra.envs.node = true; |
| 216 |
addDep(_a, extra); // require('foo') |
| 217 |
} |
| 218 |
if(len > 1) { |
| 219 |
_a = args[1]; |
| 220 |
if(_a.type === "ArrayExpression") { |
| 221 |
extra.envs.node = true; |
| 222 |
addArrayDeps(_a.elements, extra); |
| 223 |
} |
| 224 |
} |
| 225 |
} else if(callee.name === 'requirejs') { |
| 226 |
_a = args[0]; |
| 227 |
if(_a.type === "ArrayExpression") { |
| 228 |
extra.envs.amd = true; |
| 229 |
addArrayDeps(_a.elements, extra); //requirejs([foo]) |
| 230 |
} |
| 231 |
} else if(callee.name === 'define' && len > 1) {//second arg must be array |
| 232 |
_a = args[0]; |
| 233 |
if(_a.type === "Literal") { |
| 234 |
_a = args[1]; |
| 235 |
} |
| 236 |
if(_a.type === "ArrayExpression") { |
| 237 |
extra.envs.amd = true; |
| 238 |
addArrayDeps(_a.elements, extra); |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* @description Adds all of the entries from the array of deps to the global state |
| 246 |
* @param {Array} array The array of deps to add |
| 247 |
* ORION |
| 248 |
*/ |
| 249 |
function addArrayDeps(array, extra) { |
| 250 |
if(extra.deps) { |
| 251 |
var len = array.length; |
| 252 |
for(var i = 0; i < len; i++) { |
| 253 |
addDep(array[i], extra); |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* @description Adds a dependency if it has not already been added |
| 260 |
* @param {Object} node The AST node |
| 261 |
*/ |
| 262 |
function addDep(node, extra) { |
| 263 |
if(extra.deps && node.type === "Literal") { |
| 264 |
if (!extra.deps[node.value]) { |
| 265 |
extra.deps[node.value] = 1; |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
return function(node, type) { |
| 270 |
if (type === "CallExpression" || type === "NewExpression") { |
| 271 |
var extra = { |
| 272 |
deps: {}, |
| 273 |
envs: {} |
| 274 |
}; |
| 275 |
collectDeps(node.callee, node.arguments, extra); |
| 276 |
// copy all properties from extra.envs into environments |
| 277 |
var env = extra.envs; |
| 278 |
for (var prop in env) { |
| 279 |
if (env.hasOwnProperty(prop)) { |
| 280 |
environments[prop] = env[prop]; |
| 281 |
} |
| 282 |
} |
| 283 |
var deps = extra.deps; |
| 284 |
// copy all properties from extra.deps into dependencies |
| 285 |
for (var dep in deps) { |
| 286 |
if (deps.hasOwnProperty(dep) && !dependencies.hasOwnProperty(dep)) { |
| 287 |
dependencies[dep] = {type: "Literal", value: dep }; |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
var result = nextMethod.call(this, node, type); |
| 292 |
// attach trailing comments |
| 293 |
var max = comments.length; |
| 294 |
if (max !== trailingCommentsIndex) { |
| 295 |
// we got new comments since the last node |
| 296 |
var i = trailingCommentsIndex; |
| 297 |
loop: for (; i< max; i++) { |
| 298 |
var comment = comments[i]; |
| 299 |
if (result.range[1] <= comment.range[0]) { |
| 300 |
// attach the comment to the node |
| 301 |
if (!result.trailingComments) { |
| 302 |
result.trailingComments = []; |
| 303 |
} |
| 304 |
result.trailingComments.push(deepCopy(comments[i])); |
| 305 |
} else { |
| 306 |
break loop; |
| 307 |
} |
| 308 |
} |
| 309 |
trailingCommentsIndex = i; |
| 310 |
} |
| 311 |
return result; |
| 312 |
}; |
| 313 |
}); |
| 314 |
instance.extend("skipBlockComment", function(nextMethod) { |
| 315 |
return function() { |
| 316 |
var lineBreak = /\r\n?|\n|\u2028|\u2029/; |
| 317 |
var lineBreakG = new RegExp(lineBreak.source, "g"); |
| 318 |
|
| 319 |
var startLoc = this.curPosition(); |
| 320 |
var start = this.pos, end = this.input.indexOf("*/", this.pos += 2); |
| 321 |
if (end !== -1) { |
| 322 |
this.pos -= 2; |
| 323 |
return nextMethod.call(this); |
| 324 |
} |
| 325 |
this.pos += 2; |
| 326 |
// error recovery: the block comment is not complete |
| 327 |
if (this.options.locations) { |
| 328 |
lineBreakG.lastIndex = start; |
| 329 |
var match; |
| 330 |
while ((match = lineBreakG.exec(this.input)) && match.index < this.pos) { |
| 331 |
++this.curLine; |
| 332 |
this.lineStart = match.index + match[0].length; |
| 333 |
} |
| 334 |
} |
| 335 |
if (this.options.onComment) { |
| 336 |
var current = this.input.length; |
| 337 |
this.pos = current; |
| 338 |
this.options.onComment(true, this.input.slice(start + 2, current), start, current, startLoc, this.curPosition()); |
| 339 |
} |
| 340 |
}; |
| 341 |
}); |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* Reset all variables |
| 346 |
*/ |
| 347 |
function reset() { |
| 348 |
comments = []; |
| 349 |
tokens = []; |
| 350 |
leadingCommentsIndex = 0; |
| 351 |
trailingCommentsIndex = 0; |
| 352 |
} |
| 353 |
|
| 354 |
/** |
| 355 |
* Initialize all variables for a new parsing |
| 356 |
*/ |
| 357 |
function initialize() { |
| 358 |
dependencies = {}; |
| 359 |
environments = {}; |
| 360 |
reset(); |
| 361 |
error = null; |
| 362 |
needReset = true; |
| 363 |
currentOptions = Object.create(null); |
| 364 |
options = Object.create(null); |
| 365 |
} |
| 366 |
|
| 367 |
/** |
| 368 |
* @description setup all the given options to set up the acorn parsing |
| 369 |
* @param {String} text The given source code |
| 370 |
* @param {Object} options The given options |
| 371 |
* @param {Object} acorn The acorn object |
| 372 |
* @param {Object} acornloose The acorn loose object |
| 373 |
*/ |
| 374 |
function preParse(text, acorn, acornloose, file) { |
| 375 |
initialize(); |
| 376 |
if (!acorn.plugins) { |
| 377 |
acorn.plugins = Object.create(null); |
| 378 |
} |
| 379 |
acorn.plugins.acornPlugin = acornPlugin; |
| 380 |
// enabled plugins |
| 381 |
options.plugins = { |
| 382 |
"acornPlugin" : true |
| 383 |
}; |
| 384 |
|
| 385 |
if (!acornloose.pluginsLoose) { |
| 386 |
acornloose.pluginsLoose = Object.create(null); |
| 387 |
} |
| 388 |
acornloose.pluginsLoose.acornPlugin = acornPlugin; |
| 389 |
|
| 390 |
// enabled plugins |
| 391 |
options.pluginsLoose = { |
| 392 |
"acornPlugin" : true |
| 393 |
}; |
| 394 |
options.onToken = onToken; |
| 395 |
options.onComment = onComment; |
| 396 |
options.locations = true; |
| 397 |
options.ranges = true; |
| 398 |
options.sourceFile = true; |
| 399 |
options.ecmaVersion = 6; |
| 400 |
options.directSourceFile = { |
| 401 |
name: file, |
| 402 |
text: text |
| 403 |
}; |
| 404 |
currentOptions = { |
| 405 |
locations : options.locations, |
| 406 |
sourceFile : options.sourceFile, |
| 407 |
ranges : options.ranges |
| 408 |
}; |
| 409 |
} |
| 410 |
|
| 411 |
/** |
| 412 |
* @description set all the values in the postParse phase |
| 413 |
* @param {Object} the given ast tree |
| 414 |
* @param {String} text The given source code |
| 415 |
*/ |
| 416 |
function postParse(ast, text, file) { |
| 417 |
if (error) { |
| 418 |
if (ast.errors) { |
| 419 |
ast.errors.push(error); |
| 420 |
} else { |
| 421 |
ast.errors = [error]; |
| 422 |
} |
| 423 |
} |
| 424 |
ast.comments = comments; |
| 425 |
ast.tokens = tokens; |
| 426 |
ast.dependencies = []; |
| 427 |
for (var prop in dependencies) { |
| 428 |
if (dependencies.hasOwnProperty(prop)) { |
| 429 |
ast.dependencies.push(dependencies[prop]); |
| 430 |
} |
| 431 |
} |
| 432 |
ast.environments = environments; |
| 433 |
ast.errors = Util.serializeAstErrors(ast); |
| 434 |
} |
| 435 |
|
23 |
|
| 436 |
/** |
24 |
/** |
| 437 |
* Provides a shared AST. |
25 |
* Provides a shared AST. |
|
Lines 442-447
Link Here
|
| 442 |
*/ |
30 |
*/ |
| 443 |
function ASTManager(serviceRegistry) { |
31 |
function ASTManager(serviceRegistry) { |
| 444 |
this.cache = new LRU(10); |
32 |
this.cache = new LRU(10); |
|
|
33 |
this.orionAcorn = new OrionAcorn(); |
| 445 |
registry = serviceRegistry; |
34 |
registry = serviceRegistry; |
| 446 |
} |
35 |
} |
| 447 |
|
36 |
|
|
Lines 497-512
Link Here
|
| 497 |
* @returns {Object} The AST. |
86 |
* @returns {Object} The AST. |
| 498 |
*/ |
87 |
*/ |
| 499 |
parse: function(text, file) { |
88 |
parse: function(text, file) { |
| 500 |
initialize(); |
89 |
this.orionAcorn.initialize(); |
| 501 |
var start = Date.now(); |
90 |
var start = Date.now(); |
| 502 |
var ast; |
91 |
var ast; |
| 503 |
preParse(text, acorn, acorn_loose, file); |
92 |
var options = Object.create(null); |
|
|
93 |
this.orionAcorn.preParse(text, options, acorn, acorn_loose, file); |
| 504 |
try { |
94 |
try { |
| 505 |
ast = acorn.parse(text, options); |
95 |
ast = acorn.parse(text, options); |
| 506 |
} catch(e) { |
96 |
} catch(e) { |
| 507 |
ast = acorn_loose.parse_dammit(text, options); |
97 |
ast = acorn_loose.parse_dammit(text, options); |
| 508 |
} |
98 |
} |
| 509 |
postParse(ast, text, file); |
99 |
this.orionAcorn.postParse(ast, text); |
| 510 |
logTiming(Date.now() - start); |
100 |
logTiming(Date.now() - start); |
| 511 |
return ast; |
101 |
return ast; |
| 512 |
}, |
102 |
}, |