Repository navigation
Expand file tree
/
Copy pathDelphiAST.ProjectIndexer.pas
More file actions
623 lines (553 loc) · 18.4 KB
/
Copy pathDelphiAST.ProjectIndexer.pas
File metadata and controls
623 lines (553 loc) · 18.4 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
unit DelphiAST.ProjectIndexer;
{$IFDEF FPC}{$MODE DELPHI}{$ENDIF}
interface
uses
Classes,
{$IFDEF FPC_DOTTEDUNITS}
System.Generics.Defaults, System.Generics.Collections,
{$ELSE}
Generics.Defaults, Generics.Collections,
{$ENDIF}
SimpleParser.Lexer.Types,
DelphiAST, DelphiAST.Classes, DelphiAST.Consts;
type
TProjectIndexer = class
strict private type
TParsedUnitsCache = TObjectDictionary<string,TSyntaxNode>;
TUnitPathsCache = TDictionary<string,string>;
TIncludeInfo = record
FileName: string;
Content : string;
end;
TIncludeCache = TDictionary<string,TIncludeInfo>;
public type
TOption = (piUseDefinesDefinedByCompiler);
TOptions = set of TOption;
TGetUnitSyntaxEvent = procedure (Sender: TObject; const fileName: string;
var syntaxTree: TSyntaxNode; var doParseUnit, doAbort: boolean) of object;
TUnitParsedEvent = procedure (Sender: TObject; const unitName: string; const fileName: string;
var syntaxTree: TSyntaxNode; syntaxTreeFromParser: boolean; var doAbort: boolean) of object;
TUnitInfo = record
Name: string;
Path: string;
SyntaxTree: TSyntaxNode;
HasError: boolean;
ErrorInfo: record
Line: integer;
Col: integer;
Error: string;
end;
end;
TParsedUnits = class(TList<TUnitInfo>)
protected
procedure Initialize(parsedUnits: TParsedUnitsCache; unitPaths: TUnitPathsCache);
end;
TIncludeFileInfo = record
Name: string;
Path: string;
end;
TIncludeFiles = class(TList<TIncludeFileInfo>)
protected
procedure Initialize(includeCache: TIncludeCache);
end;
TProblemType = (ptCantFindFile, ptCantOpenFile, ptCantParseFile);
TProblemInfo = record
ProblemType: TProblemType;
FileName : string;
Description: string;
end;
TProblems = class(TList<TProblemInfo>)
protected
procedure LogProblem(problemType: TProblemType; const fileName, description: string);
end;
strict private type
TIncludeHandler = class(TInterfacedObject, IIncludeHandler)
strict private
[weak] FIncludeCache: TIncludeCache;
[weak] FIndexer : TProjectIndexer;
[weak] FProblems : TProblems;
FUnitFile : string;
FUnitFileFolder : string;
public
constructor Create(indexer: TProjectIndexer; includeCache: TIncludeCache;
problemList: TProblems; const currentFile: string);
function GetIncludeFileContent(const ParentFileName, FileName: string; out Content: string;
out filePath: string): Boolean;
end;
var
FAborting : boolean;
FDefines : string;
FDefinesList : TStringList;
FIncludeCache : TIncludeCache;
FIncludeFiles : TIncludeFiles;
FNotFoundUnits : TStringList;
FOnGetUnitSyntax: TGetUnitSyntaxEvent;
FOnUnitParsed : TUnitParsedEvent;
FOptions : TOptions;
FParsedUnits : TParsedUnitsCache;
FParsedUnitsInfo: TParsedUnits;
FProblems : TProblems;
FProjectFolder : string;
FSearchPath : string;
FSearchPaths : TStringList;
FUnitPaths : TUnitPathsCache;
strict protected
procedure AppendUnits(usesNode: TSyntaxNode; const filePath: string; unitList: TStrings);
procedure BuildUsesList(unitNode: TSyntaxNode; const fileName: string; isProject: boolean;
unitList: TStringList);
function FindType(node: TSyntaxNode; nodeType: TSyntaxNodeType): TSyntaxNode;
procedure GetUnitSyntax(const fileName: string; var syntaxTree: TSyntaxNode; var
doParseUnit: boolean);
procedure NotifyUnitParsed(const unitName, fileName: string; var syntaxTree: TSyntaxNode;
syntaxTreeFromParser: boolean);
procedure ParseUnit(const unitName: string; const fileName: string; isProject: boolean);
procedure PrepareSearchPath;
procedure PrepareDefines;
procedure RunParserOnUnit(const fileName: string; var syntaxTree: TSyntaxNode);
procedure ScanUsedUnits(const unitName, fileName: string; isProject: boolean;
syntaxTree: TSyntaxNode; syntaxTreeFromParser: boolean);
protected
function FindFile(const fileName: string; relativeToFolder: string; var filePath: string): boolean;
class function SafeOpenFileStream(const fileName: string; var fileStream: TStringStream;
var errorMsg: string): boolean;
public
constructor Create;
destructor Destroy; override;
procedure Index(const fileName: string);
property Defines: string read FDefines write FDefines;
property Options: TOptions read FOptions write FOptions default [piUseDefinesDefinedByCompiler];
property ParsedUnits: TParsedUnits read FParsedUnitsInfo;
property IncludeFiles: TIncludeFiles read FIncludeFiles;
property Problems: TProblems read FProblems;
property NotFoundUnits: TStringList read FNotFoundUnits;
property SearchPath: string read FSearchPath write FSearchPath;
property OnGetUnitSyntax: TGetUnitSyntaxEvent read FOnGetUnitSyntax write FOnGetUnitSyntax;
property OnUnitParsed: TUnitParsedEvent read FOnUnitParsed write FOnUnitParsed;
end;
implementation
uses
SysUtils,
SimpleParser;
{ TProjectIndexer.TParsedUnits }
procedure TProjectIndexer.TParsedUnits.Initialize(parsedUnits: TParsedUnitsCache;
unitPaths: TUnitPathsCache);
var
info : TUnitInfo;
kv : TPair<string,TSyntaxNode>;
unitPath: string;
begin
Clear;
Capacity := parsedUnits.Count;
for kv in parsedUnits do begin
if not assigned(kv.Value) then
continue; //for kv
info.Name := kv.Key;
info.SyntaxTree := kv.Value;
if not (unitPaths.TryGetValue(kv.Key + '.pas', unitPath)
or unitPaths.TryGetValue(kv.Key + '.dpr', unitPath))
then
unitPath := '';
info.Path := unitPath;
info.HasError := false; // TODO 1 -oPrimoz Gabrijelcic : fix that
Add(info);
end;
TrimExcess;
Sort(
TComparer<TUnitInfo>.Construct(
function(const Left, Right: TUnitInfo): integer
begin
Result := TOrdinalIStringComparer(TIStringComparer.Ordinal).Compare(Left.Name, Right.Name);
end));
end;
{ TProjectIndexer.TIncludeFiles }
procedure TProjectIndexer.TIncludeFiles.Initialize(includeCache: TIncludeCache);
var
info: TIncludeFileInfo;
kv : TPair<string,TIncludeInfo>;
p : integer;
begin
Clear;
Capacity := includeCache.Count;
for kv in includeCache do begin
p := Pos(#13, kv.Key);
if p = 0 then
continue; //for kv
info.Name := Copy(kv.Key, 1, p-1);
info.Path := kv.Value.FileName;
Add(info);
end;
TrimExcess;
Sort(
TComparer<TIncludeFileInfo>.Construct(
function(const Left, Right: TIncludeFileInfo): integer
begin
Result := TOrdinalIStringComparer(TIStringComparer.Ordinal).Compare(Left.Name, Right.Name);
end));
end;
{ TProjectIndexer.TProblems }
procedure TProjectIndexer.TProblems.LogProblem(problemType: TProblemType; const fileName,
description: string);
var
info: TProblemInfo;
begin
info.ProblemType := problemType;
info.FileName := fileName;
info.Description := description;
Add(info);
end;
{ TProjectIndexer }
procedure TProjectIndexer.AppendUnits(usesNode: TSyntaxNode; const filePath: string;
unitList: TStrings);
var
childNode: TSyntaxNode;
unitName : string;
unitPath : string;
begin
for childNode in usesNode.ChildNodes do
if childNode.Typ = ntUnit then begin
unitName := childNode.GetAttribute(anName);
unitList.Add(unitName);
if not FUnitPaths.ContainsKey(unitName) then begin
unitPath := childNode.GetAttribute(anPath);
if unitPath <> '' then begin
if IsRelativePath(unitPath) then
unitPath := filePath + unitPath;
FUnitPaths.Add(unitName + '.pas', unitPath);
end;
end;
end;
end;
procedure TProjectIndexer.BuildUsesList(unitNode: TSyntaxNode; const fileName: string;
isProject: boolean; unitList: TStringList);
var
fileFolder: string;
implNode : TSyntaxNode;
intfNode : TSyntaxNode;
usesNode : TSyntaxNode;
begin
fileFolder := IncludeTrailingPathDelimiter(ExtractFilePath(fileName));
if isProject then begin
usesNode := FindType(unitNode, ntUses);
if assigned(usesNode) then
AppendUnits(usesNode, fileFolder, unitList);
usesNode := FindType(unitNode, ntContains);
if assigned(usesNode) then
AppendUnits(usesNode, fileFolder, unitList);
end
else begin
intfNode := FindType(unitNode, ntInterface);
if assigned(intfNode) then begin
usesNode := FindType(intfNode, ntUses);
if assigned(usesNode) then
AppendUnits(usesNode, fileFolder, unitList);
end;
implNode := FindType(unitNode, ntImplementation);
if assigned(implNode) then begin
usesNode := FindType(implNode, ntUses);
if assigned(usesNode) then
AppendUnits(usesNode, fileFolder, unitList);
end;
end;
end;
constructor TProjectIndexer.Create;
begin
inherited Create;
FOptions := [piUseDefinesDefinedByCompiler];
FSearchPaths := TStringList.Create;
FSearchPaths.Delimiter := ';';
FSearchPaths.StrictDelimiter := true;
FDefinesList := TStringList.Create;
FDefinesList.Delimiter := ';';
FDefinesList.StrictDelimiter := true;
FParsedUnits := TParsedUnitsCache.Create([doOwnsValues], TIStringComparer.Ordinal);
FParsedUnitsInfo := TParsedUnits.Create;
FIncludeFiles := TIncludeFiles.Create;
FNotFoundUnits := TStringList.Create;
FNotFoundUnits.Sorted := true;
FNotFoundUnits.Duplicates := dupIgnore;
FProblems := TProblems.Create;
end;
destructor TProjectIndexer.Destroy;
begin
FreeAndNil(FProblems);
FreeAndNil(FNotFoundUnits);
FreeAndNil(FIncludeFiles);
FreeAndNil(FParsedUnitsInfo);
FreeAndNil(FDefinesList);
FreeAndNil(FParsedUnits);
FreeAndNil(FSearchPaths);
inherited;
end;
function TProjectIndexer.FindFile(const fileName: string; relativeToFolder: string;
var filePath: string): boolean;
var
fName : string;
searchPath: string;
function FilePresent(const testFile: string): boolean;
begin
Result := FileExists(testFile);
if Result then begin
filePath := ExpandFileName(testFile);
FUnitPaths.Add(fName, filePath);
end;
end;
begin
Result := true;
fName := fileName.DeQuotedString;
if FUnitPaths.TryGetValue(fName, filePath) then
Exit;
if relativeToFolder <> '' then
if FilePresent(relativeToFolder + fName) then
Exit;
if FilePresent(FProjectFolder + fName) then
Exit;
for searchPath in FSearchPaths do
if FilePresent(searchPath + fName) then
Exit;
if SameText(ExtractFileExt(fileName), '.pas') then
Result := false
else
Result := FindFile(fileName + '.pas', relativeToFolder, filePath);
if (not Result) and (relativeToFolder = '') {ignore include files} then
FNotFoundUnits.Add(fName);
end;
function TProjectIndexer.FindType(node: TSyntaxNode; nodeType: TSyntaxNodeType):
TSyntaxNode;
begin
if node.Typ = nodeType then
Exit(node)
else
Result := node.FindNode(nodeType);
end;
procedure TProjectIndexer.GetUnitSyntax(const fileName: string; var syntaxTree:
TSyntaxNode; var doParseUnit: boolean);
var
doAbort: boolean;
begin
doAbort := false;
doParseUnit := true;
syntaxTree := nil;
if assigned(OnGetUnitSyntax) then begin
OnGetUnitSyntax(Self, fileName, syntaxTree, doParseUnit, doAbort);
if doAbort then
FAborting := true;
end;
end;
procedure TProjectIndexer.Index(const fileName: string);
var
filePath : string;
projectName: string;
begin
FAborting := false;
FParsedUnits.Clear;
FProjectFolder := IncludeTrailingPathDelimiter(ExtractFilePath(fileName));
FIncludeCache := TIncludeCache.Create;
try
FUnitPaths := TUnitPathsCache.Create(TIStringComparer.Ordinal);
try
PrepareDefines;
PrepareSearchPath;
FNotFoundUnits.Clear;
FProblems.Clear;
filePath := ExpandFileName(fileName);
projectName := ChangeFileExt(ExtractFileName(fileName), '');
FUnitPaths.Add(projectName + '.dpr', fileName);
ParseUnit(projectName, filePath, true);
FParsedUnitsInfo.Initialize(FParsedUnits, FUnitPaths);
FIncludeFiles.Initialize(FIncludeCache);
finally FreeAndNil(FUnitPaths); end;
finally FreeAndNil(FIncludeCache); end;
end;
procedure TProjectIndexer.NotifyUnitParsed(const unitName, fileName: string; var
syntaxTree: TSyntaxNode; syntaxTreeFromParser: boolean);
var
doAbort: boolean;
begin
if assigned(OnUnitParsed) then begin
doAbort := false;
OnUnitParsed(Self, unitName, fileName, syntaxTree, syntaxTreeFromParser, doAbort);
if doAbort then
FAborting := true;
end;
end;
procedure TProjectIndexer.ParseUnit(const unitName: string; const fileName: string; isProject: boolean);
var
doParseUnit: boolean;
syntaxTree : TSyntaxNode;
begin
if FAborting then
Exit;
GetUnitSyntax(fileName, syntaxTree, doParseUnit);
if FAborting then
Exit;
if doParseUnit then
RunParserOnUnit(fileName, syntaxTree);
FParsedUnits.Add(unitName, syntaxTree);
if (not FAborting) and assigned(syntaxTree) then
ScanUsedUnits(unitName, fileName, isProject, syntaxTree, doParseUnit);
end;
procedure TProjectIndexer.PrepareSearchPath;
var
iPath: integer;
sPath: string;
begin
FSearchPaths.DelimitedText := SearchPath;
for iPath := 0 to FSearchPaths.Count - 1 do begin
sPath := FSearchPaths[iPath];
if IsRelativePath(sPath) then
sPath := FProjectFolder + sPath;
FSearchPaths[iPath] := IncludeTrailingPathDelimiter(sPath);
end;
end;
class function TProjectIndexer.SafeOpenFileStream(const fileName: string; var fileStream:
TStringStream; var errorMsg: string): boolean;
var
buf : TBytes;
encoding : TEncoding;
readStream: TStream;
begin
Result := true;
try
readStream := TFileStream.Create(fileName, fmOpenRead or fmShareDenyWrite);
except
on E: EFCreateError do begin
errorMsg := E.Message;
Result := false;
end;
on E: EFOpenError do begin
errorMsg := E.Message;
Result := false;
end;
end;
if Result then try
SetLength(buf, 4);
SetLength(buf, readStream.Read(buf[0], Length(buf)));
encoding := nil;
readStream.Position := TEncoding.GetBufferEncoding(buf, encoding);
fileStream := TStringStream.Create('', encoding);
fileStream.CopyFrom(readStream, readStream.Size - readStream.Position);
finally FreeAndNil(readStream); end;
end;
procedure TProjectIndexer.PrepareDefines;
begin
FDefinesList.DelimitedText := FDefines;
end;
procedure TProjectIndexer.RunParserOnUnit(const fileName: string; var syntaxTree:
TSyntaxNode);
var
builder : TPasSyntaxTreeBuilder;
define : string;
errorMsg : string;
fileStream: TStringStream;
begin
if not SafeOpenFileStream(fileName, fileStream, errorMsg) then
FProblems.LogProblem(ptCantOpenFile, fileName, errorMsg)
else try
builder := TPasSyntaxTreeBuilder.Create;
try
builder.IncludeHandler := TIncludeHandler.Create(Self, FIncludeCache, FProblems, fileName);
if piUseDefinesDefinedByCompiler in Options then
builder.InitDefinesDefinedByCompiler;
for define in FDefinesList do
TmwSimplePasPar(builder).Lexer.AddDefine(define);
try
syntaxTree := builder.Run(fileStream);
except
on E: ESyntaxTreeException do begin
FProblems.LogProblem(ptCantParseFile, fileName,
Format('Line %d, Column %d: %s', [E.Line, E.Col, E.Message]));
end;
on E: EEncodingError do begin
FProblems.LogProblem(ptCantParseFile, fileName, E.Message);
end;
end;
finally FreeAndNil(builder); end;
finally FreeAndNil(fileStream); end;
end; { TProjectIndexer.RunParserOnUnit }
procedure TProjectIndexer.ScanUsedUnits(const unitName, fileName: string; isProject: boolean;
syntaxTree: TSyntaxNode; syntaxTreeFromParser: boolean);
var
unitList: TStringList;
unitNode: TSyntaxNode;
usesName: string;
usesPath: string;
begin
unitNode := FindType(syntaxTree, ntUnit);
if not assigned(unitNode) then
Exit;
unitList := TStringList.Create;
try
BuildUsesList(unitNode, fileName, isProject, unitList);
NotifyUnitParsed(unitName, fileName, syntaxTree, syntaxTreeFromParser);
for usesName in unitList do begin
if FAborting then
Exit;
if not FParsedUnits.ContainsKey(usesName) then begin
if FindFile(usesName + '.pas', '', usesPath) then
ParseUnit(usesName, usesPath, false)
else
FParsedUnits.Add(usesName, nil);
end;
end;
finally FreeAndNil(unitList); end;
end;
{ TProjectIndexer.TIncludeHandler }
constructor TProjectIndexer.TIncludeHandler.Create(indexer: TProjectIndexer;
includeCache: TIncludeCache; problemList: TProblems; const currentFile: string);
begin
inherited Create;
FIndexer := indexer;
FIncludeCache := includeCache;
FProblems := problemList;
FUnitFileFolder := IncludeTrailingPathDelimiter(ExtractFilePath(currentFile));
FUnitFile := ChangeFileExt(ExtractFileName(currentFile), '');
end;
function TProjectIndexer.TIncludeHandler.GetIncludeFileContent(
const ParentFileName, fileName: string; out Content: string; out filePath: string): Boolean;
var
errorMsg : string;
fileStream : TStringStream;
fName : string;
includeInfo: TIncludeInfo;
key : string;
begin
if fileName.StartsWith('*.') then
fName := FUnitFile + fileName.Remove(0 {0-based}, 1)
else if fileName.Contains('*') then
fName := fileName.Replace('*', '', [rfReplaceAll])
else
fName := fileName;
key := fName + #13 + FUnitFileFolder;
if FIncludeCache.TryGetValue(key, includeInfo) then
begin
Content := includeInfo.Content;
filePath := includeInfo.FileName;
Exit(True);
end;
if not FIndexer.FindFile(fName, FUnitFileFolder, filePath) then begin
FProblems.LogProblem(ptCantFindFile, fName, 'Source folder: ' + FUnitFileFolder);
includeInfo.FileName := '';
includeInfo.Content := '';
FIncludeCache.Add(key, includeInfo);
Exit(False);
end;
if FIncludeCache.TryGetValue(filePath, includeInfo) then
begin
Content := includeInfo.Content;
Exit(True);
end;
if not TProjectIndexer.SafeOpenFileStream(filePath, fileStream, errorMsg) then begin
FProblems.LogProblem(ptCantOpenFile, filePath, errorMsg);
Result := False;
end
else try
Content := fileStream.DataString;
Result := True;
finally FreeAndNil(fileStream); end;
includeInfo.FileName := filePath;
includeInfo.Content := Content;
FIncludeCache.Add(fName + #13 + FUnitFileFolder, includeInfo);
includeInfo.FileName := '';
FIncludeCache.Add(filePath, includeInfo);
end;
end.