未验证 提交 8176685d 编写于 作者: J Julien Couvreur 提交者: GitHub

Merge pull request #28115 from wachulski/fix/27866/else-without-if-error-message

Fix 'else without if' parsing - better error message
......@@ -3930,6 +3930,15 @@ internal class CSharpResources {
}
}
/// <summary>
/// Looks up a localized string similar to &apos;else&apos; cannot start a statement..
/// </summary>
internal static string ERR_ElseCannotStartStatement {
get {
return ResourceManager.GetString("ERR_ElseCannotStartStatement", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Empty character literal.
/// </summary>
......
......@@ -5638,4 +5638,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_ExpressionTreeCantContainRefStruct" xml:space="preserve">
<value>Expression tree cannot contain value of ref struct or restricted type '{0}'.</value>
</data>
</root>
<data name="ERR_ElseCannotStartStatement" xml:space="preserve">
<value>'else' cannot start a statement.</value>
</data>
</root>
\ No newline at end of file
......@@ -1635,7 +1635,8 @@ internal enum ErrorCode
ERR_NullableDirectiveQualifierExpected = 8637,
WRN_CantInferNullabilityOfMethodTypeArgs = 8638,
WRN_NoBestNullabilityArrayElements = 8639,
ERR_ExpressionTreeCantContainRefStruct = 8640
ERR_ExpressionTreeCantContainRefStruct = 8640,
ERR_ElseCannotStartStatement = 8641
#endregion diagnostics introduced for C# 8.0
......
......@@ -6678,6 +6678,7 @@ private StatementSyntax ParseStatementNoDeclaration(bool allowAnyExpression)
case SyntaxKind.GotoKeyword:
return this.ParseGotoStatement();
case SyntaxKind.IfKeyword:
case SyntaxKind.ElseKeyword: // Including 'else' keyword to handle 'else without if' error cases
return this.ParseIfStatement();
case SyntaxKind.LockKeyword:
return this.ParseLockStatement();
......@@ -7338,6 +7339,7 @@ private bool IsPossibleStatement(bool acceptAccessibilityMods)
case SyntaxKind.ForEachKeyword:
case SyntaxKind.GotoKeyword:
case SyntaxKind.IfKeyword:
case SyntaxKind.ElseKeyword:
case SyntaxKind.LockKeyword:
case SyntaxKind.ReturnKeyword:
case SyntaxKind.SwitchKeyword:
......@@ -7933,14 +7935,18 @@ private GotoStatementSyntax ParseGotoStatement()
private IfStatementSyntax ParseIfStatement()
{
Debug.Assert(this.CurrentToken.Kind == SyntaxKind.IfKeyword);
var @if = this.EatToken(SyntaxKind.IfKeyword);
Debug.Assert(this.CurrentToken.Kind == SyntaxKind.IfKeyword || this.CurrentToken.Kind == SyntaxKind.ElseKeyword);
bool firstTokenIsElse = this.CurrentToken.Kind == SyntaxKind.ElseKeyword;
var @if = firstTokenIsElse
? this.EatToken(SyntaxKind.IfKeyword, ErrorCode.ERR_ElseCannotStartStatement)
: this.EatToken(SyntaxKind.IfKeyword);
var openParen = this.EatToken(SyntaxKind.OpenParenToken);
var condition = this.ParseExpressionCore();
var closeParen = this.EatToken(SyntaxKind.CloseParenToken);
var statement = this.ParseEmbeddedStatement();
var elseClause = ParseElseClauseOpt();
var statement = firstTokenIsElse ? this.ParseExpressionStatement() : this.ParseEmbeddedStatement();
var elseClause = this.ParseElseClauseOpt();
return _syntaxFactory.IfStatement(@if, openParen, condition, closeParen, statement, elseClause);
}
......@@ -8775,6 +8781,7 @@ private static bool IsInvalidSubExpression(SyntaxKind kind)
case SyntaxKind.ForEachKeyword:
case SyntaxKind.GotoKeyword:
case SyntaxKind.IfKeyword:
case SyntaxKind.ElseKeyword:
case SyntaxKind.LockKeyword:
case SyntaxKind.ReturnKeyword:
case SyntaxKind.SwitchKeyword:
......
......@@ -47,6 +47,11 @@
<target state="new">Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'</target>
<note />
</trans-unit>
<trans-unit id="ERR_ElseCannotStartStatement">
<source>'else' cannot start a statement.</source>
<target state="new">'else' cannot start a statement.</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExplicitNonNullTypesAttribute">
<source>Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</source>
<target state="new">Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</target>
......
......@@ -47,6 +47,11 @@
<target state="new">Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'</target>
<note />
</trans-unit>
<trans-unit id="ERR_ElseCannotStartStatement">
<source>'else' cannot start a statement.</source>
<target state="new">'else' cannot start a statement.</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExplicitNonNullTypesAttribute">
<source>Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</source>
<target state="new">Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</target>
......
......@@ -47,6 +47,11 @@
<target state="new">Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'</target>
<note />
</trans-unit>
<trans-unit id="ERR_ElseCannotStartStatement">
<source>'else' cannot start a statement.</source>
<target state="new">'else' cannot start a statement.</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExplicitNonNullTypesAttribute">
<source>Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</source>
<target state="new">Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</target>
......
......@@ -47,6 +47,11 @@
<target state="new">Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'</target>
<note />
</trans-unit>
<trans-unit id="ERR_ElseCannotStartStatement">
<source>'else' cannot start a statement.</source>
<target state="new">'else' cannot start a statement.</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExplicitNonNullTypesAttribute">
<source>Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</source>
<target state="new">Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</target>
......
......@@ -47,6 +47,11 @@
<target state="new">Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'</target>
<note />
</trans-unit>
<trans-unit id="ERR_ElseCannotStartStatement">
<source>'else' cannot start a statement.</source>
<target state="new">'else' cannot start a statement.</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExplicitNonNullTypesAttribute">
<source>Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</source>
<target state="new">Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</target>
......
......@@ -47,6 +47,11 @@
<target state="new">Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'</target>
<note />
</trans-unit>
<trans-unit id="ERR_ElseCannotStartStatement">
<source>'else' cannot start a statement.</source>
<target state="new">'else' cannot start a statement.</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExplicitNonNullTypesAttribute">
<source>Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</source>
<target state="new">Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</target>
......
......@@ -47,6 +47,11 @@
<target state="new">Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'</target>
<note />
</trans-unit>
<trans-unit id="ERR_ElseCannotStartStatement">
<source>'else' cannot start a statement.</source>
<target state="new">'else' cannot start a statement.</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExplicitNonNullTypesAttribute">
<source>Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</source>
<target state="new">Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</target>
......
......@@ -47,6 +47,11 @@
<target state="new">Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'</target>
<note />
</trans-unit>
<trans-unit id="ERR_ElseCannotStartStatement">
<source>'else' cannot start a statement.</source>
<target state="new">'else' cannot start a statement.</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExplicitNonNullTypesAttribute">
<source>Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</source>
<target state="new">Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</target>
......
......@@ -47,6 +47,11 @@
<target state="new">Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'</target>
<note />
</trans-unit>
<trans-unit id="ERR_ElseCannotStartStatement">
<source>'else' cannot start a statement.</source>
<target state="new">'else' cannot start a statement.</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExplicitNonNullTypesAttribute">
<source>Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</source>
<target state="new">Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</target>
......
......@@ -47,6 +47,11 @@
<target state="new">Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'</target>
<note />
</trans-unit>
<trans-unit id="ERR_ElseCannotStartStatement">
<source>'else' cannot start a statement.</source>
<target state="new">'else' cannot start a statement.</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExplicitNonNullTypesAttribute">
<source>Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</source>
<target state="new">Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</target>
......
......@@ -47,6 +47,11 @@
<target state="new">Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'</target>
<note />
</trans-unit>
<trans-unit id="ERR_ElseCannotStartStatement">
<source>'else' cannot start a statement.</source>
<target state="new">'else' cannot start a statement.</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExplicitNonNullTypesAttribute">
<source>Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</source>
<target state="new">Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</target>
......
......@@ -47,6 +47,11 @@
<target state="new">Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'</target>
<note />
</trans-unit>
<trans-unit id="ERR_ElseCannotStartStatement">
<source>'else' cannot start a statement.</source>
<target state="new">'else' cannot start a statement.</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExplicitNonNullTypesAttribute">
<source>Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</source>
<target state="new">Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</target>
......
......@@ -47,6 +47,11 @@
<target state="new">Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'</target>
<note />
</trans-unit>
<trans-unit id="ERR_ElseCannotStartStatement">
<source>'else' cannot start a statement.</source>
<target state="new">'else' cannot start a statement.</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExplicitNonNullTypesAttribute">
<source>Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</source>
<target state="new">Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</target>
......
......@@ -2054,6 +2054,40 @@ public void TestIfElse()
Assert.NotNull(ss.Else.Statement);
}
[Fact]
public void TestIfElseIf()
{
var text = "if (a) { } else if (b) { }";
var statement = this.ParseStatement(text);
Assert.NotNull(statement);
Assert.Equal(SyntaxKind.IfStatement, statement.Kind());
Assert.Equal(text, statement.ToString());
Assert.Equal(0, statement.Errors().Length);
var ss = (IfStatementSyntax)statement;
Assert.NotNull(ss.IfKeyword);
Assert.Equal(SyntaxKind.IfKeyword, ss.IfKeyword.Kind());
Assert.NotNull(ss.OpenParenToken);
Assert.NotNull(ss.Condition);
Assert.Equal("a", ss.Condition.ToString());
Assert.NotNull(ss.CloseParenToken);
Assert.NotNull(ss.Statement);
Assert.NotNull(ss.Else);
Assert.NotNull(ss.Else.ElseKeyword);
Assert.Equal(SyntaxKind.ElseKeyword, ss.Else.ElseKeyword.Kind());
Assert.NotNull(ss.Else.Statement);
var subIf = (IfStatementSyntax) ss.Else.Statement;
Assert.NotNull(subIf.IfKeyword);
Assert.Equal(SyntaxKind.IfKeyword, subIf.IfKeyword.Kind());
Assert.NotNull(subIf.Condition);
Assert.Equal("b", subIf.Condition.ToString());
Assert.NotNull(subIf.CloseParenToken);
Assert.NotNull(subIf.Statement);
}
[Fact]
public void TestLock()
{
......@@ -2741,6 +2775,311 @@ public void NullExceptionInLabeledStatement()
);
}
[WorkItem(27866, "https://github.com/dotnet/roslyn/issues/27866")]
[Fact]
public void ParseElseWithoutPrecedingIfStatement()
{
UsingStatement("else {}",
// (1,1): error CS8641: 'else' cannot start a statement.
// else {}
Diagnostic(ErrorCode.ERR_ElseCannotStartStatement, "else").WithLocation(1, 1),
// (1,1): error CS1003: Syntax error, '(' expected
// else {}
Diagnostic(ErrorCode.ERR_SyntaxError, "else").WithArguments("(", "else").WithLocation(1, 1),
// (1,1): error CS1525: Invalid expression term 'else'
// else {}
Diagnostic(ErrorCode.ERR_InvalidExprTerm, "else").WithArguments("else").WithLocation(1, 1),
// (1,1): error CS1026: ) expected
// else {}
Diagnostic(ErrorCode.ERR_CloseParenExpected, "else").WithLocation(1, 1),
// (1,1): error CS1525: Invalid expression term 'else'
// else {}
Diagnostic(ErrorCode.ERR_InvalidExprTerm, "else").WithArguments("else").WithLocation(1, 1),
// (1,1): error CS1002: ; expected
// else {}
Diagnostic(ErrorCode.ERR_SemicolonExpected, "else").WithLocation(1, 1)
);
N(SyntaxKind.IfStatement);
{
M(SyntaxKind.IfKeyword);
M(SyntaxKind.OpenParenToken);
M(SyntaxKind.IdentifierName);
{
M(SyntaxKind.IdentifierToken);
}
M(SyntaxKind.CloseParenToken);
M(SyntaxKind.ExpressionStatement);
{
M(SyntaxKind.IdentifierName);
{
M(SyntaxKind.IdentifierToken);
}
M(SyntaxKind.SemicolonToken);
}
N(SyntaxKind.ElseClause);
{
N(SyntaxKind.ElseKeyword);
N(SyntaxKind.Block);
{
N(SyntaxKind.OpenBraceToken);
N(SyntaxKind.CloseBraceToken);
}
}
}
EOF();
}
[WorkItem(27866, "https://github.com/dotnet/roslyn/issues/27866")]
[Fact]
public void ParseElseAndElseWithoutPrecedingIfStatement()
{
UsingStatement("{ else {} else {} }",
// (1,3): error CS8641: 'else' cannot start a statement.
// { else {} else {} }
Diagnostic(ErrorCode.ERR_ElseCannotStartStatement, "else").WithLocation(1, 3),
// (1,3): error CS1003: Syntax error, '(' expected
// { else {} else {} }
Diagnostic(ErrorCode.ERR_SyntaxError, "else").WithArguments("(", "else").WithLocation(1, 3),
// (1,3): error CS1525: Invalid expression term 'else'
// { else {} else {} }
Diagnostic(ErrorCode.ERR_InvalidExprTerm, "else").WithArguments("else").WithLocation(1, 3),
// (1,3): error CS1026: ) expected
// { else {} else {} }
Diagnostic(ErrorCode.ERR_CloseParenExpected, "else").WithLocation(1, 3),
// (1,3): error CS1525: Invalid expression term 'else'
// { else {} else {} }
Diagnostic(ErrorCode.ERR_InvalidExprTerm, "else").WithArguments("else").WithLocation(1, 3),
// (1,3): error CS1002: ; expected
// { else {} else {} }
Diagnostic(ErrorCode.ERR_SemicolonExpected, "else").WithLocation(1, 3),
// (1,11): error CS8641: 'else' cannot start a statement.
// { else {} else {} }
Diagnostic(ErrorCode.ERR_ElseCannotStartStatement, "else").WithLocation(1, 11),
// (1,11): error CS1003: Syntax error, '(' expected
// { else {} else {} }
Diagnostic(ErrorCode.ERR_SyntaxError, "else").WithArguments("(", "else").WithLocation(1, 11),
// (1,11): error CS1525: Invalid expression term 'else'
// { else {} else {} }
Diagnostic(ErrorCode.ERR_InvalidExprTerm, "else").WithArguments("else").WithLocation(1, 11),
// (1,11): error CS1026: ) expected
// { else {} else {} }
Diagnostic(ErrorCode.ERR_CloseParenExpected, "else").WithLocation(1, 11),
// (1,11): error CS1525: Invalid expression term 'else'
// { else {} else {} }
Diagnostic(ErrorCode.ERR_InvalidExprTerm, "else").WithArguments("else").WithLocation(1, 11),
// (1,11): error CS1002: ; expected
// { else {} else {} }
Diagnostic(ErrorCode.ERR_SemicolonExpected, "else").WithLocation(1, 11)
);
N(SyntaxKind.Block);
{
N(SyntaxKind.OpenBraceToken);
N(SyntaxKind.IfStatement);
{
M(SyntaxKind.IfKeyword);
M(SyntaxKind.OpenParenToken);
M(SyntaxKind.IdentifierName);
{
M(SyntaxKind.IdentifierToken);
}
M(SyntaxKind.CloseParenToken);
M(SyntaxKind.ExpressionStatement);
{
M(SyntaxKind.IdentifierName);
{
M(SyntaxKind.IdentifierToken);
}
M(SyntaxKind.SemicolonToken);
}
N(SyntaxKind.ElseClause);
{
N(SyntaxKind.ElseKeyword);
N(SyntaxKind.Block);
{
N(SyntaxKind.OpenBraceToken);
N(SyntaxKind.CloseBraceToken);
}
}
}
N(SyntaxKind.IfStatement);
{
M(SyntaxKind.IfKeyword);
M(SyntaxKind.OpenParenToken);
M(SyntaxKind.IdentifierName);
{
M(SyntaxKind.IdentifierToken);
}
M(SyntaxKind.CloseParenToken);
M(SyntaxKind.ExpressionStatement);
{
M(SyntaxKind.IdentifierName);
{
M(SyntaxKind.IdentifierToken);
}
M(SyntaxKind.SemicolonToken);
}
N(SyntaxKind.ElseClause);
{
N(SyntaxKind.ElseKeyword);
N(SyntaxKind.Block);
{
N(SyntaxKind.OpenBraceToken);
N(SyntaxKind.CloseBraceToken);
}
}
}
N(SyntaxKind.CloseBraceToken);
}
EOF();
}
[WorkItem(27866, "https://github.com/dotnet/roslyn/issues/27866")]
[Fact]
public void ParseSubsequentElseWithoutPrecedingIfStatement()
{
UsingStatement("{ if (a) { } else { } else { } }",
// (1,23): error CS8641: 'else' cannot start a statement.
// { if (a) { } else { } else { } }
Diagnostic(ErrorCode.ERR_ElseCannotStartStatement, "else").WithLocation(1, 23),
// (1,23): error CS1003: Syntax error, '(' expected
// { if (a) { } else { } else { } }
Diagnostic(ErrorCode.ERR_SyntaxError, "else").WithArguments("(", "else").WithLocation(1, 23),
// (1,23): error CS1525: Invalid expression term 'else'
// { if (a) { } else { } else { } }
Diagnostic(ErrorCode.ERR_InvalidExprTerm, "else").WithArguments("else").WithLocation(1, 23),
// (1,23): error CS1026: ) expected
// { if (a) { } else { } else { } }
Diagnostic(ErrorCode.ERR_CloseParenExpected, "else").WithLocation(1, 23),
// (1,23): error CS1525: Invalid expression term 'else'
// { if (a) { } else { } else { } }
Diagnostic(ErrorCode.ERR_InvalidExprTerm, "else").WithArguments("else").WithLocation(1, 23),
// (1,23): error CS1002: ; expected
// { if (a) { } else { } else { } }
Diagnostic(ErrorCode.ERR_SemicolonExpected, "else").WithLocation(1, 23)
);
N(SyntaxKind.Block);
{
N(SyntaxKind.OpenBraceToken);
N(SyntaxKind.IfStatement);
{
N(SyntaxKind.IfKeyword);
N(SyntaxKind.OpenParenToken);
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "a");
}
N(SyntaxKind.CloseParenToken);
N(SyntaxKind.Block);
{
N(SyntaxKind.OpenBraceToken);
N(SyntaxKind.CloseBraceToken);
}
N(SyntaxKind.ElseClause);
{
N(SyntaxKind.ElseKeyword);
N(SyntaxKind.Block);
{
N(SyntaxKind.OpenBraceToken);
N(SyntaxKind.CloseBraceToken);
}
}
}
N(SyntaxKind.IfStatement);
{
M(SyntaxKind.IfKeyword);
M(SyntaxKind.OpenParenToken);
M(SyntaxKind.IdentifierName);
{
M(SyntaxKind.IdentifierToken);
}
M(SyntaxKind.CloseParenToken);
M(SyntaxKind.ExpressionStatement);
{
M(SyntaxKind.IdentifierName);
{
M(SyntaxKind.IdentifierToken);
}
M(SyntaxKind.SemicolonToken);
}
N(SyntaxKind.ElseClause);
{
N(SyntaxKind.ElseKeyword);
N(SyntaxKind.Block);
{
N(SyntaxKind.OpenBraceToken);
N(SyntaxKind.CloseBraceToken);
}
}
}
N(SyntaxKind.CloseBraceToken);
}
EOF();
}
[WorkItem(27866, "https://github.com/dotnet/roslyn/issues/27866")]
[Fact]
public void ParseElseKeywordPlacedAsIfEmbeddedStatement()
{
UsingStatement("if (a) else {}",
// (1,8): error CS8641: 'else' cannot start a statement.
// if (a) else {}
Diagnostic(ErrorCode.ERR_ElseCannotStartStatement, "else").WithLocation(1, 8),
// (1,8): error CS1003: Syntax error, '(' expected
// if (a) else {}
Diagnostic(ErrorCode.ERR_SyntaxError, "else").WithArguments("(", "else").WithLocation(1, 8),
// (1,8): error CS1525: Invalid expression term 'else'
// if (a) else {}
Diagnostic(ErrorCode.ERR_InvalidExprTerm, "else").WithArguments("else").WithLocation(1, 8),
// (1,8): error CS1026: ) expected
// if (a) else {}
Diagnostic(ErrorCode.ERR_CloseParenExpected, "else").WithLocation(1, 8),
// (1,8): error CS1525: Invalid expression term 'else'
// if (a) else {}
Diagnostic(ErrorCode.ERR_InvalidExprTerm, "else").WithArguments("else").WithLocation(1, 8),
// (1,8): error CS1002: ; expected
// if (a) else {}
Diagnostic(ErrorCode.ERR_SemicolonExpected, "else").WithLocation(1, 8)
);
N(SyntaxKind.IfStatement);
{
N(SyntaxKind.IfKeyword);
N(SyntaxKind.OpenParenToken);
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "a");
}
N(SyntaxKind.CloseParenToken);
N(SyntaxKind.IfStatement);
{
M(SyntaxKind.IfKeyword);
M(SyntaxKind.OpenParenToken);
M(SyntaxKind.IdentifierName);
{
M(SyntaxKind.IdentifierToken);
}
M(SyntaxKind.CloseParenToken);
M(SyntaxKind.ExpressionStatement);
{
M(SyntaxKind.IdentifierName);
{
M(SyntaxKind.IdentifierToken);
}
M(SyntaxKind.SemicolonToken);
}
N(SyntaxKind.ElseClause);
{
N(SyntaxKind.ElseKeyword);
N(SyntaxKind.Block);
{
N(SyntaxKind.OpenBraceToken);
N(SyntaxKind.CloseBraceToken);
}
}
}
}
EOF();
}
private sealed class TokenAndTriviaWalker : CSharpSyntaxWalker
{
public int Tokens;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册