-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathamScript.Debugger.Frame.AST.pas
More file actions
333 lines (274 loc) · 8.68 KB
/
amScript.Debugger.Frame.AST.pas
File metadata and controls
333 lines (274 loc) · 8.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
unit amScript.Debugger.Frame.AST;
(*
* Copyright © 2019 Anders Melander
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*)
interface
uses
Generics.Collections,
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, ExtCtrls, ImgList, Menus, ActnList,
cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters, dxSkinsCore,
cxContainer, cxEdit, cxCustomData, cxStyles, cxTL, cxTextEdit, cxTLdxBarBuiltInMenu, cxInplaceContainer,
cxClasses, cxTLData, dxSkinsdxBarPainter, dxBar, cxFilter, dxScrollbarAnnotations,
dwsUtils,
dwsExprs,
dwsSymbols,
dwsErrors,
amScript.Debugger.API;
type
TFrame = TScriptDebuggerFrame;
TScriptDebuggerASTFrame = class(TFrame)
TreeListAST: TcxTreeList;
cxTreeList1Column1: TcxTreeListColumn;
cxStyleRepository1: TcxStyleRepository;
StyleBackground: TcxStyle;
TreeListASTColumn1: TcxTreeListColumn;
procedure TreeListASTDblClick(Sender: TObject);
strict private
procedure LoadNode(Node: TcxTreeListNode; Expression: TExprBase);
procedure UpdateInfo; overload;
procedure UpdateInfo(Expression: TExprBase); overload;
strict protected
// IScriptDebuggerWindow
procedure Initialize(const ADebugger: IScriptDebugger; AImageList, AImageListSymbols: TCustomImageList); override;
strict protected
// IScriptDebuggerWindow
procedure ScriptDebuggerNotification(Notification: TScriptDebuggerNotification); override;
public
end;
implementation
{$R *.dfm}
uses
dwsDebugger,
dwsInfo,
dwsCompiler,
dwsDataContext,
dwsUnitSymbols,
amScript.Debugger.Frame.Symbols;
procedure TScriptDebuggerASTFrame.Initialize(const ADebugger: IScriptDebugger; AImageList, AImageListSymbols: TCustomImageList);
begin
inherited Initialize(ADebugger, AImageList, AImageListSymbols);
TreeListAST.Images := AImageListSymbols;
UpdateInfo;
if (Debugger.GetDebugger.State = dsDebugSuspended) then
UpdateInfo(Debugger.GetDebugger.CurrentExpression);
end;
procedure TScriptDebuggerASTFrame.ScriptDebuggerNotification(Notification: TScriptDebuggerNotification);
begin
inherited;
case Notification of
dnCompiling:
TreeListAST.Clear;
dnCompiled:
UpdateInfo;
dnDebugSuspended:
UpdateInfo(Debugger.GetDebugger.CurrentExpression);
else
TreeListAST.FocusedNode := nil;
TreeListAST.ClearSelection;
end;
end;
function ExpressionToSomethingNice(Expression: TExprBase): string;
begin
Result := Expression.FuncSymQualifiedName;
if (Expression is TFuncExprBase) then
begin
if (TFuncExprBase(Expression).FuncSym <> nil) then
Result := TFuncExprBase(Expression).FuncSym.Description;
end;
end;
procedure TScriptDebuggerASTFrame.LoadNode(Node: TcxTreeListNode; Expression: TExprBase);
var
ChildNode: TcxTreeListNode;
i: integer;
s: string;
begin
if (Expression = nil) then
exit;
Node.ImageIndex := -1;
Node.Values[0] := Expression.ClassName;
s := ExpressionToSomethingNice(Expression);
if (s <> '') then
Node.Values[1] := s;
Node.Data := Expression;
for i := 0 to Expression.SubExprCount-1 do
begin
ChildNode := Node.AddChild;
LoadNode(ChildNode, Expression.SubExpr[i]);
end;
end;
procedure TScriptDebuggerASTFrame.TreeListASTDblClick(Sender: TObject);
var
Node: TcxTreeListNode;
Expression: TExprBase;
begin
Node := TreeListAST.FocusedNode;
if (Node = nil) or (Node.Data = nil) then
Exit;
Expression := TExprBase(Node.Data);
if (not Expression.ScriptPos.Defined) then
exit;
Debugger.ViewScriptPos(Expression.ScriptPos);
end;
function FindExpression(ANode: TcxTreeListNode; AData: Pointer): Boolean;
begin
Result := (ANode.Data <> nil) and (TExprBase(ANode.Data).ScriptPos.Defined) and (TExprBase(ANode.Data).ScriptPos.SamePosAs(TExprBase(AData).ScriptPos));
end;
procedure TScriptDebuggerASTFrame.UpdateInfo(Expression: TExprBase);
var
Node: TcxTreeListNode;
begin
if (Expression = nil) or (not Expression.ScriptPos.Defined) then
exit;
Node := TreeListAST.Find(Expression, nil, False, True, FindExpression);
if (Node <> nil) then
begin
Node.Focused := True;
Node.Selected := True;
Node.MakeVisible;
end;
end;
procedure TScriptDebuggerASTFrame.UpdateInfo;
var
LoadedUnits: TList<TUnitMainSymbol>;
procedure LoadProcSymbol(ParentNode: TcxTreeListNode; Symbol: TFuncSymbol);
var
ExecSelf: TObject;
Proc: TdwsProcedure;
Node: TcxTreeListNode;
ImageIndex: integer;
begin
if (Symbol = nil) then
exit;
if (Symbol.Executable = nil) then
Exit;
ExecSelf := Symbol.Executable.GetSelf;
if not(ExecSelf is TdwsProcedure) then
Exit;
Proc := TdwsProcedure(ExecSelf);
Node := ParentNode.AddChild;
Node.Values[0] := Symbol.QualifiedName;
ImageIndex := 0;
GetSymbolDescription(Symbol, ImageIndex);
Node.ImageIndex := ImageIndex;
Node := Node.AddChild;
Node.ImageIndex := -1;
LoadNode(Node, Proc.Expr);
end;
procedure LoadSymbol(ParentNode: TcxTreeListNode; Symbol: TSymbol);
var
Member: TSymbol;
begin
if (Symbol = nil) then
exit;
if (Symbol is TCompositeTypeSymbol) then
begin
for Member in TCompositeTypeSymbol(Symbol).Members do
LoadSymbol(ParentNode, Member);
end else
if (Symbol is TFuncSymbol) then
LoadProcSymbol(ParentNode, TFuncSymbol(Symbol));
end;
procedure LoadSymbolTable(ParentNode: TcxTreeListNode; SymbolTable: TSymbolTable);
var
i: integer;
begin
if (SymbolTable = nil) then
exit;
for i := 0 to SymbolTable.Count-1 do
LoadSymbol(ParentNode, SymbolTable[i]);
end;
procedure LoadUnit(UnitMainSymbol: TUnitMainSymbol);
var
Node, ChildNode, ChildChildNode: TcxTreeListNode;
ImageIndex: integer;
begin
if (LoadedUnits.IndexOf(UnitMainSymbol) <> -1) then
exit;
LoadedUnits.Add(UnitMainSymbol);
Node := TreeListAST.Add;
Node.Values[0] := GetSymbolDescription(UnitMainSymbol, ImageIndex);
Node.ImageIndex := ImageIndex;
LoadSymbolTable(Node, UnitMainSymbol.InterfaceTable);
LoadSymbolTable(Node, UnitMainSymbol.ImplementationTable);
LoadSymbolTable(Node, UnitMainSymbol.Table);
if (UnitMainSymbol.InitializationExpr <> nil) then
begin
ChildNode := Node.AddChild;
ChildNode.Values[0] := 'Initialization';
ChildNode.ImageIndex := 20;
ChildChildNode := ChildNode.AddChild;
LoadNode(ChildChildNode, UnitMainSymbol.InitializationExpr);
end;
if (UnitMainSymbol.FinalizationExpr <> nil) then
begin
ChildNode := Node.AddChild;
ChildNode.Values[0] := 'Finalization';
ChildNode.ImageIndex := 20;
ChildChildNode := ChildNode.AddChild;
LoadNode(ChildChildNode, UnitMainSymbol.FinalizationExpr);
end;
end;
var
ScriptProgram: IdwsProgram;
Node, ChildNode: TcxTreeListNode;
i: integer;
// PublishedSymbols: TSimpleSymbolList;
begin
TreeListAST.BeginUpdate;
try
TreeListAST.Clear;
if (Debugger.GetDebugger.Execution <> nil) then
ScriptProgram := Debugger.GetDebugger.Execution.Prog
else
ScriptProgram := nil;
if (ScriptProgram = nil) then
ScriptProgram := Debugger.GetProgram;
if (ScriptProgram = nil) then
ScriptProgram := Debugger.GetCompiledScript;
if (ScriptProgram = nil) then
exit;
Node := TreeListAST.Add;
Node.Values[0] := 'Main';
Node.ImageIndex := 20;
LoadSymbolTable(Node, ScriptProgram.ProgramObject.Table);
ChildNode := Node.AddChild;
LoadNode(ChildNode, ScriptProgram.ProgramObject.Expr);
(* Doesn't work. Was an attempt to get at the implementation section
PublishedSymbols := ScriptProgram.ProgramObject.Root.CollectAllPublishedSymbols(False);
try
for i := 0 to PublishedSymbols.Count-1 do
LoadSymbol(Node, PublishedSymbols[i]);
finally
PublishedSymbols.Free;
end;
*)
ChildNode := Node.AddChild;
ChildNode.Values[0] := 'Initialization';
ChildNode.ImageIndex := 20;
ChildNode := ChildNode.AddChild;
LoadNode(ChildNode, ScriptProgram.ProgramObject.InitExpr);
ChildNode := Node.AddChild;
ChildNode.Values[0] := 'Finalization';
ChildNode.ImageIndex := 20;
ChildNode := ChildNode.AddChild;
LoadNode(ChildNode, ScriptProgram.ProgramObject.FinalExpr);
LoadedUnits := TList<TUnitMainSymbol>.Create;
try
for i := 0 to ScriptProgram.ProgramObject.UnitMains.Count-1 do
LoadUnit(ScriptProgram.ProgramObject.UnitMains[i]);
finally
LoadedUnits.Free;
end;
finally
TreeListAST.EndUpdate;
end;
end;
initialization
RegisterClass(TScriptDebuggerASTFrame);
finalization
end.