forked from RomanYankovsky/DelphiAST
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuMainForm.pas
98 lines (82 loc) · 2.08 KB
/
uMainForm.pas
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
unit uMainForm;
{$IFDEF FPC}{$MODE Delphi}{$ENDIF}
interface
uses
{$IFNDEF FPC}
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,
{$ELSE}
SysUtils, Variants, Classes, Controls, Forms, StdCtrls,
{$ENDIF}
SimpleParser.Lexer.Types;
type
TForm2 = class(TForm)
memLog: TMemo;
btnRun: TButton;
procedure btnRunClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TIncludeHandler = class(TInterfacedObject, IIncludeHandler)
private
FPath: string;
public
constructor Create(const Path: string);
function GetIncludeFileContent(const FileName: string): string;
end;
var
Form2: TForm2;
implementation
uses
FileCtrl, IOUtils, DelphiAST, DelphiAST.Classes;
{$R *.dfm}
procedure TForm2.btnRunClick(Sender: TObject);
var
Path, FileName: string;
SyntaxTree: TSyntaxNode;
begin
memLog.Clear;
Path := ExtractFilePath(Application.ExeName) + 'Snippets\';
if not SelectDirectory('Select Folder', '', Path) then
Exit;
for FileName in TDirectory.GetFiles(Path, '*.pas', TSearchOption.soAllDirectories) do
begin
try
SyntaxTree := TPasSyntaxTreeBuilder.Run(FileName, False, TIncludeHandler.Create(Path));
try
memLog.Lines.Add('OK: ' + FileName);
finally
SyntaxTree.Free;
end;
except
on E: Exception do
begin
memLog.Lines.Add('FAILED: ' + FileName);
memLog.Lines.Add(' ' + E.ClassName);
memLog.Lines.Add(' ' + E.Message);
memLog.Repaint;
end;
end;
end;
end;
{ TIncludeHandler }
constructor TIncludeHandler.Create(const Path: string);
begin
inherited Create;
FPath := Path;
end;
function TIncludeHandler.GetIncludeFileContent(const FileName: string): string;
var
FileContent: TStringList;
begin
FileContent := TStringList.Create;
try
FileContent.LoadFromFile(TPath.Combine(FPath, FileName));
Result := FileContent.Text;
finally
FileContent.Free;
end;
end;
end.