Browse Source

r7318@daniel-notebook (orig r3349): daniel | 2008-08-14 14:44:42 +0200

Assign Node.Parent and check its value in unit tests (at least for statements+expressions)


git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@3351 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Daniel Grunwald 17 years ago
parent
commit
5ebc8f15f1
  1. 6
      src/Libraries/NRefactory/NRefactory.sln
  2. 3
      src/Libraries/NRefactory/NRefactoryASTGenerator/AST/Statements.cs
  3. 11
      src/Libraries/NRefactory/Project/Src/Ast/Generated.cs
  4. 10
      src/Libraries/NRefactory/Project/Src/Parser/CSharp/CSharpParser.cs
  5. 1721
      src/Libraries/NRefactory/Project/Src/Parser/CSharp/Parser.cs
  6. 26
      src/Libraries/NRefactory/Project/Src/Parser/CSharp/cs.ATG
  7. 1
      src/Libraries/NRefactory/Test/NRefactoryTests.csproj
  8. 47
      src/Libraries/NRefactory/Test/Parser/CheckParentVisitor.cs
  9. 1
      src/Libraries/NRefactory/Test/Parser/ParseUtilCSharp.cs

6
src/Libraries/NRefactory/NRefactory.sln

@ -1,7 +1,7 @@ @@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 10.00
# Visual Studio 2008
# SharpDevelop 3.0.0.2632
# SharpDevelop 3.0.0.3328
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NRefactory", "Project\NRefactory.csproj", "{3A9AE6AA-BC07-4A2F-972C-581E3AE2F195}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NRefactoryTests", "Test\NRefactoryTests.csproj", "{870115DD-960A-4406-A6B9-600BCDC36A03}"
@ -22,10 +22,6 @@ Global @@ -22,10 +22,6 @@ Global
{870115DD-960A-4406-A6B9-600BCDC36A03}.Debug|Any CPU.Build.0 = Debug|Any CPU
{870115DD-960A-4406-A6B9-600BCDC36A03}.Release|Any CPU.ActiveCfg = Release|Any CPU
{870115DD-960A-4406-A6B9-600BCDC36A03}.Release|Any CPU.Build.0 = Release|Any CPU
{83DD7E12-A705-4DBA-9D71-09C8973D9382}.Debug|Any CPU.Build.0 = Debug|Any CPU
{83DD7E12-A705-4DBA-9D71-09C8973D9382}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{83DD7E12-A705-4DBA-9D71-09C8973D9382}.Release|Any CPU.Build.0 = Release|Any CPU
{83DD7E12-A705-4DBA-9D71-09C8973D9382}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B22522AA-B5BF-4A58-AC6D-D4B45805521F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B22522AA-B5BF-4A58-AC6D-D4B45805521F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B22522AA-B5BF-4A58-AC6D-D4B45805521F}.Release|Any CPU.Build.0 = Release|Any CPU

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

@ -71,12 +71,15 @@ namespace NRefactoryASTGenerator.Ast @@ -71,12 +71,15 @@ namespace NRefactoryASTGenerator.Ast
public IfElseStatement(Expression condition, Statement trueStatement)
: this(condition) {
this.trueStatement.Add(Statement.CheckNull(trueStatement));
if (trueStatement != null) trueStatement.Parent = this;
}")]
[IncludeMember(@"
public IfElseStatement(Expression condition, Statement trueStatement, Statement falseStatement)
: this(condition) {
this.trueStatement.Add(Statement.CheckNull(trueStatement));
this.falseStatement.Add(Statement.CheckNull(falseStatement));
if (trueStatement != null) trueStatement.Parent = this;
if (falseStatement != null) falseStatement.Parent = this;
}")]
[IncludeBoolProperty("HasElseStatements", "return falseStatement.Count > 0;")]
[IncludeBoolProperty("HasElseIfSections", "return elseIfSections.Count > 0;")]

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

@ -1,10 +1,10 @@ @@ -1,10 +1,10 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.1434
// Dieser Code wurde von einem Tool generiert.
// Laufzeitversion:2.0.50727.3053
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// Änderungen an dieser Datei können falsches Verhalten verursachen und gehen verloren, wenn
// der Code erneut generiert wird.
// </auto-generated>
//------------------------------------------------------------------------------
@ -2210,6 +2210,7 @@ namespace ICSharpCode.NRefactory.Ast { @@ -2210,6 +2210,7 @@ namespace ICSharpCode.NRefactory.Ast {
public IfElseStatement(Expression condition, Statement trueStatement)
: this(condition) {
this.trueStatement.Add(Statement.CheckNull(trueStatement));
if (trueStatement != null) trueStatement.Parent = this;
}
@ -2217,6 +2218,8 @@ namespace ICSharpCode.NRefactory.Ast { @@ -2217,6 +2218,8 @@ namespace ICSharpCode.NRefactory.Ast {
: this(condition) {
this.trueStatement.Add(Statement.CheckNull(trueStatement));
this.falseStatement.Add(Statement.CheckNull(falseStatement));
if (trueStatement != null) trueStatement.Parent = this;
if (falseStatement != null) falseStatement.Parent = this;
}
public override object AcceptVisitor(IAstVisitor visitor, object data) {

10
src/Libraries/NRefactory/Project/Src/Parser/CSharp/CSharpParser.cs

@ -594,5 +594,15 @@ namespace ICSharpCode.NRefactory.Parser.CSharp @@ -594,5 +594,15 @@ namespace ICSharpCode.NRefactory.Parser.CSharp
item.Parent = parent;
}
}
static void SetParent<T>(List<T> list, INode parent) where T : class, INode, INullable
{
if (list != null) {
foreach (T x in list) {
if (!x.IsNull)
x.Parent = parent;
}
}
}
}
}

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

File diff suppressed because it is too large Load Diff

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

@ -864,6 +864,7 @@ StructMemberDecl<ModifierList m, List<AttributeSection> attributes> @@ -864,6 +864,7 @@ StructMemberDecl<ModifierList m, List<AttributeSection> attributes>
StartLocation = m.GetDeclarationLocation(startPos),
EndLocation = endPos
};
SetParent(parameters, operatorDeclaration);
compilationUnit.AddChild(operatorDeclaration);
.)
@ -905,6 +906,7 @@ StructMemberDecl<ModifierList m, List<AttributeSection> attributes> @@ -905,6 +906,7 @@ StructMemberDecl<ModifierList m, List<AttributeSection> attributes>
StartLocation = m.GetDeclarationLocation(startPos),
EndLocation = endPos
};
SetParent(parameters, operatorDeclaration);
compilationUnit.AddChild(operatorDeclaration);
.)
@ -1287,7 +1289,9 @@ ConstructorInitializer<out ConstructorInitializer ci> @@ -1287,7 +1289,9 @@ ConstructorInitializer<out ConstructorInitializer ci>
| "this" (. ci.ConstructorInitializerType = ConstructorInitializerType.This; .)
)
"("
[ Argument<out expr> (. if (expr != null) { ci.Arguments.Add(expr); } .) { "," Argument<out expr> (. if (expr != null) { ci.Arguments.Add(expr); } .) } ]
[ Argument<out expr> (. SafeAdd(ci, ci.Arguments, expr); .)
{ "," Argument<out expr> (. SafeAdd(ci, ci.Arguments, expr); .) }
]
")"
.
@ -1497,8 +1501,10 @@ EmbeddedStatement<out Statement statement> @@ -1497,8 +1501,10 @@ EmbeddedStatement<out Statement statement>
| "switch" (. List<SwitchSection> switchSections = new List<SwitchSection>(); .)
"(" Expr<out expr> ")"
"{" SwitchSections<switchSections>
"}" (. statement = new SwitchStatement(expr, switchSections); .)
"}"
(. statement = new SwitchStatement(expr, switchSections); .)
(. SetParent(switchSections, statement); .)
/*--- iteration statements (while, do, for, foreach): */
| "while" "(" Expr<out expr> ")"
EmbeddedStatement<out embeddedStatement>
@ -1512,7 +1518,11 @@ EmbeddedStatement<out Statement statement> @@ -1512,7 +1518,11 @@ EmbeddedStatement<out Statement statement>
"(" [ ForInitializer<out initializer> ] ";"
[ Expr<out expr> ] ";"
[ ForIterator<out iterator> ] ")"
EmbeddedStatement<out embeddedStatement> (. statement = new ForStatement(initializer, expr, iterator, embeddedStatement); .)
EmbeddedStatement<out embeddedStatement>
(. statement = new ForStatement(initializer, expr, iterator, embeddedStatement);
SetParent(initializer, statement);
SetParent(iterator, statement);
.)
| "foreach" "(" Type<out type> Identifier (. string varName = t.val; .)
"in" Expr<out expr> ")"
@ -1583,6 +1593,8 @@ IfStatement<out Statement statement> @@ -1583,6 +1593,8 @@ IfStatement<out Statement statement>
(elseStatement as IfElseStatement).TrueStatement[0]));
(statement as IfElseStatement).ElseIfSections.AddRange((elseStatement as IfElseStatement).ElseIfSections);
(statement as IfElseStatement).FalseStatement = (elseStatement as IfElseStatement).FalseStatement;
SetParent((statement as IfElseStatement).FalseStatement, statement);
SetParent((statement as IfElseStatement).ElseIfSections, statement);
}
.)
.
@ -1612,7 +1624,7 @@ SwitchSections<List<SwitchSection> switchSections> @@ -1612,7 +1624,7 @@ SwitchSections<List<SwitchSection> switchSections>
CaseLabel label;
.)
=
SwitchLabel<out label> (. if (label != null) { switchSection.SwitchLabels.Add(label); } .)
SwitchLabel<out label> (. SafeAdd(switchSection, switchSection.SwitchLabels, label); .)
(. compilationUnit.BlockStart(switchSection); .)
{
( SwitchLabel<out label>
@ -1623,7 +1635,7 @@ SwitchSections<List<SwitchSection> switchSections> @@ -1623,7 +1635,7 @@ SwitchSections<List<SwitchSection> switchSections>
switchSection = new SwitchSection();
compilationUnit.BlockStart(switchSection);
}
switchSection.SwitchLabels.Add(label);
SafeAdd(switchSection, switchSection.SwitchLabels, label);
}
.)
| Statement)
@ -1651,7 +1663,7 @@ TryStatement<out Statement tryStatement> @@ -1651,7 +1663,7 @@ TryStatement<out Statement tryStatement>
)
(.
tryStatement = new TryCatchStatement(blockStmt, catchClauses, finallyStmt);
foreach (CatchClause cc in catchClauses) cc.Parent = tryStatement;
.)
.

1
src/Libraries/NRefactory/Test/NRefactoryTests.csproj

@ -48,6 +48,7 @@ @@ -48,6 +48,7 @@
<Compile Include="General\UnitTest.cs" />
<Compile Include="Lexer\CSharp\PreprocessingTests.cs" />
<Compile Include="Lexer\VBNet\CustomLexerTests.cs" />
<Compile Include="Parser\CheckParentVisitor.cs" />
<Compile Include="Parser\Expressions\LambdaExpressionTests.cs" />
<Compile Include="Parser\Expressions\QueryExpressionTests.cs" />
<Compile Include="Output\SnippetConversion.cs" />

47
src/Libraries/NRefactory/Test/Parser/CheckParentVisitor.cs

@ -0,0 +1,47 @@ @@ -0,0 +1,47 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Daniel Grunwald"/>
// <version>$Revision$</version>
// </file>
using NUnit.Framework;
using System;
using System.Collections.Generic;
using ICSharpCode.NRefactory.Ast;
using ICSharpCode.NRefactory.Visitors;
namespace ICSharpCode.NRefactory.Tests.Ast
{
/// <summary>
/// Ensures that all nodes have the Parent property correctly set.
/// </summary>
public class CheckParentVisitor : NodeTrackingAstVisitor
{
Stack<INode> nodeStack = new Stack<INode>();
public bool StrictCheck;
public CheckParentVisitor()
{
nodeStack.Push(null);
}
protected override void BeginVisit(INode node)
{
nodeStack.Push(node);
}
protected override void EndVisit(INode node)
{
Assert.AreEqual(node, nodeStack.Pop(), "nodeStack was corrupted!");
if (StrictCheck
|| !(node is TypeReference) && !(node is InterfaceImplementation)
&& !(node is PropertyGetSetRegion) && !(node is ParameterDeclarationExpression)
&& !(node is TemplateDefinition) && !(node is EventAddRemoveRegion)
&& !(node is ICSharpCode.NRefactory.Ast.Attribute) && !(node is AttributeSection))
{
Assert.AreEqual(nodeStack.Peek(), node.Parent, "node " + node + " is missing parent: " + nodeStack.Peek());
}
}
}
}

1
src/Libraries/NRefactory/Test/Parser/ParseUtilCSharp.cs

@ -43,6 +43,7 @@ namespace ICSharpCode.NRefactory.Tests.Ast @@ -43,6 +43,7 @@ namespace ICSharpCode.NRefactory.Tests.Ast
Assert.IsTrue(parser.CompilationUnit.Children.Count > 0);
Type type = typeof(T);
Assert.IsTrue(type.IsAssignableFrom(parser.CompilationUnit.Children[0].GetType()), String.Format("Parsed expression was {0} instead of {1} ({2})", parser.CompilationUnit.Children[0].GetType(), type, parser.CompilationUnit.Children[0]));
parser.CompilationUnit.AcceptVisitor(new CheckParentVisitor(), null);
return (T)parser.CompilationUnit.Children[0];
}

Loading…
Cancel
Save