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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -4941,7 +4925,8 @@ private bool IsTrueIdentifier()
if (this.CurrentToken.Kind == SyntaxKind.IdentifierToken)
{
if (!IsCurrentTokenPartialKeywordOfPartialMethodOrType() &&
!IsCurrentTokenQueryKeywordInQuery())
!IsCurrentTokenQueryKeywordInQuery() &&
!IsCurrentTokenWhereOfConstraintClause())
{
return true;
}
Expand Down Expand Up @@ -5049,7 +5034,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 @@ -5086,7 +5071,7 @@ private PostSkipAction SkipBadTypeParameterListTokens(SeparatedSyntaxListBuilder

private TypeParameterSyntax ParseTypeParameter()
{
if (this.IsPossibleTypeParameterConstraintClauseStart())
if (this.IsCurrentTokenWhereOfConstraintClause())
{
return _syntaxFactory.TypeParameter(
default(SyntaxList<AttributeListSyntax>),
Expand Down
201 changes: 200 additions & 1 deletion src/Compilers/CSharp/Test/Syntax/Parsing/DeclarationParsingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5774,7 +5774,6 @@ class C
}
}


[Fact]
public void ParseOutVar()
{
Expand Down Expand Up @@ -5848,5 +5847,205 @@ void Foo()
}
EOF();
}

[Fact]
public void TestPartiallyWrittenConstraintClauseInBaseList1()
{
var tree = UsingTree(@"
class C<T> : where
");
N(SyntaxKind.CompilationUnit);
{
N(SyntaxKind.ClassDeclaration);
{
N(SyntaxKind.ClassKeyword);
N(SyntaxKind.IdentifierToken, "C");
N(SyntaxKind.TypeParameterList);
{
N(SyntaxKind.LessThanToken);
N(SyntaxKind.TypeParameter);
{
N(SyntaxKind.IdentifierToken, "T");
}
N(SyntaxKind.GreaterThanToken);
}
N(SyntaxKind.BaseList);
{
N(SyntaxKind.ColonToken);
N(SyntaxKind.SimpleBaseType);
{
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "where");
}
}
}
M(SyntaxKind.OpenBraceToken);
M(SyntaxKind.CloseBraceToken);
}
N(SyntaxKind.EndOfFileToken);
}
EOF();
}

[Fact]
public void TestPartiallyWrittenConstraintClauseInBaseList2()
{
var tree = UsingTree(@"
class C<T> : where T
");
N(SyntaxKind.CompilationUnit);
{
N(SyntaxKind.ClassDeclaration);
{
N(SyntaxKind.ClassKeyword);
N(SyntaxKind.IdentifierToken, "C");
N(SyntaxKind.TypeParameterList);
{
N(SyntaxKind.LessThanToken);
N(SyntaxKind.TypeParameter);
{
N(SyntaxKind.IdentifierToken, "T");
}
N(SyntaxKind.GreaterThanToken);
}
N(SyntaxKind.BaseList);
{
N(SyntaxKind.ColonToken);
N(SyntaxKind.SimpleBaseType);
{
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "where");
}
}
M(SyntaxKind.CommaToken);
N(SyntaxKind.SimpleBaseType);
{
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "T");
}
}
}
M(SyntaxKind.OpenBraceToken);
M(SyntaxKind.CloseBraceToken);
}
N(SyntaxKind.EndOfFileToken);
}
EOF();
}

[Fact]
public void TestPartiallyWrittenConstraintClauseInBaseList3()
{
var tree = UsingTree(@"
class C<T> : where T :
");
N(SyntaxKind.CompilationUnit);
{
N(SyntaxKind.ClassDeclaration);
{
N(SyntaxKind.ClassKeyword);
N(SyntaxKind.IdentifierToken, "C");
N(SyntaxKind.TypeParameterList);
{
N(SyntaxKind.LessThanToken);
N(SyntaxKind.TypeParameter);
{
N(SyntaxKind.IdentifierToken, "T");
}
N(SyntaxKind.GreaterThanToken);
}
N(SyntaxKind.BaseList);
{
N(SyntaxKind.ColonToken);
M(SyntaxKind.SimpleBaseType);
{
M(SyntaxKind.IdentifierName);
{
M(SyntaxKind.IdentifierToken);
}
}
}
N(SyntaxKind.TypeParameterConstraintClause);
{
N(SyntaxKind.WhereKeyword);
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "T");
}
N(SyntaxKind.ColonToken);
M(SyntaxKind.TypeConstraint);
{
M(SyntaxKind.IdentifierName);
{
M(SyntaxKind.IdentifierToken);
}
}
}
M(SyntaxKind.OpenBraceToken);
M(SyntaxKind.CloseBraceToken);
}
N(SyntaxKind.EndOfFileToken);
}
EOF();
}

[Fact]
public void TestPartiallyWrittenConstraintClauseInBaseList4()
{
var tree = UsingTree(@"
class C<T> : where T : X
");
N(SyntaxKind.CompilationUnit);
{
N(SyntaxKind.ClassDeclaration);
{
N(SyntaxKind.ClassKeyword);
N(SyntaxKind.IdentifierToken, "C");
N(SyntaxKind.TypeParameterList);
{
N(SyntaxKind.LessThanToken);
N(SyntaxKind.TypeParameter);
{
N(SyntaxKind.IdentifierToken, "T");
}
N(SyntaxKind.GreaterThanToken);
}
N(SyntaxKind.BaseList);
{
N(SyntaxKind.ColonToken);
M(SyntaxKind.SimpleBaseType);
{
M(SyntaxKind.IdentifierName);
{
M(SyntaxKind.IdentifierToken);
}
}
}
N(SyntaxKind.TypeParameterConstraintClause);
{
N(SyntaxKind.WhereKeyword);
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "T");
}
N(SyntaxKind.ColonToken);
N(SyntaxKind.TypeConstraint);
{
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "X");
}
}
}
M(SyntaxKind.OpenBraceToken);
M(SyntaxKind.CloseBraceToken);
}
N(SyntaxKind.EndOfFileToken);
}
EOF();
}
}
}