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

Side by Side Diff: runtime/vm/parser.cc

Issue 23452043: Introduce newline tokens into the token stream. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 3 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 (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "vm/parser.h" 5 #include "vm/parser.h"
6 6
7 #include "lib/invocation_mirror.h" 7 #include "lib/invocation_mirror.h"
8 #include "vm/bigint_operations.h" 8 #include "vm/bigint_operations.h"
9 #include "vm/bootstrap.h" 9 #include "vm/bootstrap.h"
10 #include "vm/class_finalizer.h" 10 #include "vm/class_finalizer.h"
(...skipping 1333 matching lines...) Expand 10 before | Expand all | Expand 10 after
1344 1344
1345 ReturnNode* return_node = new ReturnNode(token_pos, closure_call); 1345 ReturnNode* return_node = new ReturnNode(token_pos, closure_call);
1346 current_block_->statements->Add(return_node); 1346 current_block_->statements->Add(return_node);
1347 return CloseBlock(); 1347 return CloseBlock();
1348 } 1348 }
1349 1349
1350 1350
1351 void Parser::SkipBlock() { 1351 void Parser::SkipBlock() {
1352 ASSERT(CurrentToken() == Token::kLBRACE); 1352 ASSERT(CurrentToken() == Token::kLBRACE);
1353 GrowableArray<Token::Kind> token_stack(8); 1353 GrowableArray<Token::Kind> token_stack(8);
1354 // Adding the first kLBRACE here, because it will be consumed in the loop
1355 // right away.
1356 token_stack.Add(CurrentToken());
1354 const intptr_t block_start_pos = TokenPos(); 1357 const intptr_t block_start_pos = TokenPos();
1355 bool is_match = true; 1358 bool is_match = true;
1356 bool unexpected_token_found = false; 1359 bool unexpected_token_found = false;
1357 Token::Kind token; 1360 Token::Kind token;
1358 intptr_t token_pos; 1361 intptr_t token_pos;
1359 do { 1362 do {
1363 ConsumeToken();
1360 token = CurrentToken(); 1364 token = CurrentToken();
1361 token_pos = TokenPos(); 1365 token_pos = TokenPos();
1362 switch (token) { 1366 switch (token) {
1363 case Token::kLBRACE: 1367 case Token::kLBRACE:
1364 case Token::kLPAREN: 1368 case Token::kLPAREN:
1365 case Token::kLBRACK: 1369 case Token::kLBRACK:
1366 token_stack.Add(token); 1370 token_stack.Add(token);
1367 break; 1371 break;
1368 case Token::kRBRACE: 1372 case Token::kRBRACE:
1369 is_match = token_stack.RemoveLast() == Token::kLBRACE; 1373 is_match = token_stack.RemoveLast() == Token::kLBRACE;
1370 break; 1374 break;
1371 case Token::kRPAREN: 1375 case Token::kRPAREN:
1372 is_match = token_stack.RemoveLast() == Token::kLPAREN; 1376 is_match = token_stack.RemoveLast() == Token::kLPAREN;
1373 break; 1377 break;
1374 case Token::kRBRACK: 1378 case Token::kRBRACK:
1375 is_match = token_stack.RemoveLast() == Token::kLBRACK; 1379 is_match = token_stack.RemoveLast() == Token::kLBRACK;
1376 break; 1380 break;
1377 case Token::kEOS: 1381 case Token::kEOS:
1378 unexpected_token_found = true; 1382 unexpected_token_found = true;
1379 break; 1383 break;
1380 default: 1384 default:
1381 // nothing. 1385 // nothing.
1382 break; 1386 break;
1383 } 1387 }
1384 ConsumeToken();
1385 } while (!token_stack.is_empty() && is_match && !unexpected_token_found); 1388 } while (!token_stack.is_empty() && is_match && !unexpected_token_found);
1386 if (!is_match) { 1389 if (!is_match) {
1387 ErrorMsg(token_pos, "unbalanced '%s'", Token::Str(token)); 1390 ErrorMsg(token_pos, "unbalanced '%s'", Token::Str(token));
1388 } else if (unexpected_token_found) { 1391 } else if (unexpected_token_found) {
1389 ErrorMsg(block_start_pos, "unterminated block"); 1392 ErrorMsg(block_start_pos, "unterminated block");
1390 } 1393 }
1391 } 1394 }
1392 1395
1393 1396
1394 void Parser::ParseFormalParameter(bool allow_explicit_default_value, 1397 void Parser::ParseFormalParameter(bool allow_explicit_default_value,
(...skipping 1503 matching lines...) Expand 10 before | Expand all | Expand 10 after
2898 function_name, 2901 function_name,
2899 NULL, // No arguments. 2902 NULL, // No arguments.
2900 func.is_static() ? 2903 func.is_static() ?
2901 InvocationMirror::kStatic : 2904 InvocationMirror::kStatic :
2902 InvocationMirror::kDynamic, 2905 InvocationMirror::kDynamic,
2903 InvocationMirror::kMethod)); 2906 InvocationMirror::kMethod));
2904 end_token_pos = TokenPos(); 2907 end_token_pos = TokenPos();
2905 } else { 2908 } else {
2906 UnexpectedToken(); 2909 UnexpectedToken();
2907 } 2910 }
2911
2908 ASSERT(func.end_token_pos() == func.token_pos() || 2912 ASSERT(func.end_token_pos() == func.token_pos() ||
2909 func.end_token_pos() == end_token_pos); 2913 func.end_token_pos() == end_token_pos);
2910 func.set_end_token_pos(end_token_pos); 2914 func.set_end_token_pos(end_token_pos);
2911 SequenceNode* body = CloseBlock(); 2915 SequenceNode* body = CloseBlock();
2912 current_block_->statements->Add(body); 2916 current_block_->statements->Add(body);
2913 innermost_function_ = saved_innermost_function.raw(); 2917 innermost_function_ = saved_innermost_function.raw();
2914 last_used_try_index_ = saved_try_index; 2918 last_used_try_index_ = saved_try_index;
2915 return CloseBlock(); 2919 return CloseBlock();
2916 } 2920 }
2917 2921
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after
3181 ErrorMsg(method->name_pos, 3185 ErrorMsg(method->name_pos,
3182 "const constructor or factory '%s' may not have a function body", 3186 "const constructor or factory '%s' may not have a function body",
3183 method->name->ToCString()); 3187 method->name->ToCString());
3184 } 3188 }
3185 if (method->redirect_name != NULL) { 3189 if (method->redirect_name != NULL) {
3186 ErrorMsg(method->name_pos, 3190 ErrorMsg(method->name_pos,
3187 "Constructor with redirection may not have a function body"); 3191 "Constructor with redirection may not have a function body");
3188 } 3192 }
3189 if (CurrentToken() == Token::kLBRACE) { 3193 if (CurrentToken() == Token::kLBRACE) {
3190 SkipBlock(); 3194 SkipBlock();
3195 method_end_pos = TokenPos();
3196 ExpectToken(Token::kRBRACE);
3191 } else { 3197 } else {
3192 ConsumeToken(); 3198 ConsumeToken();
3193 SkipExpr(); 3199 SkipExpr();
3200 method_end_pos = TokenPos();
3194 ExpectSemicolon(); 3201 ExpectSemicolon();
3195 } 3202 }
3196 method_end_pos = TokenPos() - 1;
3197 } else if (IsLiteral("native")) { 3203 } else if (IsLiteral("native")) {
3198 if (method->has_abstract) { 3204 if (method->has_abstract) {
3199 ErrorMsg(method->name_pos, 3205 ErrorMsg(method->name_pos,
3200 "abstract method '%s' may not have a function body", 3206 "abstract method '%s' may not have a function body",
3201 method->name->ToCString()); 3207 method->name->ToCString());
3202 } else if (method->IsFactoryOrConstructor() && method->has_const) { 3208 } else if (method->IsFactoryOrConstructor() && method->has_const) {
3203 ErrorMsg(method->name_pos, 3209 ErrorMsg(method->name_pos,
3204 "const constructor or factory '%s' may not be native", 3210 "const constructor or factory '%s' may not be native",
3205 method->name->ToCString()); 3211 method->name->ToCString());
3206 } 3212 }
(...skipping 609 matching lines...) Expand 10 before | Expand all | Expand 10 after
3816 } 3822 }
3817 pending_classes.Add(cls, Heap::kOld); 3823 pending_classes.Add(cls, Heap::kOld);
3818 if (metadata_pos >= 0) { 3824 if (metadata_pos >= 0) {
3819 library_.AddClassMetadata(cls, metadata_pos); 3825 library_.AddClassMetadata(cls, metadata_pos);
3820 } 3826 }
3821 3827
3822 if (CurrentToken() != Token::kLBRACE) { 3828 if (CurrentToken() != Token::kLBRACE) {
3823 ErrorMsg("{ expected"); 3829 ErrorMsg("{ expected");
3824 } 3830 }
3825 SkipBlock(); 3831 SkipBlock();
3832 ExpectToken(Token::kRBRACE);
3826 } 3833 }
3827 3834
3828 3835
3829 void Parser::ParseClassDefinition(const Class& cls) { 3836 void Parser::ParseClassDefinition(const Class& cls) {
3830 TRACE_PARSER("ParseClassDefinition"); 3837 TRACE_PARSER("ParseClassDefinition");
3831 set_current_class(cls); 3838 set_current_class(cls);
3832 is_top_level_ = true; 3839 is_top_level_ = true;
3833 String& class_name = String::Handle(cls.Name()); 3840 String& class_name = String::Handle(cls.Name());
3834 const intptr_t class_pos = TokenPos(); 3841 const intptr_t class_pos = TokenPos();
3835 ClassDesc members(cls, class_name, false, class_pos); 3842 ClassDesc members(cls, class_name, false, class_pos);
(...skipping 738 matching lines...) Expand 10 before | Expand all | Expand 10 after
4574 ParamList params; 4581 ParamList params;
4575 const bool allow_explicit_default_values = true; 4582 const bool allow_explicit_default_values = true;
4576 ParseFormalParameterList(allow_explicit_default_values, false, &params); 4583 ParseFormalParameterList(allow_explicit_default_values, false, &params);
4577 4584
4578 intptr_t function_end_pos = function_pos; 4585 intptr_t function_end_pos = function_pos;
4579 if (is_external) { 4586 if (is_external) {
4580 function_end_pos = TokenPos(); 4587 function_end_pos = TokenPos();
4581 ExpectSemicolon(); 4588 ExpectSemicolon();
4582 } else if (CurrentToken() == Token::kLBRACE) { 4589 } else if (CurrentToken() == Token::kLBRACE) {
4583 SkipBlock(); 4590 SkipBlock();
4584 function_end_pos = TokenPos() - 1; 4591 function_end_pos = TokenPos();
4592 ExpectToken(Token::kRBRACE);
4585 } else if (CurrentToken() == Token::kARROW) { 4593 } else if (CurrentToken() == Token::kARROW) {
4586 ConsumeToken(); 4594 ConsumeToken();
4587 SkipExpr(); 4595 SkipExpr();
4588 function_end_pos = TokenPos(); 4596 function_end_pos = TokenPos();
4589 ExpectSemicolon(); 4597 ExpectSemicolon();
4590 } else if (IsLiteral("native")) { 4598 } else if (IsLiteral("native")) {
4591 ParseNativeDeclaration(); 4599 ParseNativeDeclaration();
4592 function_end_pos = TokenPos(); 4600 function_end_pos = TokenPos();
4593 ExpectSemicolon(); 4601 ExpectSemicolon();
4594 } else { 4602 } else {
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
4702 is_getter ? "getter" : "setter", 4710 is_getter ? "getter" : "setter",
4703 field_name->ToCString()); 4711 field_name->ToCString());
4704 } 4712 }
4705 4713
4706 intptr_t accessor_end_pos = accessor_pos; 4714 intptr_t accessor_end_pos = accessor_pos;
4707 if (is_external) { 4715 if (is_external) {
4708 accessor_end_pos = TokenPos(); 4716 accessor_end_pos = TokenPos();
4709 ExpectSemicolon(); 4717 ExpectSemicolon();
4710 } else if (CurrentToken() == Token::kLBRACE) { 4718 } else if (CurrentToken() == Token::kLBRACE) {
4711 SkipBlock(); 4719 SkipBlock();
4712 accessor_end_pos = TokenPos() - 1; 4720 accessor_end_pos = TokenPos();
4721 ExpectToken(Token::kRBRACE);
4713 } else if (CurrentToken() == Token::kARROW) { 4722 } else if (CurrentToken() == Token::kARROW) {
4714 ConsumeToken(); 4723 ConsumeToken();
4715 SkipExpr(); 4724 SkipExpr();
4716 accessor_end_pos = TokenPos(); 4725 accessor_end_pos = TokenPos();
4717 ExpectSemicolon(); 4726 ExpectSemicolon();
4718 } else if (IsLiteral("native")) { 4727 } else if (IsLiteral("native")) {
4719 ParseNativeDeclaration(); 4728 ParseNativeDeclaration();
4720 accessor_end_pos = TokenPos(); 4729 accessor_end_pos = TokenPos();
4721 ExpectSemicolon(); 4730 ExpectSemicolon();
4722 } else { 4731 } else {
(...skipping 5424 matching lines...) Expand 10 before | Expand all | Expand 10 after
10147 ExpectIdentifier("function name expected"); 10156 ExpectIdentifier("function name expected");
10148 } 10157 }
10149 if (CurrentToken() == Token::kLPAREN) { 10158 if (CurrentToken() == Token::kLPAREN) {
10150 const bool allow_explicit_default_values = true; 10159 const bool allow_explicit_default_values = true;
10151 ParamList params; 10160 ParamList params;
10152 params.skipped = true; 10161 params.skipped = true;
10153 ParseFormalParameterList(allow_explicit_default_values, false, &params); 10162 ParseFormalParameterList(allow_explicit_default_values, false, &params);
10154 } 10163 }
10155 if (CurrentToken() == Token::kLBRACE) { 10164 if (CurrentToken() == Token::kLBRACE) {
10156 SkipBlock(); 10165 SkipBlock();
10166 ExpectToken(Token::kRBRACE);
10157 } else if (CurrentToken() == Token::kARROW) { 10167 } else if (CurrentToken() == Token::kARROW) {
10158 ConsumeToken(); 10168 ConsumeToken();
10159 SkipExpr(); 10169 SkipExpr();
10160 } 10170 }
10161 } 10171 }
10162 10172
10163 10173
10164 // Skips function/method/constructor/getter/setter preambles until the formal 10174 // Skips function/method/constructor/getter/setter preambles until the formal
10165 // parameter list. It is enough to skip the tokens, since we have already 10175 // parameter list. It is enough to skip the tokens, since we have already
10166 // previously parsed the function. 10176 // previously parsed the function.
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after
10442 void Parser::SkipQualIdent() { 10452 void Parser::SkipQualIdent() {
10443 ASSERT(IsIdentifier()); 10453 ASSERT(IsIdentifier());
10444 ConsumeToken(); 10454 ConsumeToken();
10445 if (CurrentToken() == Token::kPERIOD) { 10455 if (CurrentToken() == Token::kPERIOD) {
10446 ConsumeToken(); // Consume the kPERIOD token. 10456 ConsumeToken(); // Consume the kPERIOD token.
10447 ExpectIdentifier("identifier expected after '.'"); 10457 ExpectIdentifier("identifier expected after '.'");
10448 } 10458 }
10449 } 10459 }
10450 10460
10451 } // namespace dart 10461 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698