-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSynTokenMatch.pas
378 lines (347 loc) · 10.6 KB
/
SynTokenMatch.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
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
{-------------------------------------------------------------------------------
SynWeb
Copyright (C) 2005-2009 Krystian Bigaj
*** MPL
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is Krystian Bigaj.
Alternatively, the contents of this file may be used under the terms
of the GNU Lesser General Public license (the "LGPL License"),
in which case the provisions of LGPL License are applicable instead of those
above. If you wish to allow use of your version of this file only
under the terms of the LGPL License and not to allow others to use
your version of this file under the MPL, indicate your decision by
deleting the provisions above and replace them with the notice and
other provisions required by the LGPL License. If you do not delete
the provisions above, a recipient may use your version of this file
under either the MPL or the LGPL License.
*** LGPL
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
***
You may retrieve the latest version of this file at the SynWeb home page,
located at http://sourceforge.net/projects/synweb
Contact: [email protected]
Homepage: http://flatdev.ovh.org
-------------------------------------------------------------------------------}
{$IFNDEF QSYNTOKENMATCH}
unit SynTokenMatch;
{$ENDIF}
{$I SynWeb.inc}
interface
uses
{$IFDEF SYN_CLX}
QSynEdit,
{$IFDEF UNISYNEDIT}
QSynUnicode,
{$ENDIF}
QSynEditTextBuffer,
QSynEditTypes,
QSynEditHighlighter;
{$ELSE}
SynEdit,
{$IFDEF UNISYNEDIT}
SynUnicode,
{$ENDIF}
SynEditTextBuffer,
SynEditTypes,
SynEditHighlighter;
{$ENDIF}
type
PSynTokenMatch = ^TSynTokenMatch;
TSynTokenMatch = record
{$IFDEF UNISYNEDIT}
OpenToken: UnicodeString;
CloseToken: UnicodeString;
{$ELSE}
OpenToken: String;
CloseToken: String;
{$ENDIF}
TokenKind: Integer;
end;
TSynTokenMatched = record
{$IFDEF UNISYNEDIT}
OpenToken: UnicodeString;
CloseToken: UnicodeString;
{$ELSE}
OpenToken: String;
CloseToken: String;
{$ENDIF}
OpenTokenPos: TBufferCoord;
CloseTokenPos: TBufferCoord;
TokenKind: Integer;
TokenAttri: TSynHighlighterAttributes;
end;
{
SynEditGetMatchingToken(Ex) returns:
-2 : Close and open token found
-1 : Close token found
0 : Kind not found
+1 : Open token found
+2 : Open and close token found
}
function SynEditGetMatchingToken(ASynEdit: TCustomSynEdit; APoint: TBufferCoord;
const ATokens: array of TSynTokenMatch; var AMatch: TSynTokenMatched): Integer;
function SynEditGetMatchingTokenEx(ASynEdit: TCustomSynEdit; APoint: TBufferCoord;
const ATokens: array of TSynTokenMatch; var AMatch: TSynTokenMatched): Integer;
implementation
uses
SysUtils;
type
TSynTokenBuf = record
Pos: TBufferCoord;
{$IFDEF UNISYNEDIT}
Token: UnicodeString;
{$ELSE}
Token: String;
{$ENDIF}
end;
var
FMatchStack: array of TSynTokenBuf;
FMatchOpenDup, FMatchCloseDup: array of Integer;
function SynEditGetMatchingToken(ASynEdit: TCustomSynEdit; APoint: TBufferCoord;
const ATokens: array of TSynTokenMatch; var AMatch: TSynTokenMatched): Integer;
var
TokenMatch: PSynTokenMatch;
{$IFDEF UNISYNEDIT}
Token: UnicodeString;
{$ELSE}
Token: String;
{$ENDIF}
TokenKind: Integer;
Level, DeltaLevel, I, J, FMatchStackID, OpenDupLen, CloseDupLen: Integer;
function IsOpenToken: Boolean;
var
X: Integer;
begin
for X := 0 to OpenDupLen - 1 do
if Token = ATokens[FMatchOpenDup[X]].OpenToken then
begin
Result := True;
Exit;
end;
Result := False
end;
function IsCloseToken: Boolean;
var
X: Integer;
begin
for X := 0 to CloseDupLen - 1 do
if Token = ATokens[FMatchCloseDup[X]].CloseToken then
begin
Result := True;
Exit;
end;
Result := False
end;
function CheckToken: Boolean;
begin
with ASynEdit.Highlighter do
begin
if GetTokenKind = TokenMatch^.TokenKind then
begin
Token := LowerCase(GetToken);
if IsCloseToken then
Dec(Level)
else
if IsOpenToken then
Inc(Level);
end;
if Level = 0 then
begin
SynEditGetMatchingToken := 2;
AMatch.CloseToken := GetToken;
AMatch.CloseTokenPos.Line := APoint.Line + 1;
AMatch.CloseTokenPos.Char := GetTokenPos + 1;
Result := True;
end else
begin
Next;
Result := False;
end;
end;
end;
procedure CheckTokenBack;
begin
with ASynEdit.Highlighter do
begin
if GetTokenKind = TokenMatch^.TokenKind then
begin
Token := LowerCase(GetToken);
if IsCloseToken then
begin
Dec(Level);
if FMatchStackID >= 0 then
Dec(FMatchStackID);
end else
if IsOpenToken then
begin
Inc(Level);
Inc(FMatchStackID);
if FMatchStackID >= Length(FMatchStack) then
SetLength(FMatchStack, Length(FMatchStack) + 32);
FMatchStack[FMatchStackID].Token := GetToken;
FMatchStack[FMatchStackID].Pos.Line := APoint.Line + 1;
FMatchStack[FMatchStackID].Pos.Char := GetTokenPos + 1;
end;
end;
Next;
end;
end;
begin
Result := 0;
if ASynEdit.Highlighter = nil then
Exit;
Dec(APoint.Line);
Dec(APoint.Char);
with ASynEdit, ASynEdit.Highlighter do
begin
if APoint.Line = 0 then
ResetRange
else
SetRange(TSynEditStringList(Lines).Ranges[APoint.Line - 1]);
SetLine(Lines[APoint.Line], APoint.Line);
while not GetEol and (APoint.Char >= GetTokenPos + Length(GetToken)) do
Next;
if GetEol then
Exit;
TokenKind := GetTokenKind;
I := 0;
J := Length(ATokens);
while I < J do
begin
if TokenKind = ATokens[I].TokenKind then
begin
Token := LowerCase(GetToken);
if Token = ATokens[I].OpenToken then
begin
Result := 1;
AMatch.OpenToken := GetToken;
AMatch.OpenTokenPos.Line := APoint.Line + 1;
AMatch.OpenTokenPos.Char := GetTokenPos + 1;
Break;
end else
if Token = ATokens[I].CloseToken then
begin
Result := -1;
AMatch.CloseToken := GetToken;
AMatch.CloseTokenPos.Line := APoint.Line + 1;
AMatch.CloseTokenPos.Char := GetTokenPos + 1;
Break;
end;
end;
Inc(I);
end;
if Result = 0 then
Exit;
TokenMatch := @ATokens[I];
AMatch.TokenKind := TokenKind;
AMatch.TokenAttri := GetTokenAttribute;
if J > Length(FMatchOpenDup) then
begin
SetLength(FMatchOpenDup, J);
SetLength(FMatchCloseDup, J);
end;
OpenDupLen := 0;
CloseDupLen := 0;
for I:=0 to J -1 do
if (TokenKind = ATokens[I].TokenKind) then
begin
if (TokenMatch^.OpenToken = ATokens[I].OpenToken) then
begin
FMatchCloseDup[CloseDupLen] := I;
Inc(CloseDupLen);
end;
if (TokenMatch^.CloseToken = ATokens[I].CloseToken) then
begin
FMatchOpenDup[OpenDupLen] := I;
Inc(OpenDupLen);
end;
end;
if Result = 1 then
begin
Level := 1;
Next;
while True do
begin
while not GetEol do
if CheckToken then
Exit;
Inc(APoint.Line);
if APoint.Line >= ASynEdit.Lines.Count then
Break;
SetLine(Lines[APoint.Line], APoint.Line);
end;
end else
begin
if Length(FMatchStack) < 32 then
SetLength(FMatchStack, 32);
FMatchStackID := -1;
Level := -1;
if APoint.Line = 0 then
ResetRange
else
SetRange(TSynEditStringList(Lines).Ranges[APoint.Line - 1]);
SetLine(Lines[APoint.Line], APoint.Line);
while not GetEol and (GetTokenPos < AMatch.CloseTokenPos.Char -1) do
CheckTokenBack;
if FMatchStackID > -1 then
begin
Result := -2;
AMatch.OpenToken := FMatchStack[FMatchStackID].Token;
AMatch.OpenTokenPos := FMatchStack[FMatchStackID].Pos;
end else
while APoint.Line > 0 do
begin
DeltaLevel := -Level - 1;
Dec(APoint.Line);
if APoint.Line = 0 then
ResetRange
else
SetRange(TSynEditStringList(Lines).Ranges[APoint.Line - 1]);
SetLine(Lines[APoint.Line], APoint.Line);
FMatchStackID := -1;
while not GetEol do
CheckTokenBack;
if (DeltaLevel <= FMatchStackID) then
begin
Result := -2;
AMatch.OpenToken := FMatchStack[FMatchStackID - DeltaLevel].Token;
AMatch.OpenTokenPos := FMatchStack[FMatchStackID - DeltaLevel].Pos;
Exit;
end;
end;
end;
end;
end;
function SynEditGetMatchingTokenEx(ASynEdit: TCustomSynEdit; APoint: TBufferCoord;
const ATokens: array of TSynTokenMatch; var AMatch: TSynTokenMatched): Integer;
begin
Result := SynEditGetMatchingToken(ASynEdit, APoint, ATokens, AMatch);
if (Result = 0) and (APoint.Char > 1) then
begin
Dec(APoint.Char);
Result := SynEditGetMatchingToken(ASynEdit, APoint, ATokens, AMatch);
end;
end;
initialization
// None
finalization
SetLength(FMatchStack, 0);
SetLength(FMatchOpenDup, 0);
SetLength(FMatchCloseDup, 0);
end.