From 185d87dd21fd06114e5025eb9510e1cc28dbc68d Mon Sep 17 00:00:00 2001 From: Tooru Fujisawa Date: Fri, 2 Nov 2018 15:51:06 +0900 Subject: [PATCH] Bug 1501928 - Part 3: Remove MUST_MATCH_TOKEN_MOD_WITH_REPORT* macros. r=Waldo --- js/src/frontend/Parser.cpp | 136 ++++++++++++++++++++++--------------- js/src/frontend/Parser.h | 61 ++++++++++++++--- 2 files changed, 135 insertions(+), 62 deletions(-) diff --git a/js/src/frontend/Parser.cpp b/js/src/frontend/Parser.cpp index 08bafdac0d3b..a357e19c44b2 100644 --- a/js/src/frontend/Parser.cpp +++ b/js/src/frontend/Parser.cpp @@ -73,25 +73,6 @@ using UsedNamePtr = UsedNameTracker::UsedNameMap::Ptr; using BindingNameVector = Vector; -// Read a token. Report an error and return null() if that token doesn't match -// to the condition. Do not use MUST_MATCH_TOKEN_INTERNAL directly. -#define MUST_MATCH_TOKEN_INTERNAL(cond, modifier, errorReport, failureValue) \ - JS_BEGIN_MACRO \ - TokenKind token; \ - if (!tokenStream.getToken(&token, modifier)) \ - return failureValue; \ - if (!(cond)) { \ - errorReport; \ - return failureValue; \ - } \ - JS_END_MACRO - -#define MUST_MATCH_TOKEN_MOD_WITH_REPORT_OR(tt, modifier, errorReport, failureValue) \ - MUST_MATCH_TOKEN_INTERNAL(token == tt, modifier, errorReport, failureValue) - -#define MUST_MATCH_TOKEN_MOD_WITH_REPORT(tt, modifier, errorReport) \ - MUST_MATCH_TOKEN_MOD_WITH_REPORT_OR(tt, modifier, errorReport, null()) - template static inline void PropagateTransitiveParseFlags(const T* inner, U* outer) @@ -699,17 +680,17 @@ GeneralParser::asFinalParser() const } template -template +template bool GeneralParser::mustMatchTokenInternal(ConditionT condition, Modifier modifier, - unsigned errorNumber) + ErrorReportT errorReport) { TokenKind actual; if (!tokenStream.getToken(&actual, modifier)) { return false; } if (!condition(actual)) { - error(errorNumber); + errorReport(actual); return false; } return true; @@ -4250,10 +4231,14 @@ GeneralParser::functionFormalParametersAndBody(InHandling in } if (bodyType == StatementListBody) { - MUST_MATCH_TOKEN_MOD_WITH_REPORT_OR(TokenKind::RightCurly, TokenStream::Operand, - reportMissingClosing(JSMSG_CURLY_AFTER_BODY, - JSMSG_CURLY_OPENED, openedPos), - false); + if (!mustMatchToken(TokenKind::RightCurly, TokenStream::Operand, + [this, openedPos](TokenKind actual) { + this->reportMissingClosing(JSMSG_CURLY_AFTER_BODY, + JSMSG_CURLY_OPENED, openedPos); + })) + { + return false; + } funbox->setEnd(anyChars); } else { MOZ_ASSERT(kind == FunctionSyntaxKind::Arrow); @@ -5101,9 +5086,14 @@ GeneralParser::objectBindingPattern(DeclarationKind kind, } } - MUST_MATCH_TOKEN_MOD_WITH_REPORT(TokenKind::RightCurly, TokenStream::Operand, - reportMissingClosing(JSMSG_CURLY_AFTER_LIST, - JSMSG_CURLY_OPENED, begin)); + if (!mustMatchToken(TokenKind::RightCurly, TokenStream::Operand, + [this, begin](TokenKind actual) { + this->reportMissingClosing(JSMSG_CURLY_AFTER_LIST, + JSMSG_CURLY_OPENED, begin); + })) + { + return null(); + } handler.setEndPosition(literal, pos().end); return literal; @@ -5202,9 +5192,14 @@ GeneralParser::arrayBindingPattern(DeclarationKind kind, } } - MUST_MATCH_TOKEN_MOD_WITH_REPORT(TokenKind::RightBracket, TokenStream::Operand, - reportMissingClosing(JSMSG_BRACKET_AFTER_LIST, - JSMSG_BRACKET_OPENED, begin)); + if (!mustMatchToken(TokenKind::RightBracket, TokenStream::Operand, + [this, begin](TokenKind actual) { + this->reportMissingClosing(JSMSG_BRACKET_AFTER_LIST, + JSMSG_BRACKET_OPENED, begin); + })) + { + return null(); + } handler.setEndPosition(literal, pos().end); return literal; @@ -5265,9 +5260,13 @@ GeneralParser::blockStatement(YieldHandling yieldHandling, return null(); } - MUST_MATCH_TOKEN_MOD_WITH_REPORT(TokenKind::RightCurly, TokenStream::Operand, - reportMissingClosing(errorNumber, JSMSG_CURLY_OPENED, - openedPos)); + if (!mustMatchToken(TokenKind::RightCurly, TokenStream::Operand, + [this, errorNumber, openedPos](TokenKind actual) { + this->reportMissingClosing(errorNumber, JSMSG_CURLY_OPENED, openedPos); + })) + { + return null(); + } return finishLexicalScope(scope, list); } @@ -7142,10 +7141,14 @@ GeneralParser::forStatement(YieldHandling yieldHandling) } } - MUST_MATCH_TOKEN_MOD_WITH_REPORT(TokenKind::LeftParen, TokenStream::None, - error((token == TokenKind::Await && !pc->isAsync()) - ? JSMSG_FOR_AWAIT_OUTSIDE_ASYNC - : JSMSG_PAREN_AFTER_FOR)); + if (!mustMatchToken(TokenKind::LeftParen, [this](TokenKind actual) { + this->error((actual == TokenKind::Await && !this->pc->isAsync()) + ? JSMSG_FOR_AWAIT_OUTSIDE_ASYNC + : JSMSG_PAREN_AFTER_FOR); + })) + { + return null(); + } // ParseNodeKind::ForHead, ParseNodeKind::ForIn, or // ParseNodeKind::ForOf depending on the loop type. @@ -7786,9 +7789,14 @@ GeneralParser::tryStatement(YieldHandling yieldHandling) return null(); } - MUST_MATCH_TOKEN_MOD_WITH_REPORT(TokenKind::RightCurly, TokenStream::Operand, - reportMissingClosing(JSMSG_CURLY_AFTER_TRY, - JSMSG_CURLY_OPENED, openedPos)); + if (!mustMatchToken(TokenKind::RightCurly, TokenStream::Operand, + [this, openedPos](TokenKind actual) { + this->reportMissingClosing(JSMSG_CURLY_AFTER_TRY, + JSMSG_CURLY_OPENED, openedPos); + })) + { + return null(); + } } LexicalScopeNodeType catchScope = null(); @@ -7908,9 +7916,14 @@ GeneralParser::tryStatement(YieldHandling yieldHandling) return null(); } - MUST_MATCH_TOKEN_MOD_WITH_REPORT(TokenKind::RightCurly, TokenStream::Operand, - reportMissingClosing(JSMSG_CURLY_AFTER_FINALLY, - JSMSG_CURLY_OPENED, openedPos)); + if (!mustMatchToken(TokenKind::RightCurly, TokenStream::Operand, + [this, openedPos](TokenKind actual) { + this->reportMissingClosing(JSMSG_CURLY_AFTER_FINALLY, + JSMSG_CURLY_OPENED, openedPos); + })) + { + return null(); + } } else { anyChars.ungetToken(); } @@ -7951,9 +7964,14 @@ GeneralParser::catchBlockStatement(YieldHandling yieldHandli return null(); } - MUST_MATCH_TOKEN_MOD_WITH_REPORT(TokenKind::RightCurly, TokenStream::Operand, - reportMissingClosing(JSMSG_CURLY_AFTER_CATCH, - JSMSG_CURLY_OPENED, openedPos)); + if (!mustMatchToken(TokenKind::RightCurly, TokenStream::Operand, + [this, openedPos](TokenKind actual) { + this->reportMissingClosing(JSMSG_CURLY_AFTER_CATCH, + JSMSG_CURLY_OPENED, openedPos); + })) + { + return null(); + } // The catch parameter names are not bound in the body scope, so remove // them before generating bindings. @@ -10360,9 +10378,14 @@ GeneralParser::arrayInitializer(YieldHandling yieldHandling, } } - MUST_MATCH_TOKEN_MOD_WITH_REPORT(TokenKind::RightBracket, TokenStream::Operand, - reportMissingClosing(JSMSG_BRACKET_AFTER_LIST, - JSMSG_BRACKET_OPENED, begin)); + if (!mustMatchToken(TokenKind::RightBracket, TokenStream::Operand, + [this, begin](TokenKind actual) { + this->reportMissingClosing(JSMSG_BRACKET_AFTER_LIST, + JSMSG_BRACKET_OPENED, begin); + })) + { + return null(); + } } handler.setEndPosition(literal, pos().end); @@ -10867,9 +10890,14 @@ GeneralParser::objectLiteral(YieldHandling yieldHandling, } } - MUST_MATCH_TOKEN_MOD_WITH_REPORT(TokenKind::RightCurly, TokenStream::Operand, - reportMissingClosing(JSMSG_CURLY_AFTER_LIST, - JSMSG_CURLY_OPENED, openedPos)); + if (!mustMatchToken(TokenKind::RightCurly, TokenStream::Operand, + [this, openedPos](TokenKind actual) { + this->reportMissingClosing(JSMSG_CURLY_AFTER_LIST, + JSMSG_CURLY_OPENED, openedPos); + })) + { + return null(); + } handler.setEndPosition(literal, pos().end); return literal; diff --git a/js/src/frontend/Parser.h b/js/src/frontend/Parser.h index 44ddeb3cea2f..82ae0fbc13b0 100644 --- a/js/src/frontend/Parser.h +++ b/js/src/frontend/Parser.h @@ -930,23 +930,68 @@ FOR_EACH_PARSENODE_SUBCLASS(DECLARE_TYPE) ListNodeType parse(); private: - template + /* + * Gets the next token and checks if it matches to the given `condition`. + * If it matches, returns true. + * If it doesn't match, calls `errorReport` to report the error, and + * returns false. + * If other error happens, it returns false but `errorReport` may not be + * called and other error will be thrown in that case. + * + * In any case, the already gotten token is not ungotten. + * + * The signature of `condition` is [...](TokenKind actual) -> bool, and + * the signature of `errorReport` is [...](TokenKind actual). + */ + template MOZ_MUST_USE bool mustMatchTokenInternal(ConditionT condition, Modifier modifier, - unsigned errorNumber); + ErrorReportT errorReport); public: - MOZ_MUST_USE bool mustMatchToken(TokenKind expected, Modifier modifier, unsigned errorNumber) { - return mustMatchTokenInternal([expected](TokenKind actual) { return actual == expected; }, - modifier, errorNumber); + /* + * The following mustMatchToken variants follow the behavior and parameter + * types of mustMatchTokenInternal above. + * + * If modifier is omitted, `None` is used. + * If TokenKind is passed instead of `condition`, it checks if the next + * token is the passed token. + * If error number is passed instead of `errorReport`, it reports an + * error with the passed errorNumber. + */ + MOZ_MUST_USE bool mustMatchToken(TokenKind expected, Modifier modifier, JSErrNum errorNumber) { + return mustMatchTokenInternal([expected](TokenKind actual) { + return actual == expected; + }, + modifier, + [this, errorNumber](TokenKind) { + this->error(errorNumber); + }); } - MOZ_MUST_USE bool mustMatchToken(TokenKind excpected, unsigned errorNumber) { + MOZ_MUST_USE bool mustMatchToken(TokenKind excpected, JSErrNum errorNumber) { return mustMatchToken(excpected, TokenStream::None, errorNumber); } template - MOZ_MUST_USE bool mustMatchToken(ConditionT condition, unsigned errorNumber) { - return mustMatchTokenInternal(condition, TokenStream::None, errorNumber); + MOZ_MUST_USE bool mustMatchToken(ConditionT condition, JSErrNum errorNumber) { + return mustMatchTokenInternal(condition, TokenStream::None, + [this, errorNumber](TokenKind) { + this->error(errorNumber); + }); + } + + template + MOZ_MUST_USE bool mustMatchToken(TokenKind expected, Modifier modifier, + ErrorReportT errorReport) { + return mustMatchTokenInternal([expected](TokenKind actual) { + return actual == expected; + }, + modifier, errorReport); + } + + template + MOZ_MUST_USE bool mustMatchToken(TokenKind expected, ErrorReportT errorReport) { + return mustMatchToken(expected, TokenStream::None, errorReport); } /* Report the given error at the current offset. */