if (result.IsThis()) {
result = Expression::ThisProperty();
} else {
- result = Expression::Default();
+ result = Expression::Property();
}
break;
}
if (result.IsThis()) {
result = Expression::ThisProperty();
} else {
- result = Expression::Default();
+ result = Expression::Property();
}
break;
}
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);
if (expression.IsThis()) {
expression = Expression::ThisProperty();
} else {
- expression = Expression::Default();
+ expression = Expression::Property();
}
break;
}
if (expression.IsThis()) {
expression = Expression::ThisProperty();
} else {
- expression = Expression::Default();
+ expression = Expression::Property();
}
break;
}
int end_position = scanner()->location().end_pos;
CheckOctalLiteral(start_position, end_position, CHECK_OK);
- return Expression::StrictFunction();
}
return Expression::Default();
return PreParserExpression(kThisPropertyExpression);
}
- static PreParserExpression StrictFunction() {
- return PreParserExpression(kStrictFunctionExpression);
+ static PreParserExpression Property() {
+ return PreParserExpression(kPropertyExpression);
}
bool IsIdentifier() { return (code_ & kIdentifierFlag) != 0; }
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; }
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
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) {}
// 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(
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);
+}