-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathCodeElement.pas
More file actions
112 lines (92 loc) · 2.59 KB
/
CodeElement.pas
File metadata and controls
112 lines (92 loc) · 2.59 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
unit CodeElement;
interface
uses
Classes, Types, Generics.Collections, UncountedInterfacedObject, WriterIntf;
type
TCodeElementClass = class of TCodeElement;
TCodeElement = class(TUncountedInterfacedObject, IWriter)
private
FName: string;
FSubElements: TObjectList<TCodeElement>;
FLine: Integer;
protected
FSource: TStringList;
procedure Write(ALine: string); virtual;
procedure WriteList(AList: TStrings); virtual;
procedure AddMapping(AElement: TObject; AOffset: Integer = 0; AHideName: Boolean = False); virtual;
function GetElementInScope(AName: string; AType: TCodeElementClass; AScope: TObjectList<TCodeElement>): TCodeElement;
public
constructor Create(AName: string);
destructor Destroy(); override;
function GetElement(AName: string; AType: TCodeElementClass): TCodeElement;
function GetUniqueID(APrefix: string = ''): string;
procedure GetDCPUSource(AWriter: IWriter); virtual;
property Name: string read FName write FName;
property SubElements: TObjectList<TCodeElement> read FSubElements;
property Line: Integer read FLine write FLine;
end;
implementation
uses
SysUtils;
var
GID: Integer = 0;
{ TCodeElement }
procedure TCodeElement.AddMapping;
begin
// we ignore it here :P
end;
constructor TCodeElement.Create(AName: string);
begin
FName := AName;
FLine := -1;
FSubElements := TObjectList<TCodeElement>.Create();
FSource := TStringList.Create();
end;
destructor TCodeElement.Destroy;
begin
FSource.Free;
inherited;
end;
procedure TCodeElement.GetDCPUSource;
var
LElement: TCodeElement;
begin
for LElement in FSubElements do
begin
LElement.GetDCPUSource(AWriter);
end;
end;
function TCodeElement.GetElement(AName: string;
AType: TCodeElementClass): TCodeElement;
begin
Result := GetElementInScope(AName, AType, FSubElements);
end;
function TCodeElement.GetElementInScope(AName: string; AType: TCodeElementClass;
AScope: TObjectList<TCodeElement>): TCodeElement;
var
LElement: TCodeElement;
begin
Result := nil;
for LElement in AScope do
begin
if SameText(AName, LElement.Name) and LElement.InheritsFrom(AType) then
begin
Result := LElement;
Break;
end;
end;
end;
function TCodeElement.GetUniqueID(APrefix: string = ''): string;
begin
Result := APrefix+IntToHex(GID, 4);
Inc(GID);
end;
procedure TCodeElement.Write(ALine: string);
begin
FSource.Add(ALine);
end;
procedure TCodeElement.WriteList(AList: TStrings);
begin
FSource.AddStrings(AList);
end;
end.