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 dart2js.parser; | 5 library dart2js.parser; |
6 | 6 |
7 import '../common.dart'; | 7 import '../common.dart'; |
8 import '../tokens/keyword.dart' show Keyword; | 8 import '../tokens/keyword.dart' show Keyword; |
9 import '../tokens/precedence.dart' show PrecedenceInfo; | 9 import '../tokens/precedence.dart' show PrecedenceInfo; |
10 import '../tokens/precedence_constants.dart' | 10 import '../tokens/precedence_constants.dart' |
(...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 } | 419 } |
420 token = parseFormalParameter(token, FormalParameterType.REQUIRED); | 420 token = parseFormalParameter(token, FormalParameterType.REQUIRED); |
421 } while (optional(',', token)); | 421 } while (optional(',', token)); |
422 listener.endFormalParameters(parameterCount, begin, token); | 422 listener.endFormalParameters(parameterCount, begin, token); |
423 return expect(')', token); | 423 return expect(')', token); |
424 } | 424 } |
425 | 425 |
426 Token parseFormalParameter(Token token, FormalParameterType type) { | 426 Token parseFormalParameter(Token token, FormalParameterType type) { |
427 token = parseMetadataStar(token, forParameter: true); | 427 token = parseMetadataStar(token, forParameter: true); |
428 listener.beginFormalParameter(token); | 428 listener.beginFormalParameter(token); |
| 429 |
| 430 // Skip over `covariant` token, if the next token is an identifier or |
| 431 // modifier. |
| 432 // This enables the case where `covariant` is the name of the parameter: |
| 433 // void foo(covariant); |
| 434 if (identical(token.stringValue, 'covariant') && |
| 435 token.next.isIdentifier() || isModifier(token.next)) { |
| 436 token = token.next; |
| 437 } |
429 token = parseModifiers(token); | 438 token = parseModifiers(token); |
430 // TODO(ahe): Validate that there are formal parameters if void. | 439 // TODO(ahe): Validate that there are formal parameters if void. |
431 token = parseReturnTypeOpt(token); | 440 token = parseReturnTypeOpt(token); |
432 Token thisKeyword = null; | 441 Token thisKeyword = null; |
433 if (optional('this', token)) { | 442 if (optional('this', token)) { |
434 thisKeyword = token; | 443 thisKeyword = token; |
435 // TODO(ahe): Validate field initializers are only used in | 444 // TODO(ahe): Validate field initializers are only used in |
436 // constructors, and not for function-typed arguments. | 445 // constructors, and not for function-typed arguments. |
437 token = expect('.', token.next); | 446 token = expect('.', token.next); |
438 } | 447 } |
(...skipping 542 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
981 listener.handleModifiers(modifierCount); | 990 listener.handleModifiers(modifierCount); |
982 var kind = hasTypeOrModifier | 991 var kind = hasTypeOrModifier |
983 ? MessageKind.EXTRANEOUS_MODIFIER | 992 ? MessageKind.EXTRANEOUS_MODIFIER |
984 : MessageKind.EXTRANEOUS_MODIFIER_REPLACE; | 993 : MessageKind.EXTRANEOUS_MODIFIER_REPLACE; |
985 for (Token modifier in modifierList) { | 994 for (Token modifier in modifierList) { |
986 listener.reportError(modifier, kind, {'modifier': modifier}); | 995 listener.reportError(modifier, kind, {'modifier': modifier}); |
987 } | 996 } |
988 return null; | 997 return null; |
989 } | 998 } |
990 | 999 |
| 1000 /// Removes the optional `covariant` token from the modifiers, if there |
| 1001 /// is no `static` in the list, and `covariant` is the first modifier. |
| 1002 Link<Token> removeOptCovariantTokenIfNotStatic(Link<Token> modifiers) { |
| 1003 if (modifiers.isEmpty || |
| 1004 !identical(modifiers.first.stringValue, 'covariant')) { |
| 1005 return modifiers; |
| 1006 } |
| 1007 for (Token modifier in modifiers.tail) { |
| 1008 if (identical(modifier.stringValue, 'static')) { |
| 1009 return modifiers; |
| 1010 } |
| 1011 } |
| 1012 return modifiers.tail; |
| 1013 } |
| 1014 |
991 Token parseFields(Token start, Link<Token> modifiers, Token type, | 1015 Token parseFields(Token start, Link<Token> modifiers, Token type, |
992 Token getOrSet, Token name, bool isTopLevel) { | 1016 Token getOrSet, Token name, bool isTopLevel) { |
993 bool hasType = type != null; | 1017 bool hasType = type != null; |
| 1018 |
| 1019 if (getOrSet == null && !isTopLevel) { |
| 1020 modifiers = removeOptCovariantTokenIfNotStatic(modifiers); |
| 1021 } |
| 1022 |
994 Token varFinalOrConst = | 1023 Token varFinalOrConst = |
995 expectVarFinalOrConst(modifiers, hasType, !isTopLevel); | 1024 expectVarFinalOrConst(modifiers, hasType, !isTopLevel); |
996 bool isVar = false; | 1025 bool isVar = false; |
997 bool hasModifier = false; | 1026 bool hasModifier = false; |
998 if (varFinalOrConst != null) { | 1027 if (varFinalOrConst != null) { |
999 hasModifier = true; | 1028 hasModifier = true; |
1000 isVar = optional('var', varFinalOrConst); | 1029 isVar = optional('var', varFinalOrConst); |
1001 } | 1030 } |
1002 | 1031 |
1003 if (getOrSet != null) { | 1032 if (getOrSet != null) { |
(...skipping 1965 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2969 } | 2998 } |
2970 listener.handleContinueStatement(hasTarget, continueKeyword, token); | 2999 listener.handleContinueStatement(hasTarget, continueKeyword, token); |
2971 return expectSemicolon(token); | 3000 return expectSemicolon(token); |
2972 } | 3001 } |
2973 | 3002 |
2974 Token parseEmptyStatement(Token token) { | 3003 Token parseEmptyStatement(Token token) { |
2975 listener.handleEmptyStatement(token); | 3004 listener.handleEmptyStatement(token); |
2976 return expectSemicolon(token); | 3005 return expectSemicolon(token); |
2977 } | 3006 } |
2978 } | 3007 } |
OLD | NEW |