Revision: 20125
Author: ma...@chromium.org
Date: Thu Mar 20 13:18:15 2014 UTC
Log: Make PreParser track valid left hand sides.
Notes:
- This makes PreParser produce invalid_lhs_in_assignment and
invalid_lhs_in_prefix_op. Other errors will follow as the corresponding
funcs
move to ParserBase.
- PreParserExpression::IsStrictFunction and StrictFunction() are not needed
any
more -> removed them.
R=rossb...@chromium.org
BUG=
Review URL: https://codereview.chromium.org/196343033
http://code.google.com/p/v8/source/detail?r=20125
Modified:
/branches/bleeding_edge/src/preparser.cc
/branches/bleeding_edge/src/preparser.h
/branches/bleeding_edge/test/cctest/test-parsing.cc
=======================================
--- /branches/bleeding_edge/src/preparser.cc Wed Mar 19 14:08:47 2014 UTC
+++ /branches/bleeding_edge/src/preparser.cc Thu Mar 20 13:18:15 2014 UTC
@@ -874,7 +874,7 @@
if (result.IsThis()) {
result = Expression::ThisProperty();
} else {
- result = Expression::Default();
+ result = Expression::Property();
}
break;
}
@@ -891,7 +891,7 @@
if (result.IsThis()) {
result = Expression::ThisProperty();
} else {
- result = Expression::Default();
+ result = Expression::Property();
}
break;
}
@@ -913,13 +913,17 @@
if (peek() == Token::NEW) {
Consume(Token::NEW);
ParseMemberWithNewPrefixesExpression(CHECK_OK);
+ Expression expression = Expression::Default();
if (peek() == Token::LPAREN) {
// NewExpression with arguments.
ParseArguments(CHECK_OK);
- // The expression can still continue with . or [ after the arguments.
- ParseMemberExpressionContinuation(Expression::Default(), CHECK_OK);
+ // The expression can still continue with . or [ after the
arguments. Here
+ // we need to transmit the "is valid left hand side" property of the
+ // expression.
+ expression =
+ ParseMemberExpressionContinuation(Expression::Default(),
CHECK_OK);
}
- return Expression::Default();
+ return expression;
}
// No 'new' keyword.
return ParseMemberExpression(ok);
@@ -980,7 +984,7 @@
if (expression.IsThis()) {
expression = Expression::ThisProperty();
} else {
- expression = Expression::Default();
+ expression = Expression::Property();
}
break;
}
@@ -990,7 +994,7 @@
if (expression.IsThis()) {
expression = Expression::ThisProperty();
} else {
- expression = Expression::Default();
+ expression = Expression::Property();
}
break;
}
@@ -1102,7 +1106,6 @@
int end_position = scanner()->location().end_pos;
CheckOctalLiteral(start_position, end_position, CHECK_OK);
- return Expression::StrictFunction();
}
return Expression::Default();
=======================================
--- /branches/bleeding_edge/src/preparser.h Wed Mar 19 14:08:47 2014 UTC
+++ /branches/bleeding_edge/src/preparser.h Thu Mar 20 13:18:15 2014 UTC
@@ -554,8 +554,8 @@
return PreParserExpression(kThisPropertyExpression);
}
- static PreParserExpression StrictFunction() {
- return PreParserExpression(kStrictFunctionExpression);
+ static PreParserExpression Property() {
+ return PreParserExpression(kPropertyExpression);
}
bool IsIdentifier() { return (code_ & kIdentifierFlag) != 0; }
@@ -576,7 +576,9 @@
bool IsThisProperty() { return code_ == kThisPropertyExpression; }
- bool IsStrictFunction() { return code_ == kStrictFunctionExpression; }
+ bool IsProperty() {
+ return code_ == kPropertyExpression || code_ ==
kThisPropertyExpression;
+ }
// Dummy implementation for making expression->AsCall() work (see below).
PreParserExpression* operator->() { return this; }
@@ -590,9 +592,11 @@
void set_index(int index) {} // For YieldExpressions
private:
- // First two/three bits are used as flags.
- // Bit 0 and 1 represent identifiers or strings literals, and are
- // mutually exclusive, but can both be absent.
+ // Least significant 2 bits are used as flags. Bits 0 and 1 represent
+ // identifiers or strings literals, and are mutually exclusive, but can
both
+ // be absent. If the expression is an identifier or a string literal, the
+ // other bits describe the type (see PreParserIdentifier::Type and string
+ // literal constants below).
enum {
kUnknownExpression = 0,
// Identifiers
@@ -604,10 +608,11 @@
kUseStrictString = kStringLiteralFlag | 8,
kStringLiteralMask = kUseStrictString,
- // Below here applies if neither identifier nor string literal.
- kThisExpression = 4,
- kThisPropertyExpression = 8,
- kStrictFunctionExpression = 12
+ // Below here applies if neither identifier nor string literal.
Reserve the
+ // 2 least significant bits for flags.
+ kThisExpression = 1 << 2,
+ kThisPropertyExpression = 2 << 2,
+ kPropertyExpression = 3 << 2
};
explicit PreParserExpression(int expression_code) :
code_(expression_code) {}
@@ -830,8 +835,7 @@
// Determine whether the expression is a valid assignment left-hand side.
static bool IsValidLeftHandSide(PreParserExpression expression) {
- // TODO(marja): check properly; for now, leave it to parser.
- return true;
+ return expression.IsIdentifier() || expression.IsProperty();
}
static PreParserExpression MarkExpressionAsLValue(
=======================================
--- /branches/bleeding_edge/test/cctest/test-parsing.cc Wed Mar 19 14:08:47
2014 UTC
+++ /branches/bleeding_edge/test/cctest/test-parsing.cc Thu Mar 20 13:18:15
2014 UTC
@@ -2526,3 +2526,73 @@
RunParserSyncTest(strict_context_data, bad_statement_data, kError);
RunParserSyncTest(sloppy_context_data, bad_statement_data, kError);
}
+
+
+TEST(ErrorInvalidLeftHandSide) {
+ const char* assignment_context_data[][2] = {
+ // {"", " = 1;"},
+ // {"\"use strict\"; ", " = 1;"},
+ { NULL, NULL }
+ };
+
+ const char* prefix_context_data[][2] = {
+ {"++", ";"},
+ {"\"use strict\"; ++", ";"},
+ {NULL, NULL},
+ };
+
+ const char* postfix_context_data[][2] = {
+ {"", "++;"},
+ {"\"use strict\"; ", "++;"},
+ { NULL, NULL }
+ };
+
+ // Good left hand sides for assigment or prefix / postfix operations.
+ const char* good_statement_data[] = {
+ "foo",
+ "foo.bar",
+ "foo[bar]",
+ "foo()[bar]",
+ "foo().bar",
+ "this.foo",
+ "this[foo]",
+ "new foo()[bar]",
+ "new foo().bar",
+ NULL
+ };
+
+ // Bad left hand sides for assigment or prefix / postfix operations.
+ const char* bad_statement_data_common[] = {
+ "2",
+ "foo()",
+ "null",
+ "if", // Unexpected token
+ "{x: 1}", // Unexpected token
+ "this",
+ "\"bar\"",
+ "(foo + bar)",
+ "new new foo()[bar]", // means: new (new foo()[bar])
+ "new new foo().bar", // means: new (new foo()[bar])
+ NULL
+ };
+
+ // These are not okay for assignment, but okay for prefix / postix.
+ const char* bad_statement_data_for_assignment[] = {
+ "++foo",
+ "foo++",
+ "foo + bar",
+ NULL
+ };
+
+ RunParserSyncTest(assignment_context_data, good_statement_data,
kSuccess);
+ RunParserSyncTest(assignment_context_data, bad_statement_data_common,
kError);
+ RunParserSyncTest(assignment_context_data,
bad_statement_data_for_assignment,
+ kError);
+
+ RunParserSyncTest(prefix_context_data, good_statement_data, kSuccess);
+ RunParserSyncTest(prefix_context_data, bad_statement_data_common,
kError);
+
+ RunParserSyncTest(postfix_context_data, good_statement_data, kSuccess);
+ // TODO(marja): This doesn't work yet.
+ // RunParserSyncTest(postfix_context_data, bad_statement_data_common,
kError);
+}
--
--
v8-dev mailing list
v8-dev@googlegroups.com
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to v8-dev+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.