OLD | NEW |
(Empty) | |
| 1 |
| 2 |
| 3 |
| 4 onmessage = function(event) { |
| 5 var result = {}; |
| 6 if (event.data.mimeType === "text/html") { |
| 7 var formatter = new HTMLScriptFormatter(); |
| 8 result = formatter.format(event.data.content); |
| 9 } else { |
| 10 result.mapping = { original: [0], formatted: [0] }; |
| 11 result.content = formatScript(event.data.content, result.mapping, 0, 0); |
| 12 } |
| 13 postMessage(result); |
| 14 }; |
| 15 |
| 16 function formatScript(content, mapping, offset, formattedOffset) |
| 17 { |
| 18 var formattedContent; |
| 19 try { |
| 20 var tokenizer = new Tokenizer(content); |
| 21 var builder = new FormattedContentBuilder(tokenizer.content(), mapping, offset,
formattedOffset); |
| 22 var formatter = new JavaScriptFormatter(tokenizer, builder); |
| 23 formatter.format(); |
| 24 formattedContent = builder.content(); |
| 25 } catch (e) { |
| 26 formattedContent = content; |
| 27 } |
| 28 return formattedContent; |
| 29 } |
| 30 |
| 31 WebInspector = {}; |
| 32 |
| 33 |
| 34 |
| 35 WebInspector.SourceTokenizer = function() |
| 36 { |
| 37 } |
| 38 |
| 39 WebInspector.SourceTokenizer.prototype = { |
| 40 set line(line) { |
| 41 this._line = line; |
| 42 }, |
| 43 |
| 44 set condition(condition) |
| 45 { |
| 46 this._condition = condition; |
| 47 }, |
| 48 |
| 49 get condition() |
| 50 { |
| 51 return this._condition; |
| 52 }, |
| 53 |
| 54 getLexCondition: function() |
| 55 { |
| 56 return this.condition.lexCondition; |
| 57 }, |
| 58 |
| 59 setLexCondition: function(lexCondition) |
| 60 { |
| 61 this.condition.lexCondition = lexCondition; |
| 62 }, |
| 63 |
| 64 _charAt: function(cursor) |
| 65 { |
| 66 return cursor < this._line.length ? this._line.charAt(cursor) : "\n"; |
| 67 } |
| 68 } |
| 69 |
| 70 |
| 71 WebInspector.SourceTokenizer.Registry = function() { |
| 72 this._tokenizers = {}; |
| 73 this._tokenizerConstructors = { |
| 74 "text/css": "SourceCSSTokenizer", |
| 75 "text/html": "SourceHTMLTokenizer", |
| 76 "text/javascript": "SourceJavaScriptTokenizer" |
| 77 }; |
| 78 } |
| 79 |
| 80 WebInspector.SourceTokenizer.Registry.getInstance = function() |
| 81 { |
| 82 if (!WebInspector.SourceTokenizer.Registry._instance) |
| 83 WebInspector.SourceTokenizer.Registry._instance = new WebInspector.SourceTokeniz
er.Registry(); |
| 84 return WebInspector.SourceTokenizer.Registry._instance; |
| 85 } |
| 86 |
| 87 WebInspector.SourceTokenizer.Registry.prototype = { |
| 88 getTokenizer: function(mimeType) |
| 89 { |
| 90 if (!this._tokenizerConstructors[mimeType]) |
| 91 return null; |
| 92 var tokenizerClass = this._tokenizerConstructors[mimeType]; |
| 93 var tokenizer = this._tokenizers[tokenizerClass]; |
| 94 if (!tokenizer) { |
| 95 tokenizer = new WebInspector[tokenizerClass](); |
| 96 this._tokenizers[tokenizerClass] = tokenizer; |
| 97 } |
| 98 return tokenizer; |
| 99 } |
| 100 } |
| 101 ; |
| 102 |
| 103 |
| 104 |
| 105 |
| 106 |
| 107 |
| 108 |
| 109 |
| 110 |
| 111 |
| 112 |
| 113 |
| 114 |
| 115 |
| 116 |
| 117 WebInspector.SourceHTMLTokenizer = function() |
| 118 { |
| 119 WebInspector.SourceTokenizer.call(this); |
| 120 |
| 121 |
| 122 this._lexConditions = { |
| 123 INITIAL: 0, |
| 124 COMMENT: 1, |
| 125 DOCTYPE: 2, |
| 126 TAG: 3, |
| 127 DSTRING: 4, |
| 128 SSTRING: 5 |
| 129 }; |
| 130 this.case_INITIAL = 1000; |
| 131 this.case_COMMENT = 1001; |
| 132 this.case_DOCTYPE = 1002; |
| 133 this.case_TAG = 1003; |
| 134 this.case_DSTRING = 1004; |
| 135 this.case_SSTRING = 1005; |
| 136 |
| 137 this._parseConditions = { |
| 138 INITIAL: 0, |
| 139 ATTRIBUTE: 1, |
| 140 ATTRIBUTE_VALUE: 2, |
| 141 LINKIFY: 4, |
| 142 A_NODE: 8, |
| 143 SCRIPT: 16, |
| 144 STYLE: 32 |
| 145 }; |
| 146 |
| 147 this.condition = this.createInitialCondition(); |
| 148 } |
| 149 |
| 150 WebInspector.SourceHTMLTokenizer.prototype = { |
| 151 createInitialCondition: function() |
| 152 { |
| 153 return { lexCondition: this._lexConditions.INITIAL, parseCondition: this._parseC
onditions.INITIAL }; |
| 154 }, |
| 155 |
| 156 set line(line) { |
| 157 if (this._condition.internalJavaScriptTokenizerCondition) { |
| 158 var match = /<\/script/i.exec(line); |
| 159 if (match) { |
| 160 this._internalJavaScriptTokenizer.line = line.substring(0, match.index); |
| 161 } else |
| 162 this._internalJavaScriptTokenizer.line = line; |
| 163 } else if (this._condition.internalCSSTokenizerCondition) { |
| 164 var match = /<\/style/i.exec(line); |
| 165 if (match) { |
| 166 this._internalCSSTokenizer.line = line.substring(0, match.index); |
| 167 } else |
| 168 this._internalCSSTokenizer.line = line; |
| 169 } |
| 170 this._line = line; |
| 171 }, |
| 172 |
| 173 _isExpectingAttribute: function() |
| 174 { |
| 175 return this._condition.parseCondition & this._parseConditions.ATTRIBUTE; |
| 176 }, |
| 177 |
| 178 _isExpectingAttributeValue: function() |
| 179 { |
| 180 return this._condition.parseCondition & this._parseConditions.ATTRIBUTE_VALUE; |
| 181 }, |
| 182 |
| 183 _setExpectingAttribute: function() |
| 184 { |
| 185 if (this._isExpectingAttributeValue()) |
| 186 this._condition.parseCondition ^= this._parseConditions.ATTRIBUTE_VALUE; |
| 187 this._condition.parseCondition |= this._parseConditions.ATTRIBUTE; |
| 188 }, |
| 189 |
| 190 _setExpectingAttributeValue: function() |
| 191 { |
| 192 if (this._isExpectingAttribute()) |
| 193 this._condition.parseCondition ^= this._parseConditions.ATTRIBUTE; |
| 194 this._condition.parseCondition |= this._parseConditions.ATTRIBUTE_VALUE; |
| 195 }, |
| 196 |
| 197 _stringToken: function(cursor, stringEnds) |
| 198 { |
| 199 if (!this._isExpectingAttributeValue()) { |
| 200 this.tokenType = null; |
| 201 return cursor; |
| 202 } |
| 203 this.tokenType = this._attrValueTokenType(); |
| 204 if (stringEnds) |
| 205 this._setExpectingAttribute(); |
| 206 return cursor; |
| 207 }, |
| 208 |
| 209 _attrValueTokenType: function() |
| 210 { |
| 211 if (this._condition.parseCondition & this._parseConditions.LINKIFY) { |
| 212 if (this._condition.parseCondition & this._parseConditions.A_NODE) |
| 213 return "html-external-link"; |
| 214 return "html-resource-link"; |
| 215 } |
| 216 return "html-attribute-value"; |
| 217 }, |
| 218 |
| 219 get _internalJavaScriptTokenizer() |
| 220 { |
| 221 return WebInspector.SourceTokenizer.Registry.getInstance().getTokenizer("text/ja
vascript"); |
| 222 }, |
| 223 |
| 224 get _internalCSSTokenizer() |
| 225 { |
| 226 return WebInspector.SourceTokenizer.Registry.getInstance().getTokenizer("text/cs
s"); |
| 227 }, |
| 228 |
| 229 scriptStarted: function(cursor) |
| 230 { |
| 231 this._condition.internalJavaScriptTokenizerCondition = this._internalJavaScriptT
okenizer.createInitialCondition(); |
| 232 }, |
| 233 |
| 234 scriptEnded: function(cursor) |
| 235 { |
| 236 }, |
| 237 |
| 238 styleSheetStarted: function(cursor) |
| 239 { |
| 240 this._condition.internalCSSTokenizerCondition = this._internalCSSTokenizer.creat
eInitialCondition(); |
| 241 }, |
| 242 |
| 243 styleSheetEnded: function(cursor) |
| 244 { |
| 245 }, |
| 246 |
| 247 nextToken: function(cursor) |
| 248 { |
| 249 if (this._condition.internalJavaScriptTokenizerCondition) { |
| 250 |
| 251 this.line = this._line; |
| 252 if (cursor !== this._internalJavaScriptTokenizer._line.length) { |
| 253 |
| 254 this._internalJavaScriptTokenizer.condition = this._condition.internalJavaScript
TokenizerCondition; |
| 255 var result = this._internalJavaScriptTokenizer.nextToken(cursor); |
| 256 this.tokenType = this._internalJavaScriptTokenizer.tokenType; |
| 257 this._condition.internalJavaScriptTokenizerCondition = this._internalJavaScriptT
okenizer.condition; |
| 258 return result; |
| 259 } else if (cursor !== this._line.length) |
| 260 delete this._condition.internalJavaScriptTokenizerCondition; |
| 261 } else if (this._condition.internalCSSTokenizerCondition) { |
| 262 |
| 263 this.line = this._line; |
| 264 if (cursor !== this._internalCSSTokenizer._line.length) { |
| 265 |
| 266 this._internalCSSTokenizer.condition = this._condition.internalCSSTokenizerCondi
tion; |
| 267 var result = this._internalCSSTokenizer.nextToken(cursor); |
| 268 this.tokenType = this._internalCSSTokenizer.tokenType; |
| 269 this._condition.internalCSSTokenizerCondition = this._internalCSSTokenizer.condi
tion; |
| 270 return result; |
| 271 } else if (cursor !== this._line.length) |
| 272 delete this._condition.internalCSSTokenizerCondition; |
| 273 } |
| 274 |
| 275 var cursorOnEnter = cursor; |
| 276 var gotoCase = 1; |
| 277 while (1) { |
| 278 switch (gotoCase) |
| 279 |
| 280 |
| 281 { |
| 282 case 1: var yych; |
| 283 var yyaccept = 0; |
| 284 if (this.getLexCondition() < 3) { |
| 285 if (this.getLexCondition() < 1) { |
| 286 { gotoCase = this.case_INITIAL; continue; }; |
| 287 } else { |
| 288 if (this.getLexCondition() < 2) { |
| 289 { gotoCase = this.case_COMMENT; continue; }; |
| 290 } else { |
| 291 { gotoCase = this.case_DOCTYPE; continue; }; |
| 292 } |
| 293 } |
| 294 } else { |
| 295 if (this.getLexCondition() < 4) { |
| 296 { gotoCase = this.case_TAG; continue; }; |
| 297 } else { |
| 298 if (this.getLexCondition() < 5) { |
| 299 { gotoCase = this.case_DSTRING; continue; }; |
| 300 } else { |
| 301 { gotoCase = this.case_SSTRING; continue; }; |
| 302 } |
| 303 } |
| 304 } |
| 305 |
| 306 case this.case_COMMENT: |
| 307 |
| 308 yych = this._charAt(cursor); |
| 309 if (yych <= '\f') { |
| 310 if (yych == '\n') { gotoCase = 4; continue; }; |
| 311 { gotoCase = 3; continue; }; |
| 312 } else { |
| 313 if (yych <= '\r') { gotoCase = 4; continue; }; |
| 314 if (yych == '-') { gotoCase = 6; continue; }; |
| 315 { gotoCase = 3; continue; }; |
| 316 } |
| 317 case 2: |
| 318 { this.tokenType = "html-comment"; return cursor; } |
| 319 case 3: |
| 320 yyaccept = 0; |
| 321 yych = this._charAt(YYMARKER = ++cursor); |
| 322 { gotoCase = 9; continue; }; |
| 323 case 4: |
| 324 ++cursor; |
| 325 case 5: |
| 326 { this.tokenType = null; return cursor; } |
| 327 case 6: |
| 328 yyaccept = 1; |
| 329 yych = this._charAt(YYMARKER = ++cursor); |
| 330 if (yych != '-') { gotoCase = 5; continue; }; |
| 331 case 7: |
| 332 ++cursor; |
| 333 yych = this._charAt(cursor); |
| 334 if (yych == '>') { gotoCase = 10; continue; }; |
| 335 case 8: |
| 336 yyaccept = 0; |
| 337 YYMARKER = ++cursor; |
| 338 yych = this._charAt(cursor); |
| 339 case 9: |
| 340 if (yych <= '\f') { |
| 341 if (yych == '\n') { gotoCase = 2; continue; }; |
| 342 { gotoCase = 8; continue; }; |
| 343 } else { |
| 344 if (yych <= '\r') { gotoCase = 2; continue; }; |
| 345 if (yych == '-') { gotoCase = 12; continue; }; |
| 346 { gotoCase = 8; continue; }; |
| 347 } |
| 348 case 10: |
| 349 ++cursor; |
| 350 this.setLexCondition(this._lexConditions.INITIAL); |
| 351 { this.tokenType = "html-comment"; return cursor; } |
| 352 case 12: |
| 353 ++cursor; |
| 354 yych = this._charAt(cursor); |
| 355 if (yych == '-') { gotoCase = 7; continue; }; |
| 356 cursor = YYMARKER; |
| 357 if (yyaccept <= 0) { |
| 358 { gotoCase = 2; continue; }; |
| 359 } else { |
| 360 { gotoCase = 5; continue; }; |
| 361 } |
| 362 |
| 363 case this.case_DOCTYPE: |
| 364 yych = this._charAt(cursor); |
| 365 if (yych <= '\f') { |
| 366 if (yych == '\n') { gotoCase = 18; continue; }; |
| 367 { gotoCase = 17; continue; }; |
| 368 } else { |
| 369 if (yych <= '\r') { gotoCase = 18; continue; }; |
| 370 if (yych == '>') { gotoCase = 20; continue; }; |
| 371 { gotoCase = 17; continue; }; |
| 372 } |
| 373 case 16: |
| 374 { this.tokenType = "html-doctype"; return cursor; } |
| 375 case 17: |
| 376 yych = this._charAt(++cursor); |
| 377 { gotoCase = 23; continue; }; |
| 378 case 18: |
| 379 ++cursor; |
| 380 { this.tokenType = null; return cursor; } |
| 381 case 20: |
| 382 ++cursor; |
| 383 this.setLexCondition(this._lexConditions.INITIAL); |
| 384 { this.tokenType = "html-doctype"; return cursor; } |
| 385 case 22: |
| 386 ++cursor; |
| 387 yych = this._charAt(cursor); |
| 388 case 23: |
| 389 if (yych <= '\f') { |
| 390 if (yych == '\n') { gotoCase = 16; continue; }; |
| 391 { gotoCase = 22; continue; }; |
| 392 } else { |
| 393 if (yych <= '\r') { gotoCase = 16; continue; }; |
| 394 if (yych == '>') { gotoCase = 16; continue; }; |
| 395 { gotoCase = 22; continue; }; |
| 396 } |
| 397 |
| 398 case this.case_DSTRING: |
| 399 yych = this._charAt(cursor); |
| 400 if (yych <= '\f') { |
| 401 if (yych == '\n') { gotoCase = 28; continue; }; |
| 402 { gotoCase = 27; continue; }; |
| 403 } else { |
| 404 if (yych <= '\r') { gotoCase = 28; continue; }; |
| 405 if (yych == '"') { gotoCase = 30; continue; }; |
| 406 { gotoCase = 27; continue; }; |
| 407 } |
| 408 case 26: |
| 409 { return this._stringToken(cursor); } |
| 410 case 27: |
| 411 yych = this._charAt(++cursor); |
| 412 { gotoCase = 34; continue; }; |
| 413 case 28: |
| 414 ++cursor; |
| 415 { this.tokenType = null; return cursor; } |
| 416 case 30: |
| 417 ++cursor; |
| 418 case 31: |
| 419 this.setLexCondition(this._lexConditions.TAG); |
| 420 { return this._stringToken(cursor, true); } |
| 421 case 32: |
| 422 yych = this._charAt(++cursor); |
| 423 { gotoCase = 31; continue; }; |
| 424 case 33: |
| 425 ++cursor; |
| 426 yych = this._charAt(cursor); |
| 427 case 34: |
| 428 if (yych <= '\f') { |
| 429 if (yych == '\n') { gotoCase = 26; continue; }; |
| 430 { gotoCase = 33; continue; }; |
| 431 } else { |
| 432 if (yych <= '\r') { gotoCase = 26; continue; }; |
| 433 if (yych == '"') { gotoCase = 32; continue; }; |
| 434 { gotoCase = 33; continue; }; |
| 435 } |
| 436 |
| 437 case this.case_INITIAL: |
| 438 yych = this._charAt(cursor); |
| 439 if (yych == '<') { gotoCase = 39; continue; }; |
| 440 ++cursor; |
| 441 { this.tokenType = null; return cursor; } |
| 442 case 39: |
| 443 yyaccept = 0; |
| 444 yych = this._charAt(YYMARKER = ++cursor); |
| 445 if (yych <= '/') { |
| 446 if (yych == '!') { gotoCase = 44; continue; }; |
| 447 if (yych >= '/') { gotoCase = 41; continue; }; |
| 448 } else { |
| 449 if (yych <= 'S') { |
| 450 if (yych >= 'S') { gotoCase = 42; continue; }; |
| 451 } else { |
| 452 if (yych == 's') { gotoCase = 42; continue; }; |
| 453 } |
| 454 } |
| 455 case 40: |
| 456 this.setLexCondition(this._lexConditions.TAG); |
| 457 { |
| 458 if (this._condition.parseCondition & (this._parseConditions.SCRIPT | this._parse
Conditions.STYLE)) { |
| 459 |
| 460 this.setLexCondition(this._lexConditions.INITIAL); |
| 461 this.tokenType = null; |
| 462 return cursor; |
| 463 } |
| 464 |
| 465 this._condition.parseCondition = this._parseConditions.INITIAL; |
| 466 this.tokenType = "html-tag"; |
| 467 return cursor; |
| 468 } |
| 469 case 41: |
| 470 yyaccept = 0; |
| 471 yych = this._charAt(YYMARKER = ++cursor); |
| 472 if (yych == 'S') { gotoCase = 73; continue; }; |
| 473 if (yych == 's') { gotoCase = 73; continue; }; |
| 474 { gotoCase = 40; continue; }; |
| 475 case 42: |
| 476 yych = this._charAt(++cursor); |
| 477 if (yych <= 'T') { |
| 478 if (yych == 'C') { gotoCase = 62; continue; }; |
| 479 if (yych >= 'T') { gotoCase = 63; continue; }; |
| 480 } else { |
| 481 if (yych <= 'c') { |
| 482 if (yych >= 'c') { gotoCase = 62; continue; }; |
| 483 } else { |
| 484 if (yych == 't') { gotoCase = 63; continue; }; |
| 485 } |
| 486 } |
| 487 case 43: |
| 488 cursor = YYMARKER; |
| 489 { gotoCase = 40; continue; }; |
| 490 case 44: |
| 491 yych = this._charAt(++cursor); |
| 492 if (yych <= 'C') { |
| 493 if (yych != '-') { gotoCase = 43; continue; }; |
| 494 } else { |
| 495 if (yych <= 'D') { gotoCase = 46; continue; }; |
| 496 if (yych == 'd') { gotoCase = 46; continue; }; |
| 497 { gotoCase = 43; continue; }; |
| 498 } |
| 499 yych = this._charAt(++cursor); |
| 500 if (yych == '-') { gotoCase = 54; continue; }; |
| 501 { gotoCase = 43; continue; }; |
| 502 case 46: |
| 503 yych = this._charAt(++cursor); |
| 504 if (yych == 'O') { gotoCase = 47; continue; }; |
| 505 if (yych != 'o') { gotoCase = 43; continue; }; |
| 506 case 47: |
| 507 yych = this._charAt(++cursor); |
| 508 if (yych == 'C') { gotoCase = 48; continue; }; |
| 509 if (yych != 'c') { gotoCase = 43; continue; }; |
| 510 case 48: |
| 511 yych = this._charAt(++cursor); |
| 512 if (yych == 'T') { gotoCase = 49; continue; }; |
| 513 if (yych != 't') { gotoCase = 43; continue; }; |
| 514 case 49: |
| 515 yych = this._charAt(++cursor); |
| 516 if (yych == 'Y') { gotoCase = 50; continue; }; |
| 517 if (yych != 'y') { gotoCase = 43; continue; }; |
| 518 case 50: |
| 519 yych = this._charAt(++cursor); |
| 520 if (yych == 'P') { gotoCase = 51; continue; }; |
| 521 if (yych != 'p') { gotoCase = 43; continue; }; |
| 522 case 51: |
| 523 yych = this._charAt(++cursor); |
| 524 if (yych == 'E') { gotoCase = 52; continue; }; |
| 525 if (yych != 'e') { gotoCase = 43; continue; }; |
| 526 case 52: |
| 527 ++cursor; |
| 528 this.setLexCondition(this._lexConditions.DOCTYPE); |
| 529 { this.tokenType = "html-doctype"; return cursor; } |
| 530 case 54: |
| 531 ++cursor; |
| 532 yych = this._charAt(cursor); |
| 533 if (yych <= '\f') { |
| 534 if (yych == '\n') { gotoCase = 57; continue; }; |
| 535 { gotoCase = 54; continue; }; |
| 536 } else { |
| 537 if (yych <= '\r') { gotoCase = 57; continue; }; |
| 538 if (yych != '-') { gotoCase = 54; continue; }; |
| 539 } |
| 540 ++cursor; |
| 541 yych = this._charAt(cursor); |
| 542 if (yych == '-') { gotoCase = 59; continue; }; |
| 543 { gotoCase = 43; continue; }; |
| 544 case 57: |
| 545 ++cursor; |
| 546 this.setLexCondition(this._lexConditions.COMMENT); |
| 547 { this.tokenType = "html-comment"; return cursor; } |
| 548 case 59: |
| 549 ++cursor; |
| 550 yych = this._charAt(cursor); |
| 551 if (yych != '>') { gotoCase = 54; continue; }; |
| 552 ++cursor; |
| 553 { this.tokenType = "html-comment"; return cursor; } |
| 554 case 62: |
| 555 yych = this._charAt(++cursor); |
| 556 if (yych == 'R') { gotoCase = 68; continue; }; |
| 557 if (yych == 'r') { gotoCase = 68; continue; }; |
| 558 { gotoCase = 43; continue; }; |
| 559 case 63: |
| 560 yych = this._charAt(++cursor); |
| 561 if (yych == 'Y') { gotoCase = 64; continue; }; |
| 562 if (yych != 'y') { gotoCase = 43; continue; }; |
| 563 case 64: |
| 564 yych = this._charAt(++cursor); |
| 565 if (yych == 'L') { gotoCase = 65; continue; }; |
| 566 if (yych != 'l') { gotoCase = 43; continue; }; |
| 567 case 65: |
| 568 yych = this._charAt(++cursor); |
| 569 if (yych == 'E') { gotoCase = 66; continue; }; |
| 570 if (yych != 'e') { gotoCase = 43; continue; }; |
| 571 case 66: |
| 572 ++cursor; |
| 573 this.setLexCondition(this._lexConditions.TAG); |
| 574 { |
| 575 if (this._condition.parseCondition & this._parseConditions.STYLE) { |
| 576 |
| 577 this.setLexCondition(this._lexConditions.INITIAL); |
| 578 this.tokenType = null; |
| 579 return cursor; |
| 580 } |
| 581 this.tokenType = "html-tag"; |
| 582 this._condition.parseCondition = this._parseConditions.STYLE; |
| 583 this._setExpectingAttribute(); |
| 584 return cursor; |
| 585 } |
| 586 case 68: |
| 587 yych = this._charAt(++cursor); |
| 588 if (yych == 'I') { gotoCase = 69; continue; }; |
| 589 if (yych != 'i') { gotoCase = 43; continue; }; |
| 590 case 69: |
| 591 yych = this._charAt(++cursor); |
| 592 if (yych == 'P') { gotoCase = 70; continue; }; |
| 593 if (yych != 'p') { gotoCase = 43; continue; }; |
| 594 case 70: |
| 595 yych = this._charAt(++cursor); |
| 596 if (yych == 'T') { gotoCase = 71; continue; }; |
| 597 if (yych != 't') { gotoCase = 43; continue; }; |
| 598 case 71: |
| 599 ++cursor; |
| 600 this.setLexCondition(this._lexConditions.TAG); |
| 601 { |
| 602 if (this._condition.parseCondition & this._parseConditions.SCRIPT) { |
| 603 |
| 604 this.setLexCondition(this._lexConditions.INITIAL); |
| 605 this.tokenType = null; |
| 606 return cursor; |
| 607 } |
| 608 this.tokenType = "html-tag"; |
| 609 this._condition.parseCondition = this._parseConditions.SCRIPT; |
| 610 this._setExpectingAttribute(); |
| 611 return cursor; |
| 612 } |
| 613 case 73: |
| 614 yych = this._charAt(++cursor); |
| 615 if (yych <= 'T') { |
| 616 if (yych == 'C') { gotoCase = 75; continue; }; |
| 617 if (yych <= 'S') { gotoCase = 43; continue; }; |
| 618 } else { |
| 619 if (yych <= 'c') { |
| 620 if (yych <= 'b') { gotoCase = 43; continue; }; |
| 621 { gotoCase = 75; continue; }; |
| 622 } else { |
| 623 if (yych != 't') { gotoCase = 43; continue; }; |
| 624 } |
| 625 } |
| 626 yych = this._charAt(++cursor); |
| 627 if (yych == 'Y') { gotoCase = 81; continue; }; |
| 628 if (yych == 'y') { gotoCase = 81; continue; }; |
| 629 { gotoCase = 43; continue; }; |
| 630 case 75: |
| 631 yych = this._charAt(++cursor); |
| 632 if (yych == 'R') { gotoCase = 76; continue; }; |
| 633 if (yych != 'r') { gotoCase = 43; continue; }; |
| 634 case 76: |
| 635 yych = this._charAt(++cursor); |
| 636 if (yych == 'I') { gotoCase = 77; continue; }; |
| 637 if (yych != 'i') { gotoCase = 43; continue; }; |
| 638 case 77: |
| 639 yych = this._charAt(++cursor); |
| 640 if (yych == 'P') { gotoCase = 78; continue; }; |
| 641 if (yych != 'p') { gotoCase = 43; continue; }; |
| 642 case 78: |
| 643 yych = this._charAt(++cursor); |
| 644 if (yych == 'T') { gotoCase = 79; continue; }; |
| 645 if (yych != 't') { gotoCase = 43; continue; }; |
| 646 case 79: |
| 647 ++cursor; |
| 648 this.setLexCondition(this._lexConditions.TAG); |
| 649 { |
| 650 this.tokenType = "html-tag"; |
| 651 this._condition.parseCondition = this._parseConditions.INITIAL; |
| 652 this.scriptEnded(cursor - 8); |
| 653 return cursor; |
| 654 } |
| 655 case 81: |
| 656 yych = this._charAt(++cursor); |
| 657 if (yych == 'L') { gotoCase = 82; continue; }; |
| 658 if (yych != 'l') { gotoCase = 43; continue; }; |
| 659 case 82: |
| 660 yych = this._charAt(++cursor); |
| 661 if (yych == 'E') { gotoCase = 83; continue; }; |
| 662 if (yych != 'e') { gotoCase = 43; continue; }; |
| 663 case 83: |
| 664 ++cursor; |
| 665 this.setLexCondition(this._lexConditions.TAG); |
| 666 { |
| 667 this.tokenType = "html-tag"; |
| 668 this._condition.parseCondition = this._parseConditions.INITIAL; |
| 669 this.styleSheetEnded(cursor - 7); |
| 670 return cursor; |
| 671 } |
| 672 |
| 673 case this.case_SSTRING: |
| 674 yych = this._charAt(cursor); |
| 675 if (yych <= '\f') { |
| 676 if (yych == '\n') { gotoCase = 89; continue; }; |
| 677 { gotoCase = 88; continue; }; |
| 678 } else { |
| 679 if (yych <= '\r') { gotoCase = 89; continue; }; |
| 680 if (yych == '\'') { gotoCase = 91; continue; }; |
| 681 { gotoCase = 88; continue; }; |
| 682 } |
| 683 case 87: |
| 684 { return this._stringToken(cursor); } |
| 685 case 88: |
| 686 yych = this._charAt(++cursor); |
| 687 { gotoCase = 95; continue; }; |
| 688 case 89: |
| 689 ++cursor; |
| 690 { this.tokenType = null; return cursor; } |
| 691 case 91: |
| 692 ++cursor; |
| 693 case 92: |
| 694 this.setLexCondition(this._lexConditions.TAG); |
| 695 { return this._stringToken(cursor, true); } |
| 696 case 93: |
| 697 yych = this._charAt(++cursor); |
| 698 { gotoCase = 92; continue; }; |
| 699 case 94: |
| 700 ++cursor; |
| 701 yych = this._charAt(cursor); |
| 702 case 95: |
| 703 if (yych <= '\f') { |
| 704 if (yych == '\n') { gotoCase = 87; continue; }; |
| 705 { gotoCase = 94; continue; }; |
| 706 } else { |
| 707 if (yych <= '\r') { gotoCase = 87; continue; }; |
| 708 if (yych == '\'') { gotoCase = 93; continue; }; |
| 709 { gotoCase = 94; continue; }; |
| 710 } |
| 711 |
| 712 case this.case_TAG: |
| 713 yych = this._charAt(cursor); |
| 714 if (yych <= '&') { |
| 715 if (yych <= '\r') { |
| 716 if (yych == '\n') { gotoCase = 100; continue; }; |
| 717 if (yych >= '\r') { gotoCase = 100; continue; }; |
| 718 } else { |
| 719 if (yych <= ' ') { |
| 720 if (yych >= ' ') { gotoCase = 100; continue; }; |
| 721 } else { |
| 722 if (yych == '"') { gotoCase = 102; continue; }; |
| 723 } |
| 724 } |
| 725 } else { |
| 726 if (yych <= '>') { |
| 727 if (yych <= ';') { |
| 728 if (yych <= '\'') { gotoCase = 103; continue; }; |
| 729 } else { |
| 730 if (yych <= '<') { gotoCase = 100; continue; }; |
| 731 if (yych <= '=') { gotoCase = 104; continue; }; |
| 732 { gotoCase = 106; continue; }; |
| 733 } |
| 734 } else { |
| 735 if (yych <= '[') { |
| 736 if (yych >= '[') { gotoCase = 100; continue; }; |
| 737 } else { |
| 738 if (yych == ']') { gotoCase = 100; continue; }; |
| 739 } |
| 740 } |
| 741 } |
| 742 ++cursor; |
| 743 yych = this._charAt(cursor); |
| 744 { gotoCase = 119; continue; }; |
| 745 case 99: |
| 746 { |
| 747 if (this._condition.parseCondition === this._parseConditions.SCRIPT || this._con
dition.parseCondition === this._parseConditions.STYLE) { |
| 748 |
| 749 this.tokenType = null; |
| 750 return cursor; |
| 751 } |
| 752 |
| 753 if (this._condition.parseCondition === this._parseConditions.INITIAL) { |
| 754 this.tokenType = "html-tag"; |
| 755 this._setExpectingAttribute(); |
| 756 var token = this._line.substring(cursorOnEnter, cursor); |
| 757 if (token === "a") |
| 758 this._condition.parseCondition |= this._parseConditions.A_NODE; |
| 759 else if (this._condition.parseCondition & this._parseConditions.A_NODE) |
| 760 this._condition.parseCondition ^= this._parseConditions.A_NODE; |
| 761 } else if (this._isExpectingAttribute()) { |
| 762 var token = this._line.substring(cursorOnEnter, cursor); |
| 763 if (token === "href" || token === "src") |
| 764 this._condition.parseCondition |= this._parseConditions.LINKIFY; |
| 765 else if (this._condition.parseCondition |= this._parseConditions.LINKIFY) |
| 766 this._condition.parseCondition ^= this._parseConditions.LINKIFY; |
| 767 this.tokenType = "html-attribute-name"; |
| 768 } else if (this._isExpectingAttributeValue()) |
| 769 this.tokenType = this._attrValueTokenType(); |
| 770 else |
| 771 this.tokenType = null; |
| 772 return cursor; |
| 773 } |
| 774 case 100: |
| 775 ++cursor; |
| 776 { this.tokenType = null; return cursor; } |
| 777 case 102: |
| 778 yyaccept = 0; |
| 779 yych = this._charAt(YYMARKER = ++cursor); |
| 780 { gotoCase = 115; continue; }; |
| 781 case 103: |
| 782 yyaccept = 0; |
| 783 yych = this._charAt(YYMARKER = ++cursor); |
| 784 { gotoCase = 109; continue; }; |
| 785 case 104: |
| 786 ++cursor; |
| 787 { |
| 788 if (this._isExpectingAttribute()) |
| 789 this._setExpectingAttributeValue(); |
| 790 this.tokenType = null; |
| 791 return cursor; |
| 792 } |
| 793 case 106: |
| 794 ++cursor; |
| 795 this.setLexCondition(this._lexConditions.INITIAL); |
| 796 { |
| 797 this.tokenType = "html-tag"; |
| 798 if (this._condition.parseCondition & this._parseConditions.SCRIPT) { |
| 799 this.scriptStarted(cursor); |
| 800 |
| 801 return cursor; |
| 802 } |
| 803 |
| 804 if (this._condition.parseCondition & this._parseConditions.STYLE) { |
| 805 this.styleSheetStarted(cursor); |
| 806 |
| 807 return cursor; |
| 808 } |
| 809 |
| 810 this._condition.parseCondition = this._parseConditions.INITIAL; |
| 811 return cursor; |
| 812 } |
| 813 case 108: |
| 814 ++cursor; |
| 815 yych = this._charAt(cursor); |
| 816 case 109: |
| 817 if (yych <= '\f') { |
| 818 if (yych != '\n') { gotoCase = 108; continue; }; |
| 819 } else { |
| 820 if (yych <= '\r') { gotoCase = 110; continue; }; |
| 821 if (yych == '\'') { gotoCase = 112; continue; }; |
| 822 { gotoCase = 108; continue; }; |
| 823 } |
| 824 case 110: |
| 825 ++cursor; |
| 826 this.setLexCondition(this._lexConditions.SSTRING); |
| 827 { return this._stringToken(cursor); } |
| 828 case 112: |
| 829 ++cursor; |
| 830 { return this._stringToken(cursor, true); } |
| 831 case 114: |
| 832 ++cursor; |
| 833 yych = this._charAt(cursor); |
| 834 case 115: |
| 835 if (yych <= '\f') { |
| 836 if (yych != '\n') { gotoCase = 114; continue; }; |
| 837 } else { |
| 838 if (yych <= '\r') { gotoCase = 116; continue; }; |
| 839 if (yych == '"') { gotoCase = 112; continue; }; |
| 840 { gotoCase = 114; continue; }; |
| 841 } |
| 842 case 116: |
| 843 ++cursor; |
| 844 this.setLexCondition(this._lexConditions.DSTRING); |
| 845 { return this._stringToken(cursor); } |
| 846 case 118: |
| 847 ++cursor; |
| 848 yych = this._charAt(cursor); |
| 849 case 119: |
| 850 if (yych <= '"') { |
| 851 if (yych <= '\r') { |
| 852 if (yych == '\n') { gotoCase = 99; continue; }; |
| 853 if (yych <= '\f') { gotoCase = 118; continue; }; |
| 854 { gotoCase = 99; continue; }; |
| 855 } else { |
| 856 if (yych == ' ') { gotoCase = 99; continue; }; |
| 857 if (yych <= '!') { gotoCase = 118; continue; }; |
| 858 { gotoCase = 99; continue; }; |
| 859 } |
| 860 } else { |
| 861 if (yych <= '>') { |
| 862 if (yych == '\'') { gotoCase = 99; continue; }; |
| 863 if (yych <= ';') { gotoCase = 118; continue; }; |
| 864 { gotoCase = 99; continue; }; |
| 865 } else { |
| 866 if (yych <= '[') { |
| 867 if (yych <= 'Z') { gotoCase = 118; continue; }; |
| 868 { gotoCase = 99; continue; }; |
| 869 } else { |
| 870 if (yych == ']') { gotoCase = 99; continue; }; |
| 871 { gotoCase = 118; continue; }; |
| 872 } |
| 873 } |
| 874 } |
| 875 } |
| 876 |
| 877 } |
| 878 } |
| 879 } |
| 880 |
| 881 WebInspector.SourceHTMLTokenizer.prototype.__proto__ = WebInspector.SourceTokeni
zer.prototype; |
| 882 ; |
| 883 |
| 884 HTMLScriptFormatter = function() |
| 885 { |
| 886 WebInspector.SourceHTMLTokenizer.call(this); |
| 887 } |
| 888 |
| 889 HTMLScriptFormatter.prototype = { |
| 890 format: function(content) |
| 891 { |
| 892 this.line = content; |
| 893 this._content = content; |
| 894 this._formattedContent = ""; |
| 895 this._mapping = { original: [0], formatted: [0] }; |
| 896 this._position = 0; |
| 897 |
| 898 var cursor = 0; |
| 899 while (cursor < this._content.length) |
| 900 cursor = this.nextToken(cursor); |
| 901 |
| 902 this._formattedContent += this._content.substring(this._position); |
| 903 return { content: this._formattedContent, mapping: this._mapping }; |
| 904 }, |
| 905 |
| 906 scriptStarted: function(cursor) |
| 907 { |
| 908 this._formattedContent += this._content.substring(this._position, cursor); |
| 909 this._formattedContent += "\n"; |
| 910 this._position = cursor; |
| 911 }, |
| 912 |
| 913 scriptEnded: function(cursor) |
| 914 { |
| 915 if (cursor === this._position) |
| 916 return; |
| 917 |
| 918 var scriptContent = this._content.substring(this._position, cursor); |
| 919 var formattedScriptContent = formatScript(scriptContent, this._mapping, this._po
sition, this._formattedContent.length); |
| 920 |
| 921 this._formattedContent += formattedScriptContent; |
| 922 this._position = cursor; |
| 923 }, |
| 924 |
| 925 styleSheetStarted: function(cursor) |
| 926 { |
| 927 }, |
| 928 |
| 929 styleSheetEnded: function(cursor) |
| 930 { |
| 931 } |
| 932 } |
| 933 |
| 934 HTMLScriptFormatter.prototype.__proto__ = WebInspector.SourceHTMLTokenizer.proto
type; |
| 935 |
| 936 function require() |
| 937 { |
| 938 return parse; |
| 939 } |
| 940 |
| 941 var exports = {}; |
| 942 |
| 943 |
| 944 |
| 945 |
| 946 var KEYWORDS = array_to_hash([ |
| 947 "break", |
| 948 "case", |
| 949 "catch", |
| 950 "const", |
| 951 "continue", |
| 952 "default", |
| 953 "delete", |
| 954 "do", |
| 955 "else", |
| 956 "finally", |
| 957 "for", |
| 958 "function", |
| 959 "if", |
| 960 "in", |
| 961 "instanceof", |
| 962 "new", |
| 963 "return", |
| 964 "switch", |
| 965 "throw", |
| 966 "try", |
| 967 "typeof", |
| 968 "var", |
| 969 "void", |
| 970 "while", |
| 971 "with" |
| 972 ]); |
| 973 |
| 974 var RESERVED_WORDS = array_to_hash([ |
| 975 "abstract", |
| 976 "boolean", |
| 977 "byte", |
| 978 "char", |
| 979 "class", |
| 980 "debugger", |
| 981 "double", |
| 982 "enum", |
| 983 "export", |
| 984 "extends", |
| 985 "final", |
| 986 "float", |
| 987 "goto", |
| 988 "implements", |
| 989 "import", |
| 990 "int", |
| 991 "interface", |
| 992 "long", |
| 993 "native", |
| 994 "package", |
| 995 "private", |
| 996 "protected", |
| 997 "public", |
| 998 "short", |
| 999 "static", |
| 1000 "super", |
| 1001 "synchronized", |
| 1002 "throws", |
| 1003 "transient", |
| 1004 "volatile" |
| 1005 ]); |
| 1006 |
| 1007 var KEYWORDS_BEFORE_EXPRESSION = array_to_hash([ |
| 1008 "return", |
| 1009 "new", |
| 1010 "delete", |
| 1011 "throw", |
| 1012 "else", |
| 1013 "case" |
| 1014 ]); |
| 1015 |
| 1016 var KEYWORDS_ATOM = array_to_hash([ |
| 1017 "false", |
| 1018 "null", |
| 1019 "true", |
| 1020 "undefined" |
| 1021 ]); |
| 1022 |
| 1023 var OPERATOR_CHARS = array_to_hash(characters("+-*&%=<>!?|~^")); |
| 1024 |
| 1025 var RE_HEX_NUMBER = /^0x[0-9a-f]+$/i; |
| 1026 var RE_OCT_NUMBER = /^0[0-7]+$/; |
| 1027 var RE_DEC_NUMBER = /^\d*\.?\d*(?:e[+-]?\d*(?:\d\.?|\.?\d)\d*)?$/i; |
| 1028 |
| 1029 var OPERATORS = array_to_hash([ |
| 1030 "in", |
| 1031 "instanceof", |
| 1032 "typeof", |
| 1033 "new", |
| 1034 "void", |
| 1035 "delete", |
| 1036 "++", |
| 1037 "--", |
| 1038 "+", |
| 1039 "-", |
| 1040 "!", |
| 1041 "~", |
| 1042 "&", |
| 1043 "|", |
| 1044 "^", |
| 1045 "*", |
| 1046 "/", |
| 1047 "%", |
| 1048 ">>", |
| 1049 "<<", |
| 1050 ">>>", |
| 1051 "<", |
| 1052 ">", |
| 1053 "<=", |
| 1054 ">=", |
| 1055 "==", |
| 1056 "===", |
| 1057 "!=", |
| 1058 "!==", |
| 1059 "?", |
| 1060 "=", |
| 1061 "+=", |
| 1062 "-=", |
| 1063 "/=", |
| 1064 "*=", |
| 1065 "%=", |
| 1066 ">>=", |
| 1067 "<<=", |
| 1068 ">>>=", |
| 1069 "%=", |
| 1070 "|=", |
| 1071 "^=", |
| 1072 "&=", |
| 1073 "&&", |
| 1074 "||" |
| 1075 ]); |
| 1076 |
| 1077 var WHITESPACE_CHARS = array_to_hash(characters(" \n\r\t")); |
| 1078 |
| 1079 var PUNC_BEFORE_EXPRESSION = array_to_hash(characters("[{}(,.;:")); |
| 1080 |
| 1081 var PUNC_CHARS = array_to_hash(characters("[]{}(),;:")); |
| 1082 |
| 1083 var REGEXP_MODIFIERS = array_to_hash(characters("gmsiy")); |
| 1084 |
| 1085 |
| 1086 |
| 1087 function is_alphanumeric_char(ch) { |
| 1088 ch = ch.charCodeAt(0); |
| 1089 return (ch >= 48 && ch <= 57) || |
| 1090 (ch >= 65 && ch <= 90) || |
| 1091 (ch >= 97 && ch <= 122); |
| 1092 }; |
| 1093 |
| 1094 function is_identifier_char(ch) { |
| 1095 return is_alphanumeric_char(ch) || ch == "$" || ch == "_"; |
| 1096 }; |
| 1097 |
| 1098 function is_digit(ch) { |
| 1099 ch = ch.charCodeAt(0); |
| 1100 return ch >= 48 && ch <= 57; |
| 1101 }; |
| 1102 |
| 1103 function parse_js_number(num) { |
| 1104 if (RE_HEX_NUMBER.test(num)) { |
| 1105 return parseInt(num.substr(2), 16); |
| 1106 } else if (RE_OCT_NUMBER.test(num)) { |
| 1107 return parseInt(num.substr(1), 8); |
| 1108 } else if (RE_DEC_NUMBER.test(num)) { |
| 1109 return parseFloat(num); |
| 1110 } |
| 1111 }; |
| 1112 |
| 1113 function JS_Parse_Error(message, line, col, pos) { |
| 1114 this.message = message; |
| 1115 this.line = line; |
| 1116 this.col = col; |
| 1117 this.pos = pos; |
| 1118 try { |
| 1119 ({})(); |
| 1120 } catch(ex) { |
| 1121 this.stack = ex.stack; |
| 1122 }; |
| 1123 }; |
| 1124 |
| 1125 JS_Parse_Error.prototype.toString = function() { |
| 1126 return this.message + " (line: " + this.line + ", col: " + this.col + ", pos: "
+ this.pos + ")" + "\n\n" + this.stack; |
| 1127 }; |
| 1128 |
| 1129 function js_error(message, line, col, pos) { |
| 1130 throw new JS_Parse_Error(message, line, col, pos); |
| 1131 }; |
| 1132 |
| 1133 function is_token(token, type, val) { |
| 1134 return token.type == type && (val == null || token.value == val); |
| 1135 }; |
| 1136 |
| 1137 var EX_EOF = {}; |
| 1138 |
| 1139 function tokenizer($TEXT) { |
| 1140 |
| 1141 var S = { |
| 1142 text : $TEXT.replace(/\r\n?|[\n\u2028\u2029]/g, "\n").replace(/^\uFEF
F/, ''), |
| 1143 pos : 0, |
| 1144 tokpos : 0, |
| 1145 line : 0, |
| 1146 tokline : 0, |
| 1147 col : 0, |
| 1148 tokcol : 0, |
| 1149 newline_before : false, |
| 1150 regex_allowed : false, |
| 1151 comments_before : [] |
| 1152 }; |
| 1153 |
| 1154 function peek() { return S.text.charAt(S.pos); }; |
| 1155 |
| 1156 function next(signal_eof) { |
| 1157 var ch = S.text.charAt(S.pos++); |
| 1158 if (signal_eof && !ch) |
| 1159 throw EX_EOF; |
| 1160 if (ch == "\n") { |
| 1161 S.newline_before = true; |
| 1162 ++S.line; |
| 1163 S.col = 0; |
| 1164 } else { |
| 1165 ++S.col; |
| 1166 } |
| 1167 return ch; |
| 1168 }; |
| 1169 |
| 1170 function eof() { |
| 1171 return !S.peek(); |
| 1172 }; |
| 1173 |
| 1174 function find(what, signal_eof) { |
| 1175 var pos = S.text.indexOf(what, S.pos); |
| 1176 if (signal_eof && pos == -1) throw EX_EOF; |
| 1177 return pos; |
| 1178 }; |
| 1179 |
| 1180 function start_token() { |
| 1181 S.tokline = S.line; |
| 1182 S.tokcol = S.col; |
| 1183 S.tokpos = S.pos; |
| 1184 }; |
| 1185 |
| 1186 function token(type, value, is_comment) { |
| 1187 S.regex_allowed = ((type == "operator" && !HOP(UNARY_POSTFIX, value)) || |
| 1188 (type == "keyword" && HOP(KEYWORDS_BEFORE_EXPRESSION, value)) || |
| 1189 (type == "punc" && HOP(PUNC_BEFORE_EXPRESSION, value))); |
| 1190 var ret = { |
| 1191 type : type, |
| 1192 value : value, |
| 1193 line : S.tokline, |
| 1194 col : S.tokcol, |
| 1195 pos : S.tokpos, |
| 1196 nlb : S.newline_before |
| 1197 }; |
| 1198 if (!is_comment) { |
| 1199 ret.comments_before = S.comments_before; |
| 1200 S.comments_before = []; |
| 1201 } |
| 1202 S.newline_before = false; |
| 1203 return ret; |
| 1204 }; |
| 1205 |
| 1206 function skip_whitespace() { |
| 1207 while (HOP(WHITESPACE_CHARS, peek())) |
| 1208 next(); |
| 1209 }; |
| 1210 |
| 1211 function read_while(pred) { |
| 1212 var ret = "", ch = peek(), i = 0; |
| 1213 while (ch && pred(ch, i++)) { |
| 1214 ret += next(); |
| 1215 ch = peek(); |
| 1216 } |
| 1217 return ret; |
| 1218 }; |
| 1219 |
| 1220 function parse_error(err) { |
| 1221 js_error(err, S.tokline, S.tokcol, S.tokpos); |
| 1222 }; |
| 1223 |
| 1224 function read_num(prefix) { |
| 1225 var has_e = false, after_e = false, has_x = false, has_dot = prefix == "."; |
| 1226 var num = read_while(function(ch, i){ |
| 1227 if (ch == "x" || ch == "X") { |
| 1228 if (has_x) return false; |
| 1229 return has_x = true; |
| 1230 } |
| 1231 if (!has_x && (ch == "E" || ch == "e")) { |
| 1232 if (has_e) return false; |
| 1233 return has_e = after_e = true; |
| 1234 } |
| 1235 if (ch == "-") { |
| 1236 if (after_e || (i == 0 && !prefix)) return true; |
| 1237 return false; |
| 1238 } |
| 1239 if (ch == "+") return after_e; |
| 1240 after_e = false; |
| 1241 if (ch == ".") { |
| 1242 if (!has_dot) |
| 1243 return has_dot = true; |
| 1244 return false; |
| 1245 } |
| 1246 return is_alphanumeric_char(ch); |
| 1247 }); |
| 1248 if (prefix) |
| 1249 num = prefix + num; |
| 1250 var valid = parse_js_number(num); |
| 1251 if (!isNaN(valid)) { |
| 1252 return token("num", valid); |
| 1253 } else { |
| 1254 parse_error("Invalid syntax: " + num); |
| 1255 } |
| 1256 }; |
| 1257 |
| 1258 function read_escaped_char() { |
| 1259 var ch = next(true); |
| 1260 switch (ch) { |
| 1261 case "n" : return "\n"; |
| 1262 case "r" : return "\r"; |
| 1263 case "t" : return "\t"; |
| 1264 case "b" : return "\b"; |
| 1265 case "v" : return "\v"; |
| 1266 case "f" : return "\f"; |
| 1267 case "0" : return "\0"; |
| 1268 case "x" : return String.fromCharCode(hex_bytes(2)); |
| 1269 case "u" : return String.fromCharCode(hex_bytes(4)); |
| 1270 default : return ch; |
| 1271 } |
| 1272 }; |
| 1273 |
| 1274 function hex_bytes(n) { |
| 1275 var num = 0; |
| 1276 for (; n > 0; --n) { |
| 1277 var digit = parseInt(next(true), 16); |
| 1278 if (isNaN(digit)) |
| 1279 parse_error("Invalid hex-character pattern in string"); |
| 1280 num = (num << 4) | digit; |
| 1281 } |
| 1282 return num; |
| 1283 }; |
| 1284 |
| 1285 function read_string() { |
| 1286 return with_eof_error("Unterminated string constant", function(){ |
| 1287 var quote = next(), ret = ""; |
| 1288 for (;;) { |
| 1289 var ch = next(true); |
| 1290 if (ch == "\\") ch = read_escaped_char(); |
| 1291 else if (ch == quote) break; |
| 1292 ret += ch; |
| 1293 } |
| 1294 return token("string", ret); |
| 1295 }); |
| 1296 }; |
| 1297 |
| 1298 function read_line_comment() { |
| 1299 next(); |
| 1300 var i = find("\n"), ret; |
| 1301 if (i == -1) { |
| 1302 ret = S.text.substr(S.pos); |
| 1303 S.pos = S.text.length; |
| 1304 } else { |
| 1305 ret = S.text.substring(S.pos, i); |
| 1306 S.pos = i; |
| 1307 } |
| 1308 return token("comment1", ret, true); |
| 1309 }; |
| 1310 |
| 1311 function read_multiline_comment() { |
| 1312 next(); |
| 1313 return with_eof_error("Unterminated multiline comment", function(){ |
| 1314 var i = find("*/", true), |
| 1315 text = S.text.substring(S.pos, i), |
| 1316 tok = token("comment2", text, true); |
| 1317 S.pos = i + 2; |
| 1318 S.line += text.split("\n").length - 1; |
| 1319 S.newline_before = text.indexOf("\n") >= 0; |
| 1320 return tok; |
| 1321 }); |
| 1322 }; |
| 1323 |
| 1324 function read_regexp() { |
| 1325 return with_eof_error("Unterminated regular expression", function(){ |
| 1326 var prev_backslash = false, regexp = "", ch, in_class = false; |
| 1327 while ((ch = next(true))) if (prev_backslash) { |
| 1328 regexp += "\\" + ch; |
| 1329 prev_backslash = false; |
| 1330 } else if (ch == "[") { |
| 1331 in_class = true; |
| 1332 regexp += ch; |
| 1333 } else if (ch == "]" && in_class) { |
| 1334 in_class = false; |
| 1335 regexp += ch; |
| 1336 } else if (ch == "/" && !in_class) { |
| 1337 break; |
| 1338 } else if (ch == "\\") { |
| 1339 prev_backslash = true; |
| 1340 } else { |
| 1341 regexp += ch; |
| 1342 } |
| 1343 var mods = read_while(function(ch){ |
| 1344 return HOP(REGEXP_MODIFIERS, ch); |
| 1345 }); |
| 1346 return token("regexp", [ regexp, mods ]); |
| 1347 }); |
| 1348 }; |
| 1349 |
| 1350 function read_operator(prefix) { |
| 1351 function grow(op) { |
| 1352 if (!peek()) return op; |
| 1353 var bigger = op + peek(); |
| 1354 if (HOP(OPERATORS, bigger)) { |
| 1355 next(); |
| 1356 return grow(bigger); |
| 1357 } else { |
| 1358 return op; |
| 1359 } |
| 1360 }; |
| 1361 return token("operator", grow(prefix || next())); |
| 1362 }; |
| 1363 |
| 1364 function handle_slash() { |
| 1365 next(); |
| 1366 var regex_allowed = S.regex_allowed; |
| 1367 switch (peek()) { |
| 1368 case "/": |
| 1369 S.comments_before.push(read_line_comment()); |
| 1370 S.regex_allowed = regex_allowed; |
| 1371 return next_token(); |
| 1372 case "*": |
| 1373 S.comments_before.push(read_multiline_comment()); |
| 1374 S.regex_allowed = regex_allowed; |
| 1375 return next_token(); |
| 1376 } |
| 1377 return S.regex_allowed ? read_regexp() : read_operator("/"); |
| 1378 }; |
| 1379 |
| 1380 function handle_dot() { |
| 1381 next(); |
| 1382 return is_digit(peek()) |
| 1383 ? read_num(".") |
| 1384 : token("punc", "."); |
| 1385 }; |
| 1386 |
| 1387 function read_word() { |
| 1388 var word = read_while(is_identifier_char); |
| 1389 return !HOP(KEYWORDS, word) |
| 1390 ? token("name", word) |
| 1391 : HOP(OPERATORS, word) |
| 1392 ? token("operator", word) |
| 1393 : HOP(KEYWORDS_ATOM, word) |
| 1394 ? token("atom", word) |
| 1395 : token("keyword", word); |
| 1396 }; |
| 1397 |
| 1398 function with_eof_error(eof_error, cont) { |
| 1399 try { |
| 1400 return cont(); |
| 1401 } catch(ex) { |
| 1402 if (ex === EX_EOF) parse_error(eof_error); |
| 1403 else throw ex; |
| 1404 } |
| 1405 }; |
| 1406 |
| 1407 function next_token(force_regexp) { |
| 1408 if (force_regexp) |
| 1409 return read_regexp(); |
| 1410 skip_whitespace(); |
| 1411 start_token(); |
| 1412 var ch = peek(); |
| 1413 if (!ch) return token("eof"); |
| 1414 if (is_digit(ch)) return read_num(); |
| 1415 if (ch == '"' || ch == "'") return read_string(); |
| 1416 if (HOP(PUNC_CHARS, ch)) return token("punc", next()); |
| 1417 if (ch == ".") return handle_dot(); |
| 1418 if (ch == "/") return handle_slash(); |
| 1419 if (HOP(OPERATOR_CHARS, ch)) return read_operator(); |
| 1420 if (is_identifier_char(ch)) return read_word(); |
| 1421 parse_error("Unexpected character '" + ch + "'"); |
| 1422 }; |
| 1423 |
| 1424 next_token.context = function(nc) { |
| 1425 if (nc) S = nc; |
| 1426 return S; |
| 1427 }; |
| 1428 |
| 1429 return next_token; |
| 1430 |
| 1431 }; |
| 1432 |
| 1433 |
| 1434 |
| 1435 var UNARY_PREFIX = array_to_hash([ |
| 1436 "typeof", |
| 1437 "void", |
| 1438 "delete", |
| 1439 "--", |
| 1440 "++", |
| 1441 "!", |
| 1442 "~", |
| 1443 "-", |
| 1444 "+" |
| 1445 ]); |
| 1446 |
| 1447 var UNARY_POSTFIX = array_to_hash([ "--", "++" ]); |
| 1448 |
| 1449 var ASSIGNMENT = (function(a, ret, i){ |
| 1450 while (i < a.length) { |
| 1451 ret[a[i]] = a[i].substr(0, a[i].length - 1); |
| 1452 i++; |
| 1453 } |
| 1454 return ret; |
| 1455 })( |
| 1456 ["+=", "-=", "/=", "*=", "%=", ">>=", "<<=", ">>>=", "|=", "^=", "&="], |
| 1457 { "=": true }, |
| 1458 0 |
| 1459 ); |
| 1460 |
| 1461 var PRECEDENCE = (function(a, ret){ |
| 1462 for (var i = 0, n = 1; i < a.length; ++i, ++n) { |
| 1463 var b = a[i]; |
| 1464 for (var j = 0; j < b.length; ++j) { |
| 1465 ret[b[j]] = n; |
| 1466 } |
| 1467 } |
| 1468 return ret; |
| 1469 })( |
| 1470 [ |
| 1471 ["||"], |
| 1472 ["&&"], |
| 1473 ["|"], |
| 1474 ["^"], |
| 1475 ["&"], |
| 1476 ["==", "===", "!=", "!=="], |
| 1477 ["<", ">", "<=", ">=", "in", "instanceof"], |
| 1478 [">>", "<<", ">>>"], |
| 1479 ["+", "-"], |
| 1480 ["*", "/", "%"] |
| 1481 ], |
| 1482 {} |
| 1483 ); |
| 1484 |
| 1485 var STATEMENTS_WITH_LABELS = array_to_hash([ "for", "do", "while", "switch" ]); |
| 1486 |
| 1487 var ATOMIC_START_TOKEN = array_to_hash([ "atom", "num", "string", "regexp", "nam
e" ]); |
| 1488 |
| 1489 |
| 1490 |
| 1491 function NodeWithToken(str, start, end) { |
| 1492 this.name = str; |
| 1493 this.start = start; |
| 1494 this.end = end; |
| 1495 }; |
| 1496 |
| 1497 NodeWithToken.prototype.toString = function() { return this.name; }; |
| 1498 |
| 1499 function parse($TEXT, strict_mode, embed_tokens) { |
| 1500 |
| 1501 var S = { |
| 1502 input : typeof $TEXT == "string" ? tokenizer($TEXT, true) : $TEXT, |
| 1503 token : null, |
| 1504 prev : null, |
| 1505 peeked : null, |
| 1506 in_function : 0, |
| 1507 in_loop : 0, |
| 1508 labels : [] |
| 1509 }; |
| 1510 |
| 1511 S.token = next(); |
| 1512 |
| 1513 function is(type, value) { |
| 1514 return is_token(S.token, type, value); |
| 1515 }; |
| 1516 |
| 1517 function peek() { return S.peeked || (S.peeked = S.input()); }; |
| 1518 |
| 1519 function next() { |
| 1520 S.prev = S.token; |
| 1521 if (S.peeked) { |
| 1522 S.token = S.peeked; |
| 1523 S.peeked = null; |
| 1524 } else { |
| 1525 S.token = S.input(); |
| 1526 } |
| 1527 return S.token; |
| 1528 }; |
| 1529 |
| 1530 function prev() { |
| 1531 return S.prev; |
| 1532 }; |
| 1533 |
| 1534 function croak(msg, line, col, pos) { |
| 1535 var ctx = S.input.context(); |
| 1536 js_error(msg, |
| 1537 line != null ? line : ctx.tokline, |
| 1538 col != null ? col : ctx.tokcol, |
| 1539 pos != null ? pos : ctx.tokpos); |
| 1540 }; |
| 1541 |
| 1542 function token_error(token, msg) { |
| 1543 croak(msg, token.line, token.col); |
| 1544 }; |
| 1545 |
| 1546 function unexpected(token) { |
| 1547 if (token == null) |
| 1548 token = S.token; |
| 1549 token_error(token, "Unexpected token: " + token.type + " (" + token.value + ")")
; |
| 1550 }; |
| 1551 |
| 1552 function expect_token(type, val) { |
| 1553 if (is(type, val)) { |
| 1554 return next(); |
| 1555 } |
| 1556 token_error(S.token, "Unexpected token " + S.token.type + ", expected " + type); |
| 1557 }; |
| 1558 |
| 1559 function expect(punc) { return expect_token("punc", punc); }; |
| 1560 |
| 1561 function can_insert_semicolon() { |
| 1562 return !strict_mode && ( |
| 1563 S.token.nlb || is("eof") || is("punc", "}") |
| 1564 ); |
| 1565 }; |
| 1566 |
| 1567 function semicolon() { |
| 1568 if (is("punc", ";")) next(); |
| 1569 else if (!can_insert_semicolon()) unexpected(); |
| 1570 }; |
| 1571 |
| 1572 function as() { |
| 1573 return slice(arguments); |
| 1574 }; |
| 1575 |
| 1576 function parenthesised() { |
| 1577 expect("("); |
| 1578 var ex = expression(); |
| 1579 expect(")"); |
| 1580 return ex; |
| 1581 }; |
| 1582 |
| 1583 function add_tokens(str, start, end) { |
| 1584 return new NodeWithToken(str, start, end); |
| 1585 }; |
| 1586 |
| 1587 var statement = embed_tokens ? function() { |
| 1588 var start = S.token; |
| 1589 var stmt = $statement(); |
| 1590 stmt[0] = add_tokens(stmt[0], start, prev()); |
| 1591 return stmt; |
| 1592 } : $statement; |
| 1593 |
| 1594 function $statement() { |
| 1595 if (is("operator", "/")) { |
| 1596 S.peeked = null; |
| 1597 S.token = S.input(true); |
| 1598 } |
| 1599 switch (S.token.type) { |
| 1600 case "num": |
| 1601 case "string": |
| 1602 case "regexp": |
| 1603 case "operator": |
| 1604 case "atom": |
| 1605 return simple_statement(); |
| 1606 |
| 1607 case "name": |
| 1608 return is_token(peek(), "punc", ":") |
| 1609 ? labeled_statement(prog1(S.token.value, next, next)) |
| 1610 : simple_statement(); |
| 1611 |
| 1612 case "punc": |
| 1613 switch (S.token.value) { |
| 1614 case "{": |
| 1615 return as("block", block_()); |
| 1616 case "[": |
| 1617 case "(": |
| 1618 return simple_statement(); |
| 1619 case ";": |
| 1620 next(); |
| 1621 return as("block"); |
| 1622 default: |
| 1623 unexpected(); |
| 1624 } |
| 1625 |
| 1626 case "keyword": |
| 1627 switch (prog1(S.token.value, next)) { |
| 1628 case "break": |
| 1629 return break_cont("break"); |
| 1630 |
| 1631 case "continue": |
| 1632 return break_cont("continue"); |
| 1633 |
| 1634 case "debugger": |
| 1635 semicolon(); |
| 1636 return as("debugger"); |
| 1637 |
| 1638 case "do": |
| 1639 return (function(body){ |
| 1640 expect_token("keyword", "while"); |
| 1641 return as("do", prog1(parenthesised, semicolon), body); |
| 1642 })(in_loop(statement)); |
| 1643 |
| 1644 case "for": |
| 1645 return for_(); |
| 1646 |
| 1647 case "function": |
| 1648 return function_(true); |
| 1649 |
| 1650 case "if": |
| 1651 return if_(); |
| 1652 |
| 1653 case "return": |
| 1654 if (S.in_function == 0) |
| 1655 croak("'return' outside of function"); |
| 1656 return as("return", |
| 1657 is("punc", ";") |
| 1658 ? (next(), null) |
| 1659 : can_insert_semicolon() |
| 1660 ? null |
| 1661 : prog1(expression, semicolon)); |
| 1662 |
| 1663 case "switch": |
| 1664 return as("switch", parenthesised(), switch_block_()); |
| 1665 |
| 1666 case "throw": |
| 1667 return as("throw", prog1(expression, semicolon)); |
| 1668 |
| 1669 case "try": |
| 1670 return try_(); |
| 1671 |
| 1672 case "var": |
| 1673 return prog1(var_, semicolon); |
| 1674 |
| 1675 case "const": |
| 1676 return prog1(const_, semicolon); |
| 1677 |
| 1678 case "while": |
| 1679 return as("while", parenthesised(), in_loop(statement)); |
| 1680 |
| 1681 case "with": |
| 1682 return as("with", parenthesised(), statement()); |
| 1683 |
| 1684 default: |
| 1685 unexpected(); |
| 1686 } |
| 1687 } |
| 1688 }; |
| 1689 |
| 1690 function labeled_statement(label) { |
| 1691 S.labels.push(label); |
| 1692 var start = S.token, stat = statement(); |
| 1693 if (strict_mode && !HOP(STATEMENTS_WITH_LABELS, stat[0])) |
| 1694 unexpected(start); |
| 1695 S.labels.pop(); |
| 1696 return as("label", label, stat); |
| 1697 }; |
| 1698 |
| 1699 function simple_statement() { |
| 1700 return as("stat", prog1(expression, semicolon)); |
| 1701 }; |
| 1702 |
| 1703 function break_cont(type) { |
| 1704 var name = is("name") ? S.token.value : null; |
| 1705 if (name != null) { |
| 1706 next(); |
| 1707 if (!member(name, S.labels)) |
| 1708 croak("Label " + name + " without matching loop or statement"); |
| 1709 } |
| 1710 else if (S.in_loop == 0) |
| 1711 croak(type + " not inside a loop or switch"); |
| 1712 semicolon(); |
| 1713 return as(type, name); |
| 1714 }; |
| 1715 |
| 1716 function for_() { |
| 1717 expect("("); |
| 1718 var has_var = is("keyword", "var"); |
| 1719 if (has_var) |
| 1720 next(); |
| 1721 if (is("name") && is_token(peek(), "operator", "in")) { |
| 1722 |
| 1723 var name = S.token.value; |
| 1724 next(); next(); |
| 1725 var obj = expression(); |
| 1726 expect(")"); |
| 1727 return as("for-in", has_var, name, obj, in_loop(statement)); |
| 1728 } else { |
| 1729 |
| 1730 var init = is("punc", ";") ? null : has_var ? var_() : expression(); |
| 1731 expect(";"); |
| 1732 var test = is("punc", ";") ? null : expression(); |
| 1733 expect(";"); |
| 1734 var step = is("punc", ")") ? null : expression(); |
| 1735 expect(")"); |
| 1736 return as("for", init, test, step, in_loop(statement)); |
| 1737 } |
| 1738 }; |
| 1739 |
| 1740 function function_(in_statement) { |
| 1741 var name = is("name") ? prog1(S.token.value, next) : null; |
| 1742 if (in_statement && !name) |
| 1743 unexpected(); |
| 1744 expect("("); |
| 1745 return as(in_statement ? "defun" : "function", |
| 1746 name, |
| 1747 |
| 1748 (function(first, a){ |
| 1749 while (!is("punc", ")")) { |
| 1750 if (first) first = false; else expect(","); |
| 1751 if (!is("name")) unexpected(); |
| 1752 a.push(S.token.value); |
| 1753 next(); |
| 1754 } |
| 1755 next(); |
| 1756 return a; |
| 1757 })(true, []), |
| 1758 |
| 1759 (function(){ |
| 1760 ++S.in_function; |
| 1761 var loop = S.in_loop; |
| 1762 S.in_loop = 0; |
| 1763 var a = block_(); |
| 1764 --S.in_function; |
| 1765 S.in_loop = loop; |
| 1766 return a; |
| 1767 })()); |
| 1768 }; |
| 1769 |
| 1770 function if_() { |
| 1771 var cond = parenthesised(), body = statement(), belse; |
| 1772 if (is("keyword", "else")) { |
| 1773 next(); |
| 1774 belse = statement(); |
| 1775 } |
| 1776 return as("if", cond, body, belse); |
| 1777 }; |
| 1778 |
| 1779 function block_() { |
| 1780 expect("{"); |
| 1781 var a = []; |
| 1782 while (!is("punc", "}")) { |
| 1783 if (is("eof")) unexpected(); |
| 1784 a.push(statement()); |
| 1785 } |
| 1786 next(); |
| 1787 return a; |
| 1788 }; |
| 1789 |
| 1790 var switch_block_ = curry(in_loop, function(){ |
| 1791 expect("{"); |
| 1792 var a = [], cur = null; |
| 1793 while (!is("punc", "}")) { |
| 1794 if (is("eof")) unexpected(); |
| 1795 if (is("keyword", "case")) { |
| 1796 next(); |
| 1797 cur = []; |
| 1798 a.push([ expression(), cur ]); |
| 1799 expect(":"); |
| 1800 } |
| 1801 else if (is("keyword", "default")) { |
| 1802 next(); |
| 1803 expect(":"); |
| 1804 cur = []; |
| 1805 a.push([ null, cur ]); |
| 1806 } |
| 1807 else { |
| 1808 if (!cur) unexpected(); |
| 1809 cur.push(statement()); |
| 1810 } |
| 1811 } |
| 1812 next(); |
| 1813 return a; |
| 1814 }); |
| 1815 |
| 1816 function try_() { |
| 1817 var body = block_(), bcatch, bfinally; |
| 1818 if (is("keyword", "catch")) { |
| 1819 next(); |
| 1820 expect("("); |
| 1821 if (!is("name")) |
| 1822 croak("Name expected"); |
| 1823 var name = S.token.value; |
| 1824 next(); |
| 1825 expect(")"); |
| 1826 bcatch = [ name, block_() ]; |
| 1827 } |
| 1828 if (is("keyword", "finally")) { |
| 1829 next(); |
| 1830 bfinally = block_(); |
| 1831 } |
| 1832 if (!bcatch && !bfinally) |
| 1833 croak("Missing catch/finally blocks"); |
| 1834 return as("try", body, bcatch, bfinally); |
| 1835 }; |
| 1836 |
| 1837 function vardefs() { |
| 1838 var a = []; |
| 1839 for (;;) { |
| 1840 if (!is("name")) |
| 1841 unexpected(); |
| 1842 var name = S.token.value; |
| 1843 next(); |
| 1844 if (is("operator", "=")) { |
| 1845 next(); |
| 1846 a.push([ name, expression(false) ]); |
| 1847 } else { |
| 1848 a.push([ name ]); |
| 1849 } |
| 1850 if (!is("punc", ",")) |
| 1851 break; |
| 1852 next(); |
| 1853 } |
| 1854 return a; |
| 1855 }; |
| 1856 |
| 1857 function var_() { |
| 1858 return as("var", vardefs()); |
| 1859 }; |
| 1860 |
| 1861 function const_() { |
| 1862 return as("const", vardefs()); |
| 1863 }; |
| 1864 |
| 1865 function new_() { |
| 1866 var newexp = expr_atom(false), args; |
| 1867 if (is("punc", "(")) { |
| 1868 next(); |
| 1869 args = expr_list(")"); |
| 1870 } else { |
| 1871 args = []; |
| 1872 } |
| 1873 return subscripts(as("new", newexp, args), true); |
| 1874 }; |
| 1875 |
| 1876 function expr_atom(allow_calls) { |
| 1877 if (is("operator", "new")) { |
| 1878 next(); |
| 1879 return new_(); |
| 1880 } |
| 1881 if (is("operator") && HOP(UNARY_PREFIX, S.token.value)) { |
| 1882 return make_unary("unary-prefix", |
| 1883 prog1(S.token.value, next), |
| 1884 expr_atom(allow_calls)); |
| 1885 } |
| 1886 if (is("punc")) { |
| 1887 switch (S.token.value) { |
| 1888 case "(": |
| 1889 next(); |
| 1890 return subscripts(prog1(expression, curry(expect, ")")), allow_calls); |
| 1891 case "[": |
| 1892 next(); |
| 1893 return subscripts(array_(), allow_calls); |
| 1894 case "{": |
| 1895 next(); |
| 1896 return subscripts(object_(), allow_calls); |
| 1897 } |
| 1898 unexpected(); |
| 1899 } |
| 1900 if (is("keyword", "function")) { |
| 1901 next(); |
| 1902 return subscripts(function_(false), allow_calls); |
| 1903 } |
| 1904 if (HOP(ATOMIC_START_TOKEN, S.token.type)) { |
| 1905 var atom = S.token.type == "regexp" |
| 1906 ? as("regexp", S.token.value[0], S.token.value[1]) |
| 1907 : as(S.token.type, S.token.value); |
| 1908 return subscripts(prog1(atom, next), allow_calls); |
| 1909 } |
| 1910 unexpected(); |
| 1911 }; |
| 1912 |
| 1913 function expr_list(closing, allow_trailing_comma, allow_empty) { |
| 1914 var first = true, a = []; |
| 1915 while (!is("punc", closing)) { |
| 1916 if (first) first = false; else expect(","); |
| 1917 if (allow_trailing_comma && is("punc", closing)) break; |
| 1918 if (is("punc", ",") && allow_empty) { |
| 1919 a.push([ "atom", "undefined" ]); |
| 1920 } else { |
| 1921 a.push(expression(false)); |
| 1922 } |
| 1923 } |
| 1924 next(); |
| 1925 return a; |
| 1926 }; |
| 1927 |
| 1928 function array_() { |
| 1929 return as("array", expr_list("]", !strict_mode, true)); |
| 1930 }; |
| 1931 |
| 1932 function object_() { |
| 1933 var first = true, a = []; |
| 1934 while (!is("punc", "}")) { |
| 1935 if (first) first = false; else expect(","); |
| 1936 if (!strict_mode && is("punc", "}")) |
| 1937 |
| 1938 break; |
| 1939 var type = S.token.type; |
| 1940 var name = as_property_name(); |
| 1941 if (type == "name" && (name == "get" || name == "set") && !is("punc", ":")) { |
| 1942 a.push([ as_name(), function_(false), name ]); |
| 1943 } else { |
| 1944 expect(":"); |
| 1945 a.push([ name, expression(false) ]); |
| 1946 } |
| 1947 } |
| 1948 next(); |
| 1949 return as("object", a); |
| 1950 }; |
| 1951 |
| 1952 function as_property_name() { |
| 1953 switch (S.token.type) { |
| 1954 case "num": |
| 1955 case "string": |
| 1956 return prog1(S.token.value, next); |
| 1957 } |
| 1958 return as_name(); |
| 1959 }; |
| 1960 |
| 1961 function as_name() { |
| 1962 switch (S.token.type) { |
| 1963 case "name": |
| 1964 case "operator": |
| 1965 case "keyword": |
| 1966 case "atom": |
| 1967 return prog1(S.token.value, next); |
| 1968 default: |
| 1969 unexpected(); |
| 1970 } |
| 1971 }; |
| 1972 |
| 1973 function subscripts(expr, allow_calls) { |
| 1974 if (is("punc", ".")) { |
| 1975 next(); |
| 1976 return subscripts(as("dot", expr, as_name()), allow_calls); |
| 1977 } |
| 1978 if (is("punc", "[")) { |
| 1979 next(); |
| 1980 return subscripts(as("sub", expr, prog1(expression, curry(expect, "]"))), allow_
calls); |
| 1981 } |
| 1982 if (allow_calls && is("punc", "(")) { |
| 1983 next(); |
| 1984 return subscripts(as("call", expr, expr_list(")")), true); |
| 1985 } |
| 1986 if (allow_calls && is("operator") && HOP(UNARY_POSTFIX, S.token.value)) { |
| 1987 return prog1(curry(make_unary, "unary-postfix", S.token.value, expr), |
| 1988 next); |
| 1989 } |
| 1990 return expr; |
| 1991 }; |
| 1992 |
| 1993 function make_unary(tag, op, expr) { |
| 1994 if ((op == "++" || op == "--") && !is_assignable(expr)) |
| 1995 croak("Invalid use of " + op + " operator"); |
| 1996 return as(tag, op, expr); |
| 1997 }; |
| 1998 |
| 1999 function expr_op(left, min_prec) { |
| 2000 var op = is("operator") ? S.token.value : null; |
| 2001 var prec = op != null ? PRECEDENCE[op] : null; |
| 2002 if (prec != null && prec > min_prec) { |
| 2003 next(); |
| 2004 var right = expr_op(expr_atom(true), prec); |
| 2005 return expr_op(as("binary", op, left, right), min_prec); |
| 2006 } |
| 2007 return left; |
| 2008 }; |
| 2009 |
| 2010 function expr_ops() { |
| 2011 return expr_op(expr_atom(true), 0); |
| 2012 }; |
| 2013 |
| 2014 function maybe_conditional() { |
| 2015 var expr = expr_ops(); |
| 2016 if (is("operator", "?")) { |
| 2017 next(); |
| 2018 var yes = expression(false); |
| 2019 expect(":"); |
| 2020 return as("conditional", expr, yes, expression(false)); |
| 2021 } |
| 2022 return expr; |
| 2023 }; |
| 2024 |
| 2025 function is_assignable(expr) { |
| 2026 switch (expr[0]) { |
| 2027 case "dot": |
| 2028 case "sub": |
| 2029 return true; |
| 2030 case "name": |
| 2031 return expr[1] != "this"; |
| 2032 } |
| 2033 }; |
| 2034 |
| 2035 function maybe_assign() { |
| 2036 var left = maybe_conditional(), val = S.token.value; |
| 2037 if (is("operator") && HOP(ASSIGNMENT, val)) { |
| 2038 if (is_assignable(left)) { |
| 2039 next(); |
| 2040 return as("assign", ASSIGNMENT[val], left, maybe_assign()); |
| 2041 } |
| 2042 croak("Invalid assignment"); |
| 2043 } |
| 2044 return left; |
| 2045 }; |
| 2046 |
| 2047 function expression(commas) { |
| 2048 if (arguments.length == 0) |
| 2049 commas = true; |
| 2050 var expr = maybe_assign(); |
| 2051 if (commas && is("punc", ",")) { |
| 2052 next(); |
| 2053 return as("seq", expr, expression()); |
| 2054 } |
| 2055 return expr; |
| 2056 }; |
| 2057 |
| 2058 function in_loop(cont) { |
| 2059 try { |
| 2060 ++S.in_loop; |
| 2061 return cont(); |
| 2062 } finally { |
| 2063 --S.in_loop; |
| 2064 } |
| 2065 }; |
| 2066 |
| 2067 return as("toplevel", (function(a){ |
| 2068 while (!is("eof")) |
| 2069 a.push(statement()); |
| 2070 return a; |
| 2071 })([])); |
| 2072 |
| 2073 }; |
| 2074 |
| 2075 |
| 2076 |
| 2077 function curry(f) { |
| 2078 var args = slice(arguments, 1); |
| 2079 return function() { return f.apply(this, args.concat(slice(arguments))); }; |
| 2080 }; |
| 2081 |
| 2082 function prog1(ret) { |
| 2083 if (ret instanceof Function) |
| 2084 ret = ret(); |
| 2085 for (var i = 1, n = arguments.length; --n > 0; ++i) |
| 2086 arguments[i](); |
| 2087 return ret; |
| 2088 }; |
| 2089 |
| 2090 function array_to_hash(a) { |
| 2091 var ret = {}; |
| 2092 for (var i = 0; i < a.length; ++i) |
| 2093 ret[a[i]] = true; |
| 2094 return ret; |
| 2095 }; |
| 2096 |
| 2097 function slice(a, start) { |
| 2098 return Array.prototype.slice.call(a, start == null ? 0 : start); |
| 2099 }; |
| 2100 |
| 2101 function characters(str) { |
| 2102 return str.split(""); |
| 2103 }; |
| 2104 |
| 2105 function member(name, array) { |
| 2106 for (var i = array.length; --i >= 0;) |
| 2107 if (array[i] === name) |
| 2108 return true; |
| 2109 return false; |
| 2110 }; |
| 2111 |
| 2112 function HOP(obj, prop) { |
| 2113 return Object.prototype.hasOwnProperty.call(obj, prop); |
| 2114 }; |
| 2115 |
| 2116 |
| 2117 |
| 2118 exports.tokenizer = tokenizer; |
| 2119 exports.parse = parse; |
| 2120 exports.slice = slice; |
| 2121 exports.curry = curry; |
| 2122 exports.member = member; |
| 2123 exports.array_to_hash = array_to_hash; |
| 2124 exports.PRECEDENCE = PRECEDENCE; |
| 2125 exports.KEYWORDS_ATOM = KEYWORDS_ATOM; |
| 2126 exports.RESERVED_WORDS = RESERVED_WORDS; |
| 2127 exports.KEYWORDS = KEYWORDS; |
| 2128 exports.ATOMIC_START_TOKEN = ATOMIC_START_TOKEN; |
| 2129 exports.OPERATORS = OPERATORS; |
| 2130 exports.is_alphanumeric_char = is_alphanumeric_char; |
| 2131 exports.is_identifier_char = is_identifier_char; |
| 2132 ; |
| 2133 var parse = exports; |
| 2134 |
| 2135 |
| 2136 |
| 2137 function FormattedContentBuilder(content, mapping, originalOffset, formattedOffs
et) |
| 2138 { |
| 2139 this._originalContent = content; |
| 2140 this._originalOffset = originalOffset; |
| 2141 this._lastOriginalPosition = 0; |
| 2142 |
| 2143 this._formattedContent = []; |
| 2144 this._formattedContentLength = 0; |
| 2145 this._formattedOffset = formattedOffset; |
| 2146 this._lastFormattedPosition = 0; |
| 2147 |
| 2148 this._mapping = mapping; |
| 2149 |
| 2150 this._lineNumber = 0; |
| 2151 this._nestingLevelLevel = 0; |
| 2152 } |
| 2153 |
| 2154 FormattedContentBuilder.prototype = { |
| 2155 addToken: function(token) |
| 2156 { |
| 2157 for (var i = 0; i < token.comments_before.length; ++i) |
| 2158 this._addComment(token.comments_before[i]); |
| 2159 |
| 2160 while (this._lineNumber < token.line) { |
| 2161 this._addText("\n"); |
| 2162 this._addIndent(); |
| 2163 this._needNewLine = false; |
| 2164 this._lineNumber += 1; |
| 2165 } |
| 2166 |
| 2167 if (this._needNewLine) { |
| 2168 this._addText("\n"); |
| 2169 this._addIndent(); |
| 2170 this._needNewLine = false; |
| 2171 } |
| 2172 |
| 2173 this._addMappingIfNeeded(token.pos); |
| 2174 this._addText(this._originalContent.substring(token.pos, token.endPos)); |
| 2175 this._lineNumber = token.endLine; |
| 2176 }, |
| 2177 |
| 2178 addSpace: function() |
| 2179 { |
| 2180 this._addText(" "); |
| 2181 }, |
| 2182 |
| 2183 addNewLine: function() |
| 2184 { |
| 2185 this._needNewLine = true; |
| 2186 }, |
| 2187 |
| 2188 increaseNestingLevel: function() |
| 2189 { |
| 2190 this._nestingLevelLevel += 1; |
| 2191 }, |
| 2192 |
| 2193 decreaseNestingLevel: function() |
| 2194 { |
| 2195 this._nestingLevelLevel -= 1; |
| 2196 }, |
| 2197 |
| 2198 content: function() |
| 2199 { |
| 2200 return this._formattedContent.join(""); |
| 2201 }, |
| 2202 |
| 2203 mapping: function() |
| 2204 { |
| 2205 return { original: this._originalPositions, formatted: this._formattedPositions
}; |
| 2206 }, |
| 2207 |
| 2208 _addIndent: function() |
| 2209 { |
| 2210 for (var i = 0; i < this._nestingLevelLevel * 4; ++i) |
| 2211 this.addSpace(); |
| 2212 }, |
| 2213 |
| 2214 _addComment: function(comment) |
| 2215 { |
| 2216 if (this._lineNumber < comment.line) { |
| 2217 for (var j = this._lineNumber; j < comment.line; ++j) |
| 2218 this._addText("\n"); |
| 2219 this._lineNumber = comment.line; |
| 2220 this._needNewLine = false; |
| 2221 this._addIndent(); |
| 2222 } else |
| 2223 this.addSpace(); |
| 2224 |
| 2225 this._addMappingIfNeeded(comment.pos); |
| 2226 if (comment.type === "comment1") |
| 2227 this._addText("//"); |
| 2228 else |
| 2229 this._addText("/*"); |
| 2230 |
| 2231 this._addText(comment.value); |
| 2232 |
| 2233 if (comment.type !== "comment1") { |
| 2234 this._addText("*/"); |
| 2235 var position; |
| 2236 while ((position = comment.value.indexOf("\n", position + 1)) !== -1) |
| 2237 this._lineNumber += 1; |
| 2238 } |
| 2239 }, |
| 2240 |
| 2241 _addText: function(text) |
| 2242 { |
| 2243 this._formattedContent.push(text); |
| 2244 this._formattedContentLength += text.length; |
| 2245 }, |
| 2246 |
| 2247 _addMappingIfNeeded: function(originalPosition) |
| 2248 { |
| 2249 if (originalPosition - this._lastOriginalPosition === this._formattedContentLeng
th - this._lastFormattedPosition) |
| 2250 return; |
| 2251 this._mapping.original.push(this._originalOffset + originalPosition); |
| 2252 this._lastOriginalPosition = originalPosition; |
| 2253 this._mapping.formatted.push(this._formattedOffset + this._formattedContentLengt
h); |
| 2254 this._lastFormattedPosition = this._formattedContentLength; |
| 2255 } |
| 2256 } |
| 2257 |
| 2258 var tokens = [ |
| 2259 ["EOS"], |
| 2260 ["LPAREN", "("], ["RPAREN", ")"], ["LBRACK", "["], ["RBRACK", "]"], ["LBRACE", "
{"], ["RBRACE", "}"], ["COLON", ":"], ["SEMICOLON", ";"], ["PERIOD", "."], ["CON
DITIONAL", "?"], |
| 2261 ["INC", "++"], ["DEC", "--"], |
| 2262 ["ASSIGN", "="], ["ASSIGN_BIT_OR", "|="], ["ASSIGN_BIT_XOR", "^="], ["ASSIGN_BIT
_AND", "&="], ["ASSIGN_SHL", "<<="], ["ASSIGN_SAR", ">>="], ["ASSIGN_SHR", ">>>=
"], |
| 2263 ["ASSIGN_ADD", "+="], ["ASSIGN_SUB", "-="], ["ASSIGN_MUL", "*="], ["ASSIGN_DIV",
"/="], ["ASSIGN_MOD", "%="], |
| 2264 ["COMMA", ","], ["OR", "||"], ["AND", "&&"], ["BIT_OR", "|"], ["BIT_XOR", "^"],
["BIT_AND", "&"], ["SHL", "<<"], ["SAR", ">>"], ["SHR", ">>>"], |
| 2265 ["ADD", "+"], ["SUB", "-"], ["MUL", "*"], ["DIV", "/"], ["MOD", "%"], |
| 2266 ["EQ", "=="], ["NE", "!="], ["EQ_STRICT", "==="], ["NE_STRICT", "!=="], ["LT", "
<"], ["GT", ">"], ["LTE", "<="], ["GTE", ">="], |
| 2267 ["INSTANCEOF", "instanceof"], ["IN", "in"], ["NOT", "!"], ["BIT_NOT", "~"], ["DE
LETE", "delete"], ["TYPEOF", "typeof"], ["VOID", "void"], |
| 2268 ["BREAK", "break"], ["CASE", "case"], ["CATCH", "catch"], ["CONTINUE", "continue
"], ["DEBUGGER", "debugger"], ["DEFAULT", "default"], ["DO", "do"], ["ELSE", "el
se"], ["FINALLY", "finally"], |
| 2269 ["FOR", "for"], ["FUNCTION", "function"], ["IF", "if"], ["NEW", "new"], ["RETURN
", "return"], ["SWITCH", "switch"], ["THIS", "this"], ["THROW", "throw"], ["TRY"
, "try"], ["VAR", "var"], |
| 2270 ["WHILE", "while"], ["WITH", "with"], ["NULL_LITERAL", "null"], ["TRUE_LITERAL",
"true"], ["FALSE_LITERAL", "false"], ["NUMBER"], ["STRING"], ["IDENTIFIER"], ["
CONST", "const"] |
| 2271 ]; |
| 2272 |
| 2273 var Tokens = {}; |
| 2274 for (var i = 0; i < tokens.length; ++i) |
| 2275 Tokens[tokens[i][0]] = i; |
| 2276 |
| 2277 var TokensByValue = {}; |
| 2278 for (var i = 0; i < tokens.length; ++i) { |
| 2279 if (tokens[i][1]) |
| 2280 TokensByValue[tokens[i][1]] = i; |
| 2281 } |
| 2282 |
| 2283 var TokensByType = { |
| 2284 "eof": Tokens.EOS, |
| 2285 "name": Tokens.IDENTIFIER, |
| 2286 "num": Tokens.NUMBER, |
| 2287 "regexp": Tokens.DIV, |
| 2288 "string": Tokens.STRING |
| 2289 }; |
| 2290 |
| 2291 function Tokenizer(content) |
| 2292 { |
| 2293 this._readNextToken = parse.tokenizer(content); |
| 2294 this._state = this._readNextToken.context(); |
| 2295 } |
| 2296 |
| 2297 Tokenizer.prototype = { |
| 2298 content: function() |
| 2299 { |
| 2300 return this._state.text; |
| 2301 }, |
| 2302 |
| 2303 next: function(forceRegexp) |
| 2304 { |
| 2305 var uglifyToken = this._readNextToken(forceRegexp); |
| 2306 uglifyToken.endPos = this._state.pos; |
| 2307 uglifyToken.endLine = this._state.line; |
| 2308 uglifyToken.token = this._convertUglifyToken(uglifyToken); |
| 2309 return uglifyToken; |
| 2310 }, |
| 2311 |
| 2312 _convertUglifyToken: function(uglifyToken) |
| 2313 { |
| 2314 var token = TokensByType[uglifyToken.type]; |
| 2315 if (typeof token === "number") |
| 2316 return token; |
| 2317 token = TokensByValue[uglifyToken.value]; |
| 2318 if (typeof token === "number") |
| 2319 return token; |
| 2320 throw "Unknown token type " + uglifyToken.type; |
| 2321 } |
| 2322 } |
| 2323 |
| 2324 function JavaScriptFormatter(tokenizer, builder) |
| 2325 { |
| 2326 this._tokenizer = tokenizer; |
| 2327 this._builder = builder; |
| 2328 this._token = null; |
| 2329 this._nextToken = this._tokenizer.next(); |
| 2330 } |
| 2331 |
| 2332 JavaScriptFormatter.prototype = { |
| 2333 format: function() |
| 2334 { |
| 2335 this._parseSourceElements(Tokens.EOS); |
| 2336 this._consume(Tokens.EOS); |
| 2337 }, |
| 2338 |
| 2339 _peek: function() |
| 2340 { |
| 2341 return this._nextToken.token; |
| 2342 }, |
| 2343 |
| 2344 _next: function() |
| 2345 { |
| 2346 if (this._token && this._token.token === Tokens.EOS) |
| 2347 throw "Unexpected EOS token"; |
| 2348 |
| 2349 this._builder.addToken(this._nextToken); |
| 2350 this._token = this._nextToken; |
| 2351 this._nextToken = this._tokenizer.next(this._forceRegexp); |
| 2352 this._forceRegexp = false; |
| 2353 return this._token.token; |
| 2354 }, |
| 2355 |
| 2356 _consume: function(token) |
| 2357 { |
| 2358 var next = this._next(); |
| 2359 if (next !== token) |
| 2360 throw "Unexpected token in consume: expected " + token + ", actual " + next; |
| 2361 }, |
| 2362 |
| 2363 _expect: function(token) |
| 2364 { |
| 2365 var next = this._next(); |
| 2366 if (next !== token) |
| 2367 throw "Unexpected token: expected " + token + ", actual " + next; |
| 2368 }, |
| 2369 |
| 2370 _expectSemicolon: function() |
| 2371 { |
| 2372 if (this._peek() === Tokens.SEMICOLON) |
| 2373 this._consume(Tokens.SEMICOLON); |
| 2374 }, |
| 2375 |
| 2376 _hasLineTerminatorBeforeNext: function() |
| 2377 { |
| 2378 return this._nextToken.nlb; |
| 2379 }, |
| 2380 |
| 2381 _parseSourceElements: function(endToken) |
| 2382 { |
| 2383 while (this._peek() !== endToken) { |
| 2384 this._parseStatement(); |
| 2385 this._builder.addNewLine(); |
| 2386 } |
| 2387 }, |
| 2388 |
| 2389 _parseStatementOrBlock: function() |
| 2390 { |
| 2391 if (this._peek() === Tokens.LBRACE) { |
| 2392 this._builder.addSpace(); |
| 2393 this._parseBlock(); |
| 2394 return true; |
| 2395 } |
| 2396 |
| 2397 this._builder.addNewLine(); |
| 2398 this._builder.increaseNestingLevel(); |
| 2399 this._parseStatement(); |
| 2400 this._builder.decreaseNestingLevel(); |
| 2401 }, |
| 2402 |
| 2403 _parseStatement: function() |
| 2404 { |
| 2405 switch (this._peek()) { |
| 2406 case Tokens.LBRACE: |
| 2407 return this._parseBlock(); |
| 2408 case Tokens.CONST: |
| 2409 case Tokens.VAR: |
| 2410 return this._parseVariableStatement(); |
| 2411 case Tokens.SEMICOLON: |
| 2412 return this._next(); |
| 2413 case Tokens.IF: |
| 2414 return this._parseIfStatement(); |
| 2415 case Tokens.DO: |
| 2416 return this._parseDoWhileStatement(); |
| 2417 case Tokens.WHILE: |
| 2418 return this._parseWhileStatement(); |
| 2419 case Tokens.FOR: |
| 2420 return this._parseForStatement(); |
| 2421 case Tokens.CONTINUE: |
| 2422 return this._parseContinueStatement(); |
| 2423 case Tokens.BREAK: |
| 2424 return this._parseBreakStatement(); |
| 2425 case Tokens.RETURN: |
| 2426 return this._parseReturnStatement(); |
| 2427 case Tokens.WITH: |
| 2428 return this._parseWithStatement(); |
| 2429 case Tokens.SWITCH: |
| 2430 return this._parseSwitchStatement(); |
| 2431 case Tokens.THROW: |
| 2432 return this._parseThrowStatement(); |
| 2433 case Tokens.TRY: |
| 2434 return this._parseTryStatement(); |
| 2435 case Tokens.FUNCTION: |
| 2436 return this._parseFunctionDeclaration(); |
| 2437 case Tokens.DEBUGGER: |
| 2438 return this._parseDebuggerStatement(); |
| 2439 default: |
| 2440 return this._parseExpressionOrLabelledStatement(); |
| 2441 } |
| 2442 }, |
| 2443 |
| 2444 _parseFunctionDeclaration: function() |
| 2445 { |
| 2446 this._expect(Tokens.FUNCTION); |
| 2447 this._builder.addSpace(); |
| 2448 this._expect(Tokens.IDENTIFIER); |
| 2449 this._parseFunctionLiteral() |
| 2450 }, |
| 2451 |
| 2452 _parseBlock: function() |
| 2453 { |
| 2454 this._expect(Tokens.LBRACE); |
| 2455 this._builder.addNewLine(); |
| 2456 this._builder.increaseNestingLevel(); |
| 2457 while (this._peek() !== Tokens.RBRACE) { |
| 2458 this._parseStatement(); |
| 2459 this._builder.addNewLine(); |
| 2460 } |
| 2461 this._builder.decreaseNestingLevel(); |
| 2462 this._expect(Tokens.RBRACE); |
| 2463 }, |
| 2464 |
| 2465 _parseVariableStatement: function() |
| 2466 { |
| 2467 this._parseVariableDeclarations(); |
| 2468 this._expectSemicolon(); |
| 2469 }, |
| 2470 |
| 2471 _parseVariableDeclarations: function() |
| 2472 { |
| 2473 if (this._peek() === Tokens.VAR) |
| 2474 this._consume(Tokens.VAR); |
| 2475 else |
| 2476 this._consume(Tokens.CONST) |
| 2477 this._builder.addSpace(); |
| 2478 |
| 2479 var isFirstVariable = true; |
| 2480 do { |
| 2481 if (!isFirstVariable) { |
| 2482 this._consume(Tokens.COMMA); |
| 2483 this._builder.addSpace(); |
| 2484 } |
| 2485 isFirstVariable = false; |
| 2486 this._expect(Tokens.IDENTIFIER); |
| 2487 if (this._peek() === Tokens.ASSIGN) { |
| 2488 this._builder.addSpace(); |
| 2489 this._consume(Tokens.ASSIGN); |
| 2490 this._builder.addSpace(); |
| 2491 this._parseAssignmentExpression(); |
| 2492 } |
| 2493 } while (this._peek() === Tokens.COMMA); |
| 2494 }, |
| 2495 |
| 2496 _parseExpressionOrLabelledStatement: function() |
| 2497 { |
| 2498 this._parseExpression(); |
| 2499 if (this._peek() === Tokens.COLON) { |
| 2500 this._expect(Tokens.COLON); |
| 2501 this._builder.addSpace(); |
| 2502 this._parseStatement(); |
| 2503 } |
| 2504 this._expectSemicolon(); |
| 2505 }, |
| 2506 |
| 2507 _parseIfStatement: function() |
| 2508 { |
| 2509 this._expect(Tokens.IF); |
| 2510 this._builder.addSpace(); |
| 2511 this._expect(Tokens.LPAREN); |
| 2512 this._parseExpression(); |
| 2513 this._expect(Tokens.RPAREN); |
| 2514 |
| 2515 var isBlock = this._parseStatementOrBlock(); |
| 2516 if (this._peek() === Tokens.ELSE) { |
| 2517 if (isBlock) |
| 2518 this._builder.addSpace(); |
| 2519 else |
| 2520 this._builder.addNewLine(); |
| 2521 this._next(); |
| 2522 |
| 2523 if (this._peek() === Tokens.IF) { |
| 2524 this._builder.addSpace(); |
| 2525 this._parseStatement(); |
| 2526 } else |
| 2527 this._parseStatementOrBlock(); |
| 2528 } |
| 2529 }, |
| 2530 |
| 2531 _parseContinueStatement: function() |
| 2532 { |
| 2533 this._expect(Tokens.CONTINUE); |
| 2534 var token = this._peek(); |
| 2535 if (!this._hasLineTerminatorBeforeNext() && token !== Tokens.SEMICOLON && token
!== Tokens.RBRACE && token !== Tokens.EOS) { |
| 2536 this._builder.addSpace(); |
| 2537 this._expect(Tokens.IDENTIFIER); |
| 2538 } |
| 2539 this._expectSemicolon(); |
| 2540 }, |
| 2541 |
| 2542 _parseBreakStatement: function() |
| 2543 { |
| 2544 this._expect(Tokens.BREAK); |
| 2545 var token = this._peek(); |
| 2546 if (!this._hasLineTerminatorBeforeNext() && token !== Tokens.SEMICOLON && token
!== Tokens.RBRACE && token !== Tokens.EOS) { |
| 2547 this._builder.addSpace(); |
| 2548 this._expect(Tokens.IDENTIFIER); |
| 2549 } |
| 2550 this._expectSemicolon(); |
| 2551 }, |
| 2552 |
| 2553 _parseReturnStatement: function() |
| 2554 { |
| 2555 this._expect(Tokens.RETURN); |
| 2556 var token = this._peek(); |
| 2557 if (!this._hasLineTerminatorBeforeNext() && token !== Tokens.SEMICOLON && token
!== Tokens.RBRACE && token !== Tokens.EOS) { |
| 2558 this._builder.addSpace(); |
| 2559 this._parseExpression(); |
| 2560 } |
| 2561 this._expectSemicolon(); |
| 2562 }, |
| 2563 |
| 2564 _parseWithStatement: function() |
| 2565 { |
| 2566 this._expect(Tokens.WITH); |
| 2567 this._builder.addSpace(); |
| 2568 this._expect(Tokens.LPAREN); |
| 2569 this._parseExpression(); |
| 2570 this._expect(Tokens.RPAREN); |
| 2571 this._parseStatementOrBlock(); |
| 2572 }, |
| 2573 |
| 2574 _parseCaseClause: function() |
| 2575 { |
| 2576 if (this._peek() === Tokens.CASE) { |
| 2577 this._expect(Tokens.CASE); |
| 2578 this._builder.addSpace(); |
| 2579 this._parseExpression(); |
| 2580 } else |
| 2581 this._expect(Tokens.DEFAULT); |
| 2582 this._expect(Tokens.COLON); |
| 2583 this._builder.addNewLine(); |
| 2584 |
| 2585 this._builder.increaseNestingLevel(); |
| 2586 while (this._peek() !== Tokens.CASE && this._peek() !== Tokens.DEFAULT && this._
peek() !== Tokens.RBRACE) { |
| 2587 this._parseStatement(); |
| 2588 this._builder.addNewLine(); |
| 2589 } |
| 2590 this._builder.decreaseNestingLevel(); |
| 2591 }, |
| 2592 |
| 2593 _parseSwitchStatement: function() |
| 2594 { |
| 2595 this._expect(Tokens.SWITCH); |
| 2596 this._builder.addSpace(); |
| 2597 this._expect(Tokens.LPAREN); |
| 2598 this._parseExpression(); |
| 2599 this._expect(Tokens.RPAREN); |
| 2600 this._builder.addSpace(); |
| 2601 |
| 2602 this._expect(Tokens.LBRACE); |
| 2603 this._builder.addNewLine(); |
| 2604 this._builder.increaseNestingLevel(); |
| 2605 while (this._peek() !== Tokens.RBRACE) |
| 2606 this._parseCaseClause(); |
| 2607 this._builder.decreaseNestingLevel(); |
| 2608 this._expect(Tokens.RBRACE); |
| 2609 }, |
| 2610 |
| 2611 _parseThrowStatement: function() |
| 2612 { |
| 2613 this._expect(Tokens.THROW); |
| 2614 this._builder.addSpace(); |
| 2615 this._parseExpression(); |
| 2616 this._expectSemicolon(); |
| 2617 }, |
| 2618 |
| 2619 _parseTryStatement: function() |
| 2620 { |
| 2621 this._expect(Tokens.TRY); |
| 2622 this._builder.addSpace(); |
| 2623 this._parseBlock(); |
| 2624 |
| 2625 var token = this._peek(); |
| 2626 if (token === Tokens.CATCH) { |
| 2627 this._builder.addSpace(); |
| 2628 this._consume(Tokens.CATCH); |
| 2629 this._builder.addSpace(); |
| 2630 this._expect(Tokens.LPAREN); |
| 2631 this._expect(Tokens.IDENTIFIER); |
| 2632 this._expect(Tokens.RPAREN); |
| 2633 this._builder.addSpace(); |
| 2634 this._parseBlock(); |
| 2635 token = this._peek(); |
| 2636 } |
| 2637 |
| 2638 if (token === Tokens.FINALLY) { |
| 2639 this._consume(Tokens.FINALLY); |
| 2640 this._builder.addSpace(); |
| 2641 this._parseBlock(); |
| 2642 } |
| 2643 }, |
| 2644 |
| 2645 _parseDoWhileStatement: function() |
| 2646 { |
| 2647 this._expect(Tokens.DO); |
| 2648 var isBlock = this._parseStatementOrBlock(); |
| 2649 if (isBlock) |
| 2650 this._builder.addSpace(); |
| 2651 else |
| 2652 this._builder.addNewLine(); |
| 2653 this._expect(Tokens.WHILE); |
| 2654 this._builder.addSpace(); |
| 2655 this._expect(Tokens.LPAREN); |
| 2656 this._parseExpression(); |
| 2657 this._expect(Tokens.RPAREN); |
| 2658 this._expectSemicolon(); |
| 2659 }, |
| 2660 |
| 2661 _parseWhileStatement: function() |
| 2662 { |
| 2663 this._expect(Tokens.WHILE); |
| 2664 this._builder.addSpace(); |
| 2665 this._expect(Tokens.LPAREN); |
| 2666 this._parseExpression(); |
| 2667 this._expect(Tokens.RPAREN); |
| 2668 this._parseStatementOrBlock(); |
| 2669 }, |
| 2670 |
| 2671 _parseForStatement: function() |
| 2672 { |
| 2673 this._expect(Tokens.FOR); |
| 2674 this._builder.addSpace(); |
| 2675 this._expect(Tokens.LPAREN); |
| 2676 if (this._peek() !== Tokens.SEMICOLON) { |
| 2677 if (this._peek() === Tokens.VAR || this._peek() === Tokens.CONST) { |
| 2678 this._parseVariableDeclarations(); |
| 2679 if (this._peek() === Tokens.IN) { |
| 2680 this._builder.addSpace(); |
| 2681 this._consume(Tokens.IN); |
| 2682 this._builder.addSpace(); |
| 2683 this._parseExpression(); |
| 2684 } |
| 2685 } else |
| 2686 this._parseExpression(); |
| 2687 } |
| 2688 |
| 2689 if (this._peek() !== Tokens.RPAREN) { |
| 2690 this._expect(Tokens.SEMICOLON); |
| 2691 this._builder.addSpace(); |
| 2692 if (this._peek() !== Tokens.SEMICOLON) |
| 2693 this._parseExpression(); |
| 2694 this._expect(Tokens.SEMICOLON); |
| 2695 this._builder.addSpace(); |
| 2696 if (this._peek() !== Tokens.RPAREN) |
| 2697 this._parseExpression(); |
| 2698 } |
| 2699 this._expect(Tokens.RPAREN); |
| 2700 |
| 2701 this._parseStatementOrBlock(); |
| 2702 }, |
| 2703 |
| 2704 _parseExpression: function() |
| 2705 { |
| 2706 this._parseAssignmentExpression(); |
| 2707 while (this._peek() === Tokens.COMMA) { |
| 2708 this._expect(Tokens.COMMA); |
| 2709 this._builder.addSpace(); |
| 2710 this._parseAssignmentExpression(); |
| 2711 } |
| 2712 }, |
| 2713 |
| 2714 _parseAssignmentExpression: function() |
| 2715 { |
| 2716 this._parseConditionalExpression(); |
| 2717 var token = this._peek(); |
| 2718 if (Tokens.ASSIGN <= token && token <= Tokens.ASSIGN_MOD) { |
| 2719 this._builder.addSpace(); |
| 2720 this._next(); |
| 2721 this._builder.addSpace(); |
| 2722 this._parseAssignmentExpression(); |
| 2723 } |
| 2724 }, |
| 2725 |
| 2726 _parseConditionalExpression: function() |
| 2727 { |
| 2728 this._parseBinaryExpression(); |
| 2729 if (this._peek() === Tokens.CONDITIONAL) { |
| 2730 this._builder.addSpace(); |
| 2731 this._consume(Tokens.CONDITIONAL); |
| 2732 this._builder.addSpace(); |
| 2733 this._parseAssignmentExpression(); |
| 2734 this._builder.addSpace(); |
| 2735 this._expect(Tokens.COLON); |
| 2736 this._builder.addSpace(); |
| 2737 this._parseAssignmentExpression(); |
| 2738 } |
| 2739 }, |
| 2740 |
| 2741 _parseBinaryExpression: function() |
| 2742 { |
| 2743 this._parseUnaryExpression(); |
| 2744 var token = this._peek(); |
| 2745 while (Tokens.OR <= token && token <= Tokens.IN) { |
| 2746 this._builder.addSpace(); |
| 2747 this._next(); |
| 2748 this._builder.addSpace(); |
| 2749 this._parseBinaryExpression(); |
| 2750 token = this._peek(); |
| 2751 } |
| 2752 }, |
| 2753 |
| 2754 _parseUnaryExpression: function() |
| 2755 { |
| 2756 var token = this._peek(); |
| 2757 if ((Tokens.NOT <= token && token <= Tokens.VOID) || token === Tokens.ADD || tok
en === Tokens.SUB || token === Tokens.INC || token === Tokens.DEC) { |
| 2758 this._next(); |
| 2759 if (token === Tokens.DELETE || token === Tokens.TYPEOF || token === Tokens.VOID) |
| 2760 this._builder.addSpace(); |
| 2761 this._parseUnaryExpression(); |
| 2762 } else |
| 2763 return this._parsePostfixExpression(); |
| 2764 }, |
| 2765 |
| 2766 _parsePostfixExpression: function() |
| 2767 { |
| 2768 this._parseLeftHandSideExpression(); |
| 2769 var token = this._peek(); |
| 2770 if (!this._hasLineTerminatorBeforeNext() && (token === Tokens.INC || token === T
okens.DEC)) |
| 2771 this._next(); |
| 2772 }, |
| 2773 |
| 2774 _parseLeftHandSideExpression: function() |
| 2775 { |
| 2776 if (this._peek() === Tokens.NEW) |
| 2777 this._parseNewExpression(); |
| 2778 else |
| 2779 this._parseMemberExpression(); |
| 2780 |
| 2781 while (true) { |
| 2782 switch (this._peek()) { |
| 2783 case Tokens.LBRACK: |
| 2784 this._consume(Tokens.LBRACK); |
| 2785 this._parseExpression(); |
| 2786 this._expect(Tokens.RBRACK); |
| 2787 break; |
| 2788 |
| 2789 case Tokens.LPAREN: |
| 2790 this._parseArguments(); |
| 2791 break; |
| 2792 |
| 2793 case Tokens.PERIOD: |
| 2794 this._consume(Tokens.PERIOD); |
| 2795 this._expect(Tokens.IDENTIFIER); |
| 2796 break; |
| 2797 |
| 2798 default: |
| 2799 return; |
| 2800 } |
| 2801 } |
| 2802 }, |
| 2803 |
| 2804 _parseNewExpression: function() |
| 2805 { |
| 2806 this._expect(Tokens.NEW); |
| 2807 this._builder.addSpace(); |
| 2808 if (this._peek() === Tokens.NEW) |
| 2809 this._parseNewExpression(); |
| 2810 else |
| 2811 this._parseMemberExpression(); |
| 2812 }, |
| 2813 |
| 2814 _parseMemberExpression: function() |
| 2815 { |
| 2816 if (this._peek() === Tokens.FUNCTION) { |
| 2817 this._expect(Tokens.FUNCTION); |
| 2818 if (this._peek() === Tokens.IDENTIFIER) { |
| 2819 this._builder.addSpace(); |
| 2820 this._expect(Tokens.IDENTIFIER); |
| 2821 } |
| 2822 this._parseFunctionLiteral(); |
| 2823 } else |
| 2824 this._parsePrimaryExpression(); |
| 2825 |
| 2826 while (true) { |
| 2827 switch (this._peek()) { |
| 2828 case Tokens.LBRACK: |
| 2829 this._consume(Tokens.LBRACK); |
| 2830 this._parseExpression(); |
| 2831 this._expect(Tokens.RBRACK); |
| 2832 break; |
| 2833 |
| 2834 case Tokens.PERIOD: |
| 2835 this._consume(Tokens.PERIOD); |
| 2836 this._expect(Tokens.IDENTIFIER); |
| 2837 break; |
| 2838 |
| 2839 case Tokens.LPAREN: |
| 2840 this._parseArguments(); |
| 2841 break; |
| 2842 |
| 2843 default: |
| 2844 return; |
| 2845 } |
| 2846 } |
| 2847 }, |
| 2848 |
| 2849 _parseDebuggerStatement: function() |
| 2850 { |
| 2851 this._expect(Tokens.DEBUGGER); |
| 2852 this._expectSemicolon(); |
| 2853 }, |
| 2854 |
| 2855 _parsePrimaryExpression: function() |
| 2856 { |
| 2857 switch (this._peek()) { |
| 2858 case Tokens.THIS: |
| 2859 return this._consume(Tokens.THIS); |
| 2860 case Tokens.NULL_LITERAL: |
| 2861 return this._consume(Tokens.NULL_LITERAL); |
| 2862 case Tokens.TRUE_LITERAL: |
| 2863 return this._consume(Tokens.TRUE_LITERAL); |
| 2864 case Tokens.FALSE_LITERAL: |
| 2865 return this._consume(Tokens.FALSE_LITERAL); |
| 2866 case Tokens.IDENTIFIER: |
| 2867 return this._consume(Tokens.IDENTIFIER); |
| 2868 case Tokens.NUMBER: |
| 2869 return this._consume(Tokens.NUMBER); |
| 2870 case Tokens.STRING: |
| 2871 return this._consume(Tokens.STRING); |
| 2872 case Tokens.ASSIGN_DIV: |
| 2873 return this._parseRegExpLiteral(); |
| 2874 case Tokens.DIV: |
| 2875 return this._parseRegExpLiteral(); |
| 2876 case Tokens.LBRACK: |
| 2877 return this._parseArrayLiteral(); |
| 2878 case Tokens.LBRACE: |
| 2879 return this._parseObjectLiteral(); |
| 2880 case Tokens.LPAREN: |
| 2881 this._consume(Tokens.LPAREN); |
| 2882 this._parseExpression(); |
| 2883 this._expect(Tokens.RPAREN); |
| 2884 return; |
| 2885 default: |
| 2886 return this._next(); |
| 2887 } |
| 2888 }, |
| 2889 |
| 2890 _parseArrayLiteral: function() |
| 2891 { |
| 2892 this._expect(Tokens.LBRACK); |
| 2893 this._builder.increaseNestingLevel(); |
| 2894 while (this._peek() !== Tokens.RBRACK) { |
| 2895 if (this._peek() !== Tokens.COMMA) |
| 2896 this._parseAssignmentExpression(); |
| 2897 if (this._peek() !== Tokens.RBRACK) { |
| 2898 this._expect(Tokens.COMMA); |
| 2899 this._builder.addSpace(); |
| 2900 } |
| 2901 } |
| 2902 this._builder.decreaseNestingLevel(); |
| 2903 this._expect(Tokens.RBRACK); |
| 2904 }, |
| 2905 |
| 2906 _parseObjectLiteralGetSet: function() |
| 2907 { |
| 2908 var token = this._peek(); |
| 2909 if (token === Tokens.IDENTIFIER || token === Tokens.NUMBER || token === Tokens.S
TRING || |
| 2910 Tokens.DELETE <= token && token <= Tokens.FALSE_LITERAL || |
| 2911 token === Tokens.INSTANCEOF || token === Tokens.IN || token === Tokens.CONST) { |
| 2912 this._next(); |
| 2913 this._parseFunctionLiteral(); |
| 2914 } |
| 2915 }, |
| 2916 |
| 2917 _parseObjectLiteral: function() |
| 2918 { |
| 2919 this._expect(Tokens.LBRACE); |
| 2920 this._builder.increaseNestingLevel(); |
| 2921 while (this._peek() !== Tokens.RBRACE) { |
| 2922 var token = this._peek(); |
| 2923 switch (token) { |
| 2924 case Tokens.IDENTIFIER: |
| 2925 this._consume(Tokens.IDENTIFIER); |
| 2926 var name = this._token.value; |
| 2927 if ((name === "get" || name === "set") && this._peek() !== Tokens.COLON) { |
| 2928 this._builder.addSpace(); |
| 2929 this._parseObjectLiteralGetSet(); |
| 2930 if (this._peek() !== Tokens.RBRACE) { |
| 2931 this._expect(Tokens.COMMA); |
| 2932 } |
| 2933 continue; |
| 2934 } |
| 2935 break; |
| 2936 |
| 2937 case Tokens.STRING: |
| 2938 this._consume(Tokens.STRING); |
| 2939 break; |
| 2940 |
| 2941 case Tokens.NUMBER: |
| 2942 this._consume(Tokens.NUMBER); |
| 2943 break; |
| 2944 |
| 2945 default: |
| 2946 this._next(); |
| 2947 } |
| 2948 |
| 2949 this._expect(Tokens.COLON); |
| 2950 this._builder.addSpace(); |
| 2951 this._parseAssignmentExpression(); |
| 2952 if (this._peek() !== Tokens.RBRACE) { |
| 2953 this._expect(Tokens.COMMA); |
| 2954 } |
| 2955 } |
| 2956 this._builder.decreaseNestingLevel(); |
| 2957 |
| 2958 this._expect(Tokens.RBRACE); |
| 2959 }, |
| 2960 |
| 2961 _parseRegExpLiteral: function() |
| 2962 { |
| 2963 if (this._nextToken.type === "regexp") |
| 2964 this._next(); |
| 2965 else { |
| 2966 this._forceRegexp = true; |
| 2967 this._next(); |
| 2968 } |
| 2969 }, |
| 2970 |
| 2971 _parseArguments: function() |
| 2972 { |
| 2973 this._expect(Tokens.LPAREN); |
| 2974 var done = (this._peek() === Tokens.RPAREN); |
| 2975 while (!done) { |
| 2976 this._parseAssignmentExpression(); |
| 2977 done = (this._peek() === Tokens.RPAREN); |
| 2978 if (!done) { |
| 2979 this._expect(Tokens.COMMA); |
| 2980 this._builder.addSpace(); |
| 2981 } |
| 2982 } |
| 2983 this._expect(Tokens.RPAREN); |
| 2984 }, |
| 2985 |
| 2986 _parseFunctionLiteral: function() |
| 2987 { |
| 2988 this._expect(Tokens.LPAREN); |
| 2989 var done = (this._peek() === Tokens.RPAREN); |
| 2990 while (!done) { |
| 2991 this._expect(Tokens.IDENTIFIER); |
| 2992 done = (this._peek() === Tokens.RPAREN); |
| 2993 if (!done) { |
| 2994 this._expect(Tokens.COMMA); |
| 2995 this._builder.addSpace(); |
| 2996 } |
| 2997 } |
| 2998 this._expect(Tokens.RPAREN); |
| 2999 this._builder.addSpace(); |
| 3000 |
| 3001 this._expect(Tokens.LBRACE); |
| 3002 this._builder.addNewLine(); |
| 3003 this._builder.increaseNestingLevel(); |
| 3004 this._parseSourceElements(Tokens.RBRACE); |
| 3005 this._builder.decreaseNestingLevel(); |
| 3006 this._expect(Tokens.RBRACE); |
| 3007 } |
| 3008 } |
| 3009 ; |
OLD | NEW |