Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify implementation of parsing constraints. #16429

Merged
merged 6 commits into from
Jul 17, 2017
77 changes: 31 additions & 46 deletions src/Compilers/CSharp/Portable/Parser/LanguageParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1780,8 +1780,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()
Expand All @@ -1796,42 +1796,26 @@ private BaseListSyntax ParseBaseList()
try
{
// first type
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understood correctly, the code that was removed (here and below) handled the case class C : where T : ... and class C : I1, where T : ....
Do we have parser tests for those? If not, could you add them?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will check on this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is parsed properly. That's because ParseType will not accept "where :" as a type name. I'm not sure if we have tests for this though, so i will add them.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests added.

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;
}
}

Expand All @@ -1847,11 +1831,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 &&
Expand All @@ -1870,7 +1854,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();

Expand All @@ -1880,7 +1864,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)));
}
Expand All @@ -1900,7 +1884,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;
Expand Down Expand Up @@ -1968,7 +1952,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);
}

Expand Down Expand Up @@ -2756,7 +2740,7 @@ private bool IsEndOfTypeParameterList()
return true;
}

if (IsPossibleTypeParameterConstraintClauseStart())
if (IsCurrentTokenWhereOfConstraintClause())
{
// class C<T where T :
return true;
Expand Down Expand Up @@ -4960,7 +4944,8 @@ private bool IsTrueIdentifier()
if (this.CurrentToken.Kind == SyntaxKind.IdentifierToken)
{
if (!IsCurrentTokenPartialKeywordOfPartialMethodOrType() &&
!IsCurrentTokenQueryKeywordInQuery())
!IsCurrentTokenQueryKeywordInQuery() &&
!IsCurrentTokenWhereOfConstraintClause())
{
return true;
}
Expand Down Expand Up @@ -5068,7 +5053,7 @@ private TypeParameterListSyntax ParseTypeParameterList()
// remaining parameter & commas
while (true)
{
if (this.CurrentToken.Kind == SyntaxKind.GreaterThanToken || this.IsPossibleTypeParameterConstraintClauseStart())
if (this.CurrentToken.Kind == SyntaxKind.GreaterThanToken || this.IsCurrentTokenWhereOfConstraintClause())
{
break;
}
Expand Down Expand Up @@ -5105,7 +5090,7 @@ private PostSkipAction SkipBadTypeParameterListTokens(SeparatedSyntaxListBuilder

private TypeParameterSyntax ParseTypeParameter()
{
if (this.IsPossibleTypeParameterConstraintClauseStart())
if (this.IsCurrentTokenWhereOfConstraintClause())
{
return _syntaxFactory.TypeParameter(
default(SyntaxList<AttributeListSyntax>),
Expand Down