| 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 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 } | 87 } |
| 88 token = parseFormalParameter(token); | 88 token = parseFormalParameter(token); |
| 89 ++parameterCount; | 89 ++parameterCount; |
| 90 } while (optional(',', token)); | 90 } while (optional(',', token)); |
| 91 listener.endFormalParameters(parameterCount, begin, token); | 91 listener.endFormalParameters(parameterCount, begin, token); |
| 92 return expect(')', token); | 92 return expect(')', token); |
| 93 } | 93 } |
| 94 | 94 |
| 95 Token parseFormalParameter(Token token) { | 95 Token parseFormalParameter(Token token) { |
| 96 listener.beginFormalParameter(token); | 96 listener.beginFormalParameter(token); |
| 97 if (optional('void', token)) { | 97 token = parseModifiers(token); |
| 98 // TODO(ahe): Validate that there are formal parameters. | 98 // TODO(ahe): Validate that there are formal parameters if void. |
| 99 token = parseReturnTypeOpt(token); | 99 token = parseReturnTypeOpt(token); |
| 100 } else { | |
| 101 token = parseFinalVarOrTypeOpt(token); | |
| 102 } | |
| 103 Token thisKeyword = null; | 100 Token thisKeyword = null; |
| 104 if (optional('this', token)) { | 101 if (optional('this', token)) { |
| 105 thisKeyword = token; | 102 thisKeyword = token; |
| 106 // TODO(ahe): Validate field initializers are only used in | 103 // TODO(ahe): Validate field initializers are only used in |
| 107 // constructors, and not for function-typed arguments. | 104 // constructors, and not for function-typed arguments. |
| 108 token = expect('.', token.next); | 105 token = expect('.', token.next); |
| 109 } | 106 } |
| 110 token = parseIdentifier(token); | 107 token = parseIdentifier(token); |
| 111 if (optional('(', token)) { | 108 if (optional('(', token)) { |
| 112 token = parseFormalParameters(token); | 109 token = parseFormalParameters(token); |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 // TODO(ahe): Rename this method to parseTypeOrVar? | 267 // TODO(ahe): Rename this method to parseTypeOrVar? |
| 271 Token begin = token; | 268 Token begin = token; |
| 272 int identifierCount = 1; | 269 int identifierCount = 1; |
| 273 if (isIdentifier(token)) { | 270 if (isIdentifier(token)) { |
| 274 token = parseIdentifier(token); | 271 token = parseIdentifier(token); |
| 275 while (optional('.', token)) { | 272 while (optional('.', token)) { |
| 276 // TODO(ahe): Validate that there are at most two identifiers. | 273 // TODO(ahe): Validate that there are at most two identifiers. |
| 277 token = parseIdentifier(token.next); | 274 token = parseIdentifier(token.next); |
| 278 ++identifierCount; | 275 ++identifierCount; |
| 279 } | 276 } |
| 280 } else if (optional('var', token)) { | |
| 281 listener.handleVarKeyword(token); | |
| 282 return token.next; | |
| 283 } else { | 277 } else { |
| 284 token = listener.expectedType(token); | 278 token = listener.expectedType(token); |
| 285 } | 279 } |
| 286 token = parseTypeArgumentsOpt(token); | 280 token = parseTypeArgumentsOpt(token); |
| 287 listener.endType(identifierCount, begin, token); | 281 listener.endType(identifierCount, begin, token); |
| 288 return token; | 282 return token; |
| 289 } | 283 } |
| 290 | 284 |
| 291 Token parseTypeArgumentsOpt(Token token) { | 285 Token parseTypeArgumentsOpt(Token token) { |
| 292 return parseStuff(token, | 286 return parseStuff(token, |
| (...skipping 879 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1172 } | 1166 } |
| 1173 | 1167 |
| 1174 Token parseVariablesDeclaration(Token token) { | 1168 Token parseVariablesDeclaration(Token token) { |
| 1175 token = parseVariablesDeclarationNoSemicolon(token); | 1169 token = parseVariablesDeclarationNoSemicolon(token); |
| 1176 return expectSemicolon(token); | 1170 return expectSemicolon(token); |
| 1177 } | 1171 } |
| 1178 | 1172 |
| 1179 Token parseVariablesDeclarationNoSemicolon(Token token) { | 1173 Token parseVariablesDeclarationNoSemicolon(Token token) { |
| 1180 int count = 1; | 1174 int count = 1; |
| 1181 listener.beginVariablesDeclaration(token); | 1175 listener.beginVariablesDeclaration(token); |
| 1182 token = parseFinalVarOrType(token); | 1176 token = parseModifiers(token); |
| 1177 token = parseTypeOpt(token); |
| 1183 token = parseOptionallyInitializedIdentifier(token); | 1178 token = parseOptionallyInitializedIdentifier(token); |
| 1184 while (optional(',', token)) { | 1179 while (optional(',', token)) { |
| 1185 token = parseOptionallyInitializedIdentifier(token.next); | 1180 token = parseOptionallyInitializedIdentifier(token.next); |
| 1186 ++count; | 1181 ++count; |
| 1187 } | 1182 } |
| 1188 listener.endVariablesDeclaration(count, token); | 1183 listener.endVariablesDeclaration(count, token); |
| 1189 return token; | 1184 return token; |
| 1190 } | 1185 } |
| 1191 | 1186 |
| 1192 Token parseOptionallyInitializedIdentifier(Token token) { | 1187 Token parseOptionallyInitializedIdentifier(Token token) { |
| 1193 listener.beginInitializedIdentifier(token); | 1188 listener.beginInitializedIdentifier(token); |
| 1194 token = parseIdentifier(token); | 1189 token = parseIdentifier(token); |
| 1195 token = parseVariableInitializerOpt(token); | 1190 token = parseVariableInitializerOpt(token); |
| 1196 listener.endInitializedIdentifier(); | 1191 listener.endInitializedIdentifier(); |
| 1197 return token; | 1192 return token; |
| 1198 } | 1193 } |
| 1199 | 1194 |
| 1200 Token parseFinalVarOrType(Token token) { | |
| 1201 if ('final' === token.stringValue) { | |
| 1202 listener.handleFinalKeyword(token); | |
| 1203 return parseTypeOpt(token.next); | |
| 1204 } else { | |
| 1205 return parseType(token); | |
| 1206 } | |
| 1207 } | |
| 1208 | |
| 1209 Token parseFinalVarOrTypeOpt(Token token) { | |
| 1210 final String value = token.stringValue; | |
| 1211 if ('final' === value) { | |
| 1212 listener.handleFinalKeyword(token); | |
| 1213 return parseTypeOpt(token.next); | |
| 1214 } else { | |
| 1215 return parseTypeOpt(token); | |
| 1216 } | |
| 1217 } | |
| 1218 | |
| 1219 Token parseIfStatement(Token token) { | 1195 Token parseIfStatement(Token token) { |
| 1220 Token ifToken = token; | 1196 Token ifToken = token; |
| 1221 listener.beginIfStatement(ifToken); | 1197 listener.beginIfStatement(ifToken); |
| 1222 token = expect('if', token); | 1198 token = expect('if', token); |
| 1223 token = parseParenthesizedExpression(token); | 1199 token = parseParenthesizedExpression(token); |
| 1224 token = parseStatement(token); | 1200 token = parseStatement(token); |
| 1225 Token elseToken = null; | 1201 Token elseToken = null; |
| 1226 if (optional('else', token)) { | 1202 if (optional('else', token)) { |
| 1227 elseToken = token; | 1203 elseToken = token; |
| 1228 token = parseStatement(token.next); | 1204 token = parseStatement(token.next); |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1411 } | 1387 } |
| 1412 listener.handleContinueStatement(hasTarget, continueKeyword, token); | 1388 listener.handleContinueStatement(hasTarget, continueKeyword, token); |
| 1413 return expectSemicolon(token); | 1389 return expectSemicolon(token); |
| 1414 } | 1390 } |
| 1415 | 1391 |
| 1416 Token parseEmptyStatement(Token token) { | 1392 Token parseEmptyStatement(Token token) { |
| 1417 listener.handleEmptyStatement(token); | 1393 listener.handleEmptyStatement(token); |
| 1418 return expectSemicolon(token); | 1394 return expectSemicolon(token); |
| 1419 } | 1395 } |
| 1420 } | 1396 } |
| OLD | NEW |