Browse Source

Fixed SD2-1166: C# parser: Initializer in event declaration causes parser error

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@1899 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Daniel Grunwald 20 years ago
parent
commit
d9dd2ffd90
  1. 1
      src/Libraries/NRefactory/NRefactoryASTGenerator/AST/Expressions.cs
  2. 3
      src/Libraries/NRefactory/NRefactoryASTGenerator/AST/TypeLevel.cs
  3. 31
      src/Libraries/NRefactory/Project/Src/Ast/Generated.cs
  4. 2
      src/Libraries/NRefactory/Project/Src/IAstVisitor.cs
  5. 1494
      src/Libraries/NRefactory/Project/Src/Parser/CSharp/Parser.cs
  6. 2
      src/Libraries/NRefactory/Project/Src/Parser/CSharp/cs.ATG
  7. 67
      src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/CSharpOutputVisitor.cs
  8. 8
      src/Libraries/NRefactory/Project/Src/PrettyPrinter/VBNet/VBNetOutputVisitor.cs
  9. 6
      src/Libraries/NRefactory/Project/Src/Visitors/AbstractASTVisitor.cs
  10. 6
      src/Libraries/NRefactory/Project/Src/Visitors/AbstractAstTransformer.cs
  11. 13
      src/Libraries/NRefactory/Test/Output/CSharp/CSharpOutputTest.cs
  12. 2
      src/Libraries/NRefactory/Test/Output/CSharp/VBToCSharpConverterTest.cs
  13. 2
      src/Libraries/NRefactory/Test/Parser/Expressions/AnonymousMethodTests.cs

1
src/Libraries/NRefactory/NRefactoryASTGenerator/AST/Expressions.cs

@ -156,6 +156,7 @@ namespace NRefactoryASTGenerator.Ast
class AnonymousMethodExpression : Expression { class AnonymousMethodExpression : Expression {
List<ParameterDeclarationExpression> parameters; List<ParameterDeclarationExpression> parameters;
BlockStatement body; BlockStatement body;
bool hasParameterList;
} }
class CheckedExpression : Expression { class CheckedExpression : Expression {

3
src/Libraries/NRefactory/NRefactoryASTGenerator/AST/TypeLevel.cs

@ -91,13 +91,14 @@ namespace NRefactoryASTGenerator.Ast
[IncludeBoolProperty("HasRaiseRegion", "return !raiseRegion.IsNull;")] [IncludeBoolProperty("HasRaiseRegion", "return !raiseRegion.IsNull;")]
class EventDeclaration : ParametrizedNode class EventDeclaration : ParametrizedNode
{ {
TypeReference typeReference; TypeReference typeReference;
List<InterfaceImplementation> interfaceImplementations; List<InterfaceImplementation> interfaceImplementations;
EventAddRegion addRegion; EventAddRegion addRegion;
EventRemoveRegion removeRegion; EventRemoveRegion removeRegion;
EventRaiseRegion raiseRegion; EventRaiseRegion raiseRegion;
Location bodyStart; Location bodyStart;
Location bodyEnd; Location bodyEnd;
Expression initializer;
public EventDeclaration(TypeReference typeReference, string name, Modifiers modifier, List<AttributeSection> attributes, List<ParameterDeclarationExpression> parameters) public EventDeclaration(TypeReference typeReference, string name, Modifiers modifier, List<AttributeSection> attributes, List<ParameterDeclarationExpression> parameters)
: base(modifier, attributes, name, parameters) : base(modifier, attributes, name, parameters)

31
src/Libraries/NRefactory/Project/Src/Ast/Generated.cs

@ -87,6 +87,8 @@ namespace ICSharpCode.NRefactory.Ast {
BlockStatement body; BlockStatement body;
bool hasParameterList;
public List<ParameterDeclarationExpression> Parameters { public List<ParameterDeclarationExpression> Parameters {
get { get {
return parameters; return parameters;
@ -106,6 +108,15 @@ namespace ICSharpCode.NRefactory.Ast {
} }
} }
public bool HasParameterList {
get {
return hasParameterList;
}
set {
hasParameterList = value;
}
}
public AnonymousMethodExpression() { public AnonymousMethodExpression() {
parameters = new List<ParameterDeclarationExpression>(); parameters = new List<ParameterDeclarationExpression>();
body = BlockStatement.Null; body = BlockStatement.Null;
@ -116,7 +127,7 @@ namespace ICSharpCode.NRefactory.Ast {
} }
public override string ToString() { public override string ToString() {
return string.Format("[AnonymousMethodExpression Parameters={0} Body={1}]", GetCollectionString(Parameters), Body); return string.Format("[AnonymousMethodExpression Parameters={0} Body={1} HasParameterList={2}]", GetCollectionString(Parameters), Body, HasParameterList);
} }
} }
@ -1488,6 +1499,8 @@ namespace ICSharpCode.NRefactory.Ast {
Location bodyEnd; Location bodyEnd;
Expression initializer;
public TypeReference TypeReference { public TypeReference TypeReference {
get { get {
return typeReference; return typeReference;
@ -1551,6 +1564,16 @@ namespace ICSharpCode.NRefactory.Ast {
} }
} }
public Expression Initializer {
get {
return initializer;
}
set {
initializer = value ?? Expression.Null;
if (!initializer.IsNull) initializer.Parent = this;
}
}
public EventDeclaration(TypeReference typeReference, string name, Modifiers modifier, List<AttributeSection> attributes, List<ParameterDeclarationExpression> parameters) : public EventDeclaration(TypeReference typeReference, string name, Modifiers modifier, List<AttributeSection> attributes, List<ParameterDeclarationExpression> parameters) :
base(modifier, attributes, name, parameters) { base(modifier, attributes, name, parameters) {
TypeReference = typeReference; TypeReference = typeReference;
@ -1560,6 +1583,7 @@ namespace ICSharpCode.NRefactory.Ast {
raiseRegion = EventRaiseRegion.Null; raiseRegion = EventRaiseRegion.Null;
bodyStart = Location.Empty; bodyStart = Location.Empty;
bodyEnd = Location.Empty; bodyEnd = Location.Empty;
initializer = Expression.Null;
} }
public EventDeclaration(TypeReference typeReference, Modifiers modifier, List<ParameterDeclarationExpression> parameters, List<AttributeSection> attributes, string name, List<InterfaceImplementation> interfaceImplementations) : public EventDeclaration(TypeReference typeReference, Modifiers modifier, List<ParameterDeclarationExpression> parameters, List<AttributeSection> attributes, string name, List<InterfaceImplementation> interfaceImplementations) :
@ -1571,6 +1595,7 @@ namespace ICSharpCode.NRefactory.Ast {
raiseRegion = EventRaiseRegion.Null; raiseRegion = EventRaiseRegion.Null;
bodyStart = Location.Empty; bodyStart = Location.Empty;
bodyEnd = Location.Empty; bodyEnd = Location.Empty;
initializer = Expression.Null;
} }
public bool HasAddRegion { public bool HasAddRegion {
@ -1597,8 +1622,8 @@ namespace ICSharpCode.NRefactory.Ast {
public override string ToString() { public override string ToString() {
return string.Format("[EventDeclaration TypeReference={0} InterfaceImplementations={1} AddRegion={2} Re" + return string.Format("[EventDeclaration TypeReference={0} InterfaceImplementations={1} AddRegion={2} Re" +
"moveRegion={3} RaiseRegion={4} BodyStart={5} BodyEnd={6} Name={7} Parameters={8}" + "moveRegion={3} RaiseRegion={4} BodyStart={5} BodyEnd={6} Initializer={7} Name={8" +
" Attributes={9} Modifier={10}]", TypeReference, GetCollectionString(InterfaceImplementations), AddRegion, RemoveRegion, RaiseRegion, BodyStart, BodyEnd, Name, GetCollectionString(Parameters), GetCollectionString(Attributes), Modifier); "} Parameters={9} Attributes={10} Modifier={11}]", TypeReference, GetCollectionString(InterfaceImplementations), AddRegion, RemoveRegion, RaiseRegion, BodyStart, BodyEnd, Initializer, Name, GetCollectionString(Parameters), GetCollectionString(Attributes), Modifier);
} }
} }

2
src/Libraries/NRefactory/Project/Src/IAstVisitor.cs

@ -1,7 +1,7 @@
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// <auto-generated> // <auto-generated>
// This code was generated by a tool. // This code was generated by a tool.
// Runtime Version:2.0.50727.88 // Runtime Version:2.0.50727.42
// //
// Changes to this file may cause incorrect behavior and will be lost if // Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated. // the code is regenerated.

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

File diff suppressed because it is too large Load Diff

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

@ -775,6 +775,7 @@ StructMemberDecl<ModifierList m, List<AttributeSection> attributes>
| ident (. qualident = t.val; .) | ident (. qualident = t.val; .)
) )
(. eventDecl.Name = qualident; eventDecl.EndLocation = t.EndLocation; .) (. eventDecl.Name = qualident; eventDecl.EndLocation = t.EndLocation; .)
[ "=" Expr<out expr> (. eventDecl.Initializer = expr; .) ]
[ "{" (. eventDecl.BodyStart = t.Location; .) [ "{" (. eventDecl.BodyStart = t.Location; .)
EventAccessorDecls<out addBlock, out removeBlock> EventAccessorDecls<out addBlock, out removeBlock>
"}" (. eventDecl.BodyEnd = t.EndLocation; .) "}" (. eventDecl.BodyEnd = t.EndLocation; .)
@ -1809,6 +1810,7 @@ AnonymousMethodExpr<out Expression outExpr>
"(" "("
[ FormalParameterList<p> (. expr.Parameters = p; .) ] [ FormalParameterList<p> (. expr.Parameters = p; .) ]
")" ")"
(. expr.HasParameterList = true; .)
] ]
/*--- ParseExpression doesn't set a compilation unit, */ /*--- ParseExpression doesn't set a compilation unit, */
/*--- so we can't use block then -> skip body of anonymous method */ /*--- so we can't use block then -> skip body of anonymous method */

67
src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/CSharpOutputVisitor.cs

@ -559,7 +559,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
outputFormatter.Indent(); outputFormatter.Indent();
OutputModifier(propertyGetRegion.Modifier); OutputModifier(propertyGetRegion.Modifier);
outputFormatter.PrintText("get"); outputFormatter.PrintText("get");
OutputBlock(propertyGetRegion.Block, prettyPrintOptions.PropertyGetBraceStyle); OutputBlockAllowInline(propertyGetRegion.Block, prettyPrintOptions.PropertyGetBraceStyle);
return null; return null;
} }
@ -569,7 +569,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
outputFormatter.Indent(); outputFormatter.Indent();
OutputModifier(propertySetRegion.Modifier); OutputModifier(propertySetRegion.Modifier);
outputFormatter.PrintText("set"); outputFormatter.PrintText("set");
OutputBlock(propertySetRegion.Block, prettyPrintOptions.PropertySetBraceStyle); OutputBlockAllowInline(propertySetRegion.Block, prettyPrintOptions.PropertySetBraceStyle);
return null; return null;
} }
@ -589,6 +589,14 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
} }
outputFormatter.PrintIdentifier(eventDeclaration.Name); outputFormatter.PrintIdentifier(eventDeclaration.Name);
if (!eventDeclaration.Initializer.IsNull) {
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.Assign);
outputFormatter.Space();
nodeTracker.TrackedVisit(eventDeclaration.Initializer, data);
}
if (eventDeclaration.AddRegion.IsNull && eventDeclaration.RemoveRegion.IsNull) { if (eventDeclaration.AddRegion.IsNull && eventDeclaration.RemoveRegion.IsNull) {
outputFormatter.PrintToken(Tokens.Semicolon); outputFormatter.PrintToken(Tokens.Semicolon);
outputFormatter.NewLine(); outputFormatter.NewLine();
@ -606,7 +614,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
VisitAttributes(eventAddRegion.Attributes, data); VisitAttributes(eventAddRegion.Attributes, data);
outputFormatter.Indent(); outputFormatter.Indent();
outputFormatter.PrintText("add"); outputFormatter.PrintText("add");
OutputBlock(eventAddRegion.Block, prettyPrintOptions.EventAddBraceStyle); OutputBlockAllowInline(eventAddRegion.Block, prettyPrintOptions.EventAddBraceStyle);
return null; return null;
} }
@ -615,7 +623,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
VisitAttributes(eventRemoveRegion.Attributes, data); VisitAttributes(eventRemoveRegion.Attributes, data);
outputFormatter.Indent(); outputFormatter.Indent();
outputFormatter.PrintText("remove"); outputFormatter.PrintText("remove");
OutputBlock(eventRemoveRegion.Block, prettyPrintOptions.EventRemoveBraceStyle); OutputBlockAllowInline(eventRemoveRegion.Block, prettyPrintOptions.EventRemoveBraceStyle);
return null; return null;
} }
@ -906,6 +914,46 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
nodeTracker.EndNode(blockStatement); nodeTracker.EndNode(blockStatement);
} }
void OutputBlockAllowInline(BlockStatement blockStatement, BraceStyle braceStyle)
{
OutputBlockAllowInline(blockStatement, braceStyle, true);
}
void OutputBlockAllowInline(BlockStatement blockStatement, BraceStyle braceStyle, bool useNewLine)
{
if (!blockStatement.IsNull
&& (
blockStatement.Children.Count == 0
|| blockStatement.Children.Count == 1
&& (blockStatement.Children[0] is ExpressionStatement
|| blockStatement.Children[0] is ReturnStatement
)))
{
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.OpenCurlyBrace);
outputFormatter.Space();
if (blockStatement.Children.Count != 0) {
bool doIndent = outputFormatter.DoIndent;
bool doNewLine = outputFormatter.DoNewLine;
outputFormatter.DoIndent = false;
outputFormatter.DoNewLine = false;
nodeTracker.TrackedVisit(blockStatement.Children[0], null);
outputFormatter.DoIndent = doIndent;
outputFormatter.DoNewLine = doNewLine;
outputFormatter.Space();
}
outputFormatter.PrintToken(Tokens.CloseCurlyBrace);
if (useNewLine) {
outputFormatter.NewLine();
}
} else {
OutputBlock(blockStatement, braceStyle);
}
}
public object VisitBlockStatement(BlockStatement blockStatement, object data) public object VisitBlockStatement(BlockStatement blockStatement, object data)
{ {
if (data is BraceStyle) if (data is BraceStyle)
@ -2149,11 +2197,12 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
{ {
outputFormatter.PrintToken(Tokens.Delegate); outputFormatter.PrintToken(Tokens.Delegate);
outputFormatter.PrintToken(Tokens.OpenParenthesis); if (anonymousMethodExpression.Parameters.Count > 0 || anonymousMethodExpression.HasParameterList) {
AppendCommaSeparatedList(anonymousMethodExpression.Parameters); outputFormatter.PrintToken(Tokens.OpenParenthesis);
outputFormatter.PrintToken(Tokens.CloseParenthesis); AppendCommaSeparatedList(anonymousMethodExpression.Parameters);
OutputBlock(anonymousMethodExpression.Body, this.prettyPrintOptions.MethodBraceStyle); outputFormatter.PrintToken(Tokens.CloseParenthesis);
}
OutputBlockAllowInline(anonymousMethodExpression.Body, this.prettyPrintOptions.MethodBraceStyle, false);
return null; return null;
} }

8
src/Libraries/NRefactory/Project/Src/PrettyPrinter/VBNet/VBNetOutputVisitor.cs

@ -672,6 +672,14 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
nodeTracker.TrackedVisit(eventDeclaration.TypeReference, data); nodeTracker.TrackedVisit(eventDeclaration.TypeReference, data);
PrintInterfaceImplementations(eventDeclaration.InterfaceImplementations); PrintInterfaceImplementations(eventDeclaration.InterfaceImplementations);
if (!eventDeclaration.Initializer.IsNull) {
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.Assign);
outputFormatter.Space();
nodeTracker.TrackedVisit(eventDeclaration.Initializer, data);
}
outputFormatter.NewLine(); outputFormatter.NewLine();
if (customEvent) { if (customEvent) {

6
src/Libraries/NRefactory/Project/Src/Visitors/AbstractASTVisitor.cs

@ -1,7 +1,7 @@
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// <auto-generated> // <auto-generated>
// This code was generated by a tool. // This code was generated by a tool.
// Runtime Version:2.0.50727.88 // Runtime Version:2.0.50727.42
// //
// Changes to this file may cause incorrect behavior and will be lost if // Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated. // the code is regenerated.
@ -341,6 +341,7 @@ namespace ICSharpCode.NRefactory.Visitors {
Debug.Assert((eventDeclaration.AddRegion != null)); Debug.Assert((eventDeclaration.AddRegion != null));
Debug.Assert((eventDeclaration.RemoveRegion != null)); Debug.Assert((eventDeclaration.RemoveRegion != null));
Debug.Assert((eventDeclaration.RaiseRegion != null)); Debug.Assert((eventDeclaration.RaiseRegion != null));
Debug.Assert((eventDeclaration.Initializer != null));
foreach (AttributeSection o in eventDeclaration.Attributes) { foreach (AttributeSection o in eventDeclaration.Attributes) {
Debug.Assert(o != null); Debug.Assert(o != null);
o.AcceptVisitor(this, data); o.AcceptVisitor(this, data);
@ -356,7 +357,8 @@ namespace ICSharpCode.NRefactory.Visitors {
} }
eventDeclaration.AddRegion.AcceptVisitor(this, data); eventDeclaration.AddRegion.AcceptVisitor(this, data);
eventDeclaration.RemoveRegion.AcceptVisitor(this, data); eventDeclaration.RemoveRegion.AcceptVisitor(this, data);
return eventDeclaration.RaiseRegion.AcceptVisitor(this, data); eventDeclaration.RaiseRegion.AcceptVisitor(this, data);
return eventDeclaration.Initializer.AcceptVisitor(this, data);
} }
public virtual object VisitEventRaiseRegion(EventRaiseRegion eventRaiseRegion, object data) { public virtual object VisitEventRaiseRegion(EventRaiseRegion eventRaiseRegion, object data) {

6
src/Libraries/NRefactory/Project/Src/Visitors/AbstractAstTransformer.cs

@ -1,7 +1,7 @@
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// <auto-generated> // <auto-generated>
// This code was generated by a tool. // This code was generated by a tool.
// Runtime Version:2.0.50727.88 // Runtime Version:2.0.50727.42
// //
// Changes to this file may cause incorrect behavior and will be lost if // Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated. // the code is regenerated.
@ -600,6 +600,7 @@ namespace ICSharpCode.NRefactory.Visitors {
Debug.Assert((eventDeclaration.AddRegion != null)); Debug.Assert((eventDeclaration.AddRegion != null));
Debug.Assert((eventDeclaration.RemoveRegion != null)); Debug.Assert((eventDeclaration.RemoveRegion != null));
Debug.Assert((eventDeclaration.RaiseRegion != null)); Debug.Assert((eventDeclaration.RaiseRegion != null));
Debug.Assert((eventDeclaration.Initializer != null));
for (int i = 0; i < eventDeclaration.Attributes.Count; i++) { for (int i = 0; i < eventDeclaration.Attributes.Count; i++) {
AttributeSection o = eventDeclaration.Attributes[i]; AttributeSection o = eventDeclaration.Attributes[i];
Debug.Assert(o != null); Debug.Assert(o != null);
@ -645,6 +646,9 @@ namespace ICSharpCode.NRefactory.Visitors {
nodeStack.Push(eventDeclaration.RaiseRegion); nodeStack.Push(eventDeclaration.RaiseRegion);
eventDeclaration.RaiseRegion.AcceptVisitor(this, data); eventDeclaration.RaiseRegion.AcceptVisitor(this, data);
eventDeclaration.RaiseRegion = ((EventRaiseRegion)(nodeStack.Pop())); eventDeclaration.RaiseRegion = ((EventRaiseRegion)(nodeStack.Pop()));
nodeStack.Push(eventDeclaration.Initializer);
eventDeclaration.Initializer.AcceptVisitor(this, data);
eventDeclaration.Initializer = ((Expression)(nodeStack.Pop()));
return null; return null;
} }

13
src/Libraries/NRefactory/Test/Output/CSharp/CSharpOutputTest.cs

@ -69,6 +69,12 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
"}"); "}");
} }
[Test]
public void EventWithInitializer()
{
TestTypeMember("public event EventHandler Click = delegate { };");
}
[Test] [Test]
public void Field() public void Field()
{ {
@ -326,6 +332,13 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
TestTypeMember("public abstract bool Run();"); TestTypeMember("public abstract bool Run();");
} }
[Test]
public void AnonymousMethod()
{
TestStatement("Func b = delegate { return true; };");
TestStatement("Func a = delegate() { return false; };");
}
[Test] [Test]
public void Interface() public void Interface()
{ {

2
src/Libraries/NRefactory/Test/Output/CSharp/VBToCSharpConverterTest.cs

@ -144,7 +144,7 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
public void Property() public void Property()
{ {
TestMember("ReadOnly Property A()\nGet\nReturn Nothing\nEnd Get\nEnd Property", TestMember("ReadOnly Property A()\nGet\nReturn Nothing\nEnd Get\nEnd Property",
"public object A {\n\tget {\n\t\treturn null;\n\t}\n}"); "public object A {\n\tget { return null; }\n}");
} }
[Test] [Test]

2
src/Libraries/NRefactory/Test/Parser/Expressions/AnonymousMethodTests.cs

@ -27,6 +27,7 @@ namespace ICSharpCode.NRefactory.Tests.Ast
AnonymousMethodExpression ame = Parse("delegate {}"); AnonymousMethodExpression ame = Parse("delegate {}");
Assert.AreEqual(0, ame.Parameters.Count); Assert.AreEqual(0, ame.Parameters.Count);
Assert.AreEqual(0, ame.Body.Children.Count); Assert.AreEqual(0, ame.Body.Children.Count);
Assert.IsFalse(ame.HasParameterList);
} }
[Test] [Test]
@ -45,6 +46,7 @@ namespace ICSharpCode.NRefactory.Tests.Ast
AnonymousMethodExpression ame = Parse("delegate() {}"); AnonymousMethodExpression ame = Parse("delegate() {}");
Assert.AreEqual(0, ame.Parameters.Count); Assert.AreEqual(0, ame.Parameters.Count);
Assert.AreEqual(0, ame.Body.Children.Count); Assert.AreEqual(0, ame.Body.Children.Count);
Assert.IsTrue(ame.HasParameterList);
} }
[Test] [Test]

Loading…
Cancel
Save