Issue #1658 - Part 1: Implement support for optional chaining in the JS Parser

Partially based on: https://bugzilla.mozilla.org/show_bug.cgi?id=1566143
This commit is contained in:
FranklinDM 2022-04-24 21:05:43 +08:00 committed by roytam1
commit e04a23ff98
10 changed files with 483 additions and 104 deletions

View file

@ -1285,7 +1285,7 @@ static const uint8_t firstCharKinds[] = {
/* 30+ */ _______, _______, Space, _______, String, _______, Ident, _______, _______, String,
/* 40+ */ TOK_LP, TOK_RP, _______, _______, T_COMMA,_______, _______, _______,BasePrefix, Dec,
/* 50+ */ Dec, Dec, Dec, Dec, Dec, Dec, Dec, Dec, T_COLON,TOK_SEMI,
/* 60+ */ _______, _______, _______,TOK_HOOK, _______, Ident, Ident, Ident, Ident, Ident,
/* 60+ */ _______, _______, _______, _______, _______, Ident, Ident, Ident, Ident, Ident,
/* 70+ */ Ident, Ident, Ident, Ident, Ident, Ident, Ident, Ident, Ident, Ident,
/* 80+ */ Ident, Ident, Ident, Ident, Ident, Ident, Ident, Ident, Ident, Ident,
/* 90+ */ Ident, TOK_LB, _______, TOK_RB, _______, Ident, Templat, Ident, Ident, Ident,
@ -1796,6 +1796,25 @@ TokenStream::getTokenInternal(TokenKind* ttp, Modifier modifier)
tp->type = matchChar('=') ? TOK_BITANDASSIGN : TOK_BITAND;
goto out;
case '?':
if (matchChar('.')) {
c = getCharIgnoreEOL();
if (JS7_ISDEC(c)) {
// if the code unit is followed by a number, for example it has
// the following form `<...> ?.5 <..> then it should be treated
// as a ternary rather than as an optional chain
tp->type = TOK_HOOK;
ungetCharIgnoreEOL(c);
ungetChar('.');
} else {
ungetCharIgnoreEOL(c);
tp->type = TOK_OPTCHAIN;
}
} else {
tp->type = TOK_HOOK;
}
goto out;
case '!':
if (matchChar('='))
tp->type = matchChar('=') ? TOK_STRICTNE : TOK_NE;