From 46f2a6044624adc64eee3d3ba7f2de5d77b7a7e2 Mon Sep 17 00:00:00 2001 From: Andy Wingo Date: Wed, 26 Jun 2013 11:47:50 +0200 Subject: [PATCH] Bug 885281 - Factor out TokenMatcher::matchContextualKeyword. r=Waldo --- js/src/frontend/Parser.cpp | 27 ++++++++------------------- js/src/frontend/TokenStream.h | 7 +++++++ 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/js/src/frontend/Parser.cpp b/js/src/frontend/Parser.cpp index 7d676d07da78..eaa1e1396e76 100644 --- a/js/src/frontend/Parser.cpp +++ b/js/src/frontend/Parser.cpp @@ -3692,12 +3692,9 @@ Parser::matchInOrOf(bool *isForOfp) *isForOfp = false; return true; } - if (tokenStream.matchToken(TOK_NAME)) { - if (tokenStream.currentToken().name() == context->names().of) { - *isForOfp = true; - return true; - } - tokenStream.ungetToken(); + if (tokenStream.matchContextualKeyword(context->names().of)) { + *isForOfp = true; + return true; } return false; } @@ -3764,13 +3761,9 @@ Parser::forStatement() bool isForEach = false; unsigned iflags = 0; - if (allowsForEachIn() && tokenStream.matchToken(TOK_NAME)) { - if (tokenStream.currentToken().name() == context->names().each) { - iflags = JSITER_FOREACH; - isForEach = true; - } else { - tokenStream.ungetToken(); - } + if (allowsForEachIn() && tokenStream.matchContextualKeyword(context->names().each)) { + iflags = JSITER_FOREACH; + isForEach = true; } MUST_MATCH_TOKEN(TOK_LP, JSMSG_PAREN_AFTER_FOR); @@ -5800,12 +5793,8 @@ Parser::comprehensionTail(ParseNode *kid, unsigned blockid, bo pn2->setOp(JSOP_ITER); pn2->pn_iflags = JSITER_ENUMERATE; - if (allowsForEachIn() && tokenStream.matchToken(TOK_NAME)) { - if (tokenStream.currentToken().name() == context->names().each) - pn2->pn_iflags |= JSITER_FOREACH; - else - tokenStream.ungetToken(); - } + if (allowsForEachIn() && tokenStream.matchContextualKeyword(context->names().each)) + pn2->pn_iflags |= JSITER_FOREACH; MUST_MATCH_TOKEN(TOK_LP, JSMSG_PAREN_AFTER_FOR); GenexpGuard guard(this); diff --git a/js/src/frontend/TokenStream.h b/js/src/frontend/TokenStream.h index 0a8908bc9281..0bcbbd122a16 100644 --- a/js/src/frontend/TokenStream.h +++ b/js/src/frontend/TokenStream.h @@ -631,6 +631,13 @@ class MOZ_STACK_CLASS TokenStream JS_ALWAYS_TRUE(matchToken(tt)); } + bool matchContextualKeyword(PropertyName* keyword) { + if (getToken() == TOK_NAME && currentToken().name() == keyword) + return true; + ungetToken(); + return false; + } + class MOZ_STACK_CLASS Position { public: /*