-
Notifications
You must be signed in to change notification settings - Fork 4
/
TreeSitter.Query.pas
215 lines (171 loc) · 5.47 KB
/
TreeSitter.Query.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
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
unit TreeSitter.Query;
interface
uses
TreeSitterLib, TreeSitter;
type
TTSQueryError = TreeSitterLib.TSQueryError;
TTSQueryPredicateStep = TreeSitterLib.TSQueryPredicateStep;
TTSQueryPredicateStepType = TreeSitterLib.TSQueryPredicateStepType;
TTSQueryPredicateStepArray = array of TTSQueryPredicateStep;
TTSQuantifier = TreeSitterLib.TSQuantifier;
TTSQuery = class
strict private
FQuery: PTSQuery;
public
constructor Create(ALanguage: PTSLanguage; const ASource: string;
var AErrorOffset: UInt32; var AErrorType: TTSQueryError); virtual;
destructor Destroy; override;
function PatternCount: UInt32;
function CaptureCount: UInt32;
function StringCount: UInt32;
function StartByteForPattern(APatternIndex: UInt32): UInt32;
function PredicatesForPattern(APatternIndex: UInt32): TTSQueryPredicateStepArray;
function CaptureNameForID(ACaptureIndex: UInt32): string;
function StringValueForID(AStringIndex: UInt32): string;
function QuantifierForCapture(APatternIndex, ACaptureIndex: UInt32): TTSQuantifier;
property Query: PTSQuery read FQuery;
end;
TTSQueryCapture = TreeSitterLib.TSQueryCapture;
TTSQueryCaptureArray = array of TTSQueryCapture;
TTSQueryMatch = TreeSitterLib.TSQueryMatch;
TTSQueryMatchHelper = record helper for TTSQueryMatch
function CapturesArray: TTSQueryCaptureArray;
end;
TTSQueryCursor = class
strict private
FQueryCursor: PTSQueryCursor;
function GetMatchLimit: UInt32;
procedure SetMatchLimit(const Value: UInt32);
public
constructor Create; virtual;
destructor Destroy; override;
procedure Execute(AQuery: TTSQuery; ANode: TTSNode);
function DidExceedMatchLimit: Boolean;
procedure SetMaxStartDepth(AMaxStartDepth: UInt32);
function NextMatch(var AMatch: TTSQueryMatch): Boolean;
function NextCapture(var AMatch: TTSQueryMatch; var ACaptureIndex: UInt32): Boolean;
property QueryCursor: PTSQueryCursor read FQueryCursor;
property MatchLimit: UInt32 read GetMatchLimit write SetMatchLimit;
end;
implementation
{ TTSQuery }
function TTSQuery.CaptureCount: UInt32;
begin
Result:= ts_query_capture_count(FQuery);
end;
function TTSQuery.CaptureNameForID(ACaptureIndex: UInt32): string;
var
pac: PAnsiChar;
len: UInt32;
res: AnsiString;
begin
pac:= ts_query_capture_name_for_id(FQuery, ACaptureIndex, len);
SetLength(res, len);
if len > 0 then
Move(pac[0], res[1], len * SizeOf(pac[0]));
Result:= string(res);
end;
constructor TTSQuery.Create(ALanguage: PTSLanguage; const ASource: string;
var AErrorOffset: UInt32; var AErrorType: TTSQueryError);
var
ansiSource: AnsiString;
begin
ansiSource:= AnsiString(ASource);
FQuery:= ts_query_new(ALanguage, PAnsiChar(ansiSource), Length(ansiSource),
AErrorOffset, AErrorType);
end;
destructor TTSQuery.Destroy;
begin
ts_query_delete(FQuery);
inherited;
end;
function TTSQuery.PatternCount: UInt32;
begin
Result:= ts_query_pattern_count(FQuery);
end;
function TTSQuery.PredicatesForPattern(
APatternIndex: UInt32): TTSQueryPredicateStepArray;
var
count: UInt32;
parr: PTSQueryPredicateStepArray;
begin
count:= 0;
parr:= ts_query_predicates_for_pattern(FQuery, APatternIndex, count);
if (parr <> nil) and (count > 0) then
begin
SetLength(Result, count);
Move(parr[0], Result[0], count * SizeOf(Result[0]));
end;
end;
function TTSQuery.QuantifierForCapture(APatternIndex,
ACaptureIndex: UInt32): TTSQuantifier;
begin
Result:= ts_query_capture_quantifier_for_id(FQuery, APatternIndex, ACaptureIndex);
end;
function TTSQuery.StartByteForPattern(APatternIndex: UInt32): UInt32;
begin
Result:= ts_query_start_byte_for_pattern(FQuery, APatternIndex);
end;
function TTSQuery.StringCount: UInt32;
begin
Result:= ts_query_string_count(FQuery);
end;
function TTSQuery.StringValueForID(AStringIndex: UInt32): string;
var
pac: PAnsiChar;
len: UInt32;
res: AnsiString;
begin
pac:= ts_query_string_value_for_id(FQuery, AStringIndex, len);
SetLength(res, len);
if len > 0 then
Move(pac[0], res[1], len * SizeOf(pac[0]));
Result:= string(res);
end;
{ TTSQueryCursor }
constructor TTSQueryCursor.Create;
begin
FQueryCursor:= ts_query_cursor_new;
end;
destructor TTSQueryCursor.Destroy;
begin
ts_query_cursor_delete(FQueryCursor);
inherited;
end;
function TTSQueryCursor.DidExceedMatchLimit: Boolean;
begin
Result:= ts_query_cursor_did_exceed_match_limit(FQueryCursor);
end;
procedure TTSQueryCursor.Execute(AQuery: TTSQuery; ANode: TTSNode);
begin
ts_query_cursor_exec(FQueryCursor, AQuery.Query, ANode);
end;
function TTSQueryCursor.GetMatchLimit: UInt32;
begin
Result:= ts_query_cursor_match_limit(FQueryCursor);
end;
function TTSQueryCursor.NextCapture(var AMatch: TTSQueryMatch;
var ACaptureIndex: UInt32): Boolean;
begin
Result:= ts_query_cursor_next_capture(FQueryCursor, AMatch, ACaptureIndex);
end;
function TTSQueryCursor.NextMatch(var AMatch: TTSQueryMatch): Boolean;
begin
Result:= ts_query_cursor_next_match(FQueryCursor, AMatch);
end;
procedure TTSQueryCursor.SetMatchLimit(const Value: UInt32);
begin
ts_query_cursor_set_match_limit(FQueryCursor, Value);
end;
procedure TTSQueryCursor.SetMaxStartDepth(AMaxStartDepth: UInt32);
begin
ts_query_cursor_set_max_start_depth(FQueryCursor, AMaxStartDepth);
end;
{ TTSQueryMatchHelper }
function TTSQueryMatchHelper.CapturesArray: TTSQueryCaptureArray;
begin
SetLength(Result, capture_count);
if capture_count > 0 then
Move(captures[0], Result[0], capture_count * SizeOf(captures[0]));
end;
end.