Browse Source

Fixed C# parser bug (casts to nullables).

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/2.0@1141 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Daniel Grunwald 20 years ago
parent
commit
2a2745948d
  1. 2060
      src/Libraries/NRefactory/Project/Src/Parser/CSharp/Parser.cs
  2. 6
      src/Libraries/NRefactory/Project/Src/Parser/CSharp/cs.ATG
  3. 20
      src/Libraries/NRefactory/Test/Parser/Expressions/CastExpressionTests.cs
  4. 10
      src/Libraries/NRefactory/Test/Parser/Expressions/ConditionalExpressionTests.cs
  5. 20
      src/Libraries/NRefactory/Test/Parser/Expressions/TypeOfIsExpressionTests.cs

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

File diff suppressed because it is too large Load Diff

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

@ -72,6 +72,8 @@ bool IsSimpleTypeCast ()
if (!IsTypeKWForTypeCast(ref pt)) { if (!IsTypeKWForTypeCast(ref pt)) {
return false; return false;
} }
if (pt.kind == Tokens.Question)
pt = lexer.Peek();
return pt.kind == Tokens.CloseParenthesis; return pt.kind == Tokens.CloseParenthesis;
} }
@ -2344,9 +2346,13 @@ RelationalExpr<ref Expression outExpr>
| |
( "is" ( "is"
TypeWithRestriction<out type, false, false> TypeWithRestriction<out type, false, false>
[ IF (la.kind == Tokens.Question && Tokens.CastFollower[Peek(1).kind] == false)
NullableQuestionMark<ref type> ]
(. outExpr = new TypeOfIsExpression(outExpr, type); .) (. outExpr = new TypeOfIsExpression(outExpr, type); .)
| "as" | "as"
TypeWithRestriction<out type, false, false> TypeWithRestriction<out type, false, false>
[ IF (la.kind == Tokens.Question && Tokens.CastFollower[Peek(1).kind] == false)
NullableQuestionMark<ref type> ]
(. outExpr = new CastExpression(type, outExpr, CastType.TryCast); .) (. outExpr = new CastExpression(type, outExpr, CastType.TryCast); .)
) )
} }

20
src/Libraries/NRefactory/Test/Parser/Expressions/CastExpressionTests.cs

@ -36,6 +36,26 @@ namespace ICSharpCode.NRefactory.Tests.AST
Assert.AreEqual(CastType.Cast, ce.CastType); Assert.AreEqual(CastType.Cast, ce.CastType);
} }
[Test]
public void NullableCastExpression()
{
CastExpression ce = ParseUtilCSharp.ParseExpression<CastExpression>("(int?)o");
Assert.AreEqual("System.Nullable", ce.CastTo.SystemType);
Assert.AreEqual("int", ce.CastTo.GenericTypes[0].Type);
Assert.IsTrue(ce.Expression is IdentifierExpression);
Assert.AreEqual(CastType.Cast, ce.CastType);
}
[Test]
public void NullableTryCastExpression()
{
CastExpression ce = ParseUtilCSharp.ParseExpression<CastExpression>("o as int?");
Assert.AreEqual("System.Nullable", ce.CastTo.SystemType);
Assert.AreEqual("int", ce.CastTo.GenericTypes[0].Type);
Assert.IsTrue(ce.Expression is IdentifierExpression);
Assert.AreEqual(CastType.TryCast, ce.CastType);
}
[Test] [Test]
public void GenericCastExpression() public void GenericCastExpression()
{ {

10
src/Libraries/NRefactory/Test/Parser/Expressions/ConditionalExpressionTests.cs

@ -37,6 +37,16 @@ namespace ICSharpCode.NRefactory.Tests.AST
Assert.IsTrue(ce.TrueExpression is InvocationExpression); Assert.IsTrue(ce.TrueExpression is InvocationExpression);
Assert.IsTrue(ce.FalseExpression is FieldReferenceExpression); Assert.IsTrue(ce.FalseExpression is FieldReferenceExpression);
} }
[Test]
public void CSharpConditionalIsExpressionTest2()
{
ConditionalExpression ce = ParseUtilCSharp.ParseExpression<ConditionalExpression>("a is b ? (a()) : a.B");
Assert.IsTrue(ce.Condition is TypeOfIsExpression);
Assert.IsTrue(ce.TrueExpression is ParenthesizedExpression);
Assert.IsTrue(ce.FalseExpression is FieldReferenceExpression);
}
#endregion #endregion
#region VB.NET #region VB.NET

20
src/Libraries/NRefactory/Test/Parser/Expressions/TypeOfIsExpressionTests.cs

@ -26,6 +26,26 @@ namespace ICSharpCode.NRefactory.Tests.AST
Assert.AreEqual(new int[] { 0 }, ce.TypeReference.RankSpecifier); Assert.AreEqual(new int[] { 0 }, ce.TypeReference.RankSpecifier);
Assert.IsTrue(ce.Expression is IdentifierExpression); Assert.IsTrue(ce.Expression is IdentifierExpression);
} }
[Test]
public void NullableIsExpression()
{
TypeOfIsExpression ce = ParseUtilCSharp.ParseExpression<TypeOfIsExpression>("o is int?");
Assert.AreEqual("System.Nullable", ce.TypeReference.SystemType);
Assert.AreEqual("int", ce.TypeReference.GenericTypes[0].Type);
Assert.IsTrue(ce.Expression is IdentifierExpression);
}
[Test]
public void NullableIsExpressionInBinaryOperatorExpression()
{
BinaryOperatorExpression boe;
boe = ParseUtilCSharp.ParseExpression<BinaryOperatorExpression>("o is int? == true");
TypeOfIsExpression ce = (TypeOfIsExpression)boe.Left;
Assert.AreEqual("System.Nullable", ce.TypeReference.SystemType);
Assert.AreEqual("int", ce.TypeReference.GenericTypes[0].Type);
Assert.IsTrue(ce.Expression is IdentifierExpression);
}
#endregion #endregion
#region VB.NET #region VB.NET

Loading…
Cancel
Save