Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(736)

Side by Side Diff: src/parser.cc

Issue 173273006: Unify (Pre)Parser::ParseObjectLiteral and add tests. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | src/preparser.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 3474 matching lines...) Expand 10 before | Expand all | Expand 10 after
3485 } 3485 }
3486 3486
3487 3487
3488 Handle<FixedArray> CompileTimeValue::GetElements(Handle<FixedArray> value) { 3488 Handle<FixedArray> CompileTimeValue::GetElements(Handle<FixedArray> value) {
3489 return Handle<FixedArray>(FixedArray::cast(value->get(kElementsSlot))); 3489 return Handle<FixedArray>(FixedArray::cast(value->get(kElementsSlot)));
3490 } 3490 }
3491 3491
3492 3492
3493 Expression* Parser::ParseObjectLiteral(bool* ok) { 3493 Expression* Parser::ParseObjectLiteral(bool* ok) {
3494 // ObjectLiteral :: 3494 // ObjectLiteral ::
3495 // '{' ( 3495 // '{' ((
3496 // ((IdentifierName | String | Number) ':' AssignmentExpression) 3496 // ((IdentifierName | String | Number) ':' AssignmentExpression) |
3497 // | (('get' | 'set') (IdentifierName | String | Number) FunctionLiteral) 3497 // (('get' | 'set') (IdentifierName | String | Number) FunctionLiteral)
3498 // )*[','] '}' 3498 // ) ',')* '}'
3499 // (Except that trailing comma is not required and not allowed.)
3499 3500
3500 int pos = peek_position(); 3501 int pos = peek_position();
3501 ZoneList<ObjectLiteral::Property*>* properties = 3502 ZoneList<ObjectLiteral::Property*>* properties =
3502 new(zone()) ZoneList<ObjectLiteral::Property*>(4, zone()); 3503 new(zone()) ZoneList<ObjectLiteral::Property*>(4, zone());
3503 int number_of_boilerplate_properties = 0; 3504 int number_of_boilerplate_properties = 0;
3504 bool has_function = false; 3505 bool has_function = false;
3505 3506
3506 ObjectLiteralChecker checker(this, scope_->language_mode()); 3507 ObjectLiteralChecker checker(this, scope_->language_mode());
3507 3508
3508 Expect(Token::LBRACE, CHECK_OK); 3509 Expect(Token::LBRACE, CHECK_OK);
(...skipping 13 matching lines...) Expand all
3522 bool is_setter = false; 3523 bool is_setter = false;
3523 Handle<String> id = 3524 Handle<String> id =
3524 ParseIdentifierNameOrGetOrSet(&is_getter, &is_setter, CHECK_OK); 3525 ParseIdentifierNameOrGetOrSet(&is_getter, &is_setter, CHECK_OK);
3525 if (fni_ != NULL) fni_->PushLiteralName(id); 3526 if (fni_ != NULL) fni_->PushLiteralName(id);
3526 3527
3527 if ((is_getter || is_setter) && peek() != Token::COLON) { 3528 if ((is_getter || is_setter) && peek() != Token::COLON) {
3528 // Special handling of getter and setter syntax: 3529 // Special handling of getter and setter syntax:
3529 // { ... , get foo() { ... }, ... , set foo(v) { ... v ... } , ... } 3530 // { ... , get foo() { ... }, ... , set foo(v) { ... v ... } , ... }
3530 // We have already read the "get" or "set" keyword. 3531 // We have already read the "get" or "set" keyword.
3531 Token::Value next = Next(); 3532 Token::Value next = Next();
3532 bool is_keyword = Token::IsKeyword(next);
3533 if (next != i::Token::IDENTIFIER && 3533 if (next != i::Token::IDENTIFIER &&
3534 next != i::Token::FUTURE_RESERVED_WORD && 3534 next != i::Token::FUTURE_RESERVED_WORD &&
3535 next != i::Token::FUTURE_STRICT_RESERVED_WORD && 3535 next != i::Token::FUTURE_STRICT_RESERVED_WORD &&
3536 next != i::Token::NUMBER && 3536 next != i::Token::NUMBER &&
3537 next != i::Token::STRING && 3537 next != i::Token::STRING &&
3538 !is_keyword) { 3538 !Token::IsKeyword(next)) {
3539 // Unexpected token.
3540 ReportUnexpectedToken(next); 3539 ReportUnexpectedToken(next);
3541 *ok = false; 3540 *ok = false;
3542 return NULL; 3541 return NULL;
3543 } 3542 }
3544 // Validate the property. 3543 // Validate the property.
3545 PropertyKind type = is_getter ? kGetterProperty : kSetterProperty; 3544 PropertyKind type = is_getter ? kGetterProperty : kSetterProperty;
3546 checker.CheckProperty(next, type, CHECK_OK); 3545 checker.CheckProperty(next, type, CHECK_OK);
3547 Handle<String> name = is_keyword 3546 Handle<String> name = GetSymbol();
3548 ? isolate_->factory()->InternalizeUtf8String(Token::String(next))
3549 : GetSymbol();
3550 FunctionLiteral* value = 3547 FunctionLiteral* value =
3551 ParseFunctionLiteral(name, 3548 ParseFunctionLiteral(name,
3552 scanner()->location(), 3549 scanner()->location(),
3553 false, // reserved words are allowed here 3550 false, // reserved words are allowed here
3554 false, // not a generator 3551 false, // not a generator
3555 RelocInfo::kNoPosition, 3552 RelocInfo::kNoPosition,
3556 FunctionLiteral::ANONYMOUS_EXPRESSION, 3553 FunctionLiteral::ANONYMOUS_EXPRESSION,
3557 CHECK_OK); 3554 CHECK_OK);
3558 // Allow any number of parameters for compatibilty with JSC. 3555 // Allow any number of parameters for compatibilty with JSC.
3559 // Specification only allows zero parameters for get and one for set. 3556 // Specification only allows zero parameters for get and one for set.
3560 ObjectLiteral::Property* property = 3557 ObjectLiteral::Property* property =
3561 factory()->NewObjectLiteralProperty(is_getter, value, next_pos); 3558 factory()->NewObjectLiteralProperty(is_getter, value, next_pos);
3562 if (ObjectLiteral::IsBoilerplateProperty(property)) { 3559 if (ObjectLiteral::IsBoilerplateProperty(property)) {
3563 number_of_boilerplate_properties++; 3560 number_of_boilerplate_properties++;
3564 } 3561 }
3565 properties->Add(property, zone()); 3562 properties->Add(property, zone());
3566 if (peek() != Token::RBRACE) Expect(Token::COMMA, CHECK_OK); 3563 if (peek() != Token::RBRACE) Expect(Token::COMMA, CHECK_OK);
3567 3564
3568 if (fni_ != NULL) { 3565 if (fni_ != NULL) {
3569 fni_->Infer(); 3566 fni_->Infer();
3570 fni_->Leave(); 3567 fni_->Leave();
3571 } 3568 }
3572 continue; // restart the while 3569 continue; // restart the while
3573 } 3570 }
3574 // Failed to parse as get/set property, so it's just a property 3571 // Failed to parse as get/set property, so it's just a normal property
3575 // called "get" or "set". 3572 // (which might be called "get" or "set" or something else).
3576 key = factory()->NewLiteral(id, next_pos); 3573 key = factory()->NewLiteral(id, next_pos);
3577 break; 3574 break;
3578 } 3575 }
3579 case Token::STRING: { 3576 case Token::STRING: {
3580 Consume(Token::STRING); 3577 Consume(Token::STRING);
3581 Handle<String> string = GetSymbol(); 3578 Handle<String> string = GetSymbol();
3582 if (fni_ != NULL) fni_->PushLiteralName(string); 3579 if (fni_ != NULL) fni_->PushLiteralName(string);
3583 uint32_t index; 3580 uint32_t index;
3584 if (!string.is_null() && string->AsArrayIndex(&index)) { 3581 if (!string.is_null() && string->AsArrayIndex(&index)) {
3585 key = factory()->NewNumberLiteral(index, next_pos); 3582 key = factory()->NewNumberLiteral(index, next_pos);
(...skipping 11 matching lines...) Expand all
3597 ALLOW_IMPLICIT_OCTAL | ALLOW_BINARY); 3594 ALLOW_IMPLICIT_OCTAL | ALLOW_BINARY);
3598 key = factory()->NewNumberLiteral(value, next_pos); 3595 key = factory()->NewNumberLiteral(value, next_pos);
3599 break; 3596 break;
3600 } 3597 }
3601 default: 3598 default:
3602 if (Token::IsKeyword(next)) { 3599 if (Token::IsKeyword(next)) {
3603 Consume(next); 3600 Consume(next);
3604 Handle<String> string = GetSymbol(); 3601 Handle<String> string = GetSymbol();
3605 key = factory()->NewLiteral(string, next_pos); 3602 key = factory()->NewLiteral(string, next_pos);
3606 } else { 3603 } else {
3607 // Unexpected token.
3608 Token::Value next = Next(); 3604 Token::Value next = Next();
3609 ReportUnexpectedToken(next); 3605 ReportUnexpectedToken(next);
3610 *ok = false; 3606 *ok = false;
3611 return NULL; 3607 return NULL;
3612 } 3608 }
3613 } 3609 }
3614 3610
3615 // Validate the property 3611 // Validate the property
3616 checker.CheckProperty(next, kValueProperty, CHECK_OK); 3612 checker.CheckProperty(next, kValueProperty, CHECK_OK);
3617 3613
(...skipping 1735 matching lines...) Expand 10 before | Expand all | Expand 10 after
5353 ASSERT(info()->isolate()->has_pending_exception()); 5349 ASSERT(info()->isolate()->has_pending_exception());
5354 } else { 5350 } else {
5355 result = ParseProgram(); 5351 result = ParseProgram();
5356 } 5352 }
5357 } 5353 }
5358 info()->SetFunction(result); 5354 info()->SetFunction(result);
5359 return (result != NULL); 5355 return (result != NULL);
5360 } 5356 }
5361 5357
5362 } } // namespace v8::internal 5358 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « no previous file | src/preparser.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698