| OLD | NEW |
| 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 /** | 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 (aka a token stream). | 7 * all tokens in a linked list (aka a token stream). |
| 8 * | 8 * |
| 9 * The class [Scanner] is used to generate a token stream. See the | 9 * The class [Scanner] is used to generate a token stream. See the |
| 10 * file scanner.dart. | 10 * file scanner.dart. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 * matches, "star" means zero or more matches. For example, | 24 * matches, "star" means zero or more matches. For example, |
| 25 * [parseMetadataStar] corresponds to this grammar snippet: [: | 25 * [parseMetadataStar] corresponds to this grammar snippet: [: |
| 26 * metadata* :], and [parseTypeOpt] corresponds to: [: type? :]. | 26 * metadata* :], and [parseTypeOpt] corresponds to: [: type? :]. |
| 27 */ | 27 */ |
| 28 class Parser { | 28 class Parser { |
| 29 final Listener listener; | 29 final Listener listener; |
| 30 bool mayParseFunctionExpressions = true; | 30 bool mayParseFunctionExpressions = true; |
| 31 | 31 |
| 32 Parser(this.listener); | 32 Parser(this.listener); |
| 33 | 33 |
| 34 void parseUnit(Token token) { | 34 Token parseUnit(Token token) { |
| 35 listener.beginCompilationUnit(token); |
| 36 int count = 0; |
| 35 while (token.kind !== EOF_TOKEN) { | 37 while (token.kind !== EOF_TOKEN) { |
| 36 token = parseTopLevelDeclaration(token); | 38 token = parseTopLevelDeclaration(token); |
| 39 count++; |
| 37 } | 40 } |
| 41 listener.endCompilationUnit(count, token); |
| 42 return token; |
| 38 } | 43 } |
| 39 | 44 |
| 40 Token parseTopLevelDeclaration(Token token) { | 45 Token parseTopLevelDeclaration(Token token) { |
| 41 token = parseMetadataStar(token); | 46 token = parseMetadataStar(token); |
| 42 final String value = token.stringValue; | 47 final String value = token.stringValue; |
| 43 if (value === 'interface') { | 48 if (value === 'interface') { |
| 44 return parseInterface(token); | 49 return parseInterface(token); |
| 45 } else if ((value === 'abstract') || (value === 'class')) { | 50 } else if ((value === 'abstract') || (value === 'class')) { |
| 46 return parseClass(token); | 51 return parseClass(token); |
| 47 } else if (value === 'typedef') { | 52 } else if (value === 'typedef') { |
| 48 return parseNamedFunctionAlias(token); | 53 return parseNamedFunctionAlias(token); |
| 49 } else if (value === '#') { | 54 } else if (value === '#') { |
| 50 return parseScriptTags(token); | 55 return parseScriptTags(token); |
| 51 } else if (value === 'library') { | 56 } else if (value === 'library') { |
| 52 return parseLibraryName(token); | 57 return parseLibraryName(token); |
| 53 } else if (value === 'import') { | 58 } else if (value === 'import') { |
| 54 return parseImport(token); | 59 return parseImport(token); |
| 55 } else if (value === 'export') { | 60 } else if (value === 'export') { |
| 56 return parseExport(token); | 61 return parseExport(token); |
| 62 } else if (value === 'part') { |
| 63 return parsePartOrPartOf(token); |
| 57 } else { | 64 } else { |
| 58 return parseTopLevelMember(token); | 65 return parseTopLevelMember(token); |
| 59 } | 66 } |
| 60 } | 67 } |
| 61 | 68 |
| 62 /// library qualified ';' | 69 /// library qualified ';' |
| 63 Token parseLibraryName(Token token) { | 70 Token parseLibraryName(Token token) { |
| 64 Token libraryKeyword = token; | 71 Token libraryKeyword = token; |
| 65 listener.beginLibraryName(libraryKeyword); | 72 listener.beginLibraryName(libraryKeyword); |
| 66 assert(optional('library', token)); | 73 assert(optional('library', token)); |
| 67 token = parseIdentifier(token.next); | 74 token = parseQualified(token.next); |
| 68 while (optional('.', token)) { | |
| 69 token = parseQualifiedRest(token); | |
| 70 } | |
| 71 Token semicolon = token; | 75 Token semicolon = token; |
| 72 token = expect(';', token); | 76 token = expect(';', token); |
| 73 listener.endLibraryName(libraryKeyword, semicolon); | 77 listener.endLibraryName(libraryKeyword, semicolon); |
| 74 return token; | 78 return token; |
| 75 } | 79 } |
| 76 | 80 |
| 77 /// import uri (as identifier)? combinator* ';' | 81 /// import uri (as identifier)? combinator* ';' |
| 78 Token parseImport(Token token) { | 82 Token parseImport(Token token) { |
| 79 Token importKeyword = token; | 83 Token importKeyword = token; |
| 80 listener.beginImport(importKeyword); | 84 listener.beginImport(importKeyword); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 token = parseIdentifier(token); | 152 token = parseIdentifier(token); |
| 149 int count = 1; | 153 int count = 1; |
| 150 while (optional(',', token)) { | 154 while (optional(',', token)) { |
| 151 token = parseIdentifier(token.next); | 155 token = parseIdentifier(token.next); |
| 152 count++; | 156 count++; |
| 153 } | 157 } |
| 154 listener.endIdentifierList(count); | 158 listener.endIdentifierList(count); |
| 155 return token; | 159 return token; |
| 156 } | 160 } |
| 157 | 161 |
| 162 Token parsePartOrPartOf(Token token) { |
| 163 assert(optional('part', token)); |
| 164 if (optional('of', token.next)) { |
| 165 return parsePartOf(token); |
| 166 } else { |
| 167 return parsePart(token); |
| 168 } |
| 169 } |
| 170 |
| 171 Token parsePart(Token token) { |
| 172 Token partKeyword = token; |
| 173 listener.beginPart(token); |
| 174 assert(optional('part', token)); |
| 175 token = parseLiteralStringOrRecoverExpression(token.next); |
| 176 Token semicolon = token; |
| 177 token = expect(';', token); |
| 178 listener.endPart(partKeyword, semicolon); |
| 179 return token; |
| 180 } |
| 181 |
| 182 Token parsePartOf(Token token) { |
| 183 listener.beginPartOf(token); |
| 184 assert(optional('part', token)); |
| 185 assert(optional('of', token.next)); |
| 186 Token partKeyword = token; |
| 187 token = parseQualified(token.next.next); |
| 188 Token semicolon = token; |
| 189 token = expect(';', token); |
| 190 listener.endPartOf(partKeyword, semicolon); |
| 191 return token; |
| 192 } |
| 193 |
| 158 Token parseMetadataStar(Token token) { | 194 Token parseMetadataStar(Token token) { |
| 159 while (optional('@', token)) { | 195 while (optional('@', token)) { |
| 160 token = parseMetadata(token); | 196 token = parseMetadata(token); |
| 161 } | 197 } |
| 162 return token; | 198 return token; |
| 163 } | 199 } |
| 164 | 200 |
| 165 /** | 201 /** |
| 166 * Parse | 202 * Parse |
| 167 * [: '@' qualified (‘.’ identifier)? (arguments)? :] | 203 * [: '@' qualified (‘.’ identifier)? (arguments)? :] |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 335 token = parseIdentifier(token.next); | 371 token = parseIdentifier(token.next); |
| 336 token = parseQualifiedRestOpt(token); | 372 token = parseQualifiedRestOpt(token); |
| 337 token = parseTypeVariablesOpt(token); | 373 token = parseTypeVariablesOpt(token); |
| 338 listener.endDefaultClause(defaultKeyword); | 374 listener.endDefaultClause(defaultKeyword); |
| 339 } else { | 375 } else { |
| 340 listener.handleNoDefaultClause(token); | 376 listener.handleNoDefaultClause(token); |
| 341 } | 377 } |
| 342 return token; | 378 return token; |
| 343 } | 379 } |
| 344 | 380 |
| 381 Token parseQualified(Token token) { |
| 382 token = parseIdentifier(token); |
| 383 while (optional('.', token)) { |
| 384 token = parseQualifiedRest(token); |
| 385 } |
| 386 return token; |
| 387 } |
| 388 |
| 345 Token parseQualifiedRestOpt(Token token) { | 389 Token parseQualifiedRestOpt(Token token) { |
| 346 if (optional('.', token)) { | 390 if (optional('.', token)) { |
| 347 return parseQualifiedRest(token); | 391 return parseQualifiedRest(token); |
| 348 } else { | 392 } else { |
| 349 return token; | 393 return token; |
| 350 } | 394 } |
| 351 } | 395 } |
| 352 | 396 |
| 353 Token parseQualifiedRest(Token token) { | 397 Token parseQualifiedRest(Token token) { |
| 354 assert(optional('.', token)); | 398 assert(optional('.', token)); |
| (...skipping 1690 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2045 } | 2089 } |
| 2046 listener.handleContinueStatement(hasTarget, continueKeyword, token); | 2090 listener.handleContinueStatement(hasTarget, continueKeyword, token); |
| 2047 return expectSemicolon(token); | 2091 return expectSemicolon(token); |
| 2048 } | 2092 } |
| 2049 | 2093 |
| 2050 Token parseEmptyStatement(Token token) { | 2094 Token parseEmptyStatement(Token token) { |
| 2051 listener.handleEmptyStatement(token); | 2095 listener.handleEmptyStatement(token); |
| 2052 return expectSemicolon(token); | 2096 return expectSemicolon(token); |
| 2053 } | 2097 } |
| 2054 } | 2098 } |
| OLD | NEW |