forked from gubaojian/esprima-cpp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
esprima.cpp
3539 lines (2888 loc) · 109 KB
/
esprima.cpp
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
/*
Copyright (C) 2012 Ariya Hidayat <ariya.hidayat@gmail.com>
Copyright (C) 2012 Mathias Bynens <mathias@qiwi.be>
Copyright (C) 2012 Joost-Wim Boekesteijn <joost-wim@boekesteijn.nl>
Copyright (C) 2012 Kris Kowal <kris.kowal@cixar.com>
Copyright (C) 2012 Yusuke Suzuki <utatane.tea@gmail.com>
Copyright (C) 2012 Arpad Borsos <arpad.borsos@googlemail.com>
Copyright (C) 2011 Ariya Hidayat <ariya.hidayat@gmail.com>
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.
*/
#include "esprima.h"
#include <stdarg.h>
#include <sstream>
#include <set>
#include <map>
#undef EOF
using namespace esprima;
Poolable::Poolable(Pool &pool) : poolable(pool.first) {
pool.first = this;
}
namespace Token {
enum {
BooleanLiteral = 1,
EOF = 2,
Identifier = 3,
Keyword = 4,
NullLiteral = 5,
NumericLiteral = 6,
Punctuator = 7,
StringLiteral = 8
};
}
namespace PropertyKind {
enum {
Data = 1,
Get = 2,
Set = 4
};
}
namespace Messages {
static const char *UnexpectedToken = "Unexpected token %0";
static const char *UnexpectedNumber = "Unexpected number";
static const char *UnexpectedString = "Unexpected string";
static const char *UnexpectedIdentifier = "Unexpected identifier";
static const char *UnexpectedReserved = "Unexpected reserved word";
static const char *UnexpectedEOS = "Unexpected end of input";
static const char *NewlineAfterThrow = "Illegal newline after throw";
// static const char *InvalidRegExp = "Invalid regular expression";
static const char *UnterminatedRegExp = "Invalid regular expression: missing /";
static const char *InvalidLHSInAssignment = "Invalid left-hand side in assignment";
static const char *InvalidLHSInForIn = "Invalid left-hand side in for-in";
static const char *MultipleDefaultsInSwitch = "More than one default clause in switch statement";
static const char *NoCatchOrFinally = "Missing catch or finally after try";
static const char *UnknownLabel = "Undefined label '%0'";
static const char *Redeclaration = "%0 '%1' has already been declared";
static const char *IllegalContinue = "Illegal continue statement";
static const char *IllegalBreak = "Illegal break statement";
static const char *IllegalReturn = "Illegal return statement";
static const char *StrictModeWith = "Strict mode code may not include a with statement";
static const char *StrictCatchVariable = "Catch variable may not be eval or arguments in strict mode";
static const char *StrictVarName = "Variable name may not be eval or arguments in strict mode";
static const char *StrictParamName = "Parameter name eval or arguments is not allowed in strict mode";
static const char *StrictParamDupe = "Strict mode function may not have duplicate parameter names";
static const char *StrictFunctionName = "Function name may not be eval or arguments in strict mode";
static const char *StrictOctalLiteral = "Octal literals are not allowed in strict mode.";
static const char *StrictDelete = "Delete of an unqualified identifier in strict mode.";
static const char *StrictDuplicateProperty = "Duplicate data property in object literal not allowed in strict mode";
static const char *AccessorDataProperty = "Object literal may not have data and accessor property with the same name";
static const char *AccessorGetSet = "Object literal may not have multiple get/set accessors with the same name";
static const char *StrictLHSAssignment = "Assignment to eval or arguments is not allowed in strict mode";
static const char *StrictLHSPostfix = "Postfix increment/decrement may not have eval or arguments operand in strict mode";
static const char *StrictLHSPrefix = "Prefix increment/decrement may not have eval or arguments operand in strict mode";
static const char *StrictReservedWord = "Use of future reserved word in strict mode";
}
static std::string format(std::string format, const std::string &arg0) {
size_t index = format.find("%0");
assert(index != std::string::npos);
format.replace(index, 2, arg0);
return format;
}
static std::string format(std::string format, const std::string &arg0, const std::string &arg1) {
size_t index = format.find("%0");
assert(index != std::string::npos);
format.replace(index, 2, arg0);
index = format.find("%1");
assert(index != std::string::npos);
format.replace(index, 2, arg1);
return format;
}
struct EsprimaToken : Poolable {
int type;
std::string stringValue;
double doubleValue;
bool octal;
int lineNumber;
int lineStart;
int range[2];
EsprimaToken(Pool &pool) : Poolable(pool), type(), doubleValue(), octal(), lineNumber(), lineStart() { range[0] = range[1] = 0; }
};
struct EsprimaParser {
static bool isDecimalDigit(int ch) {
return (ch >= 48 && ch <= 57); // 0..9
}
static bool isHexDigit(int ch) {
return std::string("0123456789abcdefABCDEF").find(ch) != std::string::npos;
}
static bool isOctalDigit(int ch) {
return std::string("01234567").find(ch) != std::string::npos;
}
// 7.2 White Space
static bool isWhiteSpace(int ch) {
return (ch == 32) || // space
(ch == 9) || // tab
(ch == 0xB) ||
(ch == 0xC) ||
(ch == 0xA0);
}
// 7.3 Line Terminators
static bool isLineTerminator(int ch) {
return (ch == 10) || (ch == 13) || (ch == 0x2028) || (ch == 0x2029);
}
// 7.6 Identifier Names and Identifiers
static bool isIdentifierStart(int ch) {
return (ch == 36) || (ch == 95) || // $ (dollar) and _ (underscore)
(ch >= 65 && ch <= 90) || // A..Z
(ch >= 97 && ch <= 122) || // a..z
(ch == 92); // \ (backslash)
}
static bool isIdentifierPart(int ch) {
return (ch == 36) || (ch == 95) || // $ (dollar) and _ (underscore)
(ch >= 65 && ch <= 90) || // A..Z
(ch >= 97 && ch <= 122) || // a..z
(ch >= 48 && ch <= 57) || // 0..9
(ch == 92); // \ (backslash)
}
// 7.6.1.2 Future Reserved Words
static bool isFutureReservedWord(const std::string &id) {
return
id == "class" ||
id == "enum" ||
id == "export" ||
id == "extends" ||
id == "import" ||
id == "super";
}
static bool isStrictModeReservedWord(const std::string &id) {
return
id == "implements" ||
id == "interface" ||
id == "package" ||
id == "private" ||
id == "protected" ||
id == "public" ||
id == "static" ||
id == "yield" ||
id == "let";
}
static bool isRestrictedWord(const std::string &id) {
return id == "eval" || id == "arguments";
}
// 7.6.1.1 Keywords
bool isKeyword(const std::string &id) {
if (strict && isStrictModeReservedWord(id)) {
return true;
}
// 'const' is specialized as Keyword in V8.
// 'yield' and 'let' are for compatiblity with SpiderMonkey and ES.next.
// Some others are from future reserved words.
return
(id == "if") ||
(id == "in") ||
(id == "do") ||
(id == "var") ||
(id == "for") ||
(id == "new") ||
(id == "try") ||
(id == "let") ||
(id == "this") ||
(id == "else") ||
(id == "case") ||
(id == "void") ||
(id == "with") ||
(id == "enum") ||
(id == "while") ||
(id == "break") ||
(id == "catch") ||
(id == "throw") ||
(id == "const") ||
(id == "yield") ||
(id == "class") ||
(id == "super") ||
(id == "return") ||
(id == "typeof") ||
(id == "delete") ||
(id == "switch") ||
(id == "export") ||
(id == "import") ||
(id == "default") ||
(id == "finally") ||
(id == "extends") ||
(id == "function") ||
(id == "continue") ||
(id == "debugger") ||
(id == "instanceof");
}
// 7.4 Comments
void skipComment() {
int ch;
bool blockComment;
bool lineComment;
blockComment = false;
lineComment = false;
while (index < length) {
ch = source[index];
if (lineComment) {
++index;
if (isLineTerminator(ch)) {
lineComment = false;
if (ch == 13 && source[index] == 10) {
++index;
}
++lineNumber;
lineStart = index;
}
} else if (blockComment) {
if (isLineTerminator(ch)) {
if (ch == 13 && source[index + 1] == 10) {
++index;
}
++lineNumber;
++index;
lineStart = index;
if (index >= length) {
throwError(NULL, format(Messages::UnexpectedToken, "ILLEGAL"));
}
} else {
ch = source[index++];
if (index >= length) {
throwError(NULL, format(Messages::UnexpectedToken, "ILLEGAL"));
}
// Block comment ends with '*/' (char #42, char #47).
if (ch == 42) {
ch = source[index];
if (ch == 47) {
++index;
blockComment = false;
}
}
}
} else if (ch == 47) {
ch = source[index + 1];
// Line comment starts with '//' (char #47, char #47).
if (ch == 47) {
index += 2;
lineComment = true;
} else if (ch == 42) {
// Block comment starts with '/*' (char #47, char #42).
index += 2;
blockComment = true;
if (index >= length) {
throwError(NULL, format(Messages::UnexpectedToken, "ILLEGAL"));
}
} else {
break;
}
} else if (isWhiteSpace(ch)) {
++index;
} else if (isLineTerminator(ch)) {
++index;
if (ch == 13 && source[index] == 10) {
++index;
}
++lineNumber;
lineStart = index;
} else {
break;
}
}
}
int scanHexEscape(int prefix) {
int i, len, ch, code = 0;
len = (prefix == 'u') ? 4 : 2;
for (i = 0; i < len; ++i) {
if (index < length && isHexDigit(source[index])) {
ch = source[index++];
code = code * 16 + std::string("0123456789abcdef").find(tolower(ch));
} else {
return -1;
}
}
return code;
}
std::string getEscapedIdentifier() {
int ch;
std::string id;
ch = source[index++];
id = std::string(1, ch);
// '\u' (char #92, char #117) denotes an escaped character.
if (ch == 92) {
if (source[index] != 117) {
throwError(NULL, format(Messages::UnexpectedToken, "ILLEGAL"));
}
++index;
ch = scanHexEscape('u');
if (ch == -1 || ch == '\\' || !isIdentifierStart(ch)) {
throwError(NULL, format(Messages::UnexpectedToken, "ILLEGAL"));
}
id = ch;
}
while (index < length) {
ch = source[index];
if (!isIdentifierPart(ch)) {
break;
}
++index;
id += ch;
// '\u' (char #92, char #117) denotes an escaped character.
if (ch == 92) {
id = id.substr(0, id.size() - 1);
if (source[index] != 117) {
throwError(NULL, format(Messages::UnexpectedToken, "ILLEGAL"));
}
++index;
ch = scanHexEscape('u');
if (ch == -1 || ch == '\\' || !isIdentifierPart(ch)) {
throwError(NULL, format(Messages::UnexpectedToken, "ILLEGAL"));
}
id += ch;
}
}
return id;
}
std::string getIdentifier() {
int start, ch;
start = index++;
while (index < length) {
ch = source[index];
if (ch == 92) {
// Blackslash (char #92) marks Unicode escape sequence.
index = start;
return getEscapedIdentifier();
}
if (isIdentifierPart(ch)) {
++index;
} else {
break;
}
}
return source.substr(start, index - start);
}
EsprimaToken *scanIdentifier() {
int start, type;
std::string id;
start = index;
// Backslash (char #92) starts an escaped character.
id = (source[index] == 92) ? getEscapedIdentifier() : getIdentifier();
// There is no keyword or literal with only one character.
// Thus, it must be an identifier.
if (id.size() == 1) {
type = Token::Identifier;
} else if (isKeyword(id)) {
type = Token::Keyword;
} else if (id == "null") {
type = Token::NullLiteral;
} else if (id == "true" || id == "false") {
type = Token::BooleanLiteral;
} else {
type = Token::Identifier;
}
EsprimaToken *token = new EsprimaToken(pool);
token->type = type;
token->stringValue = id;
token->lineNumber = lineNumber;
token->lineStart = lineStart;
token->range[0] = start; token->range[1] = index;
return token;
}
// 7.7 Punctuators
EsprimaToken *scanPunctuator() {
EsprimaToken *token;
int start = index;
int code = source[index];
int code2;
int ch1 = source[index];
int ch2;
int ch3;
int ch4;
switch (code) {
// Check for most common single-character punctuators.
case 46: // . dot
case 40: // ( open bracket
case 41: // ) close bracket
case 59: // ; semicolon
case 44: // , comma
case 123: // { open curly brace
case 125: // } close curly brace
case 91: // [
case 93: // ]
case 58: // :
case 63: // ?
case 126: // ~
++index;
token = new EsprimaToken(pool);
token->type = Token::Punctuator;
token->stringValue = std::string(1, code);
token->lineNumber = lineNumber;
token->lineStart = lineStart;
token->range[0] = start; token->range[1] = index;
return token;
default:
code2 = source[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;
token = new EsprimaToken(pool);
token->type = Token::Punctuator;
token->stringValue = std::string(1, code) + std::string(1, code2);
token->lineNumber = lineNumber;
token->lineStart = lineStart;
token->range[0] = start; token->range[1] = index;
return token;
case 33: // !
case 61: // =
index += 2;
// != and ==
if (source[index] == 61) {
++index;
}
token = new EsprimaToken(pool);
token->type = Token::Punctuator;
token->stringValue = source.substr(start, index - start);
token->lineNumber = lineNumber;
token->lineStart = lineStart;
token->range[0] = start; token->range[1] = index;
return token;
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;
token = new EsprimaToken(pool);
token->type = Token::Punctuator;
token->stringValue = ">>>=";
token->lineNumber = lineNumber;
token->lineStart = lineStart;
token->range[0] = start; token->range[1] = index;
return token;
}
}
// 3-character punctuators: == != >>> <<= >>=
if (ch1 == '>' && ch2 == '>' && ch3 == '>') {
index += 3;
token = new EsprimaToken(pool);
token->type = Token::Punctuator;
token->stringValue = ">>>";
token->lineNumber = lineNumber;
token->lineStart = lineStart;
token->range[0] = start; token->range[1] = index;
return token;
}
if (ch1 == '<' && ch2 == '<' && ch3 == '=') {
index += 3;
token = new EsprimaToken(pool);
token->type = Token::Punctuator;
token->stringValue = "<<=";
token->lineNumber = lineNumber;
token->lineStart = lineStart;
token->range[0] = start; token->range[1] = index;
return token;
}
if (ch1 == '>' && ch2 == '>' && ch3 == '=') {
index += 3;
token = new EsprimaToken(pool);
token->type = Token::Punctuator;
token->stringValue = ">>=";
token->lineNumber = lineNumber;
token->lineStart = lineStart;
token->range[0] = start; token->range[1] = index;
return token;
}
// Other 2-character punctuators: ++ -- << >> && ||
if (ch1 == ch2 && std::string("+-<>&|").find(ch1) != std::string::npos) {
index += 2;
token = new EsprimaToken(pool);
token->type = Token::Punctuator;
token->stringValue = std::string(1, ch1) + std::string(1, ch2);
token->lineNumber = lineNumber;
token->lineStart = lineStart;
token->range[0] = start; token->range[1] = index;
return token;
}
if (std::string("<>=!+-*%&|^/").find(ch1) != std::string::npos) {
++index;
token = new EsprimaToken(pool);
token->type = Token::Punctuator;
token->stringValue = std::string(1, ch1);
token->lineNumber = lineNumber;
token->lineStart = lineStart;
token->range[0] = start; token->range[1] = index;
return token;
}
throwError(NULL, format(Messages::UnexpectedToken, "ILLEGAL"));
}
// 7.8.3 Numeric Literals
EsprimaToken *scanHexLiteral(int start) {
std::string number = "";
while (index < length) {
if (!isHexDigit(source[index])) {
break;
}
number += source[index++];
}
if (number.size() == 0) {
throwError(NULL, format(Messages::UnexpectedToken, "ILLEGAL"));
}
if (isIdentifierStart(source[index])) {
throwError(NULL, format(Messages::UnexpectedToken, "ILLEGAL"));
}
EsprimaToken *token = new EsprimaToken(pool);
token->type = Token::NumericLiteral;
std::stringstream(number) >> std::hex >> token->doubleValue;
token->lineNumber = lineNumber;
token->lineStart = lineStart;
token->range[0] = start; token->range[1] = index;
return token;
}
EsprimaToken *scanOctalLiteral(int start) {
std::string number = std::string(1, source[index++]);
while (index < length) {
if (!isOctalDigit(source[index])) {
break;
}
number += source[index++];
}
if (isIdentifierStart(source[index]) || isDecimalDigit(source[index])) {
throwError(NULL, format(Messages::UnexpectedToken, "ILLEGAL"));
}
EsprimaToken *token = new EsprimaToken(pool);
token->type = Token::NumericLiteral;
std::stringstream(number) >> std::oct >> token->doubleValue;
token->octal = true;
token->lineNumber = lineNumber;
token->lineStart = lineStart;
token->range[0] = start; token->range[1] = index;
return token;
}
EsprimaToken *scanNumericLiteral() {
std::string number;
int start, ch;
ch = source[index];
assert(isDecimalDigit(ch) || (ch == '.') &&
"Numeric literal must start with a decimal digit or a decimal point");
start = index;
number = "";
if (ch != '.') {
number = std::string(1, 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);
}
if (isOctalDigit(ch)) {
return scanOctalLiteral(start);
}
// decimal number starts with '0' such as '09' is illegal.
if (ch && isDecimalDigit(ch)) {
throwError(NULL, format(Messages::UnexpectedToken, "ILLEGAL"));
}
}
while (isDecimalDigit(source[index])) {
number += source[index++];
}
ch = source[index];
}
if (ch == '.') {
number += source[index++];
while (isDecimalDigit(source[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 (isDecimalDigit(source[index])) {
while (isDecimalDigit(source[index])) {
number += source[index++];
}
} else {
throwError(NULL, format(Messages::UnexpectedToken, "ILLEGAL"));
}
}
if (isIdentifierStart(source[index])) {
throwError(NULL, format(Messages::UnexpectedToken, "ILLEGAL"));
}
EsprimaToken *token = new EsprimaToken(pool);
token->type = Token::NumericLiteral;
std::stringstream(number) >> token->doubleValue;
token->lineNumber = lineNumber;
token->lineStart = lineStart;
token->range[0] = start; token->range[1] = index;
return token;
}
// 7.8.4 String Literals
EsprimaToken *scanStringLiteral() {
std::string str = "";
int quote, start, ch, code, unescaped, restore;
bool octal = false;
quote = source[index];
assert((quote == '\'' || quote == '"') &&
"String literal must starts with a quote");
start = index;
++index;
while (index < length) {
ch = source[index++];
if (ch == quote) {
quote = 0;
break;
} else if (ch == '\\') {
ch = source[index++];
if (!ch || !isLineTerminator(ch)) {
switch (ch) {
case 'n':
str += '\n';
break;
case 'r':
str += '\r';
break;
case 't':
str += '\t';
break;
case 'u':
case 'x':
restore = index;
unescaped = scanHexEscape(ch);
if (unescaped != -1) {
str += unescaped;
} else {
index = restore;
str += ch;
}
break;
case 'b':
str += '\b';
break;
case 'f':
str += '\f';
break;
case 'v':
str += '\v';
break;
default:
if (isOctalDigit(ch)) {
code = ch - '0';
// \0 is not octal escape sequence
if (code != 0) {
octal = true;
}
if (index < length && isOctalDigit(source[index])) {
octal = true;
code = code * 8 + source[index++] - '0';
// 3 digits are only allowed when string starts
// with 0, 1, 2, 3
if (std::string(1, '0123').find(ch) != std::string::npos &&
index < length &&
isOctalDigit(source[index])) {
code = code * 8 + source[index++] - '0';
}
}
str += std::string(1, code);
} else {
str += ch;
}
break;
}
} else {
++lineNumber;
if (ch == '\r' && source[index] == '\n') {
++index;
}
}
} else if (isLineTerminator(ch)) {
break;
} else {
str += ch;
}
}
if (quote != 0) {
throwError(NULL, format(Messages::UnexpectedToken, "ILLEGAL"));
}
EsprimaToken *token = new EsprimaToken(pool);
token->type = Token::StringLiteral;
token->stringValue = str;
token->octal = octal;
token->lineNumber = lineNumber;
token->lineStart = lineStart;
token->range[0] = start; token->range[1] = index;
return token;
}
struct EsprimaRegExp : Poolable {
std::string literal, pattern, flags;
int range[2];
EsprimaRegExp(Pool& pool, const std::string &literal, const std::string &pattern, const std::string &flags, int rangeMin, int rangeMax)
: Poolable(pool), literal(literal), pattern(pattern), flags(flags) {
range[0] = rangeMin;
range[1] = rangeMax;
}
};
EsprimaRegExp *scanRegExp() {
std::string str;
int ch, start;
std::string pattern, flags;
bool classMarker = false;
int restore;
bool terminated = false;
lookahead = NULL;
skipComment();
start = index;
ch = source[index];
assert(ch == '/' && "Regular expression literal must start with a slash");
str = source[index++];
while (index < length) {
ch = source[index++];
str += ch;
if (classMarker) {
if (ch == ']') {
classMarker = false;
}
} else {
if (ch == '\\') {
ch = source[index++];
// ECMA-262 7.8.5
if (isLineTerminator(ch)) {
throwError(NULL, Messages::UnterminatedRegExp);
}
str += ch;
} else if (ch == '/') {
terminated = true;
break;
} else if (ch == '[') {
classMarker = true;
} else if (isLineTerminator(ch)) {
throwError(NULL, Messages::UnterminatedRegExp);
}
}
}
if (!terminated) {
throwError(NULL, Messages::UnterminatedRegExp);
}
// Exclude leading and trailing slash.
pattern = str.substr(1, str.size() - 2);
flags = "";
while (index < length) {
ch = source[index];
if (!isIdentifierPart(ch)) {
break;
}
++index;
if (ch == '\\' && index < length) {
ch = source[index];
if (ch == 'u') {
++index;
restore = index;
ch = scanHexEscape('u');
if (ch != -1) {
flags += ch;
for (str += "\\u"; restore < index; ++restore) {
str += source[restore];
}
} else {
index = restore;
flags += 'u';
str += "\\u";
}
} else {
str += '\\';
}
} else {
flags += ch;
str += ch;
}
}
peek();
return new EsprimaRegExp(pool,
str,
pattern,
flags,
start, index
);
}
bool isIdentifierName(EsprimaToken *token) {
return token->type == Token::Identifier ||
token->type == Token::Keyword ||
token->type == Token::BooleanLiteral ||
token->type == Token::NullLiteral;
}
EsprimaToken *advance() {
int ch;
skipComment();
if (index >= length) {
EsprimaToken *token = new EsprimaToken(pool);
token->type = Token::EOF;
token->lineNumber = lineNumber;
token->lineStart = lineStart;
token->range[0] = index; token->range[1] = index;
return token;
}
ch = source[index];
// Very common: ( and ) and ;
if (ch == 40 || ch == 41 || ch == 58) {
return scanPunctuator();
}
// String literal starts with single quote (#39) or double quote (#34).
if (ch == 39 || ch == 34) {
return scanStringLiteral();
}
if (isIdentifierStart(ch)) {
return scanIdentifier();
}
// Dot (.) char #46 can also start a floating-point number, hence the need
// to check the next character.
if (ch == 46) {
if (isDecimalDigit(source[index + 1])) {
return scanNumericLiteral();
}
return scanPunctuator();
}
if (isDecimalDigit(ch)) {
return scanNumericLiteral();
}