Commit 032999da authored by marja@chromium.org's avatar marja@chromium.org

Merge Parser::ReportUnexpectedToken and PreParser::ReportUnexpectedToken.

(I.e., move ReportUnexpectedToken to ParserBase.)

Because of the recent unifications, they now do the same thing.

BUG=
R=mstarzinger@chromium.org

Review URL: https://codereview.chromium.org/153743006

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@19161 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent a26bd05b
......@@ -3469,41 +3469,6 @@ DebuggerStatement* Parser::ParseDebuggerStatement(bool* ok) {
}
void Parser::ReportUnexpectedToken(Token::Value token) {
// We don't report stack overflows here, to avoid increasing the
// stack depth even further. Instead we report it after parsing is
// over, in ParseProgram/ParseJson.
if (token == Token::ILLEGAL && stack_overflow()) return;
// Four of the tokens are treated specially
switch (token) {
case Token::EOS:
return ReportMessage("unexpected_eos", Vector<const char*>::empty());
case Token::NUMBER:
return ReportMessage("unexpected_token_number",
Vector<const char*>::empty());
case Token::STRING:
return ReportMessage("unexpected_token_string",
Vector<const char*>::empty());
case Token::IDENTIFIER:
return ReportMessage("unexpected_token_identifier",
Vector<const char*>::empty());
case Token::FUTURE_RESERVED_WORD:
return ReportMessage("unexpected_reserved",
Vector<const char*>::empty());
case Token::YIELD:
case Token::FUTURE_STRICT_RESERVED_WORD:
return ReportMessage(top_scope_->is_classic_mode() ?
"unexpected_token_identifier" :
"unexpected_strict_reserved",
Vector<const char*>::empty());
default:
const char* name = Token::String(token);
ASSERT(name != NULL);
ReportMessage("unexpected_token", Vector<const char*>(&name, 1));
}
}
void Parser::ReportInvalidPreparseData(Handle<String> name, bool* ok) {
SmartArrayPointer<char> name_string = name->ToCString(DISALLOW_NULLS);
const char* element[1] = { name_string.get() };
......@@ -4485,6 +4450,42 @@ void ParserBase::ExpectContextualKeyword(Vector<const char> keyword, bool* ok) {
}
void ParserBase::ReportUnexpectedToken(Token::Value token) {
// We don't report stack overflows here, to avoid increasing the
// stack depth even further. Instead we report it after parsing is
// over, in ParseProgram.
if (token == Token::ILLEGAL && stack_overflow()) {
return;
}
Scanner::Location source_location = scanner()->location();
// Four of the tokens are treated specially
switch (token) {
case Token::EOS:
return ReportMessageAt(source_location, "unexpected_eos");
case Token::NUMBER:
return ReportMessageAt(source_location, "unexpected_token_number");
case Token::STRING:
return ReportMessageAt(source_location, "unexpected_token_string");
case Token::IDENTIFIER:
return ReportMessageAt(source_location,
"unexpected_token_identifier");
case Token::FUTURE_RESERVED_WORD:
return ReportMessageAt(source_location, "unexpected_reserved");
case Token::YIELD:
case Token::FUTURE_STRICT_RESERVED_WORD:
return ReportMessageAt(source_location,
is_classic_mode() ? "unexpected_token_identifier"
: "unexpected_strict_reserved");
default:
const char* name = Token::String(token);
ASSERT(name != NULL);
ReportMessageAt(
source_location, "unexpected_token", Vector<const char*>(&name, 1));
}
}
Literal* Parser::GetLiteralUndefined(int position) {
return factory()->NewLiteral(
isolate()->factory()->undefined_value(), position);
......
......@@ -521,6 +521,10 @@ class Parser : public ParserBase {
Mode old_mode_;
};
virtual bool is_classic_mode() {
return top_scope_->is_classic_mode();
}
// Returns NULL if parsing failed.
FunctionLiteral* ParseProgram();
......@@ -536,7 +540,6 @@ class Parser : public ParserBase {
Handle<String> source);
// Report syntax error
void ReportUnexpectedToken(Token::Value token);
void ReportInvalidPreparseData(Handle<String> name, bool* ok);
void ReportMessage(const char* message, Vector<const char*> args);
void ReportMessage(const char* message, Vector<Handle<String> > args);
......
......@@ -72,7 +72,7 @@ PreParser::PreParseResult PreParser::PreParseLazyFunction(
ReportUnexpectedToken(scanner()->current_token());
} else {
ASSERT_EQ(Token::RBRACE, scanner()->peek());
if (!is_classic_mode()) {
if (!scope_->is_classic_mode()) {
int end_pos = scanner()->location().end_pos;
CheckOctalLiteral(start_position, end_pos, &ok);
if (ok) {
......@@ -97,40 +97,6 @@ PreParser::PreParseResult PreParser::PreParseLazyFunction(
// That means that contextual checks (like a label being declared where
// it is used) are generally omitted.
void PreParser::ReportUnexpectedToken(Token::Value token) {
// We don't report stack overflows here, to avoid increasing the
// stack depth even further. Instead we report it after parsing is
// over, in ParseProgram.
if (token == Token::ILLEGAL && stack_overflow()) {
return;
}
Scanner::Location source_location = scanner()->location();
// Four of the tokens are treated specially
switch (token) {
case Token::EOS:
return ReportMessageAt(source_location, "unexpected_eos", NULL);
case Token::NUMBER:
return ReportMessageAt(source_location, "unexpected_token_number", NULL);
case Token::STRING:
return ReportMessageAt(source_location, "unexpected_token_string", NULL);
case Token::IDENTIFIER:
return ReportMessageAt(source_location,
"unexpected_token_identifier", NULL);
case Token::FUTURE_RESERVED_WORD:
return ReportMessageAt(source_location, "unexpected_reserved", NULL);
case Token::YIELD:
case Token::FUTURE_STRICT_RESERVED_WORD:
return ReportMessageAt(source_location,
is_classic_mode() ? "unexpected_token_identifier"
: "unexpected_strict_reserved",
NULL);
default:
const char* name = Token::String(token);
ReportMessageAt(source_location, "unexpected_token", name);
}
}
#define CHECK_OK ok); \
if (!*ok) return kUnknownSourceElements; \
......@@ -271,7 +237,7 @@ PreParser::Statement PreParser::ParseStatement(bool* ok) {
Scanner::Location start_location = scanner()->peek_location();
Statement statement = ParseFunctionDeclaration(CHECK_OK);
Scanner::Location end_location = scanner()->location();
if (!is_classic_mode()) {
if (!scope_->is_classic_mode()) {
ReportMessageAt(start_location.beg_pos, end_location.end_pos,
"strict_function", NULL);
*ok = false;
......@@ -480,7 +446,7 @@ PreParser::Statement PreParser::ParseExpressionOrLabelledStatement(bool* ok) {
// Expression is a single identifier, and not, e.g., a parenthesized
// identifier.
ASSERT(!expr.AsIdentifier().IsFutureReserved());
ASSERT(is_classic_mode() ||
ASSERT(scope_->is_classic_mode() ||
(!expr.AsIdentifier().IsFutureStrictReserved() &&
!expr.AsIdentifier().IsYield()));
Consume(Token::COLON);
......@@ -578,7 +544,7 @@ PreParser::Statement PreParser::ParseWithStatement(bool* ok) {
// WithStatement ::
// 'with' '(' Expression ')' Statement
Expect(Token::WITH, CHECK_OK);
if (!is_classic_mode()) {
if (!scope_->is_classic_mode()) {
Scanner::Location location = scanner()->location();
ReportMessageAt(location, "strict_mode_with", NULL);
*ok = false;
......@@ -834,7 +800,7 @@ PreParser::Expression PreParser::ParseAssignmentExpression(bool accept_IN,
return expression;
}
if (!is_classic_mode() &&
if (!scope_->is_classic_mode() &&
expression.IsIdentifier() &&
expression.AsIdentifier().IsEvalOrArguments()) {
Scanner::Location after = scanner()->location();
......@@ -928,7 +894,7 @@ PreParser::Expression PreParser::ParseUnaryExpression(bool* ok) {
op = Next();
Scanner::Location before = scanner()->peek_location();
Expression expression = ParseUnaryExpression(CHECK_OK);
if (!is_classic_mode() &&
if (!scope_->is_classic_mode() &&
expression.IsIdentifier() &&
expression.AsIdentifier().IsEvalOrArguments()) {
Scanner::Location after = scanner()->location();
......@@ -951,7 +917,7 @@ PreParser::Expression PreParser::ParsePostfixExpression(bool* ok) {
Expression expression = ParseLeftHandSideExpression(CHECK_OK);
if (!scanner()->HasAnyLineTerminatorBeforeNext() &&
Token::IsCountOp(peek())) {
if (!is_classic_mode() &&
if (!scope_->is_classic_mode() &&
expression.IsIdentifier() &&
expression.AsIdentifier().IsEvalOrArguments()) {
Scanner::Location after = scanner()->location();
......@@ -1392,7 +1358,7 @@ PreParser::Expression PreParser::ParseFunctionLiteral(bool is_generator,
}
Expect(Token::RBRACE, CHECK_OK);
if (!is_classic_mode()) {
if (!scope_->is_classic_mode()) {
int end_position = scanner()->location().end_pos;
CheckOctalLiteral(start_position, end_position, CHECK_OK);
CheckDelayedStrictModeViolation(start_position, end_position, CHECK_OK);
......@@ -1500,11 +1466,11 @@ PreParser::Identifier PreParser::ParseIdentifier(
if (next == Token::IDENTIFIER) {
PreParser::Identifier name = GetIdentifierSymbol();
if (allow_eval_or_arguments == kDontAllowEvalOrArguments &&
!is_classic_mode() && name.IsEvalOrArguments()) {
!scope_->is_classic_mode() && name.IsEvalOrArguments()) {
StrictModeIdentifierViolation(scanner()->location(), name, ok);
}
return name;
} else if (is_classic_mode() &&
} else if (scope_->is_classic_mode() &&
(next == Token::FUTURE_STRICT_RESERVED_WORD ||
(next == Token::YIELD && !scope_->is_generator()))) {
return GetIdentifierSymbol();
......@@ -1519,7 +1485,7 @@ PreParser::Identifier PreParser::ParseIdentifier(
void PreParser::SetStrictModeViolation(Scanner::Location location,
const char* type,
bool* ok) {
if (!is_classic_mode()) {
if (!scope_->is_classic_mode()) {
ReportMessageAt(location, type, NULL);
*ok = false;
return;
......@@ -1558,7 +1524,7 @@ void PreParser::StrictModeIdentifierViolation(Scanner::Location location,
} else if (identifier.IsFutureStrictReserved() || identifier.IsYield()) {
type = "unexpected_strict_reserved";
}
if (!is_classic_mode()) {
if (!scope_->is_classic_mode()) {
ReportMessageAt(location, type, NULL);
*ok = false;
return;
......
......@@ -87,6 +87,8 @@ class ParserBase {
bool stack_overflow() const { return stack_overflow_; }
void set_stack_overflow() { stack_overflow_ = true; }
virtual bool is_classic_mode() = 0;
INLINE(Token::Value peek()) {
if (stack_overflow_) return Token::ILLEGAL;
return scanner()->peek();
......@@ -142,8 +144,13 @@ class ParserBase {
static int Precedence(Token::Value token, bool accept_IN);
// Report syntax errors.
virtual void ReportUnexpectedToken(Token::Value token) = 0;
virtual void ReportMessageAt(Scanner::Location loc, const char* type) = 0;
void ReportUnexpectedToken(Token::Value token);
void ReportMessageAt(Scanner::Location location, const char* type) {
ReportMessageAt(location, type, Vector<const char*>::empty());
}
virtual void ReportMessageAt(Scanner::Location source_location,
const char* message,
Vector<const char*> args) = 0;
// Used to detect duplicates in object literals. Each of the values
// kGetterProperty, kSetterProperty and kValueProperty represents
......@@ -542,9 +549,13 @@ class PreParser : public ParserBase {
};
// Report syntax error
void ReportUnexpectedToken(Token::Value token);
void ReportMessageAt(Scanner::Location location, const char* type) {
ReportMessageAt(location, type, NULL);
void ReportMessageAt(Scanner::Location location,
const char* message,
Vector<const char*> args) {
ReportMessageAt(location.beg_pos,
location.end_pos,
message,
args.length() > 0 ? args[0] : NULL);
}
void ReportMessageAt(Scanner::Location location,
const char* type,
......@@ -625,7 +636,7 @@ class PreParser : public ParserBase {
scope_->set_language_mode(language_mode);
}
bool is_classic_mode() {
virtual bool is_classic_mode() {
return scope_->language_mode() == CLASSIC_MODE;
}
......
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