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

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

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

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

@ -87,6 +87,8 @@ namespace ICSharpCode.NRefactory.Ast { @@ -87,6 +87,8 @@ namespace ICSharpCode.NRefactory.Ast {
BlockStatement body;
bool hasParameterList;
public List<ParameterDeclarationExpression> Parameters {
get {
return parameters;
@ -106,6 +108,15 @@ namespace ICSharpCode.NRefactory.Ast { @@ -106,6 +108,15 @@ namespace ICSharpCode.NRefactory.Ast {
}
}
public bool HasParameterList {
get {
return hasParameterList;
}
set {
hasParameterList = value;
}
}
public AnonymousMethodExpression() {
parameters = new List<ParameterDeclarationExpression>();
body = BlockStatement.Null;
@ -116,7 +127,7 @@ namespace ICSharpCode.NRefactory.Ast { @@ -116,7 +127,7 @@ namespace ICSharpCode.NRefactory.Ast {
}
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 { @@ -1488,6 +1499,8 @@ namespace ICSharpCode.NRefactory.Ast {
Location bodyEnd;
Expression initializer;
public TypeReference TypeReference {
get {
return typeReference;
@ -1551,6 +1564,16 @@ namespace ICSharpCode.NRefactory.Ast { @@ -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) :
base(modifier, attributes, name, parameters) {
TypeReference = typeReference;
@ -1560,6 +1583,7 @@ namespace ICSharpCode.NRefactory.Ast { @@ -1560,6 +1583,7 @@ namespace ICSharpCode.NRefactory.Ast {
raiseRegion = EventRaiseRegion.Null;
bodyStart = 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) :
@ -1571,6 +1595,7 @@ namespace ICSharpCode.NRefactory.Ast { @@ -1571,6 +1595,7 @@ namespace ICSharpCode.NRefactory.Ast {
raiseRegion = EventRaiseRegion.Null;
bodyStart = Location.Empty;
bodyEnd = Location.Empty;
initializer = Expression.Null;
}
public bool HasAddRegion {
@ -1597,8 +1622,8 @@ namespace ICSharpCode.NRefactory.Ast { @@ -1597,8 +1622,8 @@ namespace ICSharpCode.NRefactory.Ast {
public override string ToString() {
return string.Format("[EventDeclaration TypeReference={0} InterfaceImplementations={1} AddRegion={2} Re" +
"moveRegion={3} RaiseRegion={4} BodyStart={5} BodyEnd={6} Name={7} Parameters={8}" +
" Attributes={9} Modifier={10}]", TypeReference, GetCollectionString(InterfaceImplementations), AddRegion, RemoveRegion, RaiseRegion, BodyStart, BodyEnd, Name, GetCollectionString(Parameters), GetCollectionString(Attributes), Modifier);
"moveRegion={3} RaiseRegion={4} BodyStart={5} BodyEnd={6} Initializer={7} Name={8" +
"} 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 @@ @@ -1,7 +1,7 @@
//------------------------------------------------------------------------------
// <auto-generated>
// 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
// 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> @@ -775,6 +775,7 @@ StructMemberDecl<ModifierList m, List<AttributeSection> attributes>
| ident (. qualident = t.val; .)
)
(. eventDecl.Name = qualident; eventDecl.EndLocation = t.EndLocation; .)
[ "=" Expr<out expr> (. eventDecl.Initializer = expr; .) ]
[ "{" (. eventDecl.BodyStart = t.Location; .)
EventAccessorDecls<out addBlock, out removeBlock>
"}" (. eventDecl.BodyEnd = t.EndLocation; .)
@ -1809,6 +1810,7 @@ AnonymousMethodExpr<out Expression outExpr> @@ -1809,6 +1810,7 @@ AnonymousMethodExpr<out Expression outExpr>
"("
[ FormalParameterList<p> (. expr.Parameters = p; .) ]
")"
(. expr.HasParameterList = true; .)
]
/*--- ParseExpression doesn't set a compilation unit, */
/*--- 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 @@ -559,7 +559,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
outputFormatter.Indent();
OutputModifier(propertyGetRegion.Modifier);
outputFormatter.PrintText("get");
OutputBlock(propertyGetRegion.Block, prettyPrintOptions.PropertyGetBraceStyle);
OutputBlockAllowInline(propertyGetRegion.Block, prettyPrintOptions.PropertyGetBraceStyle);
return null;
}
@ -569,7 +569,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -569,7 +569,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
outputFormatter.Indent();
OutputModifier(propertySetRegion.Modifier);
outputFormatter.PrintText("set");
OutputBlock(propertySetRegion.Block, prettyPrintOptions.PropertySetBraceStyle);
OutputBlockAllowInline(propertySetRegion.Block, prettyPrintOptions.PropertySetBraceStyle);
return null;
}
@ -589,6 +589,14 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -589,6 +589,14 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
}
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) {
outputFormatter.PrintToken(Tokens.Semicolon);
outputFormatter.NewLine();
@ -606,7 +614,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -606,7 +614,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
VisitAttributes(eventAddRegion.Attributes, data);
outputFormatter.Indent();
outputFormatter.PrintText("add");
OutputBlock(eventAddRegion.Block, prettyPrintOptions.EventAddBraceStyle);
OutputBlockAllowInline(eventAddRegion.Block, prettyPrintOptions.EventAddBraceStyle);
return null;
}
@ -615,7 +623,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -615,7 +623,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
VisitAttributes(eventRemoveRegion.Attributes, data);
outputFormatter.Indent();
outputFormatter.PrintText("remove");
OutputBlock(eventRemoveRegion.Block, prettyPrintOptions.EventRemoveBraceStyle);
OutputBlockAllowInline(eventRemoveRegion.Block, prettyPrintOptions.EventRemoveBraceStyle);
return null;
}
@ -906,6 +914,46 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -906,6 +914,46 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
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)
{
if (data is BraceStyle)
@ -2149,11 +2197,12 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -2149,11 +2197,12 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
{
outputFormatter.PrintToken(Tokens.Delegate);
outputFormatter.PrintToken(Tokens.OpenParenthesis);
AppendCommaSeparatedList(anonymousMethodExpression.Parameters);
outputFormatter.PrintToken(Tokens.CloseParenthesis);
OutputBlock(anonymousMethodExpression.Body, this.prettyPrintOptions.MethodBraceStyle);
if (anonymousMethodExpression.Parameters.Count > 0 || anonymousMethodExpression.HasParameterList) {
outputFormatter.PrintToken(Tokens.OpenParenthesis);
AppendCommaSeparatedList(anonymousMethodExpression.Parameters);
outputFormatter.PrintToken(Tokens.CloseParenthesis);
}
OutputBlockAllowInline(anonymousMethodExpression.Body, this.prettyPrintOptions.MethodBraceStyle, false);
return null;
}

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

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

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

@ -1,7 +1,7 @@ @@ -1,7 +1,7 @@
//------------------------------------------------------------------------------
// <auto-generated>
// 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
// the code is regenerated.
@ -341,6 +341,7 @@ namespace ICSharpCode.NRefactory.Visitors { @@ -341,6 +341,7 @@ namespace ICSharpCode.NRefactory.Visitors {
Debug.Assert((eventDeclaration.AddRegion != null));
Debug.Assert((eventDeclaration.RemoveRegion != null));
Debug.Assert((eventDeclaration.RaiseRegion != null));
Debug.Assert((eventDeclaration.Initializer != null));
foreach (AttributeSection o in eventDeclaration.Attributes) {
Debug.Assert(o != null);
o.AcceptVisitor(this, data);
@ -356,7 +357,8 @@ namespace ICSharpCode.NRefactory.Visitors { @@ -356,7 +357,8 @@ namespace ICSharpCode.NRefactory.Visitors {
}
eventDeclaration.AddRegion.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) {

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

@ -1,7 +1,7 @@ @@ -1,7 +1,7 @@
//------------------------------------------------------------------------------
// <auto-generated>
// 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
// the code is regenerated.
@ -600,6 +600,7 @@ namespace ICSharpCode.NRefactory.Visitors { @@ -600,6 +600,7 @@ namespace ICSharpCode.NRefactory.Visitors {
Debug.Assert((eventDeclaration.AddRegion != null));
Debug.Assert((eventDeclaration.RemoveRegion != null));
Debug.Assert((eventDeclaration.RaiseRegion != null));
Debug.Assert((eventDeclaration.Initializer != null));
for (int i = 0; i < eventDeclaration.Attributes.Count; i++) {
AttributeSection o = eventDeclaration.Attributes[i];
Debug.Assert(o != null);
@ -645,6 +646,9 @@ namespace ICSharpCode.NRefactory.Visitors { @@ -645,6 +646,9 @@ namespace ICSharpCode.NRefactory.Visitors {
nodeStack.Push(eventDeclaration.RaiseRegion);
eventDeclaration.RaiseRegion.AcceptVisitor(this, data);
eventDeclaration.RaiseRegion = ((EventRaiseRegion)(nodeStack.Pop()));
nodeStack.Push(eventDeclaration.Initializer);
eventDeclaration.Initializer.AcceptVisitor(this, data);
eventDeclaration.Initializer = ((Expression)(nodeStack.Pop()));
return null;
}

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

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

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

@ -144,7 +144,7 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter @@ -144,7 +144,7 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
public void 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]

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

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

Loading…
Cancel
Save