Browse Source

Fixed C# parser bug that occurs when the first parameter of an extension method is decorated with an attribute.

Closes icsharpcode/SharpDevelop#19.
pull/18/head
Daniel Grunwald 14 years ago
parent
commit
4bbddfd8b6
  1. 3
      src/Libraries/NRefactory/Project/Src/Ast/Enums.cs
  2. 2007
      src/Libraries/NRefactory/Project/Src/Parser/CSharp/Parser.cs
  3. 8
      src/Libraries/NRefactory/Project/Src/Parser/CSharp/cs.ATG
  4. 13
      src/Libraries/NRefactory/Test/Parser/TypeLevel/MethodDeclarationTests.cs
  5. 3
      src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/Interfaces/ParameterModifiers.cs

3
src/Libraries/NRefactory/Project/Src/Ast/Enums.cs

@ -115,7 +115,8 @@ namespace ICSharpCode.NRefactory.Ast @@ -115,7 +115,8 @@ namespace ICSharpCode.NRefactory.Ast
Out = 2,
Ref = 4,
Params = 8,
Optional = 16
Optional = 16,
This = 32
}
public enum VarianceModifier

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

File diff suppressed because it is too large Load Diff

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

@ -675,6 +675,7 @@ FixedParameter<out ParameterDeclarationExpression p> @@ -675,6 +675,7 @@ FixedParameter<out ParameterDeclarationExpression p>
"ref" (. mod = ParameterModifiers.Ref; .)
| "out" (. mod = ParameterModifiers.Out; .)
| "params" (. mod = ParameterModifiers.Params; .)
| "this" (. mod = ParameterModifiers.This; .)
]
Type<out type>
Identifier (. p = new ParameterDeclarationExpression(type, t.val, mod); .)
@ -759,7 +760,6 @@ StructMemberDecl<ModifierList m, List<AttributeSection> attributes> @@ -759,7 +760,6 @@ StructMemberDecl<ModifierList m, List<AttributeSection> attributes>
BlockStatement stmt = null;
List<TemplateDefinition> templates = new List<TemplateDefinition>();
TypeReference explicitInterface = null;
bool isExtensionMethod = false;
.)
=
/*--- constant declaration: */ (. m.Check(Modifiers.Constants); .)
@ -795,7 +795,6 @@ StructMemberDecl<ModifierList m, List<AttributeSection> attributes> @@ -795,7 +795,6 @@ StructMemberDecl<ModifierList m, List<AttributeSection> attributes>
[ TypeParameterList<templates> ]
"("
[ "this" (. isExtensionMethod = true; /* C# 3.0 */ .) ]
[ FormalParameterList<p> ] ")"
(. MethodDeclaration methodDeclaration = new MethodDeclaration {
Name = qualident,
@ -806,7 +805,7 @@ StructMemberDecl<ModifierList m, List<AttributeSection> attributes> @@ -806,7 +805,7 @@ StructMemberDecl<ModifierList m, List<AttributeSection> attributes>
StartLocation = m.GetDeclarationLocation(startPos),
EndLocation = t.EndLocation,
Templates = templates,
IsExtensionMethod = isExtensionMethod
IsExtensionMethod = p.Count > 0 && p[0].ParamModifier == ParameterModifiers.This
};
if (explicitInterface != null)
SafeAdd(methodDeclaration, methodDeclaration.InterfaceImplementations, new InterfaceImplementation(explicitInterface, qualident));
@ -1017,7 +1016,6 @@ StructMemberDecl<ModifierList m, List<AttributeSection> attributes> @@ -1017,7 +1016,6 @@ StructMemberDecl<ModifierList m, List<AttributeSection> attributes>
/* .NET 2.0 */
[ TypeParameterList<templates> ]
"("
[ "this" (. isExtensionMethod = true; .) ]
[ FormalParameterList<p> ] ")"
(.
MethodDeclaration methodDeclaration = new MethodDeclaration {
@ -1031,7 +1029,7 @@ StructMemberDecl<ModifierList m, List<AttributeSection> attributes> @@ -1031,7 +1029,7 @@ StructMemberDecl<ModifierList m, List<AttributeSection> attributes>
methodDeclaration.InterfaceImplementations.Add(new InterfaceImplementation(explicitInterface, qualident));
methodDeclaration.StartLocation = m.GetDeclarationLocation(startPos);
methodDeclaration.EndLocation = t.EndLocation;
methodDeclaration.IsExtensionMethod = isExtensionMethod;
methodDeclaration.IsExtensionMethod = p.Count > 0 && p[0].ParamModifier == ParameterModifiers.This;
methodDeclaration.Templates = templates;
AddChild(methodDeclaration);
.)

13
src/Libraries/NRefactory/Test/Parser/TypeLevel/MethodDeclarationTests.cs

@ -293,6 +293,19 @@ namespace ICSharpCode.NRefactory.Tests.Ast @@ -293,6 +293,19 @@ namespace ICSharpCode.NRefactory.Tests.Ast
Assert.AreEqual("System.String", md.Parameters[0].TypeReference.Type);
}
[Test]
public void CSharpExtensionMethodWithAttributeTest()
{
MethodDeclaration md = ParseUtilCSharp.ParseTypeMember<MethodDeclaration>(
"public static int ToInt32([Attr] this string s) { return int.Parse(s); }"
);
Assert.AreEqual("ToInt32", md.Name);
Assert.IsTrue(md.IsExtensionMethod);
Assert.AreEqual("s", md.Parameters[0].ParameterName);
Assert.AreEqual("System.String", md.Parameters[0].TypeReference.Type);
Assert.AreEqual(1, md.Parameters[0].Attributes.Count);
}
[Test]
public void CSharpVoidExtensionMethodTest()
{

3
src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/Interfaces/ParameterModifiers.cs

@ -15,6 +15,7 @@ namespace ICSharpCode.SharpDevelop.Dom @@ -15,6 +15,7 @@ namespace ICSharpCode.SharpDevelop.Dom
Out = 2,
Ref = 4,
Params = 8,
Optional = 16
Optional = 16,
This = 32
}
}

Loading…
Cancel
Save