From d2a0a89ad73ca7ee97559b3ac70fb826eb83d99e Mon Sep 17 00:00:00 2001 From: CyrusNajmabadi Date: Wed, 17 May 2017 13:46:51 -0700 Subject: [PATCH] Simplify implementation of parsing constraints. --- .../CSharp/Portable/Parser/LanguageParser.cs | 77 ++++++++----------- 1 file changed, 31 insertions(+), 46 deletions(-) diff --git a/src/Compilers/CSharp/Portable/Parser/LanguageParser.cs b/src/Compilers/CSharp/Portable/Parser/LanguageParser.cs index c3f845c305e72..011199d64b135 100644 --- a/src/Compilers/CSharp/Portable/Parser/LanguageParser.cs +++ b/src/Compilers/CSharp/Portable/Parser/LanguageParser.cs @@ -1859,8 +1859,8 @@ private bool IsPossibleMemberStartOrStop() private bool IsPossibleAggregateClauseStartOrStop() { return this.CurrentToken.Kind == SyntaxKind.ColonToken - || this.IsPossibleTypeParameterConstraintClauseStart() - || this.CurrentToken.Kind == SyntaxKind.OpenBraceToken; + || this.CurrentToken.Kind == SyntaxKind.OpenBraceToken + || this.IsCurrentTokenWhereOfConstraintClause(); } private BaseListSyntax ParseBaseList() @@ -1875,42 +1875,26 @@ private BaseListSyntax ParseBaseList() try { // first type - if (this.IsPossibleTypeParameterConstraintClauseStart()) - { - list.Add(_syntaxFactory.SimpleBaseType(this.AddError(this.CreateMissingIdentifierName(), ErrorCode.ERR_TypeExpected))); - } - else - { - TypeSyntax firstType = this.ParseType(); - - list.Add(_syntaxFactory.SimpleBaseType(firstType)); + TypeSyntax firstType = this.ParseType(); + list.Add(_syntaxFactory.SimpleBaseType(firstType)); - // any additional types - while (true) + // any additional types + while (true) + { + if (this.CurrentToken.Kind == SyntaxKind.OpenBraceToken || + this.IsCurrentTokenWhereOfConstraintClause()) { - if (this.CurrentToken.Kind == SyntaxKind.OpenBraceToken - || this.IsPossibleTypeParameterConstraintClauseStart()) - { - break; - } - else if (this.CurrentToken.Kind == SyntaxKind.CommaToken || this.IsPossibleType()) - { - list.AddSeparator(this.EatToken(SyntaxKind.CommaToken)); - if (this.IsPossibleTypeParameterConstraintClauseStart()) - { - list.Add(_syntaxFactory.SimpleBaseType(this.AddError(this.CreateMissingIdentifierName(), ErrorCode.ERR_TypeExpected))); - } - else - { - list.Add(_syntaxFactory.SimpleBaseType(this.ParseType())); - } - - continue; - } - else if (this.SkipBadBaseListTokens(ref colon, list, SyntaxKind.CommaToken) == PostSkipAction.Abort) - { - break; - } + break; + } + else if (this.CurrentToken.Kind == SyntaxKind.CommaToken || this.IsPossibleType()) + { + list.AddSeparator(this.EatToken(SyntaxKind.CommaToken)); + list.Add(_syntaxFactory.SimpleBaseType(this.ParseType())); + continue; + } + else if (this.SkipBadBaseListTokens(ref colon, list, SyntaxKind.CommaToken) == PostSkipAction.Abort) + { + break; } } @@ -1926,11 +1910,11 @@ private PostSkipAction SkipBadBaseListTokens(ref SyntaxToken colon, SeparatedSyn { return this.SkipBadSeparatedListTokensWithExpectedKind(ref colon, list, p => p.CurrentToken.Kind != SyntaxKind.CommaToken && !p.IsPossibleAttribute(), - p => p.CurrentToken.Kind == SyntaxKind.OpenBraceToken || p.IsPossibleTypeParameterConstraintClauseStart() || p.IsTerminator(), + p => p.CurrentToken.Kind == SyntaxKind.OpenBraceToken || p.IsCurrentTokenWhereOfConstraintClause() || p.IsTerminator(), expected); } - private bool IsPossibleTypeParameterConstraintClauseStart() + private bool IsCurrentTokenWhereOfConstraintClause() { return this.CurrentToken.ContextualKind == SyntaxKind.WhereKeyword && @@ -1949,7 +1933,7 @@ private void ParseTypeParameterConstraintClauses(SyntaxListBuilder list) private TypeParameterConstraintClauseSyntax ParseTypeParameterConstraintClause() { var where = this.EatContextualToken(SyntaxKind.WhereKeyword); - var name = (this.IsPossibleTypeParameterConstraintClauseStart() || !IsTrueIdentifier()) + var name = !IsTrueIdentifier() ? this.AddError(this.CreateMissingIdentifierName(), ErrorCode.ERR_IdentifierExpected) : this.ParseIdentifierName(); @@ -1959,7 +1943,7 @@ private TypeParameterConstraintClauseSyntax ParseTypeParameterConstraintClause() try { // first bound - if (this.CurrentToken.Kind == SyntaxKind.OpenBraceToken || this.IsPossibleTypeParameterConstraintClauseStart()) + if (this.CurrentToken.Kind == SyntaxKind.OpenBraceToken || this.IsCurrentTokenWhereOfConstraintClause()) { bounds.Add(_syntaxFactory.TypeConstraint(this.AddError(this.CreateMissingIdentifierName(), ErrorCode.ERR_TypeExpected))); } @@ -1979,7 +1963,7 @@ private TypeParameterConstraintClauseSyntax ParseTypeParameterConstraintClause() else if (this.CurrentToken.Kind == SyntaxKind.CommaToken || this.IsPossibleTypeParameterConstraint()) { bounds.AddSeparator(this.EatToken(SyntaxKind.CommaToken)); - if (this.IsPossibleTypeParameterConstraintClauseStart()) + if (this.IsCurrentTokenWhereOfConstraintClause()) { bounds.Add(_syntaxFactory.TypeConstraint(this.AddError(this.CreateMissingIdentifierName(), ErrorCode.ERR_TypeExpected))); break; @@ -2047,7 +2031,7 @@ private PostSkipAction SkipBadTypeParameterConstraintTokens(SeparatedSyntaxListB Debug.Assert(list.Count > 0); return this.SkipBadSeparatedListTokensWithExpectedKind(ref tmp, list, p => this.CurrentToken.Kind != SyntaxKind.CommaToken && !this.IsPossibleTypeParameterConstraint(), - p => this.CurrentToken.Kind == SyntaxKind.OpenBraceToken || this.IsPossibleTypeParameterConstraintClauseStart() || this.IsTerminator(), + p => this.CurrentToken.Kind == SyntaxKind.OpenBraceToken || this.IsCurrentTokenWhereOfConstraintClause() || this.IsTerminator(), expected); } @@ -2835,7 +2819,7 @@ private bool IsEndOfTypeParameterList() return true; } - if (IsPossibleTypeParameterConstraintClauseStart()) + if (IsCurrentTokenWhereOfConstraintClause()) { // class C),