| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 /** | 5 /** |
| 6 * An event generating parser of Dart programs. This parser expects | 6 * An event generating parser of Dart programs. This parser expects |
| 7 * all tokens in a linked list. | 7 * all tokens in a linked list. |
| 8 */ | 8 */ |
| 9 class Parser { | 9 class Parser { |
| 10 final Listener listener; | 10 final Listener listener; |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 Token skipBlock(Token token) { | 171 Token skipBlock(Token token) { |
| 172 if (!optional('{', token)) { | 172 if (!optional('{', token)) { |
| 173 return listener.expectedBlock(token); | 173 return listener.expectedBlock(token); |
| 174 } | 174 } |
| 175 BeginGroupToken beginGroupToken = token; | 175 BeginGroupToken beginGroupToken = token; |
| 176 assert(beginGroupToken.endGroup === null || | 176 assert(beginGroupToken.endGroup === null || |
| 177 beginGroupToken.endGroup.kind === $CLOSE_CURLY_BRACKET); | 177 beginGroupToken.endGroup.kind === $CLOSE_CURLY_BRACKET); |
| 178 return beginGroupToken.endGroup; | 178 return beginGroupToken.endGroup; |
| 179 } | 179 } |
| 180 | 180 |
| 181 Token skipFormals(BeginGroupToken token) { | |
| 182 expect('(', token); | |
| 183 return token.endGroup; | |
| 184 } | |
| 185 | |
| 186 Token parseClass(Token token) { | 181 Token parseClass(Token token) { |
| 187 Token begin = token; | 182 Token begin = token; |
| 188 listener.beginClassDeclaration(token); | 183 listener.beginClassDeclaration(token); |
| 189 if (optional('abstract', token)) { | 184 if (optional('abstract', token)) { |
| 190 // TODO(ahe): Notify listener about abstract modifier. | 185 // TODO(ahe): Notify listener about abstract modifier. |
| 191 token = token.next; | 186 token = token.next; |
| 192 } | 187 } |
| 193 token = parseIdentifier(token.next); | 188 token = parseIdentifier(token.next); |
| 194 token = parseTypeVariablesOpt(token); | 189 token = parseTypeVariablesOpt(token); |
| 195 Token extendsKeyword; | 190 Token extendsKeyword; |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 360 int fieldCount = 1; | 355 int fieldCount = 1; |
| 361 token = parseFieldInitializerOpt(token); | 356 token = parseFieldInitializerOpt(token); |
| 362 while (optional(',', token)) { | 357 while (optional(',', token)) { |
| 363 token = parseIdentifier(token.next); | 358 token = parseIdentifier(token.next); |
| 364 token = parseFieldInitializerOpt(token); | 359 token = parseFieldInitializerOpt(token); |
| 365 ++fieldCount; | 360 ++fieldCount; |
| 366 } | 361 } |
| 367 expectSemicolon(token); | 362 expectSemicolon(token); |
| 368 listener.endTopLevelFields(fieldCount, start, token); | 363 listener.endTopLevelFields(fieldCount, start, token); |
| 369 } else { | 364 } else { |
| 370 token = skipFormals(token).next; | 365 token = parseFormalParameters(token); |
| 371 token = parseFunctionBody(token, false); | 366 token = parseFunctionBody(token, false); |
| 372 listener.endTopLevelMethod(start, token); | 367 listener.endTopLevelMethod(start, token); |
| 373 } | 368 } |
| 374 return token.next; | 369 return token.next; |
| 375 } | 370 } |
| 376 | 371 |
| 377 Token parseFieldInitializerOpt(Token token) { | 372 Token parseFieldInitializerOpt(Token token) { |
| 378 if (optional('=', token)) { | 373 if (optional('=', token)) { |
| 379 return parseExpression(token.next); | 374 return parseExpression(token.next); |
| 380 } else { | 375 } else { |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 524 token = parseFieldInitializerOpt(token); | 519 token = parseFieldInitializerOpt(token); |
| 525 while (optional(',', token)) { | 520 while (optional(',', token)) { |
| 526 // TODO(ahe): Count these. | 521 // TODO(ahe): Count these. |
| 527 token = parseIdentifier(token.next); | 522 token = parseIdentifier(token.next); |
| 528 token = parseFieldInitializerOpt(token); | 523 token = parseFieldInitializerOpt(token); |
| 529 ++fieldCount; | 524 ++fieldCount; |
| 530 } | 525 } |
| 531 expectSemicolon(token); | 526 expectSemicolon(token); |
| 532 listener.endFields(fieldCount, start, token); | 527 listener.endFields(fieldCount, start, token); |
| 533 } else { | 528 } else { |
| 534 token = skipFormals(token).next; | 529 token = parseFormalParameters(token); |
| 535 token = parseInitializersOpt(token); | 530 token = parseInitializersOpt(token); |
| 536 if (!optional(';', token)) { | 531 token = parseFunctionBody(token, false); |
| 537 token = parseFunctionBody(token, false); | |
| 538 } | |
| 539 listener.endMethod(start, token); | 532 listener.endMethod(start, token); |
| 540 } | 533 } |
| 541 return token.next; | 534 return token.next; |
| 542 } | 535 } |
| 543 | 536 |
| 544 Token parseFactoryMethod(Token token) { | 537 Token parseFactoryMethod(Token token) { |
| 545 assert(optional('factory', token)); | 538 assert(optional('factory', token)); |
| 546 Token factoryKeyword = token; | 539 Token factoryKeyword = token; |
| 547 listener.beginFactoryMethod(factoryKeyword); | 540 listener.beginFactoryMethod(factoryKeyword); |
| 548 token = token.next; // Skip 'factory'. | 541 token = token.next; // Skip 'factory'. |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 605 token = parseFormalParameters(token); | 598 token = parseFormalParameters(token); |
| 606 bool isBlock = optional('{', token); | 599 bool isBlock = optional('{', token); |
| 607 token = parseFunctionBody(token, true); | 600 token = parseFunctionBody(token, true); |
| 608 listener.endFunction(token); | 601 listener.endFunction(token); |
| 609 return isBlock ? token.next : token; | 602 return isBlock ? token.next : token; |
| 610 } | 603 } |
| 611 | 604 |
| 612 Token parseFunctionBody(Token token, bool isExpression) { | 605 Token parseFunctionBody(Token token, bool isExpression) { |
| 613 if (optional(';', token)) { | 606 if (optional(';', token)) { |
| 614 listener.endFunctionBody(0, null, token); | 607 listener.endFunctionBody(0, null, token); |
| 615 return token.next; | 608 return token; |
| 616 } else if (optional('=>', token)) { | 609 } else if (optional('=>', token)) { |
| 617 Token begin = token; | 610 Token begin = token; |
| 618 token = parseExpression(token.next); | 611 token = parseExpression(token.next); |
| 619 if (!isExpression) { | 612 if (!isExpression) { |
| 620 expectSemicolon(token); | 613 expectSemicolon(token); |
| 621 listener.endReturnStatement(true, begin, token); | 614 listener.endReturnStatement(true, begin, token); |
| 622 } else { | 615 } else { |
| 623 listener.endReturnStatement(true, begin, null); | 616 listener.endReturnStatement(true, begin, null); |
| 624 } | 617 } |
| 625 return token; | 618 return token; |
| (...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 927 (kind === OPEN_CURLY_BRACKET_TOKEN) || | 920 (kind === OPEN_CURLY_BRACKET_TOKEN) || |
| 928 token.stringValue === '[]') { | 921 token.stringValue === '[]') { |
| 929 return parseLiteralListOrMap(token); | 922 return parseLiteralListOrMap(token); |
| 930 } else { | 923 } else { |
| 931 listener.unexpected(token); | 924 listener.unexpected(token); |
| 932 throw 'not yet implemented'; | 925 throw 'not yet implemented'; |
| 933 } | 926 } |
| 934 } | 927 } |
| 935 | 928 |
| 936 Token parseParenthesizedExpressionOrFunctionLiteral(Token token) { | 929 Token parseParenthesizedExpressionOrFunctionLiteral(Token token) { |
| 937 Token beginGroup = token; | 930 BeginGroupToken beginGroup = token; |
| 938 int kind = beginGroup.endGroup.next.kind; | 931 int kind = beginGroup.endGroup.next.kind; |
| 939 if (mayParseFunctionExpressions && | 932 if (mayParseFunctionExpressions && |
| 940 (kind === FUNCTION_TOKEN || kind === OPEN_CURLY_BRACKET_TOKEN)) { | 933 (kind === FUNCTION_TOKEN || kind === OPEN_CURLY_BRACKET_TOKEN)) { |
| 941 return parseUnamedFunction(token); | 934 return parseUnamedFunction(token); |
| 942 } else { | 935 } else { |
| 943 bool old = mayParseFunctionExpressions; | 936 bool old = mayParseFunctionExpressions; |
| 944 mayParseFunctionExpressions = true; | 937 mayParseFunctionExpressions = true; |
| 945 token = parseParenthesizedExpression(token); | 938 token = parseParenthesizedExpression(token); |
| 946 mayParseFunctionExpressions = old; | 939 mayParseFunctionExpressions = old; |
| 947 return token; | 940 return token; |
| (...skipping 469 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1417 } | 1410 } |
| 1418 listener.handleContinueStatement(hasTarget, continueKeyword, token); | 1411 listener.handleContinueStatement(hasTarget, continueKeyword, token); |
| 1419 return expectSemicolon(token); | 1412 return expectSemicolon(token); |
| 1420 } | 1413 } |
| 1421 | 1414 |
| 1422 Token parseEmptyStatement(Token token) { | 1415 Token parseEmptyStatement(Token token) { |
| 1423 listener.handleEmptyStatement(token); | 1416 listener.handleEmptyStatement(token); |
| 1424 return expectSemicolon(token); | 1417 return expectSemicolon(token); |
| 1425 } | 1418 } |
| 1426 } | 1419 } |
| OLD | NEW |