-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAST.Serialize.pas
More file actions
433 lines (387 loc) · 10.7 KB
/
AST.Serialize.pas
File metadata and controls
433 lines (387 loc) · 10.7 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
unit AST.Serialize;
interface
uses
SysUtils, Classes, Generics.Collections,
DelphiAST.Consts, DelphiAST.Classes;
type
TNodeClass = (ncSyntax, ncCompound, ncValued, ncComment);
TFullASTSerializer = class
strict private
FStream: TStream;
FStringList: TStringList;
FStringTable: TDictionary<string, Integer>;
function ReadNumber(var Num: Cardinal): Boolean;
function ReadString(var Str: string): Boolean;
function ReadNode(var Node: TSyntaxNode): Boolean;
function WriteNumber(Num: Cardinal): Boolean;
function WriteString(const S: string): Boolean;
function WriteNode(Node: TSyntaxNode): Boolean;
function CheckSignature: Boolean;
function CheckVersion: Boolean;
function CreateNode(AClass: TNodeClass; AType: TSyntaxNodeType): TSyntaxNode;
public
class function WriteToStream(Stream: TStream; Root: TSyntaxNode;
ModifiedAt: TDateTime; const SourcePath: string): Boolean;
class function ReadFromStream(Stream: TStream; out Root: TSyntaxNode;
out ModifiedAt: TDateTime; out SourcePath: string): Boolean;
class function SaveToFile(const FileName: string; Root: TSyntaxNode;
ModifiedAt: TDateTime; const SourcePath: string): Boolean;
class function LoadFromFile(const FileName: string; out Root: TSyntaxNode;
out ModifiedAt: TDateTime; out SourcePath: string): Boolean;
end;
implementation
const
CSignature: AnsiString = 'DAST_MCP_V01'#26;
CVersion: Cardinal = $03000000;
{ TFullASTSerializer }
function TFullASTSerializer.CheckSignature: Boolean;
var
Sig: AnsiString;
begin
SetLength(Sig, Length(CSignature));
Result := (FStream.Read(Sig[1], Length(CSignature)) = Length(CSignature))
and (Sig = CSignature);
end;
function TFullASTSerializer.CheckVersion: Boolean;
var
Ver: Cardinal;
begin
Result := (FStream.Read(Ver, 4) = 4) and (Ver = CVersion);
end;
function TFullASTSerializer.CreateNode(AClass: TNodeClass;
AType: TSyntaxNodeType): TSyntaxNode;
begin
case AClass of
ncSyntax: Result := TSyntaxNode.Create(AType);
ncCompound: Result := TCompoundSyntaxNode.Create(AType);
ncValued: Result := TValuedSyntaxNode.Create(AType);
ncComment: Result := TCommentNode.Create(AType);
else
raise Exception.Create('TFullASTSerializer.CreateNode: Unexpected node class');
end;
end;
function TFullASTSerializer.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 TFullASTSerializer.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 TFullASTSerializer.ReadNode(var Node: TSyntaxNode): Boolean;
var
ChildNode: TSyntaxNode;
I: Integer;
NC: TNodeClass;
Num, NumSub: Cardinal;
Str: string;
begin
Result := False;
Node := nil;
if (not ReadNumber(Num)) or (Num > Cardinal(Ord(High(TNodeClass)))) then
Exit;
NC := TNodeClass(Num);
if (not ReadNumber(Num)) or (Num > Ord(High(TSyntaxNodeType))) then
Exit;
Node := CreateNode(NC, TSyntaxNodeType(Num));
try
// Col
if (not ReadNumber(Num)) or (Num > Cardinal(High(Integer))) then Exit;
Node.Col := Num;
// Line
if (not ReadNumber(Num)) or (Num > Cardinal(High(Integer))) then Exit;
Node.Line := Num;
// LineSeq
if (not ReadNumber(Num)) or (Num > Cardinal(High(Integer))) then Exit;
Node.LineSeq := Num;
// FileName
if not ReadString(Str) then Exit;
Node.FileName := Str;
case NC of
ncCompound:
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;
ncValued:
begin
if not ReadString(Str) then Exit;
TValuedSyntaxNode(Node).Value := Str;
end;
ncComment:
begin
if not ReadString(Str) then Exit;
TCommentNode(Node).Text := Str;
end;
end;
// Attributes
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;
// Children
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 TFullASTSerializer.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 TFullASTSerializer.WriteString(const S: string): Boolean;
var
Id, Len: 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('TFullASTSerializer.WriteString: Too many strings!');
end;
U8 := UTF8Encode(S);
Len := Length(U8);
if not WriteNumber(Len) then
Exit;
if Len > 0 then
if FStream.Write(U8[1], Len) <> Len then
Exit;
end;
Result := True;
end;
function TFullASTSerializer.WriteNode(Node: TSyntaxNode): Boolean;
var
Attr: TAttributeEntry;
ChildNode: TSyntaxNode;
NC: TNodeClass;
begin
Result := False;
if Node is TCompoundSyntaxNode then
NC := ncCompound
else if Node is TValuedSyntaxNode then
NC := ncValued
else if Node is TCommentNode then
NC := ncComment
else
NC := ncSyntax;
if not WriteNumber(Ord(NC)) then Exit;
if not WriteNumber(Ord(Node.Typ)) then Exit;
if not WriteNumber(Node.Col) then Exit;
if not WriteNumber(Node.Line) then Exit;
if not WriteNumber(Node.LineSeq) then Exit;
if not WriteString(Node.FileName) then Exit;
case NC of
ncCompound:
begin
if not WriteNumber(TCompoundSyntaxNode(Node).EndCol) then Exit;
if not WriteNumber(TCompoundSyntaxNode(Node).EndLine) then Exit;
end;
ncValued:
if not WriteString(TValuedSyntaxNode(Node).Value) then Exit;
ncComment:
if not WriteString(TCommentNode(Node).Text) then Exit;
end;
if not WriteNumber(Length(Node.Attributes)) then Exit;
for Attr in Node.Attributes do
begin
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
if not WriteNode(ChildNode) then Exit;
Result := True;
end;
class function TFullASTSerializer.WriteToStream(Stream: TStream;
Root: TSyntaxNode; ModifiedAt: TDateTime; const SourcePath: string): Boolean;
var
Ser: TFullASTSerializer;
Ver: Cardinal;
ModDbl: Double;
PathU8: UTF8String;
PathLen: Cardinal;
begin
Result := False;
Ser := TFullASTSerializer.Create;
try
Ser.FStringTable := TDictionary<string, Integer>.Create;
try
Ser.FStream := Stream;
// Signature
if Stream.Write(CSignature[1], Length(CSignature)) <> Length(CSignature) then
Exit;
// Version
Ver := CVersion;
if Stream.Write(Ver, 4) <> 4 then
Exit;
// ModifiedAt
ModDbl := ModifiedAt;
if Stream.Write(ModDbl, SizeOf(Double)) <> SizeOf(Double) then
Exit;
// SourcePath (length-prefixed UTF-8)
PathU8 := UTF8Encode(SourcePath);
PathLen := Length(PathU8);
if not Ser.WriteNumber(PathLen) then
Exit;
if PathLen > 0 then
if Stream.Write(PathU8[1], PathLen) <> Integer(PathLen) then
Exit;
// Node tree
if not Ser.WriteNode(Root) then
Exit;
Result := True;
finally
Ser.FStringTable.Free;
end;
finally
Ser.Free;
end;
end;
class function TFullASTSerializer.ReadFromStream(Stream: TStream;
out Root: TSyntaxNode; out ModifiedAt: TDateTime;
out SourcePath: string): Boolean;
var
Ser: TFullASTSerializer;
ModDbl: Double;
PathLen: Cardinal;
PathU8: UTF8String;
Node: TSyntaxNode;
begin
Result := False;
Root := nil;
ModifiedAt := 0;
SourcePath := '';
Ser := TFullASTSerializer.Create;
try
Ser.FStringList := TStringList.Create;
try
Ser.FStream := Stream;
if not Ser.CheckSignature then Exit;
if not Ser.CheckVersion then Exit;
// ModifiedAt
if Stream.Read(ModDbl, SizeOf(Double)) <> SizeOf(Double) then Exit;
ModifiedAt := ModDbl;
// SourcePath
if not Ser.ReadNumber(PathLen) then Exit;
if PathLen > 0 then
begin
SetLength(PathU8, PathLen);
if Cardinal(Stream.Read(PathU8[1], PathLen)) <> PathLen then Exit;
SourcePath := UTF8ToUnicodeString(PathU8);
end;
// Node tree
if not Ser.ReadNode(Node) then Exit;
Root := Node;
Result := True;
finally
Ser.FStringList.Free;
end;
finally
Ser.Free;
end;
end;
class function TFullASTSerializer.SaveToFile(const FileName: string;
Root: TSyntaxNode; ModifiedAt: TDateTime; const SourcePath: string): Boolean;
var
FS: TFileStream;
begin
FS := TFileStream.Create(FileName, fmCreate);
try
Result := WriteToStream(FS, Root, ModifiedAt, SourcePath);
finally
FS.Free;
end;
end;
class function TFullASTSerializer.LoadFromFile(const FileName: string;
out Root: TSyntaxNode; out ModifiedAt: TDateTime;
out SourcePath: string): Boolean;
var
FS: TFileStream;
MS: TMemoryStream;
begin
FS := TFileStream.Create(FileName, fmOpenRead or fmShareDenyNone);
try
MS := TMemoryStream.Create;
try
MS.CopyFrom(FS, 0); // slurp entire file at once
MS.Position := 0;
Result := ReadFromStream(MS, Root, ModifiedAt, SourcePath);
finally
MS.Free;
end;
finally
FS.Free;
end;
end;
end.