Skip to content

Commit

Permalink
fix(lexer): tweaked ident scanning
Browse files Browse the repository at this point in the history
  • Loading branch information
KFlash committed May 26, 2019
1 parent 3708214 commit a205210
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 35 deletions.
35 changes: 18 additions & 17 deletions src/lexer/identifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { report, Errors } from '../errors';
import { unicodeLookup } from '../unicode';

export function scanIdentifier(parser: ParserState, context: Context): Token {
let hasEscape = false;
let canBeKeyword = (CharTypes[parser.currentCodePoint] & CharFlags.KeywordCandidate) !== 0;
let hasEscape: 0 | 1 = 0;
let canBeKeyword: number = CharTypes[parser.currentCodePoint] & CharFlags.KeywordCandidate;
parser.tokenValue = '';
if (parser.currentCodePoint <= 0x7e) {
if ((CharTypes[parser.currentCodePoint] & CharFlags.BackSlash) === 0) {
Expand All @@ -20,10 +20,10 @@ export function scanIdentifier(parser: ParserState, context: Context): Token {
return descKeywordTable[parser.tokenValue] || Token.Identifier;
}
} else {
hasEscape = true;
hasEscape = 1;
const code = scanIdentifierUnicodeEscape(parser);
if (!isIdentifierPart(code)) report(parser, Errors.InvalidUnicodeEscapeSequence);
canBeKeyword = (CharTypes[code] & CharFlags.KeywordCandidate) !== 0;
canBeKeyword = CharTypes[code] & CharFlags.KeywordCandidate;
parser.tokenValue += fromCodePoint(code);
}
}
Expand All @@ -34,17 +34,17 @@ export function scanIdentifier(parser: ParserState, context: Context): Token {
export function scanIdentifierSlowCase(
parser: ParserState,
context: Context,
hasEscape: boolean,
canBeKeyword: boolean
hasEscape: 0 | 1,
canBeKeyword: number
): Token {
let start = parser.index;
while (parser.index < parser.length) {
if ((parser.currentCodePoint & 8) === 8 && parser.currentCodePoint === Chars.Backslash) {
if (CharTypes[parser.currentCodePoint] & CharFlags.BackSlash) {
parser.tokenValue += parser.source.slice(start, parser.index);
hasEscape = true;
hasEscape = 1;
const code = scanIdentifierUnicodeEscape(parser);
if (!isIdentifierPart(code)) report(parser, Errors.InvalidUnicodeEscapeSequence);
canBeKeyword = canBeKeyword && (CharTypes[code] & CharFlags.KeywordCandidate) !== 0;
canBeKeyword = canBeKeyword && CharTypes[code] & CharFlags.KeywordCandidate;
parser.tokenValue += fromCodePoint(code);
start = parser.index;
} else if (
Expand All @@ -65,15 +65,16 @@ export function scanIdentifierSlowCase(

if (canBeKeyword && (length >= 2 && length <= 11)) {
const keyword: Token | undefined = descKeywordTable[parser.tokenValue as string];
if (keyword === undefined) return Token.Identifier;

if (keyword === Token.YieldKeyword) return keyword;

if ((keyword & Token.FutureReserved) === Token.FutureReserved) {
return context & Context.Strict && hasEscape ? Token.EscapedFutureReserved : keyword;
}

return context & Context.Strict && (keyword === Token.LetKeyword || keyword === Token.StaticKeyword)
return keyword === void 0
? Token.Identifier
: keyword === Token.YieldKeyword
? keyword
: (keyword & Token.FutureReserved) === Token.FutureReserved
? context & Context.Strict && hasEscape
? Token.EscapedFutureReserved
: keyword
: context & Context.Strict && (keyword === Token.LetKeyword || keyword === Token.StaticKeyword)
? Token.EscapedFutureReserved
: Token.EscapedReserved;
}
Expand Down
21 changes: 3 additions & 18 deletions test/test262-parser-tests/parser-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,12 @@ const Test262Dir = 'node_modules/test262-parser-tests';

const expectations = {
pass: [
'ba00173ff473e7da.js', //
'946bee37652a31fa.js', //
'ba00173ff473e7da.js',
'946bee37652a31fa.js',
'8b8edcb36909900b.js',
'6a220df693ce521c.js', //
'5d5b9de6d9b95f3e.js',
'5a2a8e992fa4fe37.js', //
'5a2a8e992fa4fe37.js',
'4f5419fe648c691b.js',
'44f31660bd715f05.js', //
'08a39e4289b0c3f3.js',
'046a0bb70d03d0cc.js',
'300a638d978d0f2c.js',
'110fa1efdd0868b8.js',
'1a1c717109ab67e1.js',
'206ebb4e67a6daa9.js',
Expand All @@ -33,11 +28,6 @@ const expectations = {
],
explicit: [
'3b36d546985cd9cb.js',
'44f31660bd715f05.js',
'300a638d978d0f2c.js',
'0e3ca454ddfb4729.js',
'08a39e4289b0c3f3.js',
'046a0bb70d03d0cc.js',
'110fa1efdd0868b8.js',
'1a1c717109ab67e1.js',
'206ebb4e67a6daa9.js',
Expand All @@ -49,19 +39,14 @@ const expectations = {
'e4a43066905a597b.js',
'f872cf801765a723.js',
'bf49ec8d96884562.js',
'b7b057684207633b.js',
'9f5a6dae7645976a.js',
'8af69d8f15295ed2.js',
'84633c379e4010bf.js',
'7fc173197c3cc75d.js',
'78c215fabdf13bae.js',
'66e383bfd18e66ab.js',
'647e21f8f157c338.js',
'3dbb6e166b14a6c0.js',
'324ab48c6d89125d.js',
'2687d6d9043bd5cb.js',
'1395e3a9d2acf65c.js',
'fd2a45941e114896.js',
'7b876ca5139f1ca8.js',
'3bc2b27a7430f818.js',
'e3fbcf63d7e43ead.js',
Expand Down

0 comments on commit a205210

Please sign in to comment.