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

Side by Side Diff: src/parser.cc

Issue 7549008: Preliminary code for block scopes and block contexts. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Next iteration Created 9 years, 4 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
OLDNEW
1 // Copyright 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 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 1192 matching lines...) Expand 10 before | Expand all | Expand 10 after
1203 // iterations or 'switch' statements (i.e., BreakableStatements), 1203 // iterations or 'switch' statements (i.e., BreakableStatements),
1204 // labels can be simply ignored in all other cases; except for 1204 // labels can be simply ignored in all other cases; except for
1205 // trivial labeled break statements 'label: break label' which is 1205 // trivial labeled break statements 'label: break label' which is
1206 // parsed into an empty statement. 1206 // parsed into an empty statement.
1207 1207
1208 // Keep the source position of the statement 1208 // Keep the source position of the statement
1209 int statement_pos = scanner().peek_location().beg_pos; 1209 int statement_pos = scanner().peek_location().beg_pos;
1210 Statement* stmt = NULL; 1210 Statement* stmt = NULL;
1211 switch (peek()) { 1211 switch (peek()) {
1212 case Token::LBRACE: 1212 case Token::LBRACE:
1213 return ParseBlock(labels, ok); 1213 return ParseScopedBlock(labels, ok);
1214 1214
1215 case Token::CONST: // fall through 1215 case Token::CONST: // fall through
1216 case Token::VAR: 1216 case Token::VAR:
1217 stmt = ParseVariableStatement(ok); 1217 stmt = ParseVariableStatement(ok);
1218 break; 1218 break;
1219 1219
1220 case Token::SEMICOLON: 1220 case Token::SEMICOLON:
1221 Next(); 1221 Next();
1222 return EmptyStatement(); 1222 return EmptyStatement();
1223 1223
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after
1503 if (stat && !stat->IsEmpty()) { 1503 if (stat && !stat->IsEmpty()) {
1504 result->AddStatement(stat); 1504 result->AddStatement(stat);
1505 block_finder.Update(stat); 1505 block_finder.Update(stat);
1506 } 1506 }
1507 } 1507 }
1508 Expect(Token::RBRACE, CHECK_OK); 1508 Expect(Token::RBRACE, CHECK_OK);
1509 return result; 1509 return result;
1510 } 1510 }
1511 1511
1512 1512
1513 Statement* Parser::ParseScopedBlock(ZoneStringList* labels, bool* ok) {
1514 if (!FLAG_harmony_block_scoping) return ParseBlock(labels, ok);
Kevin Millikin (Chromium) 2011/08/10 11:19:27 This is OK for now, but you should try to think of
Steven 2011/08/10 12:21:00 Yes sure. But indeed I think this should be done w
1515
1516 // Construct block expecting 16 statements.
1517 Block* body = new(zone()) Block(isolate(), labels, 16, false);
1518 Scope* saved_scope = top_scope_;
1519 Scope* block_scope = NewScope(top_scope_,
1520 Scope::BLOCK_SCOPE,
1521 inside_with());
1522 body->set_block_scope(block_scope);
1523 block_scope->DeclareLocal(isolate()->factory()->block_scope_symbol(),
1524 Variable::VAR);
1525 if (top_scope_->is_strict_mode()) {
1526 block_scope->EnableStrictMode();
1527 }
1528 top_scope_ = block_scope;
1529
1530 // Parse the statements and collect escaping labels.
1531 TargetCollector collector;
1532 Target target(&this->target_stack_, &collector);
1533 Expect(Token::LBRACE, CHECK_OK);
1534 {
1535 Target target_body(&this->target_stack_, body);
1536 InitializationBlockFinder block_finder(top_scope_, target_stack_);
1537
1538 while (peek() != Token::RBRACE) {
1539 Statement* stat = ParseStatement(NULL, CHECK_OK);
1540 if (stat && !stat->IsEmpty()) {
1541 body->AddStatement(stat);
1542 block_finder.Update(stat);
1543 }
1544 }
1545 }
1546 Expect(Token::RBRACE, CHECK_OK);
1547
1548 // Create exit block.
1549 Block* exit = new(zone()) Block(isolate(), NULL, 1, false);
1550 exit->AddStatement(new(zone()) ExitContextStatement());
1551
1552 // Return a try-finally statement.
1553 TryFinallyStatement* result = new(zone()) TryFinallyStatement(body, exit);
1554 result->set_escaping_targets(collector.targets());
1555 top_scope_ = saved_scope;
1556 return result;
1557 }
1558
1559
1513 Block* Parser::ParseVariableStatement(bool* ok) { 1560 Block* Parser::ParseVariableStatement(bool* ok) {
1514 // VariableStatement :: 1561 // VariableStatement ::
1515 // VariableDeclarations ';' 1562 // VariableDeclarations ';'
1516 1563
1517 Handle<String> ignore; 1564 Handle<String> ignore;
1518 Block* result = ParseVariableDeclarations(true, &ignore, CHECK_OK); 1565 Block* result = ParseVariableDeclarations(true, &ignore, CHECK_OK);
1519 ExpectSemicolon(CHECK_OK); 1566 ExpectSemicolon(CHECK_OK);
1520 return result; 1567 return result;
1521 } 1568 }
1522 1569
(...skipping 3573 matching lines...) Expand 10 before | Expand all | Expand 10 after
5096 result->capture_count = capture_count; 5143 result->capture_count = capture_count;
5097 } 5144 }
5098 return !parser.failed(); 5145 return !parser.failed();
5099 } 5146 }
5100 5147
5101 5148
5102 bool ParserApi::Parse(CompilationInfo* info) { 5149 bool ParserApi::Parse(CompilationInfo* info) {
5103 ASSERT(info->function() == NULL); 5150 ASSERT(info->function() == NULL);
5104 FunctionLiteral* result = NULL; 5151 FunctionLiteral* result = NULL;
5105 Handle<Script> script = info->script(); 5152 Handle<Script> script = info->script();
5153 bool save_block_scoping = FLAG_harmony_block_scoping;
5154 FLAG_harmony_block_scoping = !info->is_native() && FLAG_harmony_block_scoping;
Kevin Millikin (Chromium) 2011/08/10 11:19:27 I'm always nervous about assigning to the flags.
Steven 2011/08/10 12:21:00 Done.
5106 if (info->is_lazy()) { 5155 if (info->is_lazy()) {
5107 Parser parser(script, true, NULL, NULL); 5156 Parser parser(script, true, NULL, NULL);
5108 result = parser.ParseLazy(info); 5157 result = parser.ParseLazy(info);
5109 } else { 5158 } else {
5110 // Whether we allow %identifier(..) syntax. 5159 // Whether we allow %identifier(..) syntax.
5111 bool allow_natives_syntax = 5160 bool allow_natives_syntax =
5112 info->allows_natives_syntax() || FLAG_allow_natives_syntax; 5161 info->allows_natives_syntax() || FLAG_allow_natives_syntax;
5113 ScriptDataImpl* pre_data = info->pre_parse_data(); 5162 ScriptDataImpl* pre_data = info->pre_parse_data();
5114 Parser parser(script, allow_natives_syntax, info->extension(), pre_data); 5163 Parser parser(script, allow_natives_syntax, info->extension(), pre_data);
5115 if (pre_data != NULL && pre_data->has_error()) { 5164 if (pre_data != NULL && pre_data->has_error()) {
5116 Scanner::Location loc = pre_data->MessageLocation(); 5165 Scanner::Location loc = pre_data->MessageLocation();
5117 const char* message = pre_data->BuildMessage(); 5166 const char* message = pre_data->BuildMessage();
5118 Vector<const char*> args = pre_data->BuildArgs(); 5167 Vector<const char*> args = pre_data->BuildArgs();
5119 parser.ReportMessageAt(loc, message, args); 5168 parser.ReportMessageAt(loc, message, args);
5120 DeleteArray(message); 5169 DeleteArray(message);
5121 for (int i = 0; i < args.length(); i++) { 5170 for (int i = 0; i < args.length(); i++) {
5122 DeleteArray(args[i]); 5171 DeleteArray(args[i]);
5123 } 5172 }
5124 DeleteArray(args.start()); 5173 DeleteArray(args.start());
5125 ASSERT(info->isolate()->has_pending_exception()); 5174 ASSERT(info->isolate()->has_pending_exception());
5126 } else { 5175 } else {
5127 Handle<String> source = Handle<String>(String::cast(script->source())); 5176 Handle<String> source = Handle<String>(String::cast(script->source()));
5128 result = parser.ParseProgram(source, 5177 result = parser.ParseProgram(source,
5129 info->is_global(), 5178 info->is_global(),
5130 info->StrictMode()); 5179 info->StrictMode());
5131 } 5180 }
5132 } 5181 }
5133 5182 FLAG_harmony_block_scoping = save_block_scoping;
5134 info->SetFunction(result); 5183 info->SetFunction(result);
5135 return (result != NULL); 5184 return (result != NULL);
5136 } 5185 }
5137 5186
5138 } } // namespace v8::internal 5187 } } // namespace v8::internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698