-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathSynHighlighterSample.pas
More file actions
508 lines (444 loc) · 12.5 KB
/
Copy pathSynHighlighterSample.pas
File metadata and controls
508 lines (444 loc) · 12.5 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
{-------------------------------------------------------------------------------
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.
Code template generated with SynGen.
The original code is: D:\Dev\Projets\Fallout4Translator\SynHighlighterSample.pas, released 2016-05-29.
Description: Syntax Parser/Highlighter
The initial author of this file is stef.
Copyright (c) 2016, all rights reserved.
Contributors to the SynEdit and mwEdit projects are listed in the
Contributors.txt file.
Alternatively, the contents of this file may be used under the terms of the
GNU General Public License Version 2 or later (the "GPL"), in which case
the provisions of the GPL are applicable instead of those above.
If you wish to allow use of your version of this file only under the terms
of the GPL 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 GPL.
If you do not delete the provisions above, a recipient may use your version
of this file under either the MPL or the GPL.
$Id: $
You may retrieve the latest version of this file at the SynEdit home page,
located at http://SynEdit.SourceForge.net
-------------------------------------------------------------------------------}
{$IFNDEF QSYNHIGHLIGHTERSAMPLE}
unit SynHighlighterSample;
{$ENDIF}
{$I SynEdit.inc}
interface
uses
{$IFDEF SYN_CLX}
QGraphics,
QSynEditTypes,
QSynEditHighlighter,
QSynUnicode,
{$ELSE}
Graphics,
SynEditTypes,
SynEditHighlighter,
SynUnicode,
{$ENDIF}
SysUtils,
Classes;
type
TtkTokenKind = (
tkComment2,
tkComment3,
tkIdentifier,
tkKey,
tkNull,
tkSpace,
tkString,
tkUnknown);
TRangeState = (rsUnKnown, rshtmlComment, rsBracketComment, rsString);
TProcTableProc = procedure of object;
PIdentFuncTableFunc = ^TIdentFuncTableFunc;
TIdentFuncTableFunc = function (Index: Integer): TtkTokenKind of object;
type
TSynSampleSyn = class(TSynCustomHighlighter)
private
fRange: TRangeState;
fTokenID: TtkTokenKind;
fIdentFuncTable: array[0..1] of TIdentFuncTableFunc;
fComment2Attri: TSynHighlighterAttributes;
fComment3Attri: TSynHighlighterAttributes;
fIdentifierAttri: TSynHighlighterAttributes;
fKeyAttri: TSynHighlighterAttributes;
fSpaceAttri: TSynHighlighterAttributes;
fStringAttri: TSynHighlighterAttributes;
function HashKey(Str: PWideChar): Cardinal;
function FuncFallout(Index: Integer): TtkTokenKind;
function FuncSkyrim(Index: Integer): TtkTokenKind;
procedure IdentProc;
procedure UnknownProc;
function AltFunc(Index: Integer): TtkTokenKind;
procedure InitIdent;
function IdentKind(MayBe: PWideChar): TtkTokenKind;
procedure NullProc;
procedure SpaceProc;
procedure CRProc;
procedure LFProc;
procedure htmlCommentOpenProc;
procedure htmlCommentProc;
procedure BracketCommentOpenProc;
procedure BracketCommentProc;
procedure StringOpenProc;
procedure StringProc;
protected
function GetSampleSource: UnicodeString; override;
function IsFilterStored: Boolean; override;
public
constructor Create(AOwner: TComponent); override;
class function GetFriendlyLanguageName: UnicodeString; override;
class function GetLanguageName: string; override;
function GetRange: Pointer; override;
procedure ResetRange; override;
procedure SetRange(Value: Pointer); override;
function GetDefaultAttribute(Index: Integer): TSynHighlighterAttributes; override;
function GetEol: Boolean; override;
function GetKeyWords(TokenKind: Integer): UnicodeString; override;
function GetTokenID: TtkTokenKind;
function GetTokenAttribute: TSynHighlighterAttributes; override;
function GetTokenKind: Integer; override;
function IsIdentChar(AChar: WideChar): Boolean; override;
procedure Next; override;
published
property Comment2Attri: TSynHighlighterAttributes read fComment2Attri write fComment2Attri;
property Comment3Attri: TSynHighlighterAttributes read fComment3Attri write fComment3Attri;
property IdentifierAttri: TSynHighlighterAttributes read fIdentifierAttri write fIdentifierAttri;
property KeyAttri: TSynHighlighterAttributes read fKeyAttri write fKeyAttri;
property SpaceAttri: TSynHighlighterAttributes read fSpaceAttri write fSpaceAttri;
property StringAttri: TSynHighlighterAttributes read fStringAttri write fStringAttri;
end;
implementation
uses
{$IFDEF SYN_CLX}
QSynEditStrConst;
{$ELSE}
SynEditStrConst;
{$ENDIF}
resourcestring
SYNS_FilterGeneral = 'All files (*.*)|*.*';
SYNS_AttrComment2 = 'Comment2';
SYNS_FriendlyAttrComment2 = 'Comment2';
SYNS_AttrComment3 = 'Comment3';
SYNS_FriendlyAttrComment3 = 'Comment3';
const
// as this language is case-insensitive keywords *must* be in lowercase
KeyWords: array[0..1] of UnicodeString = (
'fallout', 'skyrim'
);
KeyIndices: array[0..1] of Integer = (
0, 1
);
procedure TSynSampleSyn.InitIdent;
var
i: Integer;
begin
for i := Low(fIdentFuncTable) to High(fIdentFuncTable) do
if KeyIndices[i] = -1 then
fIdentFuncTable[i] := AltFunc;
fIdentFuncTable[0] := FuncFallout;
fIdentFuncTable[1] := FuncSkyrim;
end;
{$Q-}
function TSynSampleSyn.HashKey(Str: PWideChar): Cardinal;
begin
Result := 0;
while IsIdentChar(Str^) do
begin
Result := Result * 4 + Ord(Str^);
inc(Str);
end;
Result := Result mod 2;
fStringLen := Str - fToIdent;
end;
{$Q+}
function TSynSampleSyn.FuncFallout(Index: Integer): TtkTokenKind;
begin
if IsCurrentToken(KeyWords[Index]) then
Result := tkKey
else
Result := tkIdentifier;
end;
function TSynSampleSyn.FuncSkyrim(Index: Integer): TtkTokenKind;
begin
if IsCurrentToken(KeyWords[Index]) then
Result := tkKey
else
Result := tkIdentifier;
end;
function TSynSampleSyn.AltFunc(Index: Integer): TtkTokenKind;
begin
Result := tkIdentifier;
end;
function TSynSampleSyn.IdentKind(MayBe: PWideChar): TtkTokenKind;
var
Key: Cardinal;
begin
fToIdent := MayBe;
Key := HashKey(MayBe);
if Key <= High(fIdentFuncTable) then
Result := fIdentFuncTable[Key](KeyIndices[Key])
else
Result := tkIdentifier;
end;
procedure TSynSampleSyn.SpaceProc;
begin
inc(Run);
fTokenID := tkSpace;
while (FLine[Run] <= #32) and not IsLineEnd(Run) do inc(Run);
end;
procedure TSynSampleSyn.NullProc;
begin
fTokenID := tkNull;
inc(Run);
end;
procedure TSynSampleSyn.CRProc;
begin
fTokenID := tkSpace;
inc(Run);
if fLine[Run] = #10 then
inc(Run);
end;
procedure TSynSampleSyn.LFProc;
begin
fTokenID := tkSpace;
inc(Run);
end;
procedure TSynSampleSyn.htmlCommentOpenProc;
begin
Inc(Run);
fRange := rshtmlComment;
fTokenID := tkComment2;
end;
procedure TSynSampleSyn.htmlCommentProc;
begin
case fLine[Run] of
#0: NullProc;
#10: LFProc;
#13: CRProc;
else
begin
fTokenID := tkComment2;
repeat
if (fLine[Run] = '>') then
begin
Inc(Run, 1);
fRange := rsUnKnown;
Break;
end;
if not IsLineEnd(Run) then
Inc(Run);
until IsLineEnd(Run);
end;
end;
end;
procedure TSynSampleSyn.BracketCommentOpenProc;
begin
Inc(Run);
fRange := rsBracketComment;
fTokenID := tkComment3;
end;
procedure TSynSampleSyn.BracketCommentProc;
begin
case fLine[Run] of
#0: NullProc;
#10: LFProc;
#13: CRProc;
else
begin
fTokenID := tkComment3;
repeat
if (fLine[Run] = ']') then
begin
Inc(Run, 1);
fRange := rsUnKnown;
Break;
end;
if not IsLineEnd(Run) then
Inc(Run);
until IsLineEnd(Run);
end;
end;
end;
procedure TSynSampleSyn.StringOpenProc;
begin
Inc(Run);
fRange := rsString;
fTokenID := tkString;
end;
procedure TSynSampleSyn.StringProc;
begin
case fLine[Run] of
#0: NullProc;
#10: LFProc;
#13: CRProc;
else
begin
fTokenID := tkString;
repeat
if (fLine[Run] = '"') then
begin
Inc(Run, 1);
fRange := rsUnKnown;
Break;
end;
if not IsLineEnd(Run) then
Inc(Run);
until IsLineEnd(Run);
end;
end;
end;
constructor TSynSampleSyn.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
fCaseSensitive := False;
fComment2Attri := TSynHighLighterAttributes.Create(SYNS_AttrComment2, SYNS_FriendlyAttrComment2);
fComment2Attri.Style := [fsBold];
fComment2Attri.Foreground := clNavy;
AddAttribute(fComment2Attri);
fComment3Attri := TSynHighLighterAttributes.Create(SYNS_AttrComment3, SYNS_FriendlyAttrComment3);
fComment3Attri.Style := [fsItalic];
fComment3Attri.Foreground := clGreen;
AddAttribute(fComment3Attri);
fIdentifierAttri := TSynHighLighterAttributes.Create(SYNS_AttrIdentifier, SYNS_FriendlyAttrIdentifier);
AddAttribute(fIdentifierAttri);
fKeyAttri := TSynHighLighterAttributes.Create(SYNS_AttrReservedWord, SYNS_FriendlyAttrReservedWord);
fKeyAttri.Style := [];
AddAttribute(fKeyAttri);
fSpaceAttri := TSynHighLighterAttributes.Create(SYNS_AttrSpace, SYNS_FriendlyAttrSpace);
AddAttribute(fSpaceAttri);
fStringAttri := TSynHighLighterAttributes.Create(SYNS_AttrString, SYNS_FriendlyAttrString);
fStringAttri.Foreground := clMaroon;
AddAttribute(fStringAttri);
SetAttributesOnChange(DefHighlightChange);
InitIdent;
fDefaultFilter := SYNS_FilterGeneral;
fRange := rsUnknown;
end;
procedure TSynSampleSyn.IdentProc;
begin
fTokenID := IdentKind(fLine + Run);
inc(Run, fStringLen);
while IsIdentChar(fLine[Run]) do
Inc(Run);
end;
procedure TSynSampleSyn.UnknownProc;
begin
inc(Run);
fTokenID := tkUnknown;
end;
procedure TSynSampleSyn.Next;
begin
fTokenPos := Run;
case fRange of
rshtmlComment: htmlCommentProc;
rsBracketComment: BracketCommentProc;
rsString: StringProc;
else
case fLine[Run] of
#0: NullProc;
#10: LFProc;
#13: CRProc;
'<': htmlCommentOpenProc;
'[': BracketCommentOpenProc;
'"': StringOpenProc;
#1..#9, #11, #12, #14..#32: SpaceProc;
'A'..'Z', 'a'..'z', '_': IdentProc;
else
UnknownProc;
end;
end;
inherited;
end;
function TSynSampleSyn.GetDefaultAttribute(Index: Integer): TSynHighLighterAttributes;
begin
case Index of
SYN_ATTR_IDENTIFIER: Result := fIdentifierAttri;
SYN_ATTR_KEYWORD: Result := fKeyAttri;
SYN_ATTR_STRING: Result := fStringAttri;
SYN_ATTR_WHITESPACE: Result := fSpaceAttri;
else
Result := nil;
end;
end;
function TSynSampleSyn.GetEol: Boolean;
begin
Result := Run = fLineLen + 1;
end;
function TSynSampleSyn.GetKeyWords(TokenKind: Integer): UnicodeString;
begin
Result :=
'Fallout,Skyrim';
end;
function TSynSampleSyn.GetTokenID: TtkTokenKind;
begin
Result := fTokenId;
end;
function TSynSampleSyn.GetTokenAttribute: TSynHighLighterAttributes;
begin
case GetTokenID of
tkComment2: Result := fComment2Attri;
tkComment3: Result := fComment3Attri;
tkIdentifier: Result := fIdentifierAttri;
tkKey: Result := fKeyAttri;
tkSpace: Result := fSpaceAttri;
tkString: Result := fStringAttri;
tkUnknown: Result := fIdentifierAttri;
else
Result := nil;
end;
end;
function TSynSampleSyn.GetTokenKind: Integer;
begin
Result := Ord(fTokenId);
end;
function TSynSampleSyn.IsIdentChar(AChar: WideChar): Boolean;
begin
case AChar of
'_', '0'..'9', 'a'..'z', 'A'..'Z':
Result := True;
else
Result := False;
end;
end;
function TSynSampleSyn.GetSampleSource: UnicodeString;
begin
Result :=
'{highlighter}';
end;
function TSynSampleSyn.IsFilterStored: Boolean;
begin
Result := fDefaultFilter <> SYNS_FilterGeneral;
end;
class function TSynSampleSyn.GetFriendlyLanguageName: UnicodeString;
begin
Result := SYNS_FriendlyLangGeneral;
end;
class function TSynSampleSyn.GetLanguageName: string;
begin
Result := SYNS_LangGeneral;
end;
procedure TSynSampleSyn.ResetRange;
begin
fRange := rsUnknown;
end;
procedure TSynSampleSyn.SetRange(Value: Pointer);
begin
fRange := TRangeState(Value);
end;
function TSynSampleSyn.GetRange: Pointer;
begin
Result := Pointer(fRange);
end;
initialization
{$IFNDEF SYN_CPPB_1}
RegisterPlaceableHighlighter(TSynSampleSyn);
{$ENDIF}
end.