Commit 86a5d0c1 authored by Toon Verwaest's avatar Toon Verwaest Committed by Commit Bot

[parser] Introduce Token:: IsMember, IsProperty, and IsTemplate

Change-Id: Iadc3d916dcda6f03c467e77fedff6d315dbb67f6
Reviewed-on: https://chromium-review.googlesource.com/c/1352299Reviewed-by: 's avatarLeszek Swirski <leszeks@chromium.org>
Commit-Queue: Toon Verwaest <verwaest@chromium.org>
Cr-Commit-Position: refs/heads/master@{#57897}
parent d5f4a33e
......@@ -855,8 +855,7 @@ class ParserBase {
// either returns false or reports an error, depending on should_throw.
// Otherwise returns true.
inline bool CheckTemplateEscapes(bool should_throw) {
DCHECK(scanner()->current_token() == Token::TEMPLATE_SPAN ||
scanner()->current_token() == Token::TEMPLATE_TAIL);
DCHECK(Token::IsTemplate(scanner()->current_token()));
if (!scanner()->has_invalid_template_escape()) return true;
// Handle error case(s)
......@@ -1113,7 +1112,7 @@ class ParserBase {
V8_INLINE ExpressionT ParseMemberExpression();
V8_INLINE ExpressionT
ParseMemberExpressionContinuation(ExpressionT expression) {
if (!Token::IsProperty(peek())) return expression;
if (!Token::IsMember(peek())) return expression;
return DoParseMemberExpressionContinuation(expression);
}
ExpressionT DoParseMemberExpressionContinuation(ExpressionT expression);
......@@ -3188,8 +3187,7 @@ ParserBase<Impl>::ParseLeftHandSideContinuation(ExpressionT result) {
/* Call */
default:
DCHECK(peek() == Token::TEMPLATE_SPAN ||
peek() == Token::TEMPLATE_TAIL);
DCHECK(Token::IsTemplate(peek()));
result = ParseTemplateLiteral(result, position(), true);
break;
}
......@@ -3377,7 +3375,7 @@ typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseSuperExpression(
FunctionKind kind = scope->function_kind();
if (IsConciseMethod(kind) || IsAccessorFunction(kind) ||
IsClassConstructor(kind)) {
if (peek() == Token::PERIOD || peek() == Token::LBRACK) {
if (Token::IsProperty(peek())) {
scope->RecordSuperPropertyUsage();
return impl()->NewSuperPropertyReference(pos);
}
......@@ -3425,7 +3423,7 @@ ParserBase<Impl>::ParseNewTargetExpression() {
template <typename Impl>
typename ParserBase<Impl>::ExpressionT
ParserBase<Impl>::DoParseMemberExpressionContinuation(ExpressionT expression) {
DCHECK(Token::IsProperty(peek()));
DCHECK(Token::IsMember(peek()));
// Parses this part of MemberExpression:
// ('[' Expression ']' | '.' Identifier | TemplateLiteral)*
do {
......@@ -3448,8 +3446,7 @@ ParserBase<Impl>::DoParseMemberExpressionContinuation(ExpressionT expression) {
break;
}
default: {
DCHECK(peek() == Token::TEMPLATE_SPAN ||
peek() == Token::TEMPLATE_TAIL);
DCHECK(Token::IsTemplate(peek()));
int pos;
if (scanner()->current_token() == Token::IDENTIFIER) {
pos = position();
......@@ -3465,7 +3462,7 @@ ParserBase<Impl>::DoParseMemberExpressionContinuation(ExpressionT expression) {
break;
}
}
} while (Token::IsProperty(peek()));
} while (Token::IsMember(peek()));
return expression;
}
......
......@@ -51,16 +51,20 @@ namespace internal {
#define TOKEN_LIST(T, K, C) \
\
/* BEGIN Property */ \
/* BEGIN PropertyOrCall */ \
/* BEGIN Member */ \
/* BEGIN Template */ \
/* ES6 Template Literals */ \
T(TEMPLATE_SPAN, nullptr, 0) \
T(TEMPLATE_TAIL, nullptr, 0) \
/* END Template */ \
\
/* Punctuators (ECMA-262, section 7.7, page 15). */ \
/* BEGIN Property */ \
T(PERIOD, ".", 0) \
T(LBRACK, "[", 0) \
/* END Property */ \
/* END Member */ \
T(LPAREN, "(", 0) \
/* END PropertyOrCall */ \
T(RPAREN, ")", 0) \
......@@ -243,10 +247,18 @@ class Token {
return IsInRange(token, NULL_LITERAL, STRING);
}
static bool IsProperty(Value token) {
static bool IsTemplate(Value token) {
return IsInRange(token, TEMPLATE_SPAN, TEMPLATE_TAIL);
}
static bool IsMember(Value token) {
return IsInRange(token, TEMPLATE_SPAN, LBRACK);
}
static bool IsProperty(Value token) {
return IsInRange(token, PERIOD, LBRACK);
}
static bool IsPropertyOrCall(Value token) {
return IsInRange(token, TEMPLATE_SPAN, LPAREN);
}
......
......@@ -398,7 +398,7 @@ TEST(IsPropertyOrCall) {
}
}
bool TokenIsProperty(Token::Value token) {
bool TokenIsMember(Token::Value token) {
switch (token) {
case Token::TEMPLATE_SPAN:
case Token::TEMPLATE_TAIL:
......@@ -410,6 +410,40 @@ bool TokenIsProperty(Token::Value token) {
}
}
bool TokenIsTemplate(Token::Value token) {
switch (token) {
case Token::TEMPLATE_SPAN:
case Token::TEMPLATE_TAIL:
return true;
default:
return false;
}
}
bool TokenIsProperty(Token::Value token) {
switch (token) {
case Token::PERIOD:
case Token::LBRACK:
return true;
default:
return false;
}
}
TEST(IsMember) {
for (int i = 0; i < Token::NUM_TOKENS; i++) {
Token::Value token = static_cast<Token::Value>(i);
CHECK_EQ(TokenIsMember(token), Token::IsMember(token));
}
}
TEST(IsTemplate) {
for (int i = 0; i < Token::NUM_TOKENS; i++) {
Token::Value token = static_cast<Token::Value>(i);
CHECK_EQ(TokenIsTemplate(token), Token::IsTemplate(token));
}
}
TEST(IsProperty) {
for (int i = 0; i < Token::NUM_TOKENS; i++) {
Token::Value token = static_cast<Token::Value>(i);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment