| 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<L extends Listener> { | 9 class Parser<L extends Listener> { |
| 10 final L listener; | 10 final L listener; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 token = skipBlock(token); | 58 token = skipBlock(token); |
| 59 listener.endInterface(token); | 59 listener.endInterface(token); |
| 60 return token.next; | 60 return token.next; |
| 61 } | 61 } |
| 62 | 62 |
| 63 Token parseNamedFunctionAlias(Token token) { | 63 Token parseNamedFunctionAlias(Token token) { |
| 64 listener.beginFunctionTypeAlias(token); | 64 listener.beginFunctionTypeAlias(token); |
| 65 token = parseReturnTypeOpt(next(token)); | 65 token = parseReturnTypeOpt(next(token)); |
| 66 token = parseIdentifier(token); | 66 token = parseIdentifier(token); |
| 67 token = parseTypeVariablesOpt(token); | 67 token = parseTypeVariablesOpt(token); |
| 68 token = parseParameters(token); | 68 token = parseFormalParameters(token); |
| 69 listener.endFunctionTypeAlias(token); | 69 listener.endFunctionTypeAlias(token); |
| 70 return expect(';', token); | 70 return expect(';', token); |
| 71 } | 71 } |
| 72 | 72 |
| 73 Token parseReturnTypeOpt(Token token) { | 73 Token parseReturnTypeOpt(Token token) { |
| 74 if (token.stringValue === 'void') { | 74 if (token.stringValue === 'void') { |
| 75 listener.voidType(token); | 75 listener.voidType(token); |
| 76 return next(token); | 76 return next(token); |
| 77 } else { | 77 } else { |
| 78 return parseTypeOpt(token); | 78 return parseTypeOpt(token); |
| 79 } | 79 } |
| 80 } | 80 } |
| 81 | 81 |
| 82 Token parseParameters(Token token) { | 82 Token parseFormalParameters(Token token) { |
| 83 Token begin = token; |
| 84 listener.beginFormalParameters(begin); |
| 83 expect('(', token); | 85 expect('(', token); |
| 84 if (optional(')', next(token))) { | 86 int parameterCount = 0; |
| 85 return next(next(token)); | 87 if (optional(')', token.next)) { |
| 88 listener.endFormalParameters(parameterCount, begin, token.next); |
| 89 return token.next.next; |
| 86 } | 90 } |
| 87 do { | 91 do { |
| 88 // TODO(ahe): Handle 'final' and 'var'. | 92 listener.beginFormalParameter(token); |
| 89 token = parseTypeOpt(next(token)); | 93 token = parseTypeOpt(next(token)); |
| 90 token = parseIdentifier(token); | 94 token = parseIdentifier(token); |
| 95 listener.endFormalParameter(token); |
| 96 ++parameterCount; |
| 91 } while (optional(',', token)); | 97 } while (optional(',', token)); |
| 98 listener.endFormalParameters(parameterCount, begin, token); |
| 92 return expect(')', token); | 99 return expect(')', token); |
| 93 } | 100 } |
| 94 | 101 |
| 95 Token parseTypeOpt(Token token) { | 102 Token parseTypeOpt(Token token) { |
| 96 final nextValue = token.next.stringValue; | 103 final nextValue = token.next.stringValue; |
| 97 switch (true) { | 104 switch (true) { |
| 98 case nextValue === '<': | 105 case nextValue === '<': |
| 99 case nextValue === '.': | 106 case nextValue === '.': |
| 100 case isIdentifier(token.next): | 107 case isIdentifier(token.next): |
| 101 return parseType(token); | 108 return parseType(token); |
| 102 default: | 109 default: |
| 110 listener.handleNoType(token); |
| 103 return token; | 111 return token; |
| 104 } | 112 } |
| 105 } | 113 } |
| 106 | 114 |
| 107 bool isIdentifier(Token token) { | 115 bool isIdentifier(Token token) { |
| 108 final kind = token.kind; | 116 final kind = token.kind; |
| 109 switch (true) { | 117 switch (true) { |
| 110 case kind === IDENTIFIER_TOKEN: | 118 case kind === IDENTIFIER_TOKEN: |
| 111 return true; | 119 return true; |
| 112 case kind === KEYWORD_TOKEN: | 120 case kind === KEYWORD_TOKEN: |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 Token parseFunction(Token token) { | 344 Token parseFunction(Token token) { |
| 337 listener.beginFunction(token); | 345 listener.beginFunction(token); |
| 338 token = parseReturnTypeOpt(token); | 346 token = parseReturnTypeOpt(token); |
| 339 listener.beginFunctionName(token); | 347 listener.beginFunctionName(token); |
| 340 token = parseIdentifier(token); | 348 token = parseIdentifier(token); |
| 341 listener.endFunctionName(token); | 349 listener.endFunctionName(token); |
| 342 token = parseFormalParameters(token); | 350 token = parseFormalParameters(token); |
| 343 return parseFunctionBody(token); | 351 return parseFunctionBody(token); |
| 344 } | 352 } |
| 345 | 353 |
| 346 Token parseFormalParameters(Token token) { | |
| 347 Token begin = token; | |
| 348 listener.beginFormalParameters(begin); | |
| 349 expect('(', token); | |
| 350 int parameterCount = 0; | |
| 351 if (optional(')', token.next)) { | |
| 352 listener.endFormalParameters(parameterCount, begin, token.next); | |
| 353 return token.next.next; | |
| 354 } | |
| 355 do { | |
| 356 token = parseType(next(token)); // TODO(ahe): Types are optional. | |
| 357 token = parseIdentifier(token); | |
| 358 ++parameterCount; | |
| 359 } while (optional(',', token)); | |
| 360 listener.endFormalParameters(parameterCount, begin, token); | |
| 361 return expect(')', token); | |
| 362 } | |
| 363 | |
| 364 Token parseFunctionBody(Token token) { | 354 Token parseFunctionBody(Token token) { |
| 365 if (optional(';', token)) { | 355 if (optional(';', token)) { |
| 366 listener.endFunctionBody(0, null, token); | 356 listener.endFunctionBody(0, null, token); |
| 367 return token.next; | 357 return token.next; |
| 368 } | 358 } |
| 369 // TODO(ahe): Handle '=>' syntax. | 359 // TODO(ahe): Handle '=>' syntax. |
| 370 Token begin = token; | 360 Token begin = token; |
| 371 int statementCount = 0; | 361 int statementCount = 0; |
| 372 listener.beginFunctionBody(begin); | 362 listener.beginFunctionBody(begin); |
| 373 token = checkEof(expect('{', token)); | 363 token = checkEof(expect('{', token)); |
| (...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 672 int statementCount = 0; | 662 int statementCount = 0; |
| 673 token = expect('{', token); | 663 token = expect('{', token); |
| 674 while (!optional('}', token)) { | 664 while (!optional('}', token)) { |
| 675 token = parseStatement(token); | 665 token = parseStatement(token); |
| 676 ++statementCount; | 666 ++statementCount; |
| 677 } | 667 } |
| 678 listener.endBlock(statementCount, begin, token); | 668 listener.endBlock(statementCount, begin, token); |
| 679 return expect('}', token); | 669 return expect('}', token); |
| 680 } | 670 } |
| 681 } | 671 } |
| OLD | NEW |