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 { ...@@ -855,8 +855,7 @@ class ParserBase {
// either returns false or reports an error, depending on should_throw. // either returns false or reports an error, depending on should_throw.
// Otherwise returns true. // Otherwise returns true.
inline bool CheckTemplateEscapes(bool should_throw) { inline bool CheckTemplateEscapes(bool should_throw) {
DCHECK(scanner()->current_token() == Token::TEMPLATE_SPAN || DCHECK(Token::IsTemplate(scanner()->current_token()));
scanner()->current_token() == Token::TEMPLATE_TAIL);
if (!scanner()->has_invalid_template_escape()) return true; if (!scanner()->has_invalid_template_escape()) return true;
// Handle error case(s) // Handle error case(s)
...@@ -1113,7 +1112,7 @@ class ParserBase { ...@@ -1113,7 +1112,7 @@ class ParserBase {
V8_INLINE ExpressionT ParseMemberExpression(); V8_INLINE ExpressionT ParseMemberExpression();
V8_INLINE ExpressionT V8_INLINE ExpressionT
ParseMemberExpressionContinuation(ExpressionT expression) { ParseMemberExpressionContinuation(ExpressionT expression) {
if (!Token::IsProperty(peek())) return expression; if (!Token::IsMember(peek())) return expression;
return DoParseMemberExpressionContinuation(expression); return DoParseMemberExpressionContinuation(expression);
} }
ExpressionT DoParseMemberExpressionContinuation(ExpressionT expression); ExpressionT DoParseMemberExpressionContinuation(ExpressionT expression);
...@@ -3188,8 +3187,7 @@ ParserBase<Impl>::ParseLeftHandSideContinuation(ExpressionT result) { ...@@ -3188,8 +3187,7 @@ ParserBase<Impl>::ParseLeftHandSideContinuation(ExpressionT result) {
/* Call */ /* Call */
default: default:
DCHECK(peek() == Token::TEMPLATE_SPAN || DCHECK(Token::IsTemplate(peek()));
peek() == Token::TEMPLATE_TAIL);
result = ParseTemplateLiteral(result, position(), true); result = ParseTemplateLiteral(result, position(), true);
break; break;
} }
...@@ -3377,7 +3375,7 @@ typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseSuperExpression( ...@@ -3377,7 +3375,7 @@ typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseSuperExpression(
FunctionKind kind = scope->function_kind(); FunctionKind kind = scope->function_kind();
if (IsConciseMethod(kind) || IsAccessorFunction(kind) || if (IsConciseMethod(kind) || IsAccessorFunction(kind) ||
IsClassConstructor(kind)) { IsClassConstructor(kind)) {
if (peek() == Token::PERIOD || peek() == Token::LBRACK) { if (Token::IsProperty(peek())) {
scope->RecordSuperPropertyUsage(); scope->RecordSuperPropertyUsage();
return impl()->NewSuperPropertyReference(pos); return impl()->NewSuperPropertyReference(pos);
} }
...@@ -3425,7 +3423,7 @@ ParserBase<Impl>::ParseNewTargetExpression() { ...@@ -3425,7 +3423,7 @@ ParserBase<Impl>::ParseNewTargetExpression() {
template <typename Impl> template <typename Impl>
typename ParserBase<Impl>::ExpressionT typename ParserBase<Impl>::ExpressionT
ParserBase<Impl>::DoParseMemberExpressionContinuation(ExpressionT expression) { ParserBase<Impl>::DoParseMemberExpressionContinuation(ExpressionT expression) {
DCHECK(Token::IsProperty(peek())); DCHECK(Token::IsMember(peek()));
// Parses this part of MemberExpression: // Parses this part of MemberExpression:
// ('[' Expression ']' | '.' Identifier | TemplateLiteral)* // ('[' Expression ']' | '.' Identifier | TemplateLiteral)*
do { do {
...@@ -3448,8 +3446,7 @@ ParserBase<Impl>::DoParseMemberExpressionContinuation(ExpressionT expression) { ...@@ -3448,8 +3446,7 @@ ParserBase<Impl>::DoParseMemberExpressionContinuation(ExpressionT expression) {
break; break;
} }
default: { default: {
DCHECK(peek() == Token::TEMPLATE_SPAN || DCHECK(Token::IsTemplate(peek()));
peek() == Token::TEMPLATE_TAIL);
int pos; int pos;
if (scanner()->current_token() == Token::IDENTIFIER) { if (scanner()->current_token() == Token::IDENTIFIER) {
pos = position(); pos = position();
...@@ -3465,7 +3462,7 @@ ParserBase<Impl>::DoParseMemberExpressionContinuation(ExpressionT expression) { ...@@ -3465,7 +3462,7 @@ ParserBase<Impl>::DoParseMemberExpressionContinuation(ExpressionT expression) {
break; break;
} }
} }
} while (Token::IsProperty(peek())); } while (Token::IsMember(peek()));
return expression; return expression;
} }
......
...@@ -51,16 +51,20 @@ namespace internal { ...@@ -51,16 +51,20 @@ namespace internal {
#define TOKEN_LIST(T, K, C) \ #define TOKEN_LIST(T, K, C) \
\ \
/* BEGIN Property */ \
/* BEGIN PropertyOrCall */ \ /* BEGIN PropertyOrCall */ \
/* BEGIN Member */ \
/* BEGIN Template */ \
/* ES6 Template Literals */ \ /* ES6 Template Literals */ \
T(TEMPLATE_SPAN, nullptr, 0) \ T(TEMPLATE_SPAN, nullptr, 0) \
T(TEMPLATE_TAIL, nullptr, 0) \ T(TEMPLATE_TAIL, nullptr, 0) \
/* END Template */ \
\ \
/* Punctuators (ECMA-262, section 7.7, page 15). */ \ /* Punctuators (ECMA-262, section 7.7, page 15). */ \
/* BEGIN Property */ \
T(PERIOD, ".", 0) \ T(PERIOD, ".", 0) \
T(LBRACK, "[", 0) \ T(LBRACK, "[", 0) \
/* END Property */ \ /* END Property */ \
/* END Member */ \
T(LPAREN, "(", 0) \ T(LPAREN, "(", 0) \
/* END PropertyOrCall */ \ /* END PropertyOrCall */ \
T(RPAREN, ")", 0) \ T(RPAREN, ")", 0) \
...@@ -243,10 +247,18 @@ class Token { ...@@ -243,10 +247,18 @@ class Token {
return IsInRange(token, NULL_LITERAL, STRING); 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); return IsInRange(token, TEMPLATE_SPAN, LBRACK);
} }
static bool IsProperty(Value token) {
return IsInRange(token, PERIOD, LBRACK);
}
static bool IsPropertyOrCall(Value token) { static bool IsPropertyOrCall(Value token) {
return IsInRange(token, TEMPLATE_SPAN, LPAREN); return IsInRange(token, TEMPLATE_SPAN, LPAREN);
} }
......
...@@ -398,7 +398,7 @@ TEST(IsPropertyOrCall) { ...@@ -398,7 +398,7 @@ TEST(IsPropertyOrCall) {
} }
} }
bool TokenIsProperty(Token::Value token) { bool TokenIsMember(Token::Value token) {
switch (token) { switch (token) {
case Token::TEMPLATE_SPAN: case Token::TEMPLATE_SPAN:
case Token::TEMPLATE_TAIL: case Token::TEMPLATE_TAIL:
...@@ -410,6 +410,40 @@ bool TokenIsProperty(Token::Value token) { ...@@ -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) { TEST(IsProperty) {
for (int i = 0; i < Token::NUM_TOKENS; i++) { for (int i = 0; i < Token::NUM_TOKENS; i++) {
Token::Value token = static_cast<Token::Value>(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