1 | unit UProducerTreeView;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, SourceCode, ComCtrls, Producer, StrUtils;
|
---|
7 |
|
---|
8 | type
|
---|
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 |
|
---|
41 | implementation
|
---|
42 |
|
---|
43 | { TProducerTreeView }
|
---|
44 |
|
---|
45 | procedure TProducerTreeView.AddNodeBeginEnd(Node: TTreeNode;
|
---|
46 | BeginEnd: TBeginEnd);
|
---|
47 | var
|
---|
48 | NewNode: TTreeNode;
|
---|
49 | NewNode2: TTreeNode;
|
---|
50 | I: Integer;
|
---|
51 | begin
|
---|
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]));
|
---|
55 | end;
|
---|
56 |
|
---|
57 | procedure TProducerTreeView.AddNodeWhileDo(Node: TTreeNode; WhileDo: TWhileDo);
|
---|
58 | var
|
---|
59 | NewNode: TTreeNode;
|
---|
60 | NewNode2: TTreeNode;
|
---|
61 | begin
|
---|
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);
|
---|
67 | end;
|
---|
68 |
|
---|
69 | procedure TProducerTreeView.AddNodeForToDo(Node: TTreeNode; ForToDo: TForToDo);
|
---|
70 | var
|
---|
71 | NewNode: TTreeNode;
|
---|
72 | NewNode2: TTreeNode;
|
---|
73 | NewNode3: TTreeNode;
|
---|
74 | I: Integer;
|
---|
75 | begin
|
---|
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;
|
---|
88 | end;
|
---|
89 |
|
---|
90 | procedure TProducerTreeView.AddNodeIfThenElse(Node: TTreeNode; IfThenElse: TIfThenElse);
|
---|
91 | var
|
---|
92 | NewNode: TTreeNode;
|
---|
93 | NewNode2: TTreeNode;
|
---|
94 | NewNode3: TTreeNode;
|
---|
95 | I: Integer;
|
---|
96 | begin
|
---|
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;
|
---|
106 | end;
|
---|
107 |
|
---|
108 | procedure TProducerTreeView.AddNodeMethodCall(Node: TTreeNode; Method: TFunctionCall);
|
---|
109 | var
|
---|
110 | NewNode: TTreeNode;
|
---|
111 | begin
|
---|
112 | NewNode := TreeView.Items.AddChild(Node, Method.FunctionRef.Name);
|
---|
113 | end;
|
---|
114 |
|
---|
115 | procedure TProducerTreeView.AddNodeAssignment(Node: TTreeNode; Assignment: TAssignment);
|
---|
116 | var
|
---|
117 | NewNode: TTreeNode;
|
---|
118 | begin
|
---|
119 | NewNode := TreeView.Items.AddChild(Node, Assignment.Target.Name + ' := ');
|
---|
120 | AddNodeExpression(NewNode, Assignment.Source);
|
---|
121 | end;
|
---|
122 |
|
---|
123 | procedure TProducerTreeView.AddNodeCommand(Node: TTreeNode; Command: TCommand);
|
---|
124 | begin
|
---|
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));
|
---|
142 | end;
|
---|
143 |
|
---|
144 | procedure TProducerTreeView.AddNodeExpression(Node: TTreeNode; Expression: TExpression
|
---|
145 | );
|
---|
146 | var
|
---|
147 | NewNode: TTreeNode;
|
---|
148 | begin
|
---|
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;
|
---|
160 | end;
|
---|
161 |
|
---|
162 | procedure TProducerTreeView.WriteNode(Target: TStringList; Node: TTreeNode;
|
---|
163 | Indetation: Integer);
|
---|
164 | var
|
---|
165 | I: Integer;
|
---|
166 | begin
|
---|
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 + '/>');
|
---|
174 | end;
|
---|
175 |
|
---|
176 | procedure TProducerTreeView.AssignToStringList(Target: TStringList);
|
---|
177 | begin
|
---|
178 | with Target do begin
|
---|
179 | Clear;
|
---|
180 | if Assigned(TreeView.TopItem) then
|
---|
181 | WriteNode(Target, TreeView.TopItem, 0);
|
---|
182 | end;
|
---|
183 | end;
|
---|
184 |
|
---|
185 | procedure TProducerTreeView.AddNodeVariableList(Node: TTreeNode;
|
---|
186 | Variables: TVariableList);
|
---|
187 | var
|
---|
188 | NewNode: TTreeNode;
|
---|
189 | NewNode2: TTreeNode;
|
---|
190 | I: Integer;
|
---|
191 | begin
|
---|
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;
|
---|
199 | end;
|
---|
200 |
|
---|
201 | procedure TProducerTreeView.AddNodeConstantList(Node: TTreeNode;
|
---|
202 | Constants: TConstantList);
|
---|
203 | var
|
---|
204 | NewNode: TTreeNode;
|
---|
205 | NewNode2: TTreeNode;
|
---|
206 | I: Integer;
|
---|
207 | begin
|
---|
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;
|
---|
215 | end;
|
---|
216 |
|
---|
217 | procedure TProducerTreeView.AddNodeTypeList(Node: TTreeNode;
|
---|
218 | Types: TTypeList);
|
---|
219 | var
|
---|
220 | NewNode: TTreeNode;
|
---|
221 | I: Integer;
|
---|
222 | begin
|
---|
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;
|
---|
229 | end;
|
---|
230 |
|
---|
231 | procedure TProducerTreeView.AddNodeType(Node: TTreeNode; AType: TType);
|
---|
232 | var
|
---|
233 | NewNode: TTreeNode;
|
---|
234 | NewNode2: TTreeNode;
|
---|
235 | begin
|
---|
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;
|
---|
255 | end;
|
---|
256 |
|
---|
257 | procedure TProducerTreeView.AddNodeMethodList(Node: TTreeNode;
|
---|
258 | Methods: TFunctionList);
|
---|
259 | var
|
---|
260 | NewNode: TTreeNode;
|
---|
261 | I: Integer;
|
---|
262 | begin
|
---|
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;
|
---|
276 | end;
|
---|
277 |
|
---|
278 | procedure TProducerTreeView.AddNodeParameterList(Node: TTreeNode;
|
---|
279 | Parameters: TParameterList);
|
---|
280 | var
|
---|
281 | I: Integer;
|
---|
282 | NewNode: TTreeNode;
|
---|
283 | begin
|
---|
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;
|
---|
291 | end;
|
---|
292 |
|
---|
293 | procedure TProducerTreeView.AddNodeTypeRecord(Node: TTreeNode; TypeRecord: TTypeRecord
|
---|
294 | );
|
---|
295 | var
|
---|
296 | I: Integer;
|
---|
297 | begin
|
---|
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;
|
---|
304 | end;
|
---|
305 |
|
---|
306 | procedure TProducerTreeView.AddNodeModule(Node: TTreeNode; Module: TModule);
|
---|
307 | var
|
---|
308 | NewNode: TTreeNode;
|
---|
309 | I: Integer;
|
---|
310 | TypeName: string;
|
---|
311 | begin
|
---|
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 |
|
---|
326 | end;
|
---|
327 |
|
---|
328 | procedure TProducerTreeView.AddNodeUses(Node: TTreeNode;
|
---|
329 | UsedModuleList: TUsedModuleList);
|
---|
330 | var
|
---|
331 | NewNode: TTreeNode;
|
---|
332 | I: Integer;
|
---|
333 | begin
|
---|
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;
|
---|
338 | end;
|
---|
339 |
|
---|
340 | procedure TProducerTreeView.AddNodeProgram(Node: TTreeNode; Code: TProgram);
|
---|
341 | var
|
---|
342 | NewNode: TTreeNode;
|
---|
343 | I: Integer;
|
---|
344 | begin
|
---|
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;
|
---|
349 | end;
|
---|
350 |
|
---|
351 | procedure TProducerTreeView.Produce(Module: TModule);
|
---|
352 | begin
|
---|
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;
|
---|
360 | end;
|
---|
361 |
|
---|
362 | constructor TProducerTreeView.Create;
|
---|
363 | begin
|
---|
364 | TreeView := TTreeView.Create(nil);
|
---|
365 | end;
|
---|
366 |
|
---|
367 | destructor TProducerTreeView.Destroy;
|
---|
368 | begin
|
---|
369 | TreeView.Free;
|
---|
370 | inherited Destroy;
|
---|
371 | end;
|
---|
372 |
|
---|
373 | end.
|
---|
374 |
|
---|