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
|
import { ParserState, Context, Flags } from '../common';
import { Token } from '../token';
import { advanceChar, toHex, NumberKind } from './common';
import { CharTypes, CharFlags, isIdentifierStart } from './charClassifier';
import { Chars } from '../chars';
import { report, Errors, reportScannerError } from '../errors';
/**
* Scans numeric literal
*
* @param parser Parser object
* @param context Context masks
* @param isFloat
*/
export function scanNumber(parser: ParserState, context: Context, kind: NumberKind): Token {
// DecimalLiteral ::
// DecimalIntegerLiteral . DecimalDigits_opt
// . DecimalDigits
let char = parser.currentChar;
let value: any = 0;
let digit = 9;
let atStart = kind & NumberKind.Float ? 0 : 1;
let digits = 0;
let allowSeparator: 0 | 1 = 0;
if (kind & NumberKind.Float) {
value = '.' + scanDecimalDigitsOrSeparator(parser, char);
char = parser.currentChar;
// It is a Syntax Error if the MV is not an integer. (dot decimalDigits)
if (char === Chars.LowerN) report(parser, Errors.InvalidBigInt);
} else {
if (char === Chars.Zero) {
char = advanceChar(parser);
// Hex
if ((char | 32) === Chars.LowerX) {
kind = NumberKind.Hex | NumberKind.ValidBigIntKind;
char = advanceChar(parser); // skips 'X', 'x'
while (CharTypes[char] & (CharFlags.Hex | CharFlags.Underscore)) {
if (char === Chars.Underscore) {
if (!allowSeparator) report(parser, Errors.ContinuousNumericSeparator);
allowSeparator = 0;
char = advanceChar(parser);
continue;
}
allowSeparator = 1;
value = value * 0x10 + toHex(char);
digits++;
char = advanceChar(parser);
}
if (digits < 1 || !allowSeparator) {
report(parser, digits < 1 ? Errors.MissingHexDigits : Errors.TrailingNumericSeparator);
}
// Octal
} else if ((char | 32) === Chars.LowerO) {
kind = NumberKind.Octal | NumberKind.ValidBigIntKind;
char = advanceChar(parser); // skips 'X', 'x'
while (CharTypes[char] & (CharFlags.Octal | CharFlags.Underscore)) {
if (char === Chars.Underscore) {
if (!allowSeparator) {
report(parser, Errors.ContinuousNumericSeparator);
}
allowSeparator = 0;
char = advanceChar(parser);
continue;
}
allowSeparator = 1;
value = value * 8 + (char - Chars.Zero);
digits++;
char = advanceChar(parser);
}
if (digits < 1 || !allowSeparator) {
report(parser, digits < 1 ? Errors.Unexpected : Errors.TrailingNumericSeparator);
}
} else if ((char | 32) === Chars.LowerB) {
kind = NumberKind.Binary | NumberKind.ValidBigIntKind;
char = advanceChar(parser); // skips 'B', 'b'
while (CharTypes[char] & (CharFlags.Binary | CharFlags.Underscore)) {
if (char === Chars.Underscore) {
if (!allowSeparator) {
report(parser, Errors.ContinuousNumericSeparator);
}
allowSeparator = 0;
char = advanceChar(parser);
continue;
}
allowSeparator = 1;
value = value * 2 + (char - Chars.Zero);
digits++;
char = advanceChar(parser);
}
if (digits < 1 || !allowSeparator) {
report(parser, digits < 1 ? Errors.Unexpected : Errors.TrailingNumericSeparator);
}
} else if (CharTypes[char] & CharFlags.Octal) {
// Octal integer literals are not permitted in strict mode code
if (context & Context.Strict) report(parser, Errors.StrictOctalEscape);
kind = NumberKind.ImplicitOctal;
while (CharTypes[char] & CharFlags.Decimal) {
if (CharTypes[char] & CharFlags.ImplicitOctalDigits) {
kind = NumberKind.NonOctalDecimal;
atStart = 0;
break;
}
value = value * 8 + (char - Chars.Zero);
char = advanceChar(parser);
}
} else if (CharTypes[char] & CharFlags.ImplicitOctalDigits) {
if (context & Context.Strict) report(parser, Errors.StrictOctalEscape);
parser.flags |= Flags.Octals;
kind = NumberKind.NonOctalDecimal;
} else if (char === Chars.Underscore) {
report(parser, Errors.Unexpected);
}
}
// Parse decimal digits and allow trailing fractional part
if (kind & NumberKind.DecimalNumberKind) {
if (atStart) {
while (digit >= 0 && CharTypes[char] & (CharFlags.Decimal | CharFlags.Underscore)) {
if (char === Chars.Underscore) {
char = advanceChar(parser);
if (char === Chars.Underscore || kind & NumberKind.NonOctalDecimal) {
reportScannerError(
parser.index,
parser.line,
parser.index + 1 /* skips `_` */,
Errors.ContinuousNumericSeparator
);
}
allowSeparator = 1;
continue;
}
allowSeparator = 0;
value = 10 * value + (char - Chars.Zero);
char = advanceChar(parser);
--digit;
}
if (allowSeparator) {
reportScannerError(
parser.index,
parser.line,
parser.index + 1 /* skips `_` */,
Errors.TrailingNumericSeparator
);
}
if (digit >= 0 && !isIdentifierStart(char) && char !== Chars.Period) {
// Most numbers are pure decimal integers without fractional component
// or exponential notation. Handle that with optimized code.
parser.tokenValue = value;
if (context & Context.OptionsRaw) parser.tokenRaw = parser.source.slice(parser.tokenPos, parser.index);
return Token.NumericLiteral;
}
}
value += scanDecimalDigitsOrSeparator(parser, char);
char = parser.currentChar;
// Consume any decimal dot and fractional component.
if (char === Chars.Period) {
if (advanceChar(parser) === Chars.Underscore) report(parser, Errors.Unexpected);
kind = NumberKind.Float;
value += '.' + scanDecimalDigitsOrSeparator(parser, parser.currentChar);
char = parser.currentChar;
}
}
}
const end = parser.index;
let isBigInt: 0 | 1 = 0;
if (char === Chars.LowerN && kind & NumberKind.ValidBigIntKind) {
isBigInt = 1;
char = advanceChar(parser);
} else {
// Consume any exponential notation.
if ((char | 32) === Chars.LowerE) {
char = advanceChar(parser);
// '-', '+'
if (CharTypes[char] & CharFlags.Exponent) char = advanceChar(parser);
const { index } = parser;
// Exponential notation must contain at least one digit
if ((CharTypes[char] & CharFlags.Decimal) < 1) report(parser, Errors.MissingExponent);
// Consume exponential digits
value += parser.source.substring(end, index) + scanDecimalDigitsOrSeparator(parser, char);
char = parser.currentChar;
}
}
// The source character immediately following a numeric literal must
// not be an identifier start or a decimal digit
if ((parser.index < parser.end && CharTypes[char] & CharFlags.Decimal) || isIdentifierStart(char)) {
report(parser, Errors.IDStartAfterNumber);
}
if (isBigInt) {
parser.tokenRaw = parser.source.slice(parser.tokenPos, parser.index);
parser.tokenValue = BigInt(value);
return Token.BigIntLiteral;
}
parser.tokenValue =
kind & (NumberKind.ImplicitOctal | NumberKind.Binary | NumberKind.Hex | NumberKind.Octal)
? value
: kind & NumberKind.NonOctalDecimal
? parseFloat(parser.source.substring(parser.tokenPos, parser.index))
: +value;
if (context & Context.OptionsRaw) parser.tokenRaw = parser.source.slice(parser.tokenPos, parser.index);
return Token.NumericLiteral;
}
/**
* Scans numeric literal and skip underscore '_' if it exist
*
* @param parser Parser object
* @param char Code point
*/
export function scanDecimalDigitsOrSeparator(parser: ParserState, char: number): string {
let allowSeparator: 0 | 1 = 0;
let start = parser.index;
let ret = '';
while (CharTypes[char] & (CharFlags.Decimal | CharFlags.Underscore)) {
if (char === Chars.Underscore) {
const { index } = parser;
char = advanceChar(parser);
if (char === Chars.Underscore) {
reportScannerError(
parser.index,
parser.line,
parser.index + 1 /* skips `_` */,
Errors.ContinuousNumericSeparator
);
}
allowSeparator = 1;
ret += parser.source.substring(start, index);
start = parser.index;
continue;
}
allowSeparator = 0;
char = advanceChar(parser);
}
if (allowSeparator) {
reportScannerError(parser.index, parser.line, parser.index + 1 /* skips `_` */, Errors.TrailingNumericSeparator);
}
return ret + parser.source.substring(start, parser.index);
}
|