Added ifdef DOTNET35
[castle.git] / Experiments / Attic / Rook / ASTViewer / TreeWalker.cs
blob60445e4168e8c584f0b8132ca818fbd5aa91670f
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace ASTViewer
17 using System;
18 using System.Collections;
20 using System.Windows.Forms;
22 using Castle.Rook.Compiler.AST;
23 using Castle.Rook.Compiler.Visitors;
26 public class TreeWalker : DepthFirstVisitor
28 private TreeNodeCollection nodes;
29 private Stack nodeStack = new Stack();
31 public TreeWalker(CompilationUnit unit, TreeNodeCollection nodes)
33 this.nodes = nodes;
35 VisitNode(unit);
38 public TreeNode CurrentNode
40 get { return nodeStack.Peek() as TreeNode; }
43 public override void VisitCompilationUnit(CompilationUnit compilationUnit)
45 nodeStack.Push( nodes.Add("CompilationUnit") );
46 CurrentNode.Tag = compilationUnit;
48 base.VisitCompilationUnit(compilationUnit);
50 nodeStack.Pop();
53 public override bool VisitSourceUnit(SourceUnit unit)
55 nodeStack.Push( CurrentNode.Nodes.Add("SourceUnit") );
56 CurrentNode.Tag = unit;
58 base.VisitSourceUnit(unit);
60 nodeStack.Pop();
62 return true;
65 public override bool VisitNamespace(NamespaceDescriptor ns)
67 nodeStack.Push( CurrentNode.Nodes.Add("Namespace " + ns.Name) );
68 CurrentNode.Tag = ns;
70 CurrentNode.EnsureVisible();
72 base.VisitNamespace(ns);
74 nodeStack.Pop();
76 return true;
79 public override bool VisitTypeDefinitionStatement(TypeDefinitionStatement typeDef)
81 nodeStack.Push( CurrentNode.Nodes.Add("Type " + typeDef.Name ) );
82 CurrentNode.Tag = typeDef;
84 CurrentNode.EnsureVisible();
86 base.VisitTypeDefinitionStatement(typeDef);
88 nodeStack.Pop();
90 return true;
93 public override bool VisitMethodDefinitionStatement(MethodDefinitionStatement methodDef)
95 nodeStack.Push( CurrentNode.Nodes.Add("Method " + methodDef.Name + "[ret " + methodDef.ReturnType + "]" ));
96 CurrentNode.Tag = methodDef;
98 CurrentNode.EnsureVisible();
100 base.VisitMethodDefinitionStatement(methodDef);
102 nodeStack.Pop();
104 return true;
107 public override void VisitConstructorDefinitionStatement(ConstructorDefinitionStatement statement)
109 nodeStack.Push( CurrentNode.Nodes.Add("Constructor " + statement.Name ));
110 CurrentNode.Tag = statement;
112 CurrentNode.EnsureVisible();
114 base.VisitConstructorDefinitionStatement(statement);
116 nodeStack.Pop();
119 public override void VisitTypeReference(TypeReference reference)
121 nodeStack.Push( CurrentNode.Nodes.Add("TypeReference " + reference.TypeName + " Resolved to [" + reference.ResolvedType + "]"));
122 CurrentNode.Tag = reference;
124 CurrentNode.EnsureVisible();
126 base.VisitTypeReference(reference);
128 nodeStack.Pop();
131 public override bool VisitIdentifier(Identifier identifier)
133 nodeStack.Push( CurrentNode.Nodes.Add("Identifier " + identifier.Name + " - " + identifier.TypeReference ));
134 CurrentNode.Tag = identifier;
136 CurrentNode.EnsureVisible();
138 base.VisitIdentifier(identifier);
140 nodeStack.Pop();
142 return true;
145 public override void VisitParameterVarIdentifier(ParameterVarIdentifier identifier)
147 nodeStack.Push( CurrentNode.Nodes.Add("ParameterVarIdentifier " + identifier.Name + " - " + identifier.TypeReference ));
148 CurrentNode.Tag = identifier;
150 base.VisitParameterVarIdentifier(identifier);
152 nodeStack.Pop();
155 public override void VisitOpaqueIdentifier(OpaqueIdentifier identifier)
157 nodeStack.Push( CurrentNode.Nodes.Add("OpaqueIdentifier " + identifier.Name + " - " + identifier.TypeReference ));
158 CurrentNode.Tag = identifier;
160 base.VisitOpaqueIdentifier(identifier);
162 nodeStack.Pop();
165 public override void VisitExpressionStatement(ExpressionStatement statement)
167 nodeStack.Push( CurrentNode.Nodes.Add("ExpressionStatement "));
168 CurrentNode.Tag = statement;
170 base.VisitExpressionStatement(statement);
172 nodeStack.Pop();
175 public override void VisitMethodInvocationExpression(MethodInvocationExpression invocationExpression)
177 nodeStack.Push( CurrentNode.Nodes.Add("MethodInvocationExpression " + invocationExpression.Designator));
178 CurrentNode.Tag = invocationExpression;
180 base.VisitMethodInvocationExpression(invocationExpression);
182 nodeStack.Pop();
185 public override void VisitVariableReferenceExpression(VariableReferenceExpression expression)
187 nodeStack.Push( CurrentNode.Nodes.Add("VariableReferenceExpression - " + expression.Identifier.Name));
188 CurrentNode.Tag = expression;
190 base.VisitVariableReferenceExpression(expression);
192 nodeStack.Pop();
195 public override void VisitConstExpression(ConstExpression expression)
197 nodeStack.Push( CurrentNode.Nodes.Add("ConstExpression " + expression.Value + " " + expression.ValueType ));
198 CurrentNode.Tag = expression;
200 base.VisitConstExpression(expression);
202 nodeStack.Pop();
205 public override void VisitBlockExpression(BlockExpression expression)
207 nodeStack.Push( CurrentNode.Nodes.Add("BlockExpression [" + expression.Parameters.Count + " params ] " ));
208 CurrentNode.Tag = expression;
210 base.VisitBlockExpression(expression);
212 nodeStack.Pop();
215 public override void VisitMemberAccessExpression(MemberAccessExpression accessExpression)
217 nodeStack.Push( CurrentNode.Nodes.Add("MemberAccessExpression [" + accessExpression.Name + " ] " ));
218 CurrentNode.Tag = accessExpression;
220 base.VisitMemberAccessExpression(accessExpression);
222 nodeStack.Pop();