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 library fasta.parser.parser; | 5 library fasta.parser.parser; |
6 | 6 |
7 import '../fasta_codes.dart' show Code, Message, Template; | 7 import '../fasta_codes.dart' show Code, Message, Template; |
8 | 8 |
9 import '../fasta_codes.dart' as fasta; | 9 import '../fasta_codes.dart' as fasta; |
10 | 10 |
(...skipping 1310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1321 // `(',' | '=' | ';')`. | 1321 // `(',' | '=' | ';')`. |
1322 | 1322 |
1323 // TODO(ahe): Generate type events and call | 1323 // TODO(ahe): Generate type events and call |
1324 // parseVariablesDeclarationRest instead. | 1324 // parseVariablesDeclarationRest instead. |
1325 return parseVariablesDeclaration(begin); | 1325 return parseVariablesDeclaration(begin); |
1326 } else if (OPEN_PAREN_TOKEN == afterIdKind) { | 1326 } else if (OPEN_PAREN_TOKEN == afterIdKind) { |
1327 // We are looking at `type identifier '('`. | 1327 // We are looking at `type identifier '('`. |
1328 if (looksLikeFunctionBody(getClose(afterId).next)) { | 1328 if (looksLikeFunctionBody(getClose(afterId).next)) { |
1329 // We are looking at `type identifier '(' ... ')'` followed | 1329 // We are looking at `type identifier '(' ... ')'` followed |
1330 // `( '{' | '=>' | 'async' | 'sync' )`. | 1330 // `( '{' | '=>' | 'async' | 'sync' )`. |
1331 return parseFunctionDeclaration(begin); | 1331 return parseLocalFunctionDeclaration(begin); |
1332 } | 1332 } |
1333 } else if (identical(afterIdKind, LT_TOKEN)) { | 1333 } else if (identical(afterIdKind, LT_TOKEN)) { |
1334 // We are looking at `type identifier '<'`. | 1334 // We are looking at `type identifier '<'`. |
1335 Token afterTypeVariables = getClose(afterId)?.next; | 1335 Token afterTypeVariables = getClose(afterId)?.next; |
1336 if (afterTypeVariables != null && | 1336 if (afterTypeVariables != null && |
1337 optional("(", afterTypeVariables)) { | 1337 optional("(", afterTypeVariables)) { |
1338 if (looksLikeFunctionBody(getClose(afterTypeVariables).next)) { | 1338 if (looksLikeFunctionBody(getClose(afterTypeVariables).next)) { |
1339 // We are looking at "type identifier '<' ... '>' '(' ... ')'" | 1339 // We are looking at "type identifier '<' ... '>' '(' ... ')'" |
1340 // followed by '{', '=>', 'async', or 'sync'. | 1340 // followed by '{', '=>', 'async', or 'sync'. |
1341 return parseFunctionDeclaration(begin); | 1341 return parseLocalFunctionDeclaration(begin); |
1342 } | 1342 } |
1343 } | 1343 } |
1344 } | 1344 } |
1345 // Fall-through to expression statement. | 1345 // Fall-through to expression statement. |
1346 } else { | 1346 } else { |
1347 token = begin; | 1347 token = begin; |
1348 if (optional(':', token.next)) { | 1348 if (optional(':', token.next)) { |
1349 return parseLabeledStatement(token); | 1349 return parseLabeledStatement(token); |
1350 } else if (optional('(', token.next)) { | 1350 } else if (optional('(', token.next)) { |
1351 if (looksLikeFunctionBody(getClose(token.next).next)) { | 1351 if (looksLikeFunctionBody(getClose(token.next).next)) { |
1352 return parseFunctionDeclaration(token); | 1352 return parseLocalFunctionDeclaration(token); |
1353 } | 1353 } |
1354 } else if (optional('<', token.next)) { | 1354 } else if (optional('<', token.next)) { |
1355 Token afterTypeVariables = getClose(token.next)?.next; | 1355 Token afterTypeVariables = getClose(token.next)?.next; |
1356 if (afterTypeVariables != null && | 1356 if (afterTypeVariables != null && |
1357 optional("(", afterTypeVariables)) { | 1357 optional("(", afterTypeVariables)) { |
1358 if (looksLikeFunctionBody(getClose(afterTypeVariables).next)) { | 1358 if (looksLikeFunctionBody(getClose(afterTypeVariables).next)) { |
1359 return parseFunctionDeclaration(token); | 1359 return parseLocalFunctionDeclaration(token); |
1360 } | 1360 } |
1361 } | 1361 } |
1362 // Fall through to expression statement. | 1362 // Fall through to expression statement. |
1363 } | 1363 } |
1364 } | 1364 } |
1365 return parseExpressionStatement(begin); | 1365 return parseExpressionStatement(begin); |
1366 | 1366 |
1367 case TypeContinuation.ExpressionStatementOrConstDeclaration: | 1367 case TypeContinuation.ExpressionStatementOrConstDeclaration: |
1368 Token identifier; | 1368 Token identifier; |
1369 if (looksLikeType && token.isIdentifier) { | 1369 if (looksLikeType && token.isIdentifier) { |
(...skipping 1083 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2453 token = parseFormalParameters(token, MemberKind.Local); | 2453 token = parseFormalParameters(token, MemberKind.Local); |
2454 AsyncModifier savedAsyncModifier = asyncState; | 2454 AsyncModifier savedAsyncModifier = asyncState; |
2455 token = parseAsyncModifier(token); | 2455 token = parseAsyncModifier(token); |
2456 bool isBlock = optional('{', token); | 2456 bool isBlock = optional('{', token); |
2457 token = parseFunctionBody(token, true, false); | 2457 token = parseFunctionBody(token, true, false); |
2458 asyncState = savedAsyncModifier; | 2458 asyncState = savedAsyncModifier; |
2459 listener.endFunctionExpression(beginToken, token); | 2459 listener.endFunctionExpression(beginToken, token); |
2460 return isBlock ? token.next : token; | 2460 return isBlock ? token.next : token; |
2461 } | 2461 } |
2462 | 2462 |
2463 Token parseFunctionDeclaration(Token token) { | 2463 Token parseLocalFunctionDeclaration(Token token) { |
2464 listener.beginFunctionDeclaration(token); | 2464 listener.beginLocalFunctionDeclaration(token); |
2465 Token beginToken = token; | 2465 Token beginToken = token; |
2466 token = parseModifiers(token, MemberKind.Local); | 2466 token = parseModifiers(token, MemberKind.Local); |
2467 listener.beginFunctionName(token); | 2467 listener.beginFunctionName(token); |
2468 token = parseIdentifier(token, IdentifierContext.localFunctionDeclaration); | 2468 token = parseIdentifier(token, IdentifierContext.localFunctionDeclaration); |
2469 token = parseQualifiedRestOpt( | 2469 token = parseQualifiedRestOpt( |
2470 token, IdentifierContext.localFunctionDeclarationContinuation); | 2470 token, IdentifierContext.localFunctionDeclarationContinuation); |
2471 listener.endFunctionName(beginToken, token); | 2471 listener.endFunctionName(beginToken, token); |
2472 token = parseTypeVariablesOpt(token); | 2472 token = parseTypeVariablesOpt(token); |
2473 token = parseFormalParametersOpt(token, MemberKind.Local); | 2473 token = parseFormalParametersOpt(token, MemberKind.Local); |
2474 token = parseInitializersOpt(token); | 2474 token = parseInitializersOpt(token); |
2475 AsyncModifier savedAsyncModifier = asyncState; | 2475 AsyncModifier savedAsyncModifier = asyncState; |
2476 token = parseAsyncModifier(token); | 2476 token = parseAsyncModifier(token); |
2477 token = parseFunctionBody(token, false, true); | 2477 token = parseFunctionBody(token, false, true); |
2478 asyncState = savedAsyncModifier; | 2478 asyncState = savedAsyncModifier; |
2479 token = token.next; | 2479 token = token.next; |
2480 listener.endFunctionDeclaration(token); | 2480 listener.endLocalFunctionDeclaration(token); |
2481 return token; | 2481 return token; |
2482 } | 2482 } |
2483 | 2483 |
2484 /// Parses a named function expression which isn't legal syntax in Dart. | 2484 /// Parses a named function expression which isn't legal syntax in Dart. |
2485 /// Useful for recovering from Javascript code being pasted into a Dart | 2485 /// Useful for recovering from Javascript code being pasted into a Dart |
2486 /// proram, as it will interpret `function foo() {}` as a named function | 2486 /// proram, as it will interpret `function foo() {}` as a named function |
2487 /// expression with return type `function` and name `foo`. | 2487 /// expression with return type `function` and name `foo`. |
2488 Token parseNamedFunctionExpression(Token token) { | 2488 Token parseNamedFunctionExpression(Token token) { |
2489 Token beginToken = token; | 2489 Token beginToken = token; |
2490 listener.beginNamedFunctionExpression(token); | 2490 listener.beginNamedFunctionExpression(token); |
(...skipping 1644 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4135 } | 4135 } |
4136 | 4136 |
4137 Token reportUnexpectedToken(Token token) { | 4137 Token reportUnexpectedToken(Token token) { |
4138 return reportUnrecoverableErrorWithToken( | 4138 return reportUnrecoverableErrorWithToken( |
4139 token, fasta.templateUnexpectedToken); | 4139 token, fasta.templateUnexpectedToken); |
4140 } | 4140 } |
4141 } | 4141 } |
4142 | 4142 |
4143 // TODO(ahe): Remove when analyzer supports generalized function syntax. | 4143 // TODO(ahe): Remove when analyzer supports generalized function syntax. |
4144 typedef _MessageWithArgument<T> = Message Function(T); | 4144 typedef _MessageWithArgument<T> = Message Function(T); |
OLD | NEW |