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. 1079
      src/Libraries/NRefactory/Project/Src/Parser/CSharp/Parser.cs
  2. 31
      src/Libraries/NRefactory/Project/Src/Parser/CSharp/cs.ATG
  3. 11
      src/Libraries/NRefactory/Test/Parser/Expressions/DefaultValueExpressionTests.cs

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

File diff suppressed because it is too large Load Diff

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

@ -1715,8 +1715,6 @@ VariableInitializer<out Expression initializerExpression>
Expr<out initializerExpression> Expr<out initializerExpression>
| ArrayInitializer<out initializerExpression> | ArrayInitializer<out initializerExpression>
| "stackalloc" Type<out type> "[" Expr<out expr> "]" (. initializerExpression = new StackAllocExpression(type, expr); .) | "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> OverloadableOperator<out OverloadableOperatorType op>
@ -1890,7 +1888,7 @@ EmbeddedStatement<out Statement statement>
} .) } .)
| "switch" (. List<SwitchSection> switchSections = new List<SwitchSection>(); SwitchSection switchSection; .) | "switch" (. List<SwitchSection> switchSections = new List<SwitchSection>(); SwitchSection switchSection; .)
"(" Expr<out expr> ")" "(" Expr<out expr> ")"
"{" { SwitchSection<out switchSection> (. switchSections.Add(switchSection); .) } "{" SwitchSections<switchSections>
"}" (. statement = new SwitchStatement(expr, switchSections); .) "}" (. statement = new SwitchStatement(expr, switchSections); .)
/*--- iteration statements (while, do, for, foreach): */ /*--- iteration statements (while, do, for, foreach): */
| "while" "(" Expr<out expr> ")" | "while" "(" Expr<out expr> ")"
@ -1964,22 +1962,29 @@ ForIterator<out List<Statement> iterator>
StatementExpr<out stmt> (. iterator.Add(stmt);.) { "," StatementExpr<out stmt> (. iterator.Add(stmt); .) } StatementExpr<out stmt> (. iterator.Add(stmt);.) { "," StatementExpr<out stmt> (. iterator.Add(stmt); .) }
. .
SwitchSection<out SwitchSection stmt> SwitchSections<List<SwitchSection> switchSections>
(. (.
SwitchSection switchSection = new SwitchSection(); SwitchSection switchSection = new SwitchSection();
CaseLabel label; CaseLabel label;
.) .)
= =
SwitchLabel<out label> (. if (label != null) { switchSection.SwitchLabels.Add(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);
}
.)
| Statement)
} }
(. compilationUnit.BlockStart(switchSection); .) (. compilationUnit.BlockEnd(); switchSections.Add(switchSection); .)
Statement { Statement }
(.
compilationUnit.BlockEnd();
stmt = switchSection;
.)
. .
SwitchLabel<out CaseLabel label> SwitchLabel<out CaseLabel label>
@ -2218,9 +2223,7 @@ PrimaryExpr<out Expression pexpr>
| TypeWithRestriction<out type, true, true> | TypeWithRestriction<out type, true, true>
) ")" (. pexpr = new TypeOfExpression(type); .) ) ")" (. pexpr = new TypeOfExpression(type); .)
| IF (la.kind == Tokens.Default && Peek(1).kind == Tokens.OpenParenthesis) | "default" "(" Type<out type> ")" (. pexpr = new DefaultValueExpression(type); .)
/* possible conflict with switch's default */
"default" "(" Type<out type> ")" (. pexpr = new DefaultValueExpression(type); .)
| "sizeof" "(" Type<out type> ")" (. pexpr = new SizeOfExpression(type); .) | "sizeof" "(" Type<out type> ")" (. pexpr = new SizeOfExpression(type); .)
| "checked" "(" Expr<out expr> ")" (. pexpr = new CheckedExpression(expr); .) | "checked" "(" Expr<out expr> ")" (. pexpr = new CheckedExpression(expr); .)
| "unchecked" "(" Expr<out expr> ")" (. pexpr = new UncheckedExpression(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
[Test] [Test]
public void CSharpFullQualifiedDefaultValue() 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); Assert.AreEqual("MyNamespace.N1.MyType", toe.TypeReference.Type);
} }
@ -46,5 +47,13 @@ namespace ICSharpCode.NRefactory.Tests.AST
DefaultValueExpression dve = (DefaultValueExpression)lvd.Variables[0].Initializer; DefaultValueExpression dve = (DefaultValueExpression)lvd.Variables[0].Initializer;
Assert.AreEqual("T", dve.TypeReference.Type); 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