-
Notifications
You must be signed in to change notification settings - Fork 0
/
esfmt_bundle.js
10991 lines (9297 loc) · 314 KB
/
esfmt_bundle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
var esfmt = require('./package/esfmt');
window.esfmt = esfmt;
},{"./package/esfmt":18}],2:[function(require,module,exports){
/*
Copyright (C) 2015 Fred K. Schott <[email protected]>
Copyright (C) 2013 Ariya Hidayat <[email protected]>
Copyright (C) 2013 Thaddee Tyl <[email protected]>
Copyright (C) 2013 Mathias Bynens <[email protected]>
Copyright (C) 2012 Ariya Hidayat <[email protected]>
Copyright (C) 2012 Mathias Bynens <[email protected]>
Copyright (C) 2012 Joost-Wim Boekesteijn <[email protected]>
Copyright (C) 2012 Kris Kowal <[email protected]>
Copyright (C) 2012 Yusuke Suzuki <[email protected]>
Copyright (C) 2012 Arpad Borsos <[email protected]>
Copyright (C) 2011 Ariya Hidayat <[email protected]>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*eslint no-undefined:0, no-use-before-define: 0*/
"use strict";
var syntax = require("./lib/syntax"),
tokenInfo = require("./lib/token-info"),
astNodeTypes = require("./lib/ast-node-types"),
astNodeFactory = require("./lib/ast-node-factory"),
defaultFeatures = require("./lib/features"),
Messages = require("./lib/messages"),
XHTMLEntities = require("./lib/xhtml-entities"),
StringMap = require("./lib/string-map"),
commentAttachment = require("./lib/comment-attachment");
var Token = tokenInfo.Token,
TokenName = tokenInfo.TokenName,
FnExprTokens = tokenInfo.FnExprTokens,
Regex = syntax.Regex,
PropertyKind,
source,
strict,
index,
lineNumber,
lineStart,
length,
lookahead,
state,
extra;
PropertyKind = {
Data: 1,
Get: 2,
Set: 4
};
// Ensure the condition is true, otherwise throw an error.
// This is only to have a better contract semantic, i.e. another safety net
// to catch a logic error. The condition shall be fulfilled in normal case.
// Do NOT use this to enforce a certain condition on any user input.
function assert(condition, message) {
/* istanbul ignore if */
if (!condition) {
throw new Error("ASSERT: " + message);
}
}
// 7.4 Comments
function addComment(type, value, start, end, loc) {
var comment;
assert(typeof start === "number", "Comment must have valid position");
// Because the way the actual token is scanned, often the comments
// (if any) are skipped twice during the lexical analysis.
// Thus, we need to skip adding a comment if the comment array already
// handled it.
if (state.lastCommentStart >= start) {
return;
}
state.lastCommentStart = start;
comment = {
type: type,
value: value
};
if (extra.range) {
comment.range = [start, end];
}
if (extra.loc) {
comment.loc = loc;
}
extra.comments.push(comment);
if (extra.attachComment) {
commentAttachment.addComment(comment);
}
}
function skipSingleLineComment(offset) {
var start, loc, ch, comment;
start = index - offset;
loc = {
start: {
line: lineNumber,
column: index - lineStart - offset
}
};
while (index < length) {
ch = source.charCodeAt(index);
++index;
if (syntax.isLineTerminator(ch)) {
if (extra.comments) {
comment = source.slice(start + offset, index - 1);
loc.end = {
line: lineNumber,
column: index - lineStart - 1
};
addComment("Line", comment, start, index - 1, loc);
}
if (ch === 13 && source.charCodeAt(index) === 10) {
++index;
}
++lineNumber;
lineStart = index;
return;
}
}
if (extra.comments) {
comment = source.slice(start + offset, index);
loc.end = {
line: lineNumber,
column: index - lineStart
};
addComment("Line", comment, start, index, loc);
}
}
function skipMultiLineComment() {
var start, loc, ch, comment;
if (extra.comments) {
start = index - 2;
loc = {
start: {
line: lineNumber,
column: index - lineStart - 2
}
};
}
while (index < length) {
ch = source.charCodeAt(index);
if (syntax.isLineTerminator(ch)) {
if (ch === 0x0D && source.charCodeAt(index + 1) === 0x0A) {
++index;
}
++lineNumber;
++index;
lineStart = index;
if (index >= length) {
throwError({}, Messages.UnexpectedToken, "ILLEGAL");
}
} else if (ch === 0x2A) {
// Block comment ends with "*/".
if (source.charCodeAt(index + 1) === 0x2F) {
++index;
++index;
if (extra.comments) {
comment = source.slice(start + 2, index - 2);
loc.end = {
line: lineNumber,
column: index - lineStart
};
addComment("Block", comment, start, index, loc);
}
return;
}
++index;
} else {
++index;
}
}
throwError({}, Messages.UnexpectedToken, "ILLEGAL");
}
function skipComment() {
var ch, start;
start = (index === 0);
while (index < length) {
ch = source.charCodeAt(index);
if (syntax.isWhiteSpace(ch)) {
++index;
} else if (syntax.isLineTerminator(ch)) {
++index;
if (ch === 0x0D && source.charCodeAt(index) === 0x0A) {
++index;
}
++lineNumber;
lineStart = index;
start = true;
} else if (ch === 0x2F) { // U+002F is "/"
ch = source.charCodeAt(index + 1);
if (ch === 0x2F) {
++index;
++index;
skipSingleLineComment(2);
start = true;
} else if (ch === 0x2A) { // U+002A is "*"
++index;
++index;
skipMultiLineComment();
} else {
break;
}
} else if (start && ch === 0x2D) { // U+002D is "-"
// U+003E is ">"
if ((source.charCodeAt(index + 1) === 0x2D) && (source.charCodeAt(index + 2) === 0x3E)) {
// "-->" is a single-line comment
index += 3;
skipSingleLineComment(3);
} else {
break;
}
} else if (ch === 0x3C) { // U+003C is "<"
if (source.slice(index + 1, index + 4) === "!--") {
++index; // `<`
++index; // `!`
++index; // `-`
++index; // `-`
skipSingleLineComment(4);
} else {
break;
}
} else {
break;
}
}
}
function scanHexEscape(prefix) {
var i, len, ch, code = 0;
len = (prefix === "u") ? 4 : 2;
for (i = 0; i < len; ++i) {
if (index < length && syntax.isHexDigit(source[index])) {
ch = source[index++];
code = code * 16 + "0123456789abcdef".indexOf(ch.toLowerCase());
} else {
return "";
}
}
return String.fromCharCode(code);
}
/**
* Scans an extended unicode code point escape sequence from source. Throws an
* error if the sequence is empty or if the code point value is too large.
* @returns {string} The string created by the Unicode escape sequence.
* @private
*/
function scanUnicodeCodePointEscape() {
var ch, code, cu1, cu2;
ch = source[index];
code = 0;
// At least one hex digit is required.
if (ch === "}") {
throwError({}, Messages.UnexpectedToken, "ILLEGAL");
}
while (index < length) {
ch = source[index++];
if (!syntax.isHexDigit(ch)) {
break;
}
code = code * 16 + "0123456789abcdef".indexOf(ch.toLowerCase());
}
if (code > 0x10FFFF || ch !== "}") {
throwError({}, Messages.UnexpectedToken, "ILLEGAL");
}
// UTF-16 Encoding
if (code <= 0xFFFF) {
return String.fromCharCode(code);
}
cu1 = ((code - 0x10000) >> 10) + 0xD800;
cu2 = ((code - 0x10000) & 1023) + 0xDC00;
return String.fromCharCode(cu1, cu2);
}
function getEscapedIdentifier() {
var ch, id;
ch = source.charCodeAt(index++);
id = String.fromCharCode(ch);
// "\u" (U+005C, U+0075) denotes an escaped character.
if (ch === 0x5C) {
if (source.charCodeAt(index) !== 0x75) {
throwError({}, Messages.UnexpectedToken, "ILLEGAL");
}
++index;
ch = scanHexEscape("u");
if (!ch || ch === "\\" || !syntax.isIdentifierStart(ch.charCodeAt(0))) {
throwError({}, Messages.UnexpectedToken, "ILLEGAL");
}
id = ch;
}
while (index < length) {
ch = source.charCodeAt(index);
if (!syntax.isIdentifierPart(ch)) {
break;
}
++index;
id += String.fromCharCode(ch);
// "\u" (U+005C, U+0075) denotes an escaped character.
if (ch === 0x5C) {
id = id.substr(0, id.length - 1);
if (source.charCodeAt(index) !== 0x75) {
throwError({}, Messages.UnexpectedToken, "ILLEGAL");
}
++index;
ch = scanHexEscape("u");
if (!ch || ch === "\\" || !syntax.isIdentifierPart(ch.charCodeAt(0))) {
throwError({}, Messages.UnexpectedToken, "ILLEGAL");
}
id += ch;
}
}
return id;
}
function getIdentifier() {
var start, ch;
start = index++;
while (index < length) {
ch = source.charCodeAt(index);
if (ch === 0x5C) {
// Blackslash (U+005C) marks Unicode escape sequence.
index = start;
return getEscapedIdentifier();
}
if (syntax.isIdentifierPart(ch)) {
++index;
} else {
break;
}
}
return source.slice(start, index);
}
function scanIdentifier() {
var start, id, type;
start = index;
// Backslash (U+005C) starts an escaped character.
id = (source.charCodeAt(index) === 0x5C) ? getEscapedIdentifier() : getIdentifier();
// There is no keyword or literal with only one character.
// Thus, it must be an identifier.
if (id.length === 1) {
type = Token.Identifier;
} else if (syntax.isKeyword(id, strict, extra.ecmaFeatures)) {
type = Token.Keyword;
} else if (id === "null") {
type = Token.NullLiteral;
} else if (id === "true" || id === "false") {
type = Token.BooleanLiteral;
} else {
type = Token.Identifier;
}
return {
type: type,
value: id,
lineNumber: lineNumber,
lineStart: lineStart,
range: [start, index]
};
}
// 7.7 Punctuators
function scanPunctuator() {
var start = index,
code = source.charCodeAt(index),
code2,
ch1 = source[index],
ch2,
ch3,
ch4;
switch (code) {
// Check for most common single-character punctuators.
case 40: // ( open bracket
case 41: // ) close bracket
case 59: // ; semicolon
case 44: // , comma
case 91: // [
case 93: // ]
case 58: // :
case 63: // ?
case 126: // ~
++index;
if (extra.tokenize && code === 40) {
extra.openParenToken = extra.tokens.length;
}
return {
type: Token.Punctuator,
value: String.fromCharCode(code),
lineNumber: lineNumber,
lineStart: lineStart,
range: [start, index]
};
case 123: // { open curly brace
case 125: // } close curly brace
++index;
if (extra.tokenize && code === 123) {
extra.openCurlyToken = extra.tokens.length;
}
// lookahead2 function can cause tokens to be scanned twice and in doing so
// would wreck the curly stack by pushing the same token onto the stack twice.
// curlyLastIndex ensures each token is pushed or popped exactly once
if (index > state.curlyLastIndex) {
state.curlyLastIndex = index;
if (code === 123) {
state.curlyStack.push("{");
} else {
state.curlyStack.pop();
}
}
return {
type: Token.Punctuator,
value: String.fromCharCode(code),
lineNumber: lineNumber,
lineStart: lineStart,
range: [start, index]
};
default:
code2 = source.charCodeAt(index + 1);
// "=" (char #61) marks an assignment or comparison operator.
if (code2 === 61) {
switch (code) {
case 37: // %
case 38: // &
case 42: // *:
case 43: // +
case 45: // -
case 47: // /
case 60: // <
case 62: // >
case 94: // ^
case 124: // |
index += 2;
return {
type: Token.Punctuator,
value: String.fromCharCode(code) + String.fromCharCode(code2),
lineNumber: lineNumber,
lineStart: lineStart,
range: [start, index]
};
case 33: // !
case 61: // =
index += 2;
// !== and ===
if (source.charCodeAt(index) === 61) {
++index;
}
return {
type: Token.Punctuator,
value: source.slice(start, index),
lineNumber: lineNumber,
lineStart: lineStart,
range: [start, index]
};
default:
break;
}
}
break;
}
// Peek more characters.
ch2 = source[index + 1];
ch3 = source[index + 2];
ch4 = source[index + 3];
// 4-character punctuator: >>>=
if (ch1 === ">" && ch2 === ">" && ch3 === ">") {
if (ch4 === "=") {
index += 4;
return {
type: Token.Punctuator,
value: ">>>=",
lineNumber: lineNumber,
lineStart: lineStart,
range: [start, index]
};
}
}
// 3-character punctuators: === !== >>> <<= >>=
if (ch1 === ">" && ch2 === ">" && ch3 === ">") {
index += 3;
return {
type: Token.Punctuator,
value: ">>>",
lineNumber: lineNumber,
lineStart: lineStart,
range: [start, index]
};
}
if (ch1 === "<" && ch2 === "<" && ch3 === "=") {
index += 3;
return {
type: Token.Punctuator,
value: "<<=",
lineNumber: lineNumber,
lineStart: lineStart,
range: [start, index]
};
}
if (ch1 === ">" && ch2 === ">" && ch3 === "=") {
index += 3;
return {
type: Token.Punctuator,
value: ">>=",
lineNumber: lineNumber,
lineStart: lineStart,
range: [start, index]
};
}
// The ... operator (spread, restParams, JSX, etc.)
if (extra.ecmaFeatures.spread ||
extra.ecmaFeatures.restParams ||
extra.ecmaFeatures.experimentalObjectRestSpread ||
(extra.ecmaFeatures.jsx && state.inJSXSpreadAttribute)
) {
if (ch1 === "." && ch2 === "." && ch3 === ".") {
index += 3;
return {
type: Token.Punctuator,
value: "...",
lineNumber: lineNumber,
lineStart: lineStart,
range: [start, index]
};
}
}
// Other 2-character punctuators: ++ -- << >> && ||
if (ch1 === ch2 && ("+-<>&|".indexOf(ch1) >= 0)) {
index += 2;
return {
type: Token.Punctuator,
value: ch1 + ch2,
lineNumber: lineNumber,
lineStart: lineStart,
range: [start, index]
};
}
// the => for arrow functions
if (extra.ecmaFeatures.arrowFunctions) {
if (ch1 === "=" && ch2 === ">") {
index += 2;
return {
type: Token.Punctuator,
value: "=>",
lineNumber: lineNumber,
lineStart: lineStart,
range: [start, index]
};
}
}
if ("<>=!+-*%&|^/".indexOf(ch1) >= 0) {
++index;
return {
type: Token.Punctuator,
value: ch1,
lineNumber: lineNumber,
lineStart: lineStart,
range: [start, index]
};
}
if (ch1 === ".") {
++index;
return {
type: Token.Punctuator,
value: ch1,
lineNumber: lineNumber,
lineStart: lineStart,
range: [start, index]
};
}
throwError({}, Messages.UnexpectedToken, "ILLEGAL");
}
// 7.8.3 Numeric Literals
function scanHexLiteral(start) {
var number = "";
while (index < length) {
if (!syntax.isHexDigit(source[index])) {
break;
}
number += source[index++];
}
if (number.length === 0) {
throwError({}, Messages.UnexpectedToken, "ILLEGAL");
}
if (syntax.isIdentifierStart(source.charCodeAt(index))) {
throwError({}, Messages.UnexpectedToken, "ILLEGAL");
}
return {
type: Token.NumericLiteral,
value: parseInt("0x" + number, 16),
lineNumber: lineNumber,
lineStart: lineStart,
range: [start, index]
};
}
function scanBinaryLiteral(start) {
var ch, number = "";
while (index < length) {
ch = source[index];
if (ch !== "0" && ch !== "1") {
break;
}
number += source[index++];
}
if (number.length === 0) {
// only 0b or 0B
throwError({}, Messages.UnexpectedToken, "ILLEGAL");
}
if (index < length) {
ch = source.charCodeAt(index);
/* istanbul ignore else */
if (syntax.isIdentifierStart(ch) || syntax.isDecimalDigit(ch)) {
throwError({}, Messages.UnexpectedToken, "ILLEGAL");
}
}
return {
type: Token.NumericLiteral,
value: parseInt(number, 2),
lineNumber: lineNumber,
lineStart: lineStart,
range: [start, index]
};
}
function scanOctalLiteral(prefix, start) {
var number, octal;
if (syntax.isOctalDigit(prefix)) {
octal = true;
number = "0" + source[index++];
} else {
octal = false;
++index;
number = "";
}
while (index < length) {
if (!syntax.isOctalDigit(source[index])) {
break;
}
number += source[index++];
}
if (!octal && number.length === 0) {
// only 0o or 0O
throwError({}, Messages.UnexpectedToken, "ILLEGAL");
}
if (syntax.isIdentifierStart(source.charCodeAt(index)) || syntax.isDecimalDigit(source.charCodeAt(index))) {
throwError({}, Messages.UnexpectedToken, "ILLEGAL");
}
return {
type: Token.NumericLiteral,
value: parseInt(number, 8),
octal: octal,
lineNumber: lineNumber,
lineStart: lineStart,
range: [start, index]
};
}
function scanNumericLiteral() {
var number, start, ch;
ch = source[index];
assert(syntax.isDecimalDigit(ch.charCodeAt(0)) || (ch === "."),
"Numeric literal must start with a decimal digit or a decimal point");
start = index;
number = "";
if (ch !== ".") {
number = source[index++];
ch = source[index];
// Hex number starts with "0x".
// Octal number starts with "0".
if (number === "0") {
if (ch === "x" || ch === "X") {
++index;
return scanHexLiteral(start);
}
// Binary number in ES6 starts with '0b'
if (extra.ecmaFeatures.binaryLiterals) {
if (ch === "b" || ch === "B") {
++index;
return scanBinaryLiteral(start);
}
}
if ((extra.ecmaFeatures.octalLiterals && (ch === "o" || ch === "O")) || syntax.isOctalDigit(ch)) {
return scanOctalLiteral(ch, start);
}
// decimal number starts with "0" such as "09" is illegal.
if (ch && syntax.isDecimalDigit(ch.charCodeAt(0))) {
throwError({}, Messages.UnexpectedToken, "ILLEGAL");
}
}
while (syntax.isDecimalDigit(source.charCodeAt(index))) {
number += source[index++];
}
ch = source[index];
}
if (ch === ".") {
number += source[index++];
while (syntax.isDecimalDigit(source.charCodeAt(index))) {
number += source[index++];
}
ch = source[index];
}
if (ch === "e" || ch === "E") {
number += source[index++];
ch = source[index];
if (ch === "+" || ch === "-") {
number += source[index++];
}
if (syntax.isDecimalDigit(source.charCodeAt(index))) {
while (syntax.isDecimalDigit(source.charCodeAt(index))) {
number += source[index++];
}
} else {
throwError({}, Messages.UnexpectedToken, "ILLEGAL");
}
}
if (syntax.isIdentifierStart(source.charCodeAt(index))) {
throwError({}, Messages.UnexpectedToken, "ILLEGAL");
}
return {
type: Token.NumericLiteral,
value: parseFloat(number),
lineNumber: lineNumber,
lineStart: lineStart,
range: [start, index]
};
}
/**
* Scan a string escape sequence and return its special character.
* @param {string} ch The starting character of the given sequence.
* @returns {Object} An object containing the character and a flag
* if the escape sequence was an octal.
* @private
*/
function scanEscapeSequence(ch) {
var code,
unescaped,
restore,
escapedCh,
octal = false;
// An escape sequence cannot be empty
if (!ch) {
throwError({}, Messages.UnexpectedToken, "ILLEGAL");
}
if (syntax.isLineTerminator(ch.charCodeAt(0))) {
++lineNumber;
if (ch === "\r" && source[index] === "\n") {
++index;
}
lineStart = index;
escapedCh = "";
} else if (ch === "u" && source[index] === "{") {
// Handle ES6 extended unicode code point escape sequences.
if (extra.ecmaFeatures.unicodeCodePointEscapes) {
++index;
escapedCh = scanUnicodeCodePointEscape();
} else {
throwError({}, Messages.UnexpectedToken, "ILLEGAL");
}
} else if (ch === "u" || ch === "x") {
// Handle other unicode and hex codes normally
restore = index;
unescaped = scanHexEscape(ch);
if (unescaped) {
escapedCh = unescaped;
} else {
index = restore;
escapedCh = ch;
}
} else if (ch === "n") {
escapedCh = "\n";
} else if (ch === "r") {
escapedCh = "\r";
} else if (ch === "t") {
escapedCh = "\t";
} else if (ch === "b") {
escapedCh = "\b";
} else if (ch === "f") {
escapedCh = "\f";
} else if (ch === "v") {
escapedCh = "\v";
} else if (syntax.isOctalDigit(ch)) {
code = "01234567".indexOf(ch);
// \0 is not octal escape sequence
if (code !== 0) {
octal = true;
}
if (index < length && syntax.isOctalDigit(source[index])) {
octal = true;
code = code * 8 + "01234567".indexOf(source[index++]);
// 3 digits are only allowed when string starts with 0, 1, 2, 3
if ("0123".indexOf(ch) >= 0 &&
index < length &&
syntax.isOctalDigit(source[index])) {
code = code * 8 + "01234567".indexOf(source[index++]);
}
}
escapedCh = String.fromCharCode(code);
} else {
escapedCh = ch;
}
return {
ch: escapedCh,
octal: octal
};
}
function scanStringLiteral() {
var str = "",
ch,
escapedSequence,
octal = false,
start = index,
startLineNumber = lineNumber,
startLineStart = lineStart,
quote = source[index];
assert((quote === "'" || quote === "\""),
"String literal must starts with a quote");
++index;
while (index < length) {
ch = source[index++];
if (syntax.isLineTerminator(ch.charCodeAt(0))) {
break;
} else if (ch === quote) {
quote = "";
break;
} else if (ch === "\\") {
ch = source[index++];
escapedSequence = scanEscapeSequence(ch);
str += escapedSequence.ch;
octal = escapedSequence.octal || octal;
} else {
str += ch;
}
}
if (quote !== "") {
throwError({}, Messages.UnexpectedToken, "ILLEGAL");
}
return {
type: Token.StringLiteral,
value: str,
octal: octal,
startLineNumber: startLineNumber,
startLineStart: startLineStart,
lineNumber: lineNumber,
lineStart: lineStart,
range: [start, index]
};
}
/**
* Scan a template string and return a token. This scans both the first and
* subsequent pieces of a template string and assumes that the first backtick
* or the closing } have already been scanned.
* @returns {Token} The template string token.
* @private
*/
function scanTemplate() {
var cooked = "",
ch,
escapedSequence,
start = index,
terminated = false,
tail = false,
head = (source[index] === "`");
++index;
while (index < length) {
ch = source[index++];
if (ch === "`") {
tail = true;
terminated = true;
break;
} else if (ch === "$") {
if (source[index] === "{") {
++index;