Browse Source

Fixed forum-8951: C# parser bug with "default(T)".

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/2.0@1500 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Daniel Grunwald 19 years ago
parent
commit
53c8c4d56c
  1. 1071
      src/Libraries/NRefactory/Project/Src/Parser/CSharp/Parser.cs
  2. 29
      src/Libraries/NRefactory/Project/Src/Parser/CSharp/cs.ATG
  3. 11
      src/Libraries/NRefactory/Test/Parser/Expressions/DefaultValueExpressionTests.cs

1071
src/Libraries/NRefactory/Project/Src/Parser/CSharp/Parser.cs

File diff suppressed because it is too large Load Diff

29
src/Libraries/NRefactory/Project/Src/Parser/CSharp/cs.ATG

@ -1715,8 +1715,6 @@ VariableInitializer<out Expression initializerExpression> @@ -1715,8 +1715,6 @@ VariableInitializer<out Expression initializerExpression>
Expr<out initializerExpression>
| ArrayInitializer<out initializerExpression>
| "stackalloc" Type<out type> "[" Expr<out expr> "]" (. initializerExpression = new StackAllocExpression(type, expr); .)
| /* workaround for coco bug? doesn't work in Expr production in this case. */
"default" "(" Type<out type> ")" (. initializerExpression = new DefaultValueExpression(type); .)
.
OverloadableOperator<out OverloadableOperatorType op>
@ -1890,7 +1888,7 @@ EmbeddedStatement<out Statement statement> @@ -1890,7 +1888,7 @@ EmbeddedStatement<out Statement statement>
} .)
| "switch" (. List<SwitchSection> switchSections = new List<SwitchSection>(); SwitchSection switchSection; .)
"(" Expr<out expr> ")"
"{" { SwitchSection<out switchSection> (. switchSections.Add(switchSection); .) }
"{" SwitchSections<switchSections>
"}" (. statement = new SwitchStatement(expr, switchSections); .)
/*--- iteration statements (while, do, for, foreach): */
| "while" "(" Expr<out expr> ")"
@ -1964,22 +1962,29 @@ ForIterator<out List<Statement> iterator> @@ -1964,22 +1962,29 @@ ForIterator<out List<Statement> iterator>
StatementExpr<out stmt> (. iterator.Add(stmt);.) { "," StatementExpr<out stmt> (. iterator.Add(stmt); .) }
.
SwitchSection<out SwitchSection stmt>
SwitchSections<List<SwitchSection> switchSections>
(.
SwitchSection switchSection = new SwitchSection();
CaseLabel label;
.)
=
SwitchLabel<out label> (. if (label != null) { switchSection.SwitchLabels.Add(label); } .)
(. compilationUnit.BlockStart(switchSection); .)
{
SwitchLabel<out label> (. if (label != null) { switchSection.SwitchLabels.Add(label); } .)
( SwitchLabel<out label>
(. if (label != null) {
if (switchSection.Children.Count > 0) {
// open new section
compilationUnit.BlockEnd(); switchSections.Add(switchSection);
switchSection = new SwitchSection();
compilationUnit.BlockStart(switchSection);
}
switchSection.SwitchLabels.Add(label);
}
(. compilationUnit.BlockStart(switchSection); .)
Statement { Statement }
(.
compilationUnit.BlockEnd();
stmt = switchSection;
.)
| Statement)
}
(. compilationUnit.BlockEnd(); switchSections.Add(switchSection); .)
.
SwitchLabel<out CaseLabel label>
@ -2218,9 +2223,7 @@ PrimaryExpr<out Expression pexpr> @@ -2218,9 +2223,7 @@ PrimaryExpr<out Expression pexpr>
| TypeWithRestriction<out type, true, true>
) ")" (. pexpr = new TypeOfExpression(type); .)
| IF (la.kind == Tokens.Default && Peek(1).kind == Tokens.OpenParenthesis)
/* possible conflict with switch's default */
"default" "(" Type<out type> ")" (. pexpr = new DefaultValueExpression(type); .)
| "default" "(" Type<out type> ")" (. pexpr = new DefaultValueExpression(type); .)
| "sizeof" "(" Type<out type> ")" (. pexpr = new SizeOfExpression(type); .)
| "checked" "(" Expr<out expr> ")" (. pexpr = new CheckedExpression(expr); .)
| "unchecked" "(" Expr<out expr> ")" (. pexpr = new UncheckedExpression(expr); .)

11
src/Libraries/NRefactory/Test/Parser/Expressions/DefaultValueExpressionTests.cs

@ -26,7 +26,8 @@ namespace ICSharpCode.NRefactory.Tests.AST @@ -26,7 +26,8 @@ namespace ICSharpCode.NRefactory.Tests.AST
[Test]
public void CSharpFullQualifiedDefaultValue()
{
DefaultValueExpression toe = ParseUtilCSharp.ParseExpression<DefaultValueExpression>("default(MyNamespace.N1.MyType)");
DefaultValueExpression toe = ParseUtilCSharp.ParseExpression<DefaultValueExpression>("default(global::MyNamespace.N1.MyType)");
Assert.IsTrue(toe.TypeReference.IsGlobal);
Assert.AreEqual("MyNamespace.N1.MyType", toe.TypeReference.Type);
}
@ -46,5 +47,13 @@ namespace ICSharpCode.NRefactory.Tests.AST @@ -46,5 +47,13 @@ namespace ICSharpCode.NRefactory.Tests.AST
DefaultValueExpression dve = (DefaultValueExpression)lvd.Variables[0].Initializer;
Assert.AreEqual("T", dve.TypeReference.Type);
}
[Test]
public void CSharpDefaultValueInReturnStatement()
{
ReturnStatement rs = ParseUtilCSharp.ParseStatement<ReturnStatement>("return default(T);");
DefaultValueExpression dve = (DefaultValueExpression)rs.Expression;
Assert.AreEqual("T", dve.TypeReference.Type);
}
}
}

Loading…
Cancel
Save