-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathConsoleReadPasFile.dpr
68 lines (61 loc) · 1.53 KB
/
ConsoleReadPasFile.dpr
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
program ConsoleReadPasFile;
{$APPTYPE CONSOLE}
{$R *.res}
uses
Classes,
System.SysUtils,
IOUtils,
TreeSitter in 'TreeSitter.pas',
TreeSitterLib in 'TreeSitterLib.pas';
function tree_sitter_pascal(): PTSLanguage; cdecl; external 'tree-sitter-pascal';
procedure ReadAndParsePasFile(const AFileName: string);
var
parser: TTSParser;
fs: TFileStream;
tree: TTSTree;
begin
tree:= nil;
parser:= nil;
fs:= TFileStream.Create(AFileName, fmOpenRead or fmShareDenyNone);
try
parser:= TTSParser.Create;
parser.Language:= tree_sitter_pascal;
tree:= parser.parse(
function (AByteIndex: UInt32; APosition: TTSPoint; var ABytesRead: UInt32): TBytes
const
BufSize = 10 * 1024;
begin
if fs.Seek(AByteIndex, soFromBeginning) < 0 then
begin
ABytesRead:= 0;
Exit;
end;
SetLength(Result, BufSize);
try
ABytesRead:= fs.Read(Result, BufSize);
except
ABytesRead:= 0;
end;
SetLength(Result, ABytesRead);
end, TTSInputEncoding.TSInputEncodingUTF8);
WriteLn(tree.RootNode.ToString);
finally
tree.Free;
parser.Free;
fs.Free;
end;
end;
var
fn: string;
begin
try
fn:= TPath.Combine(TPath.GetDirectoryName(ParamStr(0)), '..\..\TreeSitter.pas');
if TFile.Exists(fn) then
ReadAndParsePasFile(fn) else
raise Exception.CreateFmt('Failed to find file to parse: "%s"', [fn]);
ReadLn;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.