Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(128)

Side by Side Diff: pkg/analyzer/lib/src/generated/parser.dart

Issue 2628973003: Add preliminary parser support for generic function types (Closed)
Patch Set: Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, 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 analyzer.src.generated.parser; 5 library analyzer.src.generated.parser;
6 6
7 import 'dart:collection'; 7 import 'dart:collection';
8 import "dart:math" as math; 8 import "dart:math" as math;
9 9
10 import 'package:analyzer/dart/ast/ast.dart'; 10 import 'package:analyzer/dart/ast/ast.dart';
(...skipping 1517 matching lines...) Expand 10 before | Expand all | Expand 10 after
1528 _currentToken = _injectToken( 1528 _currentToken = _injectToken(
1529 new Parser_SyntheticKeywordToken(Keyword.GET, _currentToken.offset)); 1529 new Parser_SyntheticKeywordToken(Keyword.GET, _currentToken.offset));
1530 return parseGetter(commentAndMetadata, modifiers.externalKeyword, 1530 return parseGetter(commentAndMetadata, modifiers.externalKeyword,
1531 modifiers.staticKeyword, type); 1531 modifiers.staticKeyword, type);
1532 } 1532 }
1533 return parseInitializedIdentifierList(commentAndMetadata, 1533 return parseInitializedIdentifierList(commentAndMetadata,
1534 modifiers.staticKeyword, _validateModifiersForField(modifiers), type); 1534 modifiers.staticKeyword, _validateModifiersForField(modifiers), type);
1535 } 1535 }
1536 1536
1537 /** 1537 /**
1538 * Parse a class type alias. The [commentAndMetadata] is the metadata to be
1539 * associated with the member. The [abstractKeyword] is the token representing
1540 * the 'abstract' keyword. The [classKeyword] is the token representing the
1541 * 'class' keyword. Return the class type alias that was parsed.
1542 *
1543 * This method assumes that the current token matches an identifier.
1544 *
1545 * classTypeAlias ::=
1546 * identifier typeParameters? '=' 'abstract'? mixinApplication
1547 *
1548 * mixinApplication ::=
1549 * type withClause implementsClause? ';'
1550 */
1551 ClassTypeAlias parseClassTypeAlias(CommentAndMetadata commentAndMetadata,
1552 Token abstractKeyword, Token classKeyword) {
1553 SimpleIdentifier className =
1554 _parseSimpleIdentifierUnchecked(isDeclaration: true);
1555 TypeParameterList typeParameters = null;
1556 if (_matches(TokenType.LT)) {
1557 typeParameters = parseTypeParameterList();
1558 }
1559 return _parseClassTypeAliasAfterName(commentAndMetadata, abstractKeyword,
1560 classKeyword, className, typeParameters);
1561 }
1562
1563 /**
1564 * Parse a single combinator. Return the combinator that was parsed, or `null` 1538 * Parse a single combinator. Return the combinator that was parsed, or `null`
1565 * if no combinator is found. 1539 * if no combinator is found.
1566 * 1540 *
1567 * combinator ::= 1541 * combinator ::=
1568 * 'show' identifier (',' identifier)* 1542 * 'show' identifier (',' identifier)*
1569 * | 'hide' identifier (',' identifier)* 1543 * | 'hide' identifier (',' identifier)*
1570 */ 1544 */
1571 Combinator parseCombinator() { 1545 Combinator parseCombinator() {
1572 if (_matchesString(_SHOW)) { 1546 if (_matchesString(_SHOW)) {
1573 return astFactory.showCombinator(getAndAdvance(), parseIdentifierList()); 1547 return astFactory.showCombinator(getAndAdvance(), parseIdentifierList());
(...skipping 1199 matching lines...) Expand 10 before | Expand all | Expand 10 after
2773 * Parse the 'final', 'const', 'var' or type preceding a variable declaration. 2747 * Parse the 'final', 'const', 'var' or type preceding a variable declaration.
2774 * The [optional] is `true` if the keyword and type are optional. Return the 2748 * The [optional] is `true` if the keyword and type are optional. Return the
2775 * 'final', 'const', 'var' or type that was parsed. 2749 * 'final', 'const', 'var' or type that was parsed.
2776 * 2750 *
2777 * finalConstVarOrType ::= 2751 * finalConstVarOrType ::=
2778 * 'final' type? 2752 * 'final' type?
2779 * | 'const' type? 2753 * | 'const' type?
2780 * | 'var' 2754 * | 'var'
2781 * | type 2755 * | type
2782 */ 2756 */
2783 FinalConstVarOrType parseFinalConstVarOrType(bool optional) { 2757 FinalConstVarOrType parseFinalConstVarOrType(bool optional,
2758 {bool inFunctionType = false}) {
2784 Token keywordToken = null; 2759 Token keywordToken = null;
2785 TypeName type = null; 2760 TypeName type = null;
2786 Keyword keyword = _currentToken.keyword; 2761 Keyword keyword = _currentToken.keyword;
2787 if (keyword == Keyword.FINAL || keyword == Keyword.CONST) { 2762 if (keyword == Keyword.FINAL || keyword == Keyword.CONST) {
2788 keywordToken = getAndAdvance(); 2763 keywordToken = getAndAdvance();
2789 if (_isTypedIdentifier(_currentToken)) { 2764 if (_isTypedIdentifier(_currentToken)) {
2790 type = parseTypeName(false); 2765 type = parseTypeName(false);
2791 } else { 2766 } else {
2792 // Support `final/*=T*/ x;` 2767 // Support `final/*=T*/ x;`
2793 type = _parseOptionalTypeNameComment(); 2768 type = _parseOptionalTypeNameComment();
2794 } 2769 }
2795 } else if (keyword == Keyword.VAR) { 2770 } else if (keyword == Keyword.VAR) {
2796 keywordToken = getAndAdvance(); 2771 keywordToken = getAndAdvance();
2797 // Support `var/*=T*/ x;` 2772 // Support `var/*=T*/ x;`
2798 type = _parseOptionalTypeNameComment(); 2773 type = _parseOptionalTypeNameComment();
2799 if (type != null) { 2774 if (type != null) {
2800 // Clear the keyword to prevent an error. 2775 // Clear the keyword to prevent an error.
2801 keywordToken = null; 2776 keywordToken = null;
2802 } 2777 }
2803 } else if (_isTypedIdentifier(_currentToken)) { 2778 } else if (_isTypedIdentifier(_currentToken)) {
2804 type = parseReturnType(); 2779 type = parseReturnType();
2780 } else if (inFunctionType && _matchesIdentifier()) {
2781 type = parseTypeAnnotation();
2805 } else if (!optional) { 2782 } else if (!optional) {
2806 _reportErrorForCurrentToken( 2783 _reportErrorForCurrentToken(
2807 ParserErrorCode.MISSING_CONST_FINAL_VAR_OR_TYPE); 2784 ParserErrorCode.MISSING_CONST_FINAL_VAR_OR_TYPE);
2808 } else { 2785 } else {
2809 // Support parameters such as `(/*=K*/ key, /*=V*/ value)` 2786 // Support parameters such as `(/*=K*/ key, /*=V*/ value)`
2810 // This is not supported if the type is required. 2787 // This is not supported if the type is required.
2811 type = _parseOptionalTypeNameComment(); 2788 type = _parseOptionalTypeNameComment();
2812 } 2789 }
2813 return new FinalConstVarOrType(keywordToken, type); 2790 return new FinalConstVarOrType(keywordToken, type);
2814 } 2791 }
2815 2792
2816 /** 2793 /**
2817 * Parse a formal parameter. At most one of `isOptional` and `isNamed` can be 2794 * Parse a formal parameter. At most one of `isOptional` and `isNamed` can be
2818 * `true`. The [kind] is the kind of parameter being expected based on the 2795 * `true`. The [kind] is the kind of parameter being expected based on the
2819 * presence or absence of group delimiters. Return the formal parameter that 2796 * presence or absence of group delimiters. Return the formal parameter that
2820 * was parsed. 2797 * was parsed.
2821 * 2798 *
2822 * defaultFormalParameter ::= 2799 * defaultFormalParameter ::=
2823 * normalFormalParameter ('=' expression)? 2800 * normalFormalParameter ('=' expression)?
2824 * 2801 *
2825 * defaultNamedParameter ::= 2802 * defaultNamedParameter ::=
2826 * normalFormalParameter ('=' expression)? 2803 * normalFormalParameter ('=' expression)?
2827 * normalFormalParameter (':' expression)? 2804 * normalFormalParameter (':' expression)?
2828 */ 2805 */
2829 FormalParameter parseFormalParameter(ParameterKind kind) { 2806 FormalParameter parseFormalParameter(ParameterKind kind,
2830 NormalFormalParameter parameter = parseNormalFormalParameter(); 2807 {bool inFunctionType = false}) {
2808 NormalFormalParameter parameter =
2809 parseNormalFormalParameter(inFunctionType: inFunctionType);
2831 TokenType type = _currentToken.type; 2810 TokenType type = _currentToken.type;
2832 if (type == TokenType.EQ) { 2811 if (type == TokenType.EQ) {
2812 if (inFunctionType) {
2813 _reportErrorForCurrentToken(
2814 ParserErrorCode.DEFAULT_VALUE_IN_FUNCTION_TYPE);
2815 }
2833 Token separator = getAndAdvance(); 2816 Token separator = getAndAdvance();
2834 Expression defaultValue = parseExpression2(); 2817 Expression defaultValue = parseExpression2();
2835 if (kind == ParameterKind.REQUIRED) { 2818 if (kind == ParameterKind.REQUIRED) {
2836 _reportErrorForNode( 2819 _reportErrorForNode(
2837 ParserErrorCode.POSITIONAL_PARAMETER_OUTSIDE_GROUP, parameter); 2820 ParserErrorCode.POSITIONAL_PARAMETER_OUTSIDE_GROUP, parameter);
2838 kind = ParameterKind.POSITIONAL; 2821 kind = ParameterKind.POSITIONAL;
2822 } else if (kind == ParameterKind.NAMED &&
2823 inFunctionType &&
2824 parameter.identifier == null) {
2825 _reportErrorForCurrentToken(
2826 ParserErrorCode.MISSING_NAME_FOR_NAMED_PARAMETER);
2839 } 2827 }
2840 return astFactory.defaultFormalParameter( 2828 return astFactory.defaultFormalParameter(
2841 parameter, kind, separator, defaultValue); 2829 parameter, kind, separator, defaultValue);
2842 } else if (type == TokenType.COLON) { 2830 } else if (type == TokenType.COLON) {
2831 if (inFunctionType) {
2832 _reportErrorForCurrentToken(
2833 ParserErrorCode.DEFAULT_VALUE_IN_FUNCTION_TYPE);
2834 }
2843 Token separator = getAndAdvance(); 2835 Token separator = getAndAdvance();
2844 Expression defaultValue = parseExpression2(); 2836 Expression defaultValue = parseExpression2();
2845 if (kind == ParameterKind.POSITIONAL) { 2837 if (kind == ParameterKind.REQUIRED) {
2838 _reportErrorForNode(
2839 ParserErrorCode.NAMED_PARAMETER_OUTSIDE_GROUP, parameter);
2840 kind = ParameterKind.NAMED;
2841 } else if (kind == ParameterKind.POSITIONAL) {
2846 _reportErrorForToken( 2842 _reportErrorForToken(
2847 ParserErrorCode.WRONG_SEPARATOR_FOR_POSITIONAL_PARAMETER, 2843 ParserErrorCode.WRONG_SEPARATOR_FOR_POSITIONAL_PARAMETER,
2848 separator); 2844 separator);
2849 } else if (kind == ParameterKind.REQUIRED) { 2845 } else if (kind == ParameterKind.NAMED &&
2850 _reportErrorForNode( 2846 inFunctionType &&
2851 ParserErrorCode.NAMED_PARAMETER_OUTSIDE_GROUP, parameter); 2847 parameter.identifier == null) {
2852 kind = ParameterKind.NAMED; 2848 _reportErrorForCurrentToken(
2849 ParserErrorCode.MISSING_NAME_FOR_NAMED_PARAMETER);
2853 } 2850 }
2854 return astFactory.defaultFormalParameter( 2851 return astFactory.defaultFormalParameter(
2855 parameter, kind, separator, defaultValue); 2852 parameter, kind, separator, defaultValue);
2856 } else if (kind != ParameterKind.REQUIRED) { 2853 } else if (kind != ParameterKind.REQUIRED) {
2854 if (kind == ParameterKind.NAMED &&
2855 inFunctionType &&
2856 parameter.identifier == null) {
2857 _reportErrorForCurrentToken(
2858 ParserErrorCode.MISSING_NAME_FOR_NAMED_PARAMETER);
2859 }
2857 return astFactory.defaultFormalParameter(parameter, kind, null, null); 2860 return astFactory.defaultFormalParameter(parameter, kind, null, null);
2858 } 2861 }
2859 return parameter; 2862 return parameter;
2860 } 2863 }
2861 2864
2862 /** 2865 /**
2863 * Parse a list of formal parameters. Return the formal parameters that were 2866 * Parse a list of formal parameters. Return the formal parameters that were
2864 * parsed. 2867 * parsed.
2865 * 2868 *
2866 * formalParameterList ::= 2869 * formalParameterList ::=
2867 * '(' ')' 2870 * '(' ')'
2868 * | '(' normalFormalParameters (',' optionalFormalParameters)? ')' 2871 * | '(' normalFormalParameters (',' optionalFormalParameters)? ')'
2869 * | '(' optionalFormalParameters ')' 2872 * | '(' optionalFormalParameters ')'
2870 * 2873 *
2871 * normalFormalParameters ::= 2874 * normalFormalParameters ::=
2872 * normalFormalParameter (',' normalFormalParameter)* 2875 * normalFormalParameter (',' normalFormalParameter)*
2873 * 2876 *
2874 * optionalFormalParameters ::= 2877 * optionalFormalParameters ::=
2875 * optionalPositionalFormalParameters 2878 * optionalPositionalFormalParameters
2876 * | namedFormalParameters 2879 * | namedFormalParameters
2877 * 2880 *
2878 * optionalPositionalFormalParameters ::= 2881 * optionalPositionalFormalParameters ::=
2879 * '[' defaultFormalParameter (',' defaultFormalParameter)* ']' 2882 * '[' defaultFormalParameter (',' defaultFormalParameter)* ']'
2880 * 2883 *
2881 * namedFormalParameters ::= 2884 * namedFormalParameters ::=
2882 * '{' defaultNamedParameter (',' defaultNamedParameter)* '}' 2885 * '{' defaultNamedParameter (',' defaultNamedParameter)* '}'
2883 */ 2886 */
2884 FormalParameterList parseFormalParameterList() { 2887 FormalParameterList parseFormalParameterList({bool inFunctionType = false}) {
2885 if (_matches(TokenType.OPEN_PAREN)) { 2888 if (_matches(TokenType.OPEN_PAREN)) {
2886 return _parseFormalParameterListUnchecked(); 2889 return _parseFormalParameterListUnchecked(inFunctionType: inFunctionType);
2887 } 2890 }
2888 // TODO(brianwilkerson) Improve the error message. 2891 // TODO(brianwilkerson) Improve the error message.
2889 _reportErrorForCurrentToken( 2892 _reportErrorForCurrentToken(
2890 ParserErrorCode.EXPECTED_TOKEN, [TokenType.OPEN_PAREN.lexeme]); 2893 ParserErrorCode.EXPECTED_TOKEN, [TokenType.OPEN_PAREN.lexeme]);
2891 // Recovery: Check for an unmatched closing paren and parse parameters until 2894 // Recovery: Check for an unmatched closing paren and parse parameters until
2892 // it is reached. 2895 // it is reached.
2893 return _parseFormalParameterListAfterParen( 2896 return _parseFormalParameterListAfterParen(
2894 _createSyntheticToken(TokenType.OPEN_PAREN)); 2897 _createSyntheticToken(TokenType.OPEN_PAREN));
2895 } 2898 }
2896 2899
(...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after
3260 FunctionExpression parseFunctionExpression() { 3263 FunctionExpression parseFunctionExpression() {
3261 TypeParameterList typeParameters = _parseGenericMethodTypeParameters(); 3264 TypeParameterList typeParameters = _parseGenericMethodTypeParameters();
3262 FormalParameterList parameters = parseFormalParameterList(); 3265 FormalParameterList parameters = parseFormalParameterList();
3263 _validateFormalParameterList(parameters); 3266 _validateFormalParameterList(parameters);
3264 FunctionBody body = 3267 FunctionBody body =
3265 parseFunctionBody(false, ParserErrorCode.MISSING_FUNCTION_BODY, true); 3268 parseFunctionBody(false, ParserErrorCode.MISSING_FUNCTION_BODY, true);
3266 return astFactory.functionExpression(typeParameters, parameters, body); 3269 return astFactory.functionExpression(typeParameters, parameters, body);
3267 } 3270 }
3268 3271
3269 /** 3272 /**
3273 * Parse the portion of a generic function type following the [returnType].
3274 *
3275 * functionType ::=
3276 * returnType? 'Function' typeParameters? parameterTypeList
3277 * parameterTypeList ::=
3278 * '(' ')' |
3279 * | '(' normalParameterTypes ','? ')' |
3280 * | '(' normalParameterTypes ',' optionalParameterTypes ')' |
3281 * | '(' optionalParameterTypes ')'
3282 * normalParameterTypes ::=
3283 * normalParameterType (',' normalParameterType)*
3284 * normalParameterType ::=
3285 * type | typedIdentifier
3286 * optionalParameterTypes ::=
3287 * optionalPositionalParameterTypes | namedParameterTypes
3288 * optionalPositionalParameterTypes ::=
3289 * '[' normalParameterTypes ','? ']'
3290 * namedParameterTypes ::=
3291 * '{' typedIdentifier (',' typedIdentifier)* ','? '}'
3292 * typedIdentifier ::=
3293 * type identifier
3294 */
3295 GenericFunctionType parseGenericFunctionTypeAfterReturnType(
3296 TypeAnnotation returnType) {
3297 Token functionKeyword = null;
3298 if (_matchesString('Function')) {
3299 functionKeyword = getAndAdvance();
3300 } else if (_matchesIdentifier()) {
3301 _reportErrorForCurrentToken(ParserErrorCode.NAMED_FUNCTION_TYPE);
3302 } else {
3303 _reportErrorForCurrentToken(ParserErrorCode.MISSING_FUNCTION_KEYWORD);
3304 }
3305 TypeParameterList typeParameters = null;
3306 if (_matches(TokenType.LT)) {
3307 typeParameters = parseTypeParameterList();
3308 }
3309 FormalParameterList parameters =
3310 parseFormalParameterList(inFunctionType: true);
3311 return astFactory.genericFunctionType(
3312 returnType, functionKeyword, typeParameters, parameters);
3313 }
3314
3315 /**
3316 * Parse a generic function type alias.
3317 *
3318 * This method assumes that the current token is an identifier.
3319 *
3320 * genericTypeAlias ::=
3321 * 'typedef' identifier typeParameterList? '=' functionType ';'
3322 */
3323 GenericTypeAlias parseGenericTypeAlias(
3324 CommentAndMetadata commentAndMetadata, Token keyword) {
3325 Identifier name = _parseSimpleIdentifierUnchecked(isDeclaration: true);
3326 TypeParameterList typeParameters = null;
3327 if (_matches(TokenType.LT)) {
3328 typeParameters = parseTypeParameterList();
3329 }
3330 Token equals = _expect(TokenType.EQ);
3331 TypeAnnotation functionType = parseTypeAnnotation();
3332 Token semicolon = _expect(TokenType.SEMICOLON);
3333 if (functionType is! GenericFunctionType) {
3334 // TODO(brianwilkerson) Generate an error and recover (better than this).
3335 return astFactory.genericTypeAlias(
3336 commentAndMetadata.comment,
3337 commentAndMetadata.metadata,
3338 keyword,
3339 name,
3340 typeParameters,
3341 equals,
3342 null,
3343 semicolon);
3344 }
3345 return astFactory.genericTypeAlias(
3346 commentAndMetadata.comment,
3347 commentAndMetadata.metadata,
3348 keyword,
3349 name,
3350 typeParameters,
3351 equals,
3352 functionType,
3353 semicolon);
3354 }
3355
3356 /**
3270 * Parse a getter. The [commentAndMetadata] is the documentation comment and 3357 * Parse a getter. The [commentAndMetadata] is the documentation comment and
3271 * metadata to be associated with the declaration. The externalKeyword] is the 3358 * metadata to be associated with the declaration. The externalKeyword] is the
3272 * 'external' token. The staticKeyword] is the static keyword, or `null` if 3359 * 'external' token. The staticKeyword] is the static keyword, or `null` if
3273 * the getter is not static. The [returnType] the return type that has already 3360 * the getter is not static. The [returnType] the return type that has already
3274 * been parsed, or `null` if there was no return type. Return the getter that 3361 * been parsed, or `null` if there was no return type. Return the getter that
3275 * was parsed. 3362 * was parsed.
3276 * 3363 *
3277 * This method assumes that the current token matches `Keyword.GET`. 3364 * This method assumes that the current token matches `Keyword.GET`.
3278 * 3365 *
3279 * getter ::= 3366 * getter ::=
(...skipping 728 matching lines...) Expand 10 before | Expand all | Expand 10 after
4008 * functionSignature: 4095 * functionSignature:
4009 * metadata returnType? identifier typeParameters? formalParameterList 4096 * metadata returnType? identifier typeParameters? formalParameterList
4010 * 4097 *
4011 * fieldFormalParameter ::= 4098 * fieldFormalParameter ::=
4012 * metadata finalConstVarOrType? 'this' '.' identifier 4099 * metadata finalConstVarOrType? 'this' '.' identifier
4013 * 4100 *
4014 * simpleFormalParameter ::= 4101 * simpleFormalParameter ::=
4015 * declaredIdentifier 4102 * declaredIdentifier
4016 * | metadata identifier 4103 * | metadata identifier
4017 */ 4104 */
4018 NormalFormalParameter parseNormalFormalParameter() { 4105 NormalFormalParameter parseNormalFormalParameter(
4106 {bool inFunctionType = false}) {
4019 CommentAndMetadata commentAndMetadata = parseCommentAndMetadata(); 4107 CommentAndMetadata commentAndMetadata = parseCommentAndMetadata();
4020 FinalConstVarOrType holder = parseFinalConstVarOrType(true); 4108 FinalConstVarOrType holder = parseFinalConstVarOrType(!inFunctionType,
4109 inFunctionType: inFunctionType);
4021 Token thisKeyword = null; 4110 Token thisKeyword = null;
4022 Token period = null; 4111 Token period = null;
4023 if (_matchesKeyword(Keyword.THIS)) { 4112 if (_matchesKeyword(Keyword.THIS)) {
4024 thisKeyword = getAndAdvance(); 4113 thisKeyword = getAndAdvance();
4025 period = _expect(TokenType.PERIOD); 4114 period = _expect(TokenType.PERIOD);
4026 } 4115 }
4116 if (!_matchesIdentifier() && inFunctionType) {
4117 return astFactory.simpleFormalParameter(commentAndMetadata.comment,
4118 commentAndMetadata.metadata, holder.keyword, holder.type, null);
4119 }
4027 SimpleIdentifier identifier = parseSimpleIdentifier(); 4120 SimpleIdentifier identifier = parseSimpleIdentifier();
4028 TypeParameterList typeParameters = _parseGenericMethodTypeParameters(); 4121 TypeParameterList typeParameters = _parseGenericMethodTypeParameters();
4029 if (_matches(TokenType.OPEN_PAREN)) { 4122 if (_matches(TokenType.OPEN_PAREN)) {
4030 FormalParameterList parameters = _parseFormalParameterListUnchecked(); 4123 FormalParameterList parameters = _parseFormalParameterListUnchecked();
4031 if (thisKeyword == null) { 4124 if (thisKeyword == null) {
4032 if (holder.keyword != null) { 4125 if (holder.keyword != null) {
4033 _reportErrorForToken( 4126 _reportErrorForToken(
4034 ParserErrorCode.FUNCTION_TYPED_PARAMETER_VAR, holder.keyword); 4127 ParserErrorCode.FUNCTION_TYPED_PARAMETER_VAR, holder.keyword);
4035 } 4128 }
4036 Token question = null; 4129 Token question = null;
(...skipping 816 matching lines...) Expand 10 before | Expand all | Expand 10 after
4853 } 4946 }
4854 4947
4855 /** 4948 /**
4856 * Parse a type alias. The [commentAndMetadata] is the metadata to be 4949 * Parse a type alias. The [commentAndMetadata] is the metadata to be
4857 * associated with the member. Return the type alias that was parsed. 4950 * associated with the member. Return the type alias that was parsed.
4858 * 4951 *
4859 * This method assumes that the current token matches [Keyword.TYPEDEF]. 4952 * This method assumes that the current token matches [Keyword.TYPEDEF].
4860 * 4953 *
4861 * typeAlias ::= 4954 * typeAlias ::=
4862 * 'typedef' typeAliasBody 4955 * 'typedef' typeAliasBody
4956 * | genericTypeAlias
4863 * 4957 *
4864 * typeAliasBody ::= 4958 * typeAliasBody ::=
4865 * classTypeAlias 4959 * functionTypeAlias
4866 * | functionTypeAlias
4867 *
4868 * classTypeAlias ::=
4869 * identifier typeParameters? '=' 'abstract'? mixinApplication
4870 *
4871 * mixinApplication ::=
4872 * qualified withClause implementsClause? ';'
4873 * 4960 *
4874 * functionTypeAlias ::= 4961 * functionTypeAlias ::=
4875 * functionPrefix typeParameterList? formalParameterList ';' 4962 * functionPrefix typeParameterList? formalParameterList ';'
4876 * 4963 *
4877 * functionPrefix ::= 4964 * functionPrefix ::=
4878 * returnType? name 4965 * returnType? name
4879 */ 4966 */
4880 TypeAlias parseTypeAlias(CommentAndMetadata commentAndMetadata) { 4967 TypeAlias parseTypeAlias(CommentAndMetadata commentAndMetadata) {
4881 Token keyword = getAndAdvance(); 4968 Token keyword = getAndAdvance();
4882 if (_matchesIdentifier()) { 4969 if (_matchesIdentifier()) {
4883 Token next = _peek(); 4970 Token next = _peek();
4884 if (_tokenMatches(next, TokenType.LT)) { 4971 if (_tokenMatches(next, TokenType.LT)) {
4885 next = _skipTypeParameterList(next); 4972 next = _skipTypeParameterList(next);
4886 if (next != null && _tokenMatches(next, TokenType.EQ)) { 4973 if (next != null && _tokenMatches(next, TokenType.EQ)) {
4887 TypeAlias typeAlias = 4974 TypeAlias typeAlias =
4888 parseClassTypeAlias(commentAndMetadata, null, keyword); 4975 parseGenericTypeAlias(commentAndMetadata, keyword);
4889 _reportErrorForToken(
4890 ParserErrorCode.DEPRECATED_CLASS_TYPE_ALIAS, keyword);
4891 return typeAlias; 4976 return typeAlias;
4892 } 4977 }
4893 } else if (_tokenMatches(next, TokenType.EQ)) { 4978 } else if (_tokenMatches(next, TokenType.EQ)) {
4894 TypeAlias typeAlias = 4979 TypeAlias typeAlias =
4895 parseClassTypeAlias(commentAndMetadata, null, keyword); 4980 parseGenericTypeAlias(commentAndMetadata, keyword);
4896 _reportErrorForToken(
4897 ParserErrorCode.DEPRECATED_CLASS_TYPE_ALIAS, keyword);
4898 return typeAlias; 4981 return typeAlias;
4899 } 4982 }
4900 } 4983 }
4901 return _parseFunctionTypeAlias(commentAndMetadata, keyword); 4984 return _parseFunctionTypeAlias(commentAndMetadata, keyword);
4902 } 4985 }
4903 4986
4904 /** 4987 /**
4988 * Parse a type.
4989 *
4990 * type ::=
4991 * typeWithoutFunction
4992 * | functionType
4993 */
4994 TypeAnnotation parseTypeAnnotation() {
4995 if (_matchesString('Function')) {
4996 // Generic function type with no return type.
4997 return parseGenericFunctionTypeAfterReturnType(null);
4998 }
4999 TypeAnnotation type = parseReturnType();
5000 while (_matchesString('Function')) {
5001 type = parseGenericFunctionTypeAfterReturnType(type);
5002 }
5003 return type;
5004 }
5005
5006 /**
4905 * Parse a list of type arguments. Return the type argument list that was 5007 * Parse a list of type arguments. Return the type argument list that was
4906 * parsed. 5008 * parsed.
4907 * 5009 *
4908 * This method assumes that the current token matches `TokenType.LT`. 5010 * This method assumes that the current token matches `TokenType.LT`.
4909 * 5011 *
4910 * typeArguments ::= 5012 * typeArguments ::=
4911 * '<' typeList '>' 5013 * '<' typeList '>'
4912 * 5014 *
4913 * typeList ::= 5015 * typeList ::=
4914 * type (',' type)* 5016 * type (',' type)*
(...skipping 1445 matching lines...) Expand 10 before | Expand all | Expand 10 after
6360 commentAndMetadata.metadata[0]); 6462 commentAndMetadata.metadata[0]);
6361 } 6463 }
6362 return astFactory.enumConstantDeclaration( 6464 return astFactory.enumConstantDeclaration(
6363 commentAndMetadata.comment, commentAndMetadata.metadata, name); 6465 commentAndMetadata.comment, commentAndMetadata.metadata, name);
6364 } 6466 }
6365 6467
6366 /** 6468 /**
6367 * Parse a list of formal parameters given that the list starts with the given 6469 * Parse a list of formal parameters given that the list starts with the given
6368 * [leftParenthesis]. Return the formal parameters that were parsed. 6470 * [leftParenthesis]. Return the formal parameters that were parsed.
6369 */ 6471 */
6370 FormalParameterList _parseFormalParameterListAfterParen( 6472 FormalParameterList _parseFormalParameterListAfterParen(Token leftParenthesis,
6371 Token leftParenthesis) { 6473 {bool inFunctionType = false}) {
6372 if (_matches(TokenType.CLOSE_PAREN)) { 6474 if (_matches(TokenType.CLOSE_PAREN)) {
6373 return astFactory.formalParameterList( 6475 return astFactory.formalParameterList(
6374 leftParenthesis, null, null, null, getAndAdvance()); 6476 leftParenthesis, null, null, null, getAndAdvance());
6375 } 6477 }
6376 // 6478 //
6377 // Even though it is invalid to have default parameters outside of brackets, 6479 // Even though it is invalid to have default parameters outside of brackets,
6378 // required parameters inside of brackets, or multiple groups of default and 6480 // required parameters inside of brackets, or multiple groups of default and
6379 // named parameters, we allow all of these cases so that we can recover 6481 // named parameters, we allow all of these cases so that we can recover
6380 // better. 6482 // better.
6381 // 6483 //
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
6434 if (leftSquareBracket != null && !reportedMixedGroups) { 6536 if (leftSquareBracket != null && !reportedMixedGroups) {
6435 _reportErrorForCurrentToken(ParserErrorCode.MIXED_PARAMETER_GROUPS); 6537 _reportErrorForCurrentToken(ParserErrorCode.MIXED_PARAMETER_GROUPS);
6436 reportedMixedGroups = true; 6538 reportedMixedGroups = true;
6437 } 6539 }
6438 leftCurlyBracket = getAndAdvance(); 6540 leftCurlyBracket = getAndAdvance();
6439 kind = ParameterKind.NAMED; 6541 kind = ParameterKind.NAMED;
6440 } 6542 }
6441 // 6543 //
6442 // Parse and record the parameter. 6544 // Parse and record the parameter.
6443 // 6545 //
6444 FormalParameter parameter = parseFormalParameter(kind); 6546 FormalParameter parameter =
6547 parseFormalParameter(kind, inFunctionType: inFunctionType);
6445 parameters.add(parameter); 6548 parameters.add(parameter);
6446 if (kind == ParameterKind.REQUIRED && wasOptionalParameter) { 6549 if (kind == ParameterKind.REQUIRED && wasOptionalParameter) {
6447 _reportErrorForNode( 6550 _reportErrorForNode(
6448 ParserErrorCode.NORMAL_BEFORE_OPTIONAL_PARAMETERS, parameter); 6551 ParserErrorCode.NORMAL_BEFORE_OPTIONAL_PARAMETERS, parameter);
6449 } 6552 }
6450 // 6553 //
6451 // Handle the end of parameter groups. 6554 // Handle the end of parameter groups.
6452 // 6555 //
6453 // TODO(brianwilkerson) Improve the detection and reporting of missing and 6556 // TODO(brianwilkerson) Improve the detection and reporting of missing and
6454 // mismatched delimiters. 6557 // mismatched delimiters.
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
6523 return astFactory.formalParameterList(leftParenthesis, parameters, 6626 return astFactory.formalParameterList(leftParenthesis, parameters,
6524 leftSquareBracket, rightSquareBracket, rightParenthesis); 6627 leftSquareBracket, rightSquareBracket, rightParenthesis);
6525 } 6628 }
6526 6629
6527 /** 6630 /**
6528 * Parse a list of formal parameters. Return the formal parameters that were 6631 * Parse a list of formal parameters. Return the formal parameters that were
6529 * parsed. 6632 * parsed.
6530 * 6633 *
6531 * This method assumes that the current token matches `TokenType.OPEN_PAREN`. 6634 * This method assumes that the current token matches `TokenType.OPEN_PAREN`.
6532 */ 6635 */
6533 FormalParameterList _parseFormalParameterListUnchecked() { 6636 FormalParameterList _parseFormalParameterListUnchecked(
6534 return _parseFormalParameterListAfterParen(getAndAdvance()); 6637 {bool inFunctionType = false}) {
6638 return _parseFormalParameterListAfterParen(getAndAdvance(),
6639 inFunctionType: inFunctionType);
6535 } 6640 }
6536 6641
6537 /** 6642 /**
6538 * Parse a function declaration statement. The [commentAndMetadata] is the 6643 * Parse a function declaration statement. The [commentAndMetadata] is the
6539 * documentation comment and metadata to be associated with the declaration. 6644 * documentation comment and metadata to be associated with the declaration.
6540 * The [returnType] is the return type, or `null` if there is no return type. 6645 * The [returnType] is the return type, or `null` if there is no return type.
6541 * Return the function declaration statement that was parsed. 6646 * Return the function declaration statement that was parsed.
6542 * 6647 *
6543 * functionDeclarationStatement ::= 6648 * functionDeclarationStatement ::=
6544 * functionSignature functionBody 6649 * functionSignature functionBody
(...skipping 1644 matching lines...) Expand 10 before | Expand all | Expand 10 after
8189 */ 8294 */
8190 Parser_SyntheticKeywordToken(Keyword keyword, int offset) 8295 Parser_SyntheticKeywordToken(Keyword keyword, int offset)
8191 : super(keyword, offset); 8296 : super(keyword, offset);
8192 8297
8193 @override 8298 @override
8194 int get length => 0; 8299 int get length => 0;
8195 8300
8196 @override 8301 @override
8197 Token copy() => new Parser_SyntheticKeywordToken(keyword, offset); 8302 Token copy() => new Parser_SyntheticKeywordToken(keyword, offset);
8198 } 8303 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/dart/error/syntactic_errors.dart ('k') | pkg/analyzer/test/generated/parser_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698