Skip to content

Commit

Permalink
Simplify implementation of parsing constraints.
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi committed May 17, 2017
1 parent bab05cb commit d2a0a89
Showing 1 changed file with 31 additions and 46 deletions.
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 @@ -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()
Expand All @@ -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;
}
}

Expand All @@ -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 &&
Expand All @@ -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();

Expand All @@ -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)));
}
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}

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

if (IsPossibleTypeParameterConstraintClauseStart())
if (IsCurrentTokenWhereOfConstraintClause())
{
// class C<T where T :
return true;
Expand Down Expand Up @@ -5041,7 +5025,8 @@ private bool IsTrueIdentifier()
if (this.CurrentToken.Kind == SyntaxKind.IdentifierToken)
{
if (!IsCurrentTokenPartialKeywordOfPartialMethodOrType() &&
!IsCurrentTokenQueryKeywordInQuery())
!IsCurrentTokenQueryKeywordInQuery() &&
!IsCurrentTokenWhereOfConstraintClause())
{
return true;
}
Expand Down Expand Up @@ -5149,7 +5134,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 @@ -5186,7 +5171,7 @@ private PostSkipAction SkipBadTypeParameterListTokens(SeparatedSyntaxListBuilder

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

0 comments on commit d2a0a89

Please sign in to comment.