source: trunk/IDE/UProducerTreeView.pas

Last change on this file was 75, checked in by chronos, 6 months ago
  • Modified: Removed U prefix from unit names.
  • Modified: Updated Common package.
File size: 12.0 KB
Line 
1unit UProducerTreeView;
2
3interface
4
5uses
6 Classes, SysUtils, SourceCode, ComCtrls, Producer, StrUtils;
7
8type
9
10 { TProducerTreeView }
11
12 TProducerTreeView = class(TProducer)
13 private
14 procedure AddNodeAssignment(Node: TTreeNode; Assignment: TAssignment);
15 procedure AddNodeBeginEnd(Node: TTreeNode; BeginEnd: TBeginEnd);
16 procedure AddNodeIfThenElse(Node: TTreeNode; IfThenElse: TIfThenElse);
17 procedure AddNodeMethodCall(Node: TTreeNode; Method: TFunctionCall);
18 procedure AddNodeMethodList(Node: TTreeNode; Methods: TFunctionList);
19 procedure AddNodeParameterList(Node: TTreeNode; Parameters: TParameterList);
20 procedure AddNodeTypeRecord(Node: TTreeNode; TypeRecord: TTypeRecord);
21 procedure AddNodeTypeList(Node: TTreeNode; Types: TTypeList);
22 procedure AddNodeType(Node: TTreeNode; AType: TType);
23 procedure AddNodeVariableList(Node: TTreeNode; Variables: TVariableList);
24 procedure AddNodeConstantList(Node: TTreeNode; Constants: TConstantList);
25 procedure AddNodeModule(Node: TTreeNode; Module: TModule);
26 procedure AddNodeUses(Node: TTreeNode; UsedModuleList: TUsedModuleList);
27 procedure AddNodeProgram(Node: TTreeNode; Code: TProgram);
28 procedure AddNodeWhileDo(Node: TTreeNode; WhileDo: TWhileDo);
29 procedure AddNodeForToDo(Node: TTreeNode; ForToDo: TForToDo);
30 procedure AddNodeCommand(Node: TTreeNode; Command: TCommand);
31 procedure AddNodeExpression(Node: TTreeNode; Expression: TExpression);
32 procedure WriteNode(Target: TStringList; Node: TTreeNode; Indetation: Integer);
33 public
34 TreeView: TTreeView;
35 procedure AssignToStringList(Target: TStringList); override;
36 procedure Produce(Module: TModule); override;
37 constructor Create;
38 destructor Destroy; override;
39 end;
40
41implementation
42
43{ TProducerTreeView }
44
45procedure TProducerTreeView.AddNodeBeginEnd(Node: TTreeNode;
46 BeginEnd: TBeginEnd);
47var
48 NewNode: TTreeNode;
49 NewNode2: TTreeNode;
50 I: Integer;
51begin
52 NewNode := TreeView.Items.AddChild(Node, 'begin-end');
53 for I := 0 to BeginEnd.Commands.Count - 1 do
54 AddNodeCommand(NewNode, TCommand(BeginEnd.Commands[I]));
55end;
56
57procedure TProducerTreeView.AddNodeWhileDo(Node: TTreeNode; WhileDo: TWhileDo);
58var
59 NewNode: TTreeNode;
60 NewNode2: TTreeNode;
61begin
62 NewNode := TreeView.Items.AddChild(Node, 'while');
63 NewNode2 := TreeView.Items.AddChild(NewNode, 'condition');
64 AddNodeExpression(NewNode2, WhileDo.Condition);
65 NewNode2 := TreeView.Items.AddChild(NewNode, 'do');
66 AddNodeCommand(NewNode2, WhileDo.Command);
67end;
68
69procedure TProducerTreeView.AddNodeForToDo(Node: TTreeNode; ForToDo: TForToDo);
70var
71 NewNode: TTreeNode;
72 NewNode2: TTreeNode;
73 NewNode3: TTreeNode;
74 I: Integer;
75begin
76 with ForToDo do begin
77 NewNode := TreeView.Items.AddChild(Node, 'for');
78 NewNode2 := TreeView.Items.AddChild(NewNode, 'control');
79 if Assigned(ControlVariable) then
80 NewNode3 := TreeView.Items.AddChild(NewNode2, ControlVariable.Name);
81 NewNode2 := TreeView.Items.AddChild(NewNode, 'from');
82 AddNodeExpression(NewNode2, Start);
83 NewNode2 := TreeView.Items.AddChild(NewNode, 'to');
84 AddNodeExpression(NewNode2, Stop);
85 NewNode2 := TreeView.Items.AddChild(NewNode, 'do');
86 AddNodeCommand(NewNode2, Command);
87 end;
88end;
89
90procedure TProducerTreeView.AddNodeIfThenElse(Node: TTreeNode; IfThenElse: TIfThenElse);
91var
92 NewNode: TTreeNode;
93 NewNode2: TTreeNode;
94 NewNode3: TTreeNode;
95 I: Integer;
96begin
97 NewNode := TreeView.Items.AddChild(Node, 'if');
98 NewNode2 := TreeView.Items.AddChild(NewNode, 'condition');
99 AddNodeExpression(NewNode2, IfThenElse.Condition);
100 NewNode2 := TreeView.Items.AddChild(NewNode, 'then');
101 AddNodeCommand(NewNode2, IfThenElse.Command);
102 if Assigned(IfThenElse.ElseCommand) then begin
103 NewNode2 := TreeView.Items.AddChild(NewNode, 'else');
104 AddNodeCommand(NewNode2, IfThenElse.ElseCommand);
105 end;
106end;
107
108procedure TProducerTreeView.AddNodeMethodCall(Node: TTreeNode; Method: TFunctionCall);
109var
110 NewNode: TTreeNode;
111begin
112 NewNode := TreeView.Items.AddChild(Node, Method.FunctionRef.Name);
113end;
114
115procedure TProducerTreeView.AddNodeAssignment(Node: TTreeNode; Assignment: TAssignment);
116var
117 NewNode: TTreeNode;
118begin
119 NewNode := TreeView.Items.AddChild(Node, Assignment.Target.Name + ' := ');
120 AddNodeExpression(NewNode, Assignment.Source);
121end;
122
123procedure TProducerTreeView.AddNodeCommand(Node: TTreeNode; Command: TCommand);
124begin
125 if Command is TBeginEnd then
126 AddNodeBeginEnd(Node, TBeginEnd(Command))
127 else
128 if Command is TWhileDo then
129 AddNodeWhileDo(Node, TWhileDo(Command))
130 else
131 if Command is TFunctionCall then
132 AddNodeMethodCall(Node, TFunctionCall(Command))
133 else
134 if Command is TIfThenElse then
135 AddNodeIfThenElse(Node, TIfThenElse(Command))
136 else
137 if Command is TForToDo then
138 AddNodeForToDo(Node, TForToDo(Command))
139 else
140 if Command is TAssignment then
141 AddNodeAssignment(Node, TAssignment(Command));
142end;
143
144procedure TProducerTreeView.AddNodeExpression(Node: TTreeNode; Expression: TExpression
145 );
146var
147 NewNode: TTreeNode;
148begin
149 case Expression.NodeType of
150 ntConstant: NewNode := TreeView.Items.AddChild(Node, Expression.Value);
151 ntVariable: NewNode := TreeView.Items.AddChild(Node, Expression.Variable.Name);
152 ntFunction: NewNode := TreeView.Items.AddChild(Node, Expression.FunctionCall.FunctionRef.Name);
153 ntOperator: begin
154 NewNode := TreeView.Items.AddChild(Node, Expression.OperatorName);
155 AddNodeExpression(NewNode, TExpression(Expression.SubItems.First));
156 AddNodeExpression(NewNode, TExpression(Expression.SubItems.Last));
157 end;
158 ntNone: ;
159 end;
160end;
161
162procedure TProducerTreeView.WriteNode(Target: TStringList; Node: TTreeNode;
163 Indetation: Integer);
164var
165 I: Integer;
166begin
167 if Node.Count > 0 then begin
168 Target.Add(DupeString(' ', Indetation) + '<' + Node.Text + '>');
169 for I := 0 to Node.Count - 1 do
170 WriteNode(Target, Node.Items[I], Indetation + 1);
171 Target.Add(DupeString(' ', Indetation) + '</' + Node.Text + '>');
172 end else
173 Target.Add(DupeString(' ', Indetation) + '<' + Node.Text + '/>');
174end;
175
176procedure TProducerTreeView.AssignToStringList(Target: TStringList);
177begin
178 with Target do begin
179 Clear;
180 if Assigned(TreeView.TopItem) then
181 WriteNode(Target, TreeView.TopItem, 0);
182 end;
183end;
184
185procedure TProducerTreeView.AddNodeVariableList(Node: TTreeNode;
186 Variables: TVariableList);
187var
188 NewNode: TTreeNode;
189 NewNode2: TTreeNode;
190 I: Integer;
191begin
192 if Variables.Count > 0 then begin
193 NewNode := TreeView.Items.AddChild(Node, 'var');
194 for I := 0 to Variables.Count - 1 do
195 with TVariable(Variables[I]) do begin
196 NewNode2 := TreeView.Items.AddChild(NewNode, Name + ': ' + ValueType.Name);
197 end;
198 end;
199end;
200
201procedure TProducerTreeView.AddNodeConstantList(Node: TTreeNode;
202 Constants: TConstantList);
203var
204 NewNode: TTreeNode;
205 NewNode2: TTreeNode;
206 I: Integer;
207begin
208 if Constants.Count > 0 then begin
209 NewNode := TreeView.Items.AddChild(Node, 'const');
210 for I := 0 to Constants.Count - 1 do
211 with TConstant(Constants[I]) do begin
212 NewNode2 := TreeView.Items.AddChild(NewNode, Name + ': ' + ValueType.Name + ' = ' + Value);
213 end;
214 end;
215end;
216
217procedure TProducerTreeView.AddNodeTypeList(Node: TTreeNode;
218 Types: TTypeList);
219var
220 NewNode: TTreeNode;
221 I: Integer;
222begin
223 if Types.Count > 0 then begin
224 NewNode := TreeView.Items.AddChild(Node, 'type');
225 for I := 0 to Types.Count - 1 do
226 with TType(Types[I]) do
227 if (not Internal) then AddNodeType(NewNode, TType(Types[I]));
228 end;
229end;
230
231procedure TProducerTreeView.AddNodeType(Node: TTreeNode; AType: TType);
232var
233 NewNode: TTreeNode;
234 NewNode2: TTreeNode;
235begin
236 with AType do begin
237 if AType is TTypeRecord then begin
238 NewNode := TreeView.Items.AddChild(Node, Name + ' = record');
239 AddNodeTypeRecord(NewNode, TTypeRecord(AType));
240 end else
241 if AType is TTypeArray then begin
242 NewNode := TreeView.Items.AddChild(Node, Name + ' = array');
243 if Assigned(TTypeArray(AType).IndexType) then begin
244 NewNode2 := TreeView.Items.AddChild(NewNode, 'Index');
245 AddNodeType(NewNode2, TTypeArray(AType).IndexType);
246 end;
247 if Assigned(TTypeArray(AType).ItemType) then begin
248 NewNode2 := TreeView.Items.AddChild(NewNode, 'ItemType');
249 AddNodeType(NewNode2, TTypeArray(AType).ItemType);
250 end;
251 end else if Assigned(UsedType) then
252 NewNode := TreeView.Items.AddChild(Node, Name + ' = ' + UsedType.Name)
253 else NewNode := TreeView.Items.AddChild(Node, Name);
254 end;
255end;
256
257procedure TProducerTreeView.AddNodeMethodList(Node: TTreeNode;
258 Methods: TFunctionList);
259var
260 NewNode: TTreeNode;
261 I: Integer;
262begin
263 for I := 0 to Methods.Count - 1 do
264 with TFunction(Methods[I]) do
265 if (not Internal) then begin
266 if FunctionType = ftFunction then
267 NewNode := TreeView.Items.AddChild(Node, 'function ' + Name)
268 else NewNode := TreeView.Items.AddChild(Node, 'procedure ' + Name);
269 AddNodeParameterList(NewNode, Parameters);
270 AddNodeMethodList(NewNode, Functions);
271 AddNodeConstantList(NewNode, Constants);
272 AddNodeVariableList(NewNode, Variables);
273 AddNodeTypeList(NewNode, Types);
274 AddNodeBeginEnd(NewNode, Code);
275 end;
276end;
277
278procedure TProducerTreeView.AddNodeParameterList(Node: TTreeNode;
279 Parameters: TParameterList);
280var
281 I: Integer;
282 NewNode: TTreeNode;
283begin
284 if Parameters.Count > 0 then begin
285 NewNode := TreeView.Items.AddChild(Node, 'parametery');
286 for I := 0 to Parameters.Count - 1 do
287 with TParameter(Parameters[I]) do begin
288 TreeView.Items.AddChild(NewNode, Name + ': ' + ValueType.Name);
289 end;
290 end;
291end;
292
293procedure TProducerTreeView.AddNodeTypeRecord(Node: TTreeNode; TypeRecord: TTypeRecord
294 );
295var
296 I: Integer;
297begin
298 if TypeRecord.CommonBlock.Types.Count > 0 then begin
299 for I := 0 to TypeRecord.CommonBlock.Types.Count - 1 do
300 with TType(TypeRecord.CommonBlock.Types[I]) do
301 if not Internal then
302 AddNodeType(Node, TType(TypeRecord.CommonBlock.Types[I]));
303 end;
304end;
305
306procedure TProducerTreeView.AddNodeModule(Node: TTreeNode; Module: TModule);
307var
308 NewNode: TTreeNode;
309 I: Integer;
310 TypeName: string;
311begin
312 if Module is TModulePackage then TypeName := 'package'
313 else if Module is TModuleProgram then
314 with TModuleProgram(Module) do begin
315 TypeName := 'program';
316 NewNode := TreeView.Items.AddChild(Node, TypeName + ' ' + Module.Name);
317 AddNodeUses(NewNode, UsedModules);
318 AddNodeMethodList(NewNode, Body.Functions);
319 AddNodeConstantList(NewNode, Body.Constants);
320 AddNodeVariableList(NewNode, Body.Variables);
321 AddNodeTypeList(NewNode, Body.Types);
322 AddNodeBeginEnd(NewNode, Body.Code);
323 end else if Module is TModuleUnit then TypeName := 'unit'
324 else if Module is TModuleLibrary then TypeName := 'library';
325
326end;
327
328procedure TProducerTreeView.AddNodeUses(Node: TTreeNode;
329 UsedModuleList: TUsedModuleList);
330var
331 NewNode: TTreeNode;
332 I: Integer;
333begin
334 NewNode := TreeView.Items.AddChild(Node, 'uses');
335 for I := 0 to UsedModuleList.Count - 1 do begin
336 TreeView.Items.AddChild(NewNode, TUsedModule(UsedModuleList[I]).Name);
337 end;
338end;
339
340procedure TProducerTreeView.AddNodeProgram(Node: TTreeNode; Code: TProgram);
341var
342 NewNode: TTreeNode;
343 I: Integer;
344begin
345 NewNode := TreeView.Items.AddChild(Node, 'application');
346 for I := 0 to Code.Modules.Count - 1 do begin
347 AddNodeModule(NewNode, TModule(Code.Modules[I]));
348 end;
349end;
350
351procedure TProducerTreeView.Produce(Module: TModule);
352begin
353 with TreeView do begin
354 BeginUpdate;
355 Items.Clear;
356 AddNodeModule(TopItem, Module);
357 if Assigned(TopItem) then TopItem.Expand(True);
358 EndUpdate;
359 end;
360end;
361
362constructor TProducerTreeView.Create;
363begin
364 TreeView := TTreeView.Create(nil);
365end;
366
367destructor TProducerTreeView.Destroy;
368begin
369 TreeView.Free;
370 inherited Destroy;
371end;
372
373end.
374
Note: See TracBrowser for help on using the repository browser.