1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
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
)
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
);
53 public override bool VisitSourceUnit(SourceUnit unit
)
55 nodeStack
.Push( CurrentNode
.Nodes
.Add("SourceUnit") );
56 CurrentNode
.Tag
= unit
;
58 base.VisitSourceUnit(unit
);
65 public override bool VisitNamespace(NamespaceDescriptor ns
)
67 nodeStack
.Push( CurrentNode
.Nodes
.Add("Namespace " + ns
.Name
) );
70 CurrentNode
.EnsureVisible();
72 base.VisitNamespace(ns
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
165 public override void VisitExpressionStatement(ExpressionStatement statement
)
167 nodeStack
.Push( CurrentNode
.Nodes
.Add("ExpressionStatement "));
168 CurrentNode
.Tag
= statement
;
170 base.VisitExpressionStatement(statement
);
175 public override void VisitMethodInvocationExpression(MethodInvocationExpression invocationExpression
)
177 nodeStack
.Push( CurrentNode
.Nodes
.Add("MethodInvocationExpression " + invocationExpression
.Designator
));
178 CurrentNode
.Tag
= invocationExpression
;
180 base.VisitMethodInvocationExpression(invocationExpression
);
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
);
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
);
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
);
215 public override void VisitMemberAccessExpression(MemberAccessExpression accessExpression
)
217 nodeStack
.Push( CurrentNode
.Nodes
.Add("MemberAccessExpression [" + accessExpression
.Name
+ " ] " ));
218 CurrentNode
.Tag
= accessExpression
;
220 base.VisitMemberAccessExpression(accessExpression
);