-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAST.Parser.pas
More file actions
1013 lines (904 loc) · 25.2 KB
/
AST.Parser.pas
File metadata and controls
1013 lines (904 loc) · 25.2 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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit AST.Parser;
interface
uses
SysUtils, Classes, Generics.Collections, IOUtils, System.SyncObjs,
DelphiAST, DelphiAST.Classes, DelphiAST.Consts,
SimpleParser.Lexer.Types;
type
TCachedTree = record
Node: TSyntaxNode;
ModifiedAt: TDateTime;
end;
TSimpleIncludeHandler = class(TInterfacedObject, IIncludeHandler)
private
FRoots: TArray<string>;
public
constructor Create(const AProjectRoot: string); overload;
constructor Create(const ARoots: TArray<string>); overload;
function GetIncludeFileContent(const ParentFileName, IncludeName: string;
out Content: string; out FileName: string): Boolean;
end;
TParseState = (psIdle, psParsing);
TParseStatus = record
State: TParseState;
TotalFiles: Integer;
ParsedFiles: Integer;
CachedFiles: Integer;
FailedFiles: Integer;
end;
TASTParser = class
private
FRoots: TArray<string>;
FCache: TDictionary<string, TCachedTree>;
FIncludeHandler: IIncludeHandler;
FLock: TLightweightMREW;
FCacheDir: string;
FWatcher: TObject;
FParseState: Integer;
FHasParsed: Integer;
FTotalFiles: Integer;
FParsedFiles: Integer;
FCachedFiles: Integer;
FFailedFiles: Integer;
FProjectFiles: TList<string>;
FRewalking: Integer;
FProjectFilesLock: TLightweightMREW;
FParseThread: TThread;
FParseThreadLock: TCriticalSection;
FCancelled: Integer;
FFileIndex: TDictionary<string, string>;
FExcludeFiles: TArray<string>;
procedure WaitForParseThread;
procedure BuildFileIndex;
function GetProjectRoot: string;
function ResolveUnitFile(const UnitName: string): string;
procedure ParseProjectFiles;
procedure InitCacheDir;
function GetCacheFilePath(const Key: string): string;
procedure SaveCacheEntryToDisk(const Key: string; const Entry: TCachedTree);
function TryLoadCacheEntryFromDisk(const CacheFile: string;
out Key: string; out Entry: TCachedTree): Boolean;
procedure LoadPersistedCache;
procedure HandleFileChanged(const AFullPath: string);
public
constructor Create(const AProjectRoot: string); overload;
constructor Create(const ARoots: TArray<string>); overload;
destructor Destroy; override;
function ListFiles(const NameFilter: string = ''): TArray<string>;
function ParseFile(const AFileName: string): TSyntaxNode;
procedure ParseAllFiles;
function GetAllTrees: TArray<TPair<string, TSyntaxNode>>;
procedure ClearCache;
procedure InvalidateFile(const AFullPath: string);
function ResolveFilePath(const AFileName: string): string;
procedure Reconfigure(const ARoots: TArray<string>; const AExcludeFiles: TArray<string> = nil);
function IsConfigured: Boolean;
function IsParsing: Boolean;
function IsReady: Boolean;
function GetParseStatus: TParseStatus;
property ProjectRoot: string read GetProjectRoot;
end;
implementation
uses
System.Hash, System.Masks, AST.Serialize, AST.Watcher, AST.Query;
{ TSimpleIncludeHandler }
constructor TSimpleIncludeHandler.Create(const AProjectRoot: string);
begin
inherited Create;
FRoots := TArray<string>.Create(AProjectRoot);
end;
constructor TSimpleIncludeHandler.Create(const ARoots: TArray<string>);
begin
inherited Create;
FRoots := Copy(ARoots);
end;
function TSimpleIncludeHandler.GetIncludeFileContent(
const ParentFileName, IncludeName: string;
out Content: string; out FileName: string): Boolean;
var
Dir, FullPath: string;
I: Integer;
begin
Result := False;
Content := '';
FileName := '';
// Search parent directory first
Dir := ExtractFilePath(ParentFileName);
FullPath := TPath.Combine(Dir, IncludeName);
if FileExists(FullPath) then
begin
FileName := FullPath;
Content := TFile.ReadAllText(FullPath);
Exit(True);
end;
// Then search all roots
for I := 0 to High(FRoots) do
begin
FullPath := TPath.Combine(FRoots[I], IncludeName);
if FileExists(FullPath) then
begin
FileName := FullPath;
Content := TFile.ReadAllText(FullPath);
Exit(True);
end;
end;
end;
{ TASTParser }
function TASTParser.GetProjectRoot: string;
begin
if Length(FRoots) > 0 then
Result := FRoots[0]
else
Result := '';
end;
constructor TASTParser.Create(const AProjectRoot: string);
begin
Create(TArray<string>.Create(AProjectRoot));
end;
constructor TASTParser.Create(const ARoots: TArray<string>);
var
I: Integer;
Thread: TThread;
begin
inherited Create;
FCache := TDictionary<string, TCachedTree>.Create;
FProjectFiles := TList<string>.Create;
FFileIndex := TDictionary<string, string>.Create;
FRewalking := 0;
FParseThreadLock := TCriticalSection.Create;
FCancelled := 0;
if Length(ARoots) > 0 then
begin
SetLength(FRoots, Length(ARoots));
for I := 0 to High(ARoots) do
FRoots[I] := IncludeTrailingPathDelimiter(ExpandFileName(ARoots[I]));
FIncludeHandler := TSimpleIncludeHandler.Create(FRoots);
InitCacheDir;
// Start file watcher
FWatcher := TDirectoryWatcher.Create(FRoots,
procedure(APath: string)
begin
HandleFileChanged(APath);
end);
TDirectoryWatcher(FWatcher).Start;
// Eager-parse project files (dependency-driven) in background - tracked thread
Thread := TThread.CreateAnonymousThread(
procedure
begin
try
ParseProjectFiles;
except
on E: Exception do
WriteLn(ErrOutput, '[delphi-ast] Background parse failed: ' + E.Message);
end;
end);
Thread.FreeOnTerminate := False;
FParseThreadLock.Enter;
try
FParseThread := Thread;
finally
FParseThreadLock.Leave;
end;
Thread.Start;
end;
end;
destructor TASTParser.Destroy;
var
Entry: TCachedTree;
begin
// Stop watcher first
if FWatcher <> nil then
begin
TDirectoryWatcher(FWatcher).Stop;
FWatcher.Free;
end;
// Wait for any background parse thread to complete
WaitForParseThread;
FIncludeHandler := nil;
FLock.BeginWrite;
try
for Entry in FCache.Values do
Entry.Node.Free;
FCache.Free;
FProjectFiles.Free;
FFileIndex.Free;
finally
FLock.EndWrite;
end;
FParseThreadLock.Free;
inherited;
end;
procedure TASTParser.InitCacheDir;
var
Joined, HashStr: string;
I: Integer;
begin
Joined := '';
for I := 0 to High(FRoots) do
begin
if I > 0 then
Joined := Joined + ';';
Joined := Joined + LowerCase(FRoots[I]);
end;
HashStr := Copy(THashMD5.GetHashString(Joined), 1, 12);
FCacheDir := IncludeTrailingPathDelimiter(
TPath.Combine(TPath.GetTempPath, 'DelphiAST_MCP_' + HashStr));
if not DirectoryExists(FCacheDir) then
ForceDirectories(FCacheDir);
end;
function TASTParser.GetCacheFilePath(const Key: string): string;
begin
Result := TPath.Combine(FCacheDir,
THashMD5.GetHashString(LowerCase(Key)) + '.dast');
end;
procedure TASTParser.SaveCacheEntryToDisk(const Key: string;
const Entry: TCachedTree);
var
CacheFile, TmpFile: string;
begin
try
CacheFile := GetCacheFilePath(Key);
TmpFile := CacheFile + '.tmp';
if TFullASTSerializer.SaveToFile(TmpFile, Entry.Node, Entry.ModifiedAt, Key) then
begin
if FileExists(CacheFile) then
DeleteFile(CacheFile);
RenameFile(TmpFile, CacheFile);
end
else
begin
if FileExists(TmpFile) then
DeleteFile(TmpFile);
end;
except
on E: Exception do
WriteLn(ErrOutput, '[delphi-ast] Failed to save cache for ' + Key + ': ' + E.Message);
end;
end;
function TASTParser.TryLoadCacheEntryFromDisk(const CacheFile: string;
out Key: string; out Entry: TCachedTree): Boolean;
var
Root: TSyntaxNode;
ModifiedAt: TDateTime;
SourcePath: string;
FileTime: TDateTime;
begin
Result := False;
Key := '';
Entry.Node := nil;
Entry.ModifiedAt := 0;
try
if not TFullASTSerializer.LoadFromFile(CacheFile, Root, ModifiedAt, SourcePath) then
begin
DeleteFile(CacheFile);
Exit;
end;
// Validate source file still exists and timestamp matches
if not FileExists(SourcePath) then
begin
Root.Free;
DeleteFile(CacheFile);
Exit;
end;
FileTime := TFile.GetLastWriteTime(SourcePath);
if FileTime > ModifiedAt then
begin
Root.Free;
DeleteFile(CacheFile);
Exit;
end;
// Canonicalize path to match ParseFile's key generation
Key := LowerCase(TPath.GetFullPath(SourcePath));
Entry.Node := Root;
Entry.ModifiedAt := ModifiedAt;
Result := True;
except
on E: Exception do
begin
WriteLn(ErrOutput, '[delphi-ast] Failed to load cache file ' + CacheFile + ': ' + E.Message);
try
DeleteFile(CacheFile);
except
end;
end;
end;
end;
procedure TASTParser.LoadPersistedCache;
var
Files: TArray<string>;
F, Key: string;
Entry: TCachedTree;
Loaded: Integer;
begin
if not DirectoryExists(FCacheDir) then
Exit;
Files := TDirectory.GetFiles(FCacheDir, '*.dast');
Loaded := 0;
for F in Files do
begin
if TryLoadCacheEntryFromDisk(F, Key, Entry) then
begin
FLock.BeginWrite;
try
if not FCache.ContainsKey(Key) then
begin
WriteLn(ErrOutput, '[delphi-ast] Adding '+key+' to cache');
FCache.Add(Key, Entry);
Inc(Loaded);
end else
begin
WriteLn(ErrOutput, '[delphi-ast] '+key+' already in cache');
Entry.Node.Free; // Already loaded by another path
end;
finally
FLock.EndWrite;
end;
end;
end;
if Loaded > 0 then
WriteLn(ErrOutput, '[delphi-ast] Loaded ' + IntToStr(Loaded) + ' files from disk cache');
end;
procedure TASTParser.HandleFileChanged(const AFullPath: string);
var
Thread: TThread;
begin
WriteLn(ErrOutput, '[delphi-ast] File changed: ' + AFullPath);
InvalidateFile(AFullPath);
// Re-parse the changed file immediately
try
ParseFile(AFullPath);
WriteLn(ErrOutput, '[delphi-ast] Re-parsed: ' + AFullPath);
except
on E: Exception do
WriteLn(ErrOutput, '[delphi-ast] Failed to re-parse ' + AFullPath + ': ' + E.Message);
end;
// Re-walk dependencies to handle added/removed uses clauses
// Use CompareExchange to prevent concurrent re-walks
if TInterlocked.CompareExchange(FRewalking, 1, 0) = 0 then
begin
// Wait for any running parse thread to complete first
WaitForParseThread;
// Spawn tracked thread for re-walk
Thread := TThread.CreateAnonymousThread(
procedure
begin
try
ParseProjectFiles;
finally
TInterlocked.Exchange(FRewalking, 0);
end;
end);
Thread.FreeOnTerminate := False;
FParseThreadLock.Enter;
try
FParseThread := Thread;
finally
FParseThreadLock.Leave;
end;
Thread.Start;
end
else
begin
WriteLn(ErrOutput, '[delphi-ast] Re-walk already in progress, skipping');
end;
end;
procedure TASTParser.Reconfigure(const ARoots: TArray<string>; const AExcludeFiles: TArray<string> = nil);
var
Entry: TCachedTree;
I: Integer;
Thread: TThread;
begin
// 1. Stop existing watcher
if FWatcher <> nil then
begin
TDirectoryWatcher(FWatcher).Stop;
FreeAndNil(FWatcher);
end;
// 2. Cancel and wait for any running parse thread
TInterlocked.Exchange(FCancelled, 1);
WaitForParseThread;
TInterlocked.Exchange(FCancelled, 0);
// 3. Clear all cached trees
FLock.BeginWrite;
try
for Entry in FCache.Values do
Entry.Node.Free;
FCache.Clear;
finally
FLock.EndWrite;
end;
// 4. Set new roots and exclusions
SetLength(FRoots, Length(ARoots));
for I := 0 to High(ARoots) do
FRoots[I] := IncludeTrailingPathDelimiter(ExpandFileName(ARoots[I]));
FExcludeFiles := AExcludeFiles;
// 5. Reinitialize
FIncludeHandler := TSimpleIncludeHandler.Create(FRoots);
InitCacheDir;
// Reset parse state - will be ready again after ParseAllFiles completes
TInterlocked.Exchange(FHasParsed, 0);
// 6. Start new watcher
FWatcher := TDirectoryWatcher.Create(FRoots,
procedure(APath: string)
begin
HandleFileChanged(APath);
end);
TDirectoryWatcher(FWatcher).Start;
// 7. Background parse - tracked thread
Thread := TThread.CreateAnonymousThread(
procedure
begin
try
ParseProjectFiles;
except
on E: Exception do
WriteLn(ErrOutput, '[delphi-ast] Background parse failed: ' + E.Message);
end;
end);
Thread.FreeOnTerminate := False;
FParseThreadLock.Enter;
try
FParseThread := Thread;
finally
FParseThreadLock.Leave;
end;
Thread.Start;
end;
function TASTParser.IsConfigured: Boolean;
begin
Result := Length(FRoots) > 0;
end;
function TASTParser.IsParsing: Boolean;
begin
Result := FParseState = Integer(psParsing);
end;
function TASTParser.IsReady: Boolean;
begin
Result := (FHasParsed = 1) and (FParseState = Integer(psIdle));
end;
function TASTParser.GetParseStatus: TParseStatus;
begin
Result.State := TParseState(FParseState);
Result.TotalFiles := FTotalFiles;
Result.ParsedFiles := FParsedFiles;
Result.CachedFiles := FCachedFiles;
Result.FailedFiles := FFailedFiles;
end;
procedure TASTParser.WaitForParseThread;
var
Thread: TThread;
begin
FParseThreadLock.Enter;
try
Thread := FParseThread;
FParseThread := nil;
finally
FParseThreadLock.Leave;
end;
if Thread <> nil then
begin
Thread.WaitFor;
Thread.Free;
end;
end;
function TASTParser.ListFiles(const NameFilter: string): TArray<string>;
var
Files: TStringList;
FullPath, RelPath, LowerFilter, Root, Ext: string;
I: Integer;
begin
Files := TStringList.Create;
try
Files.Sorted := True;
Files.Duplicates := dupIgnore;
LowerFilter := LowerCase(NameFilter);
// Get the project root for relative path calculation
Root := '';
if Length(FRoots) > 0 then
Root := FRoots[0];
// Return only project files discovered via dependency walk (protected by lock)
FProjectFilesLock.BeginRead;
try
for FullPath in FProjectFiles do
begin
Ext := LowerCase(ExtractFileExt(FullPath));
if (Ext <> '.pas') and (Ext <> '.dpr') and (Ext <> '.dpk') then
Continue;
RelPath := FullPath;
if (Root <> '') and FullPath.StartsWith(Root, True) then
RelPath := FullPath.Substring(Length(Root));
if (LowerFilter = '') or
(Pos(LowerFilter, LowerCase(ExtractFileName(FullPath))) > 0) then
Files.Add(RelPath);
end;
finally
FProjectFilesLock.EndRead;
end;
Result := Files.ToStringArray;
finally
Files.Free;
end;
end;
function TASTParser.ResolveFilePath(const AFileName: string): string;
var
I: Integer;
FullPath: string;
begin
if Length(FRoots) = 0 then
raise Exception.Create('No project configured. Call set_project first.');
if not TPath.IsRelativePath(AFileName) then
Exit(AFileName);
// Check each root for file existence
for I := 0 to High(FRoots) do
begin
FullPath := TPath.Combine(FRoots[I], AFileName);
if FileExists(FullPath) then
Exit(FullPath);
end;
// Fall back to first root
Result := TPath.Combine(FRoots[0], AFileName);
end;
function TASTParser.ParseFile(const AFileName: string): TSyntaxNode;
var
FullPath, Key: string;
Entry: TCachedTree;
FileTime: TDateTime;
begin
FullPath := ResolveFilePath(AFileName);
FullPath := TPath.GetFullPath(FullPath); // Canonicalize path to avoid duplicate cache entries
Key := LowerCase(FullPath);
// Fast path: read lock check
FLock.BeginRead;
try
if FCache.TryGetValue(Key, Entry) then
begin
FileTime := TFile.GetLastWriteTime(FullPath);
if FileTime <= Entry.ModifiedAt then
Exit(Entry.Node);
end;
finally
FLock.EndRead;
end;
// Slow path: parse outside lock
if not FileExists(FullPath) then
raise Exception.CreateFmt('File not found: %s', [FullPath]);
Entry.Node := TPasSyntaxTreeBuilder.Run(FullPath, False, FIncludeHandler);
Entry.ModifiedAt := TFile.GetLastWriteTime(FullPath);
// Write lock: store result
FLock.BeginWrite;
try
// Double-check: another thread may have parsed it
if FCache.ContainsKey(Key) then
begin
var OldEntry := FCache[Key];
OldEntry.Node.Free;
FCache.Remove(Key);
end;
FCache.Add(Key, Entry);
Result := Entry.Node;
finally
FLock.EndWrite;
end;
// Save to disk cache (outside lock)
SaveCacheEntryToDisk(Key, Entry);
end;
procedure TASTParser.ParseAllFiles;
var
Files: TArray<string>;
F, FullPath, Key: string;
Parsed, Failed, Cached: Integer;
Entry: TCachedTree;
AlreadyCached: Boolean;
begin
// Set parsing state and reset counters
TInterlocked.Exchange(FParseState, Integer(psParsing));
TInterlocked.Exchange(FParsedFiles, 0);
TInterlocked.Exchange(FCachedFiles, 0);
TInterlocked.Exchange(FFailedFiles, 0);
TInterlocked.Exchange(FTotalFiles, 0);
Parsed := 0;
Failed := 0;
Cached := 0;
try
// First load persisted cache from disk
LoadPersistedCache;
Files := ListFiles('');
TInterlocked.Exchange(FTotalFiles, Length(Files));
for F in Files do
begin
try
FullPath := ResolveFilePath(F);
Key := LowerCase(FullPath);
// Check if already in cache with fresh timestamp
AlreadyCached := False;
FLock.BeginRead;
try
if FCache.TryGetValue(Key, Entry) then
begin
if FileExists(FullPath) and
(TFile.GetLastWriteTime(FullPath) <= Entry.ModifiedAt) then
begin
AlreadyCached := True;
Inc(Cached);
TInterlocked.Increment(FCachedFiles);
end;
end;
finally
FLock.EndRead;
end;
if not AlreadyCached then
begin
ParseFile(F);
Inc(Parsed);
TInterlocked.Increment(FParsedFiles);
end;
except
on E: Exception do
begin
Inc(Failed);
TInterlocked.Increment(FFailedFiles);
WriteLn(ErrOutput, '[delphi-ast] Failed to parse ' + F + ': ' + E.Message);
end;
end;
end;
WriteLn(ErrOutput, '[delphi-ast] Eager parse complete: ' +
IntToStr(Cached) + ' from cache, ' +
IntToStr(Parsed) + ' parsed, ' +
IntToStr(Failed) + ' failed');
finally
TInterlocked.Exchange(FParseState, Integer(psIdle));
TInterlocked.Exchange(FHasParsed, 1);
end;
end;
function TASTParser.ResolveUnitFile(const UnitName: string): string;
var
SearchKey: string;
begin
// O(1) lookup using pre-built file index
SearchKey := LowerCase(UnitName + '.pas');
if not FFileIndex.TryGetValue(SearchKey, Result) then
Result := '';
end;
function IsExcludedFile(const FileName: string; const Patterns: TArray<string>): Boolean;
var
Pattern: string;
begin
for Pattern in Patterns do
if MatchesMask(FileName, Pattern) then
Exit(True);
Result := False;
end;
procedure TASTParser.BuildFileIndex;
var
I: Integer;
Files: TArray<string>;
F, FileName: string;
begin
FFileIndex.Clear;
for I := 0 to High(FRoots) do
begin
try
Files := TDirectory.GetFiles(FRoots[I], '*.pas');
for F in Files do
begin
FileName := LowerCase(ExtractFileName(F));
if IsExcludedFile(FileName, FExcludeFiles) then
Continue;
if not FFileIndex.ContainsKey(FileName) then
FFileIndex.Add(FileName, TPath.GetFullPath(F));
end;
Files := TDirectory.GetFiles(FRoots[I], '*.dpr');
for F in Files do
begin
FileName := LowerCase(ExtractFileName(F));
if IsExcludedFile(FileName, FExcludeFiles) then
Continue;
if not FFileIndex.ContainsKey(FileName) then
FFileIndex.Add(FileName, TPath.GetFullPath(F));
end;
except
on E: Exception do
WriteLn(ErrOutput, '[delphi-ast] BuildFileIndex error for ' + FRoots[I] + ': ' + E.Message);
end;
end;
WriteLn(ErrOutput, '[delphi-ast] File index built: ' + IntToStr(FFileIndex.Count) + ' files');
end;
procedure TASTParser.ParseProjectFiles;
var
DPRFiles: TArray<string>;
DPR, FullPath, UnitFile, Key: string;
Queue: TQueue<string>;
Visited: TDictionary<string, Boolean>;
UnitNames: TArray<string>;
UnitName: string;
Tree: TSyntaxNode;
Parsed, Failed, Cached: Integer;
Entry: TCachedTree;
AlreadyCached: Boolean;
begin
// Set parsing state and reset counters
TInterlocked.Exchange(FParseState, Integer(psParsing));
TInterlocked.Exchange(FParsedFiles, 0);
TInterlocked.Exchange(FCachedFiles, 0);
TInterlocked.Exchange(FFailedFiles, 0);
TInterlocked.Exchange(FTotalFiles, 0);
Parsed := 0;
Failed := 0;
Cached := 0;
// Clear project files list (protected by lock)
FProjectFilesLock.BeginWrite;
try
FProjectFiles.Clear;
finally
FProjectFilesLock.EndWrite;
end;
// Load persisted cache from disk first
LoadPersistedCache;
// Build filename-to-path index for O(1) unit resolution
BuildFileIndex;
// Find all DPR files in the project root (first root only)
if Length(FRoots) = 0 then
begin
TInterlocked.Exchange(FParseState, Integer(psIdle));
TInterlocked.Exchange(FHasParsed, 1);
Exit;
end;
DPRFiles := TDirectory.GetFiles(FRoots[0], '*.dpr');
if Length(DPRFiles) = 0 then
raise Exception.Create('No .dpr files found in project root: ' + FRoots[0]);
Queue := TQueue<string>.Create;
Visited := TDictionary<string, Boolean>.Create;
try
// Add all DPR files to the queue
for DPR in DPRFiles do
begin
FullPath := TPath.GetFullPath(DPR);
Queue.Enqueue(FullPath);
Visited.AddOrSetValue(LowerCase(FullPath), True);
end;
// BFS: process files and discover dependencies
while Queue.Count > 0 do
begin
// Check for cancellation
if FCancelled = 1 then
Break;
FullPath := Queue.Dequeue;
Key := LowerCase(FullPath);
try
WriteLn(ErrOutput, '[delphi-ast] Checking: '+key);
// Check if already in cache with fresh timestamp
AlreadyCached := False;
FLock.BeginRead;
try
if FCache.TryGetValue(Key, Entry) then
begin
if FileExists(FullPath) and
(TFile.GetLastWriteTime(FullPath) <= Entry.ModifiedAt) then
begin
AlreadyCached := True;
Inc(Cached);
TInterlocked.Increment(FCachedFiles);
end;
end;
finally
FLock.EndRead;
end;
// Add to project files (protected by lock)
FProjectFilesLock.BeginWrite;
try
FProjectFiles.Add(FullPath);
finally
FProjectFilesLock.EndWrite;
end;
// Use cached tree directly when available, otherwise parse
if AlreadyCached then
Tree := Entry.Node
else
begin
Tree := ParseFile(FullPath);
Inc(Parsed);
TInterlocked.Increment(FParsedFiles);
end;
// Extract uses clauses and add new units to queue
if Tree <> nil then
begin
UnitNames := ExtractUnitNames(Tree);
for UnitName in UnitNames do
begin
// Try to resolve unit name to a file path
UnitFile := ResolveUnitFile(UnitName);
if UnitFile <> '' then
begin
// If not already visited, add to queue
if not Visited.ContainsKey(LowerCase(UnitFile)) then
begin
Visited.AddOrSetValue(LowerCase(UnitFile), True);
Queue.Enqueue(UnitFile);
end;
end;
end;
end;
except
on E: Exception do
begin
Inc(Failed);
TInterlocked.Increment(FFailedFiles);
WriteLn(ErrOutput, '[delphi-ast] Failed to parse ' + FullPath + ': ' + E.Message);
end;
end;
end;
FProjectFilesLock.BeginRead;
try
TInterlocked.Exchange(FTotalFiles, FProjectFiles.Count);
finally
FProjectFilesLock.EndRead;
end;
WriteLn(ErrOutput, '[delphi-ast] Project parse complete: ' +
IntToStr(Cached) + ' from cache, ' +
IntToStr(Parsed) + ' parsed, ' +
IntToStr(Failed) + ' failed');
finally
Queue.Free;
Visited.Free;
TInterlocked.Exchange(FParseState, Integer(psIdle));
TInterlocked.Exchange(FHasParsed, 1);
end;
end;
function TASTParser.GetAllTrees: TArray<TPair<string, TSyntaxNode>>;
var
Pair: TPair<string, TCachedTree>;
List: TList<TPair<string, TSyntaxNode>>;
begin
List := TList<TPair<string, TSyntaxNode>>.Create;
try
FLock.BeginRead;
try
for Pair in FCache do
List.Add(TPair<string, TSyntaxNode>.Create(Pair.Key, Pair.Value.Node));
finally
FLock.EndRead;
end;
Result := List.ToArray;
finally
List.Free;
end;
end;
procedure TASTParser.ClearCache;
var
Entry: TCachedTree;
begin
FLock.BeginWrite;
try
for Entry in FCache.Values do
Entry.Node.Free;
FCache.Clear;
finally
FLock.EndWrite;
end;
end;
procedure TASTParser.InvalidateFile(const AFullPath: string);
var
Key: string;
Entry: TCachedTree;
CacheFile: string;
begin
Key := LowerCase(AFullPath);
FLock.BeginWrite;
try
if FCache.TryGetValue(Key, Entry) then
begin
Entry.Node.Free;
FCache.Remove(Key);
end;
finally