Repository navigation
Expand file tree
/
Copy pathDelphiAST.Serialize.Binary.pas
More file actions
335 lines (292 loc) · 8.08 KB
/
Copy pathDelphiAST.Serialize.Binary.pas
File metadata and controls
335 lines (292 loc) · 8.08 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
334
335
unit DelphiAST.Serialize.Binary;
{$IFDEF FPC}{$MODE DELPHI}{$ENDIF}
interface
uses
Classes,
{$IFDEF FPC_DOTTEDUNITS}System.Generics.Collections{$ELSE}Generics.Collections{$ENDIF},
DelphiAST.Consts,
DelphiAST.Classes;
type
TNodeClass = (ntSyntax, ntCompound, ntValued, ntComment);
TBinarySerializer = class
strict private
FStream : TStream;
FStringList : TStringList;
FStringTable: TDictionary<string,integer>;
strict protected
function CheckSignature: boolean;
function CheckVersion: boolean;
function CreateNode(nodeClass: TNodeClass; nodeType: TSyntaxNodeType): TSyntaxNode;
function ReadNode(var node: TSyntaxNode): boolean;
function ReadNumber(var num: cardinal): Boolean;
function ReadString(var str: string): boolean;
function WriteNode(Node: TSyntaxNode): Boolean;
function WriteNumber(Num: cardinal): Boolean;
function WriteString(const S: string): Boolean;
public
function Read(Stream: TStream; var Root: TSyntaxNode): boolean;
function Write(Stream: TStream; Root: TSyntaxNode): boolean;
end;
implementation
uses
SysUtils;
var
CSignature: AnsiString = 'DAST binary file'#26;
function TBinarySerializer.CheckSignature: boolean;
var
sig: AnsiString;
begin
SetLength(sig, Length(CSignature));
Result := (FStream.Read(sig[1], Length(CSignature)) = Length(CSignature))
and (sig = CSignature);
end;
function TBinarySerializer.CheckVersion: boolean;
var
version: Integer;
begin
Result := (FStream.Read(version, 4) = 4)
and ((version AND $FFFF0000) = $01000000);
end;
function TBinarySerializer.CreateNode(nodeClass: TNodeClass; nodeType: TSyntaxNodeType):
TSyntaxNode;
begin
case nodeClass of
ntSyntax: Result := TSyntaxNode.Create(nodeType);
ntCompound: Result := TCompoundSyntaxNode.Create(nodeType);
ntValued: Result := TValuedSyntaxNode.Create(nodeType);
ntComment: Result := TCommentNode.Create(nodeType);
else raise Exception.Create('TBinarySerializer.CreateNode: Unexpected node class');
end;
end;
function TBinarySerializer.Read(Stream: TStream; var Root: TSyntaxNode): boolean;
var
node: TSyntaxNode;
begin
Result := false;
FStringList := TStringList.Create;
try
FStream := Stream;
if not CheckSignature then
Exit;
if not CheckVersion then
Exit;
if not ReadNode(node) then
Exit;
Root := node;
finally FStringList.Free; end;
Result := true;
end;
function TBinarySerializer.ReadNode(var node: TSyntaxNode): boolean;
var
childNode: TSyntaxNode;
i : Integer;
nodeClass: TNodeClass;
num : cardinal;
numSub: cardinal;
str : string;
begin
Result := false;
node := nil;
if (not ReadNumber(num)) or (num > cardinal(Ord(High(TNodeClass)))) then
Exit;
nodeClass := TNodeClass(num);
if (not ReadNumber(num)) or (num > Ord(High(TSyntaxNodeType))) then
Exit;
node := CreateNode(nodeClass, TSyntaxNodeType(num));
try
if (not ReadNumber(num)) or (num > cardinal(High(integer))) then
Exit;
Node.Col := num;
if (not ReadNumber(num)) or (num > cardinal(High(integer))) then
Exit;
Node.Line := num;
case nodeClass of
ntCompound:
begin
if (not ReadNumber(num)) or (num > cardinal(High(integer))) then
Exit;
TCompoundSyntaxNode(Node).EndCol := num;
if (not ReadNumber(num)) or (num > cardinal(High(integer))) then
Exit;
TCompoundSyntaxNode(Node).EndLine := num;
end;
ntValued:
begin
if not ReadString(str) then
Exit;
TValuedSyntaxNode(Node).Value := str;
end;
ntComment:
begin
if not ReadString(str) then
Exit;
TCommentNode(Node).Text := str;
end;
end;
if not ReadNumber(numSub) then
Exit;
for i := 1 to numSub do begin
if (not ReadNumber(num)) or (num > cardinal(Ord(High(TAttributeName)))) then
Exit;
if not ReadString(str) then
Exit;
Node.SetAttribute(TAttributeName(num), str);
end;
if not ReadNumber(numSub) then
Exit;
for i := 1 to numSub do begin
if not ReadNode(childNode) then
Exit;
Node.AddChild(childNode);
end;
Result := true;
finally
if not Result then begin
node.Free;
node := nil;
end;
end;
end;
function TBinarySerializer.ReadNumber(var num: cardinal): Boolean;
var
lowPart: byte;
shift : Integer;
begin
Result := false;
shift := 0;
num := 0;
repeat
if FStream.Read(lowPart, 1) <> 1 then
Exit;
num := num OR ((lowPart AND $7F) SHL shift);
Inc(shift, 7);
until (lowPart AND $80) = 0;
Result := true;
end;
function TBinarySerializer.ReadString(var str: string): boolean;
var
id: integer;
len: cardinal;
u8: UTF8String;
begin
Result := false;
if not ReadNumber(len) then
Exit;
if (len SHR 24) = $FF then begin
id := len AND $00FFFFFF;
if id >= FStringList.Count then
Exit;
str := FStringList[id];
end
else begin
SetLength(u8, len);
if len > 0 then
if cardinal(FStream.Read(u8[1], len)) <> len then
Exit;
str := UTF8ToUnicodeString(u8);
if Length(Str) > 4 then
FStringList.Add(str);
end;
Result := true;
end;
function TBinarySerializer.Write(Stream: TStream; Root: TSyntaxNode): boolean;
var
version: Integer;
begin
Result := false;
FStringTable := TDictionary<string,integer>.Create;
try
FStream := Stream;
if FStream.Write(CSignature[1], Length(CSignature)) <> Length(CSignature) then
Exit;
version := $01000000;
if FStream.Write(version, 4) <> 4 then
Exit;
if not WriteNode(Root) then
Exit;
finally FStringTable.Free; end;
Result := true;
end;
function TBinarySerializer.WriteNode(Node: TSyntaxNode): Boolean;
var
attr : TAttributeEntry;
childNode: TSyntaxNode;
nodeClass: TNodeClass;
begin
Result := false;
if Node is TCompoundSyntaxNode then
nodeClass := ntCompound
else if Node is TValuedSyntaxNode then
nodeClass := ntValued
else if Node is TCommentNode then
nodeClass := ntComment
else
nodeClass := ntSyntax;
if not WriteNumber(Ord(nodeClass)) then Exit;
if not WriteNumber(Ord(Node.Typ)) then Exit;
if not WriteNumber(Node.Col) then Exit;
if not WriteNumber(Node.Line) then Exit;
case nodeClass of
ntCompound:
begin
if not WriteNumber(TCompoundSyntaxNode(Node).EndCol) then Exit;
if not WriteNumber(TCompoundSyntaxNode(Node).EndLine) then Exit;
end;
ntValued:
if not WriteString(TValuedSyntaxNode(Node).Value) then Exit;
ntComment:
if not WriteString(TCommentNode(Node).Text) then Exit;
end;
if not WriteNumber(Length(Node.Attributes)) then Exit;
for attr in Node.Attributes do begin // causes dynamic array assignment, yuck
if not WriteNumber(Ord(attr.Key)) then Exit;
if not WriteString(attr.Value) then Exit;
end;
if not WriteNumber(Length(Node.ChildNodes)) then Exit;
for childNode in Node.ChildNodes do // causes dynamic array assignment, yuck
if not WriteNode(childNode) then Exit;
Result := true;
end;
function TBinarySerializer.WriteNumber(Num: cardinal): Boolean;
var
lowPart: byte;
begin
Result := false;
repeat
lowPart := Num AND $7F;
Num := Num SHR 7;
if Num <> 0 then
lowPart := lowPart OR $80;
if FStream.Write(lowPart, 1) <> 1 then
Exit;
until Num = 0;
Result := true;
end;
function TBinarySerializer.WriteString(const S: string): Boolean;
var
i: Integer;
id: integer;
u8: UTF8String;
begin
Result := false;
if (Length(S) > 4) and FStringTable.TryGetValue(S, id) then begin
if not WriteNumber(cardinal(id) OR $FF000000) then
Exit;
end
else begin
if Length(S) > 4 then begin
FStringTable.Add(S, FStringTable.Count);
if FStringTable.Count > $FFFFFF then
raise Exception.Create('TBinarySerializer.WriteString: Too many strings!');
end;
u8 := UTF8Encode(s);
i := Length(u8);
if not WriteNumber(i) then
Exit;
if i > 0 then
if FStream.Write(u8[1], i) <> i then
Exit;
end;
Result := true;
end;
end.