-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdwsTokenizer.pas
More file actions
1738 lines (1546 loc) · 47.1 KB
/
Copy pathdwsTokenizer.pas
File metadata and controls
1738 lines (1546 loc) · 47.1 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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{**********************************************************************}
{ }
{ "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 Initial Developer of the Original Code is Matthias }
{ Ackermann. For other initial contributors, see contributors.txt }
{ Subsequent portions Copyright Creative IT. }
{ }
{ Current maintainer: Eric Grange }
{ }
{**********************************************************************}
unit dwsTokenizer;
{$I dws.inc}
interface
uses
SysUtils, Classes, TypInfo,
dwsScriptSource, dwsErrors, dwsStrings, dwsXPlatform, dwsUtils, dwsXXHash
{$ifdef FPC},lazutf8{$endif};
type
TTokenType =
(
ttNone, ttStrVal, ttIntVal, ttFloatVal, ttNAME, ttSWITCH,
ttLAZY, ttVAR, ttCONST, ttRESOURCESTRING,
ttTYPE, ttRECORD, ttARRAY, ttSET, ttDOT, ttDOTDOT, ttOF, ttENUM, ttFLAGS,
ttTRY, ttEXCEPT, ttRAISE, ttFINALLY, ttON, ttREAD, ttWRITE, ttPROPERTY,
ttFUNCTION, ttPROCEDURE, ttCONSTRUCTOR, ttDESTRUCTOR, ttMETHOD, ttLAMBDA, ttOPERATOR,
ttCLASS, ttNIL, ttIS, ttAS, ttIMPLEMENTS, ttINDEX, ttOBJECT,
ttVIRTUAL, ttOVERRIDE, ttREINTRODUCE, ttINHERITED, ttFINAL, ttNEW,
ttABSTRACT, ttSEALED, ttSTATIC, ttPARTIAL, ttDEPRECATED, ttOVERLOAD,
ttEXTERNAL, ttEXPORT, ttFORWARD, ttINLINE, ttEMPTY, ttIN,
ttENSURE, ttREQUIRE, ttINVARIANTS, ttOLD,
ttINTERFACE, ttIMPLEMENTATION, ttINITIALIZATION, ttFINALIZATION,
ttHELPER, ttSTRICT,
ttASM, ttBEGIN, ttEND, ttBREAK, ttCONTINUE, ttEXIT,
ttIF, ttTHEN, ttELSE, ttWITH, ttWHILE, ttREPEAT, ttUNTIL, ttFOR, ttTO, ttDOWNTO, ttDO,
ttCASE,
ttTRUE, ttFALSE,
ttAND, ttOR, ttXOR, ttDIV, ttMOD, ttNOT, ttSHL, ttSHR, ttSAR,
ttPLUS, ttMINUS, ttIMPLIES, ttIMPLICIT,
ttTIMES, ttDIVIDE, ttPERCENT, ttCARET, ttAT, ttTILDE,
ttDOLLAR, ttEXCLAMATION, ttQUESTION, ttQUESTIONQUESTION, ttQUESTIONDOT,
ttEQ, ttNOTEQ, ttGTR, ttGTREQ, ttLESS, ttLESSEQ, ttEQGTR,
ttLESSLESS, ttGTRGTR, ttPIPE, ttPIPEPIPE, ttAMP, ttAMPAMP,
ttSEMI, ttCOMMA, ttCOLON,
ttASSIGN, ttPLUS_ASSIGN, ttMINUS_ASSIGN, ttTIMES_ASSIGN, ttDIVIDE_ASSIGN,
ttPERCENT_ASSIGN, ttCARET_ASSIGN, ttAT_ASSIGN, ttTILDE_ASSIGN,
ttBLEFT, ttBRIGHT, ttALEFT, ttARIGHT, ttCLEFT, ttCRIGHT,
ttDEFAULT, ttUSES, ttUNIT, ttNAMESPACE,
ttPRIVATE, ttPROTECTED, ttPUBLIC, ttPUBLISHED,
ttPROGRAM, ttLIBRARY,
// Tokens for compatibility to Delphi
ttREGISTER, ttPASCAL, ttCDECL, ttSAFECALL, ttSTDCALL, ttFASTCALL, ttREFERENCE
);
TTokenTypes = set of TTokenType;
// TTokenBuffer
//
TTokenBuffer = record
Len : Integer;
Capacity : Integer;
CaseSensitive : Boolean;
Buffer : array of Char;
Unifier : TStringUnifier;
procedure AppendChar(c : Char);
procedure Grow;
function LastChar : Char;
function ToStr : String; overload; inline;
procedure ToStr(var result : String); overload;
procedure AppendMultiToStr(var result : String);
procedure AppendToStr(var result : String);
procedure ToUpperStr(var result : String); overload;
function UpperFirstChar : Char;
function UpperMatchLen(const str : String) : Boolean;
function MatchLen(const str : String) : Boolean;
procedure RaiseInvalidIntegerConstant;
function BinToInt64 : Int64;
function HexToInt64 : Int64;
function ToInt64 : Int64;
function ToFloat : Double;
function ToType : TTokenType;
function ToAlphaType : TTokenType;
function ToAlphaTypeCaseSensitive : TTokenType;
class function StringToTokenType(const str : String) : TTokenType; static;
end;
TToken = ^TTokenRecord;
TTokenRecord = record
private
FString : String;
FNext : TToken;
public
FScriptPos : TScriptPos;
FFloat : Double;
FInteger : Int64;
FTyp : TTokenType;
property AsString : String read FString;
function EmptyString : Boolean; inline;
end;
TCharsType = set of AnsiChar;
TTransition = class;
TState = class (TRefCountedObject)
private
FOwnedTransitions : TTightList;
FTransitions : array [#0..#127] of TTransition;
public
destructor Destroy; override;
function FindTransition(c : Char) : TTransition; inline;
procedure AddTransition(const chrs : TCharsType; o : TTransition);
procedure AddEOFTransition(o : TTransition);
procedure SetTransition(c : AnsiChar; o : TTransition); inline;
procedure SetElse(o : TTransition);
end;
TConvertAction = (caNone, caClear, caName, caNameEscaped,
caBin, caHex, caInteger, caFloat,
caChar, caCharHex, caString, caMultiLineString,
caSwitch, caDotDot, caAmp, caAmpAmp);
TTransitionOptions = set of (toStart, toFinal);
TTransition = class (TRefCountedObject)
private
NextState : TState;
Start : Boolean; // Marks the begin of a Token
Final : Boolean; // Marks the end of a Token
Action : TConvertAction;
IsError : Boolean;
Consume : Boolean;
Seek : Boolean;
public
constructor Create(nstate: TState; opts: TTransitionOptions; actn: TConvertAction);
end;
TElseTransition = class(TTransition)
constructor Create(actn : TConvertAction);
end;
TErrorTransition = class(TTransition)
private
ErrorMessage : String;
public
constructor Create(const msg : String);
end;
TCheckTransition = class(TTransition);
TSeekTransition = class (TCheckTransition) // Transition, next Char
constructor Create(nstate: TState; opts: TTransitionOptions; actn: TConvertAction);
end;
TConsumeTransition = class (TSeekTransition) // Transition, consume Char, next Char
constructor Create(nstate: TState; opts: TTransitionOptions; actn: TConvertAction);
end;
TSwitchHandler = function(const switchName : String) : Boolean of object;
TTokenizer = class;
TTokenizerRules = class
private
FStates : TObjectList<TState>;
FEOFTransition : TErrorTransition;
FReservedNames : TTokenTypes;
FSymbolTokens : TTokenTypes;
FReservedTokens : TTokenTypes;
FCaseSensitive : Boolean;
protected
function CreateState : TState;
function StartState : TState; virtual; abstract;
public
constructor Create; virtual;
destructor Destroy; override;
procedure PrepareStates;
function CreateTokenizer(msgs : TdwsCompileMessageList; unifier : TStringUnifier) : TTokenizer;
property ReservedNames : TTokenTypes read FReservedNames write FReservedNames;
property SymbolTokens : TTokenTypes read FSymbolTokens write FSymbolTokens;
property ReservedTokens : TTokenTypes read FReservedTokens;
property CaseSensitive : Boolean read FCaseSensitive write FCaseSensitive;
end;
TTokenizerSourceInfo = record
FPathName : TFileName;
FText : String;
FDefaultPos : TScriptPos;
FHotPos : TScriptPos;
FCurPos : TScriptPos;
FPosPtr : PChar;
end;
PTokenizerSourceInfo = ^TTokenizerSourceInfo;
TTokenizerConditional = (tcIf, tcElse);
TTokenizerConditionalInfo = record
Conditional : TTokenizerConditional;
ScriptPos : TScriptPos;
end;
TTokenizerEndSourceFileEvent = procedure (sourceFile : TSourceFile) of object;
TTokenizer = class
private
FTokenBuf : TTokenBuffer;
FNextToken : TToken;
FRules : TTokenizerRules;
FStartState : TState;
FToken : TToken;
FSource : TTokenizerSourceInfo;
FSwitchHandler : TSwitchHandler;
FSwitchProcessor : TSwitchHandler;
FMsgs : TdwsCompileMessageList;
FConditionalDefines : IAutoStrings;
FConditionalDepth : TSimpleStack<TTokenizerConditionalInfo>;
FTokenPool : TToken;
FSourceStack : array of TTokenizerSourceInfo;
FOnEndSourceFile : TTokenizerEndSourceFileEvent;
procedure AllocateToken;
procedure ReleaseToken;
procedure HandleChar(var tokenBuf : TTokenBuffer; var result : TToken);
procedure HandleBin(var tokenBuf : TTokenBuffer; var result : TToken);
procedure HandleHexa(var tokenBuf : TTokenBuffer; var result : TToken);
procedure HandleInteger(var tokenBuf : TTokenBuffer; var result : TToken);
procedure HandleFloat(var tokenBuf : TTokenBuffer; var result : TToken);
function HandleSwitch : Boolean;
procedure ConsumeToken;
procedure ReadToken;
procedure AddCompilerStopFmtTokenBuffer(const formatString : String);
public
constructor Create(rules : TTokenizerRules; msgs : TdwsCompileMessageList;
unifier : TStringUnifier = nil);
destructor Destroy; override;
procedure BeginSourceFile(sourceFile : TSourceFile; const pathName : TFileName = '');
procedure EndSourceFile;
function GetToken : TToken; inline;
function HasTokens : Boolean;
procedure KillToken; inline;
function Test(t : TTokenType) : Boolean;
function TestAny(const t : TTokenTypes) : TTokenType;
function TestDelete(t : TTokenType) : Boolean;
function TestDeleteAny(const t : TTokenTypes) : TTokenType;
function TestName : Boolean;
function TestAnyName : Boolean;
function TestDeleteNamePos(var aName : String; var aPos : TScriptPos) : Boolean; inline;
function TestDeleteAnyNamePos(var aName : String; var aPos : TScriptPos) : Boolean; inline;
procedure SkipTo(t : TTokenType);
procedure SimulateToken(t : TTokenType; const scriptPos : TScriptPos);
procedure SimulateStringToken(const scriptPos : TScriptPos; const str : String);
procedure SimulateIntegerToken(const scriptPos : TScriptPos; const i : Int64);
procedure SimulateNameToken(const scriptPos : TScriptPos; const name : String);
property PosPtr : PChar read FSource.FPosPtr;
property Text : String read FSource.FText;
property DefaultPos : TScriptPos read FSource.FDefaultPos;
property HotPos : TScriptPos read FSource.FHotPos;
property CurrentPos : TScriptPos read FSource.FCurPos;
property PathName : TFileName read FSource.FPathName;
function SafePathName : String; inline;
property ConditionalDepth : TSimpleStack<TTokenizerConditionalInfo> read FConditionalDepth;
property Rules : TTokenizerRules read FRules;
property SwitchHandler : TSwitchHandler read FSwitchHandler write FSwitchHandler;
property SwitchProcessor : TSwitchHandler read FSwitchProcessor write FSwitchProcessor;
property ConditionalDefines : IAutoStrings read FConditionalDefines write FConditionalDefines;
property OnEndSourceFile : TTokenizerEndSourceFileEvent read FOnEndSourceFile write FOnEndSourceFile;
end;
const
cTokenStrings : array [TTokenType] of String = (
'', 'UnicodeString Literal', 'Integer Literal', 'Float Literal', 'NAME', 'SWITCH',
'LAZY', 'VAR', 'CONST', 'RESOURCESTRING',
'TYPE', 'RECORD', 'ARRAY', 'SET', '.', '..', 'OF', 'ENUM', 'FLAGS',
'TRY', 'EXCEPT', 'RAISE', 'FINALLY', 'ON', 'READ', 'WRITE', 'PROPERTY',
'FUNCTION', 'PROCEDURE', 'CONSTRUCTOR', 'DESTRUCTOR', 'METHOD', 'LAMBDA', 'OPERATOR',
'CLASS', 'NIL', 'IS', 'AS', 'IMPLEMENTS', 'INDEX', 'OBJECT',
'VIRTUAL', 'OVERRIDE', 'REINTRODUCE', 'INHERITED', 'FINAL', 'NEW',
'ABSTRACT', 'SEALED', 'STATIC', 'PARTIAL', 'DEPRECATED', 'OVERLOAD',
'EXTERNAL', 'EXPORT', 'FORWARD', 'INLINE', 'EMPTY', 'IN',
'ENSURE', 'REQUIRE', 'INVARIANTS', 'OLD',
'INTERFACE', 'IMPLEMENTATION', 'INITIALIZATION', 'FINALIZATION',
'HELPER', 'STRICT',
'ASM', 'BEGIN', 'END', 'BREAK', 'CONTINUE', 'EXIT',
'IF', 'THEN', 'ELSE', 'WITH', 'WHILE', 'REPEAT', 'UNTIL', 'FOR', 'TO', 'DOWNTO', 'DO',
'CASE',
'TRUE', 'FALSE',
'AND', 'OR', 'XOR', 'DIV', 'MOD', 'NOT', 'SHL', 'SHR', 'SAR',
'+', '-', 'IMPLIES', 'IMPLICIT',
'*', '/', '%', '^', '@', '~', '$', '!', '?', '??', '?.',
'=', '<>', '>', '>=', '<', '<=', '=>',
'<<', '>>', '|', '||', '&', '&&',
';', ',', ':',
':=', '+=', '-=', '*=', '/=',
'%=', '^=', '@=', '~=',
'(', ')', '[', ']', '{', '}',
'DEFAULT', 'USES', 'UNIT', 'NAMESPACE',
'PRIVATE', 'PROTECTED', 'PUBLIC', 'PUBLISHED',
'PROGRAM', 'LIBRARY',
'REGISTER', 'PASCAL', 'CDECL', 'SAFECALL', 'STDCALL', 'FASTCALL', 'REFERENCE'
);
function TokenTypesToString(const tt : TTokenTypes) : String;
// ------------------------------------------------------------------
// ------------------------------------------------------------------
// ------------------------------------------------------------------
implementation
// ------------------------------------------------------------------
// ------------------------------------------------------------------
// ------------------------------------------------------------------
const
cFormatSettings : TFormatSettings = ( DecimalSeparator : {%H-}'.' );
// TokenTypesToString
//
function TokenTypesToString(const tt : TTokenTypes) : String;
var
t : TTokenType;
begin
for t in tt do begin
if Result<>'' then
Result:=Result+' or ';
case t of
ttIntVal, ttStrVal, ttFloatVal :
Result:=Result+cTokenStrings[t];
else
Result:=Result+'"'+cTokenStrings[t]+'"';
end;
end;
end;
// EmptyString
//
function TTokenRecord.EmptyString : Boolean;
begin
Result:=(FString='');
end;
// AppendChar
//
procedure TTokenBuffer.AppendChar(c : Char);
begin
if Len>=Capacity then Grow;
Buffer[Len]:=c;
Inc(Len);
end;
// Grow
//
procedure TTokenBuffer.Grow;
begin
if Capacity=0 then
Capacity:=256
else Capacity:=Capacity*2;
SetLength(Buffer, Capacity);
end;
// LastChar
//
function TTokenBuffer.LastChar : Char;
begin
if Len>0 then
Result:=Buffer[Len-1]
else Result:=#0;
end;
// ToStr
//
function TTokenBuffer.ToStr : String;
begin
ToStr(Result);
end;
// ToStr
//
procedure TTokenBuffer.ToStr(var result : String);
begin
case Len of
0 : result := '';
1 : UnifyAssignChar(@Buffer[0], result);
else
if Unifier <> nil then
Unifier.UnifyAssignP(@Buffer[0], Len, result)
else SetString(result, PChar(@Buffer[0]), Len);
end;
end;
// AppendToStr
//
procedure TTokenBuffer.AppendToStr(var result : String);
var
n : Integer;
begin
if Len>0 then begin
n:=Length(result);
SetLength(result, n+Len);
Move(Buffer[0], PChar(Pointer(result))[n], Len*SizeOf(Char));
end;
end;
// AppendMultiToStr
//
procedure TTokenBuffer.AppendMultiToStr(var result : String);
var
i, n, k, minWhite, white : Integer;
leftWhite, firstIsCRLF, firstLine : Boolean;
begin
if Len=0 then Exit;
// count nb lines and minimum whitespace, also detect if first line is whitespace + CRLF
minWhite:=MaxInt;
leftWhite:=True;
white:=0;
firstIsCRLF:=False;
firstLine:=True;
for i:=0 to Len-1 do begin
case Buffer[i] of
' ' : if leftWhite then Inc(white);
#13 : ;
#10 : begin
if firstLine then begin
if leftWhite then
firstIsCRLF:=True;
firstLine:=False;
end;
if not leftWhite then begin
if white<minWhite then
minWhite:=white;
leftWhite:=True;
end;
white:=0;
end;
else
leftWhite:=False;
end;
end;
// ok now collect and remove indents
k:=Length(result);
SetLength(result, k+Len); // allocate for worst case
i:=0;
n:=Len;
// do we have to remove indents?
if firstIsCRLF then begin
// skip first line
while Buffer[i]<>#10 do
Inc(i);
Inc(i);
end;
leftWhite:=(minWhite>0);
white:=0;
while i<n do begin
case Buffer[i] of
' ' : begin
if leftWhite and (white<minWhite) then
Inc(white)
else begin
Inc(k);
result[k]:=' ';
end;
end;
#10 : begin
leftWhite:=(minWhite>0);
white:=0;
Inc(k);
result[k]:=Buffer[i];
end
else
leftWhite:=False;
Inc(k);
result[k]:=Buffer[i];
end;
Inc(i);
end;
SetLength(result, k);
end;
// ToUpperStr
//
procedure TTokenBuffer.ToUpperStr(var result : String);
var
i : Integer;
ch : Char;
pResult : PChar;
begin
if Len=0 then
result:=''
else begin
SetLength(result, Len);
pResult:=PChar(result);
for i:=0 to Len-1 do begin
ch:=Buffer[i];
case ch of
'a'..'z' : pResult[i]:=Char(Word(ch) xor $0020)
else
pResult[i]:=ch;
end;
end;
end;
end;
// UpperFirstChar
//
function TTokenBuffer.UpperFirstChar : Char;
begin
if Len=0 then
Result:=#0
else begin
Result:=Buffer[0];
case Result of
'a'..'z' : Result:=Char(Word(Result) xor $0020)
end;
end;
end;
// RaiseInvalidIntegerConstant
//
procedure TTokenBuffer.RaiseInvalidIntegerConstant;
begin
raise EIntOverflow.CreateFmt(TOK_InvalidIntegerConstant, [ToStr]);
end;
// BinToInt64
//
function TTokenBuffer.BinToInt64 : Int64;
var
i : Integer;
begin
Result:=0;
for i:=2 to Len-1 do begin
// highest bit already set, if we're still here we'll overflow
if Result<0 then
RaiseInvalidIntegerConstant;
case Ord(Buffer[i]) of
Ord('1') : Result:=(Result shl 1) or 1;
Ord('0') : Result:=(Result shl 1);
end;
end;
end;
// BinToInt64
//
function TTokenBuffer.HexToInt64 : Int64;
var
i : Integer;
v : Integer;
begin
if Buffer[0]='$' then
i:=1 // $ form
else i:=2; // 0x form
Result:=0;
while i<Len do begin
// highest nibble already set, if we're still here we'll overflow
if (Result shr 60)>0 then RaiseInvalidIntegerConstant;
v:=Ord(Buffer[i]);
Inc(i);
case v of
Ord('0')..Ord('9') : v:=v-Ord('0');
Ord('a')..Ord('f') : v:=v-(Ord('a')-10);
Ord('A')..Ord('F') : v:=v-(Ord('A')-10);
else
continue;
end;
Result:=(Result shl 4) or v;
end;
end;
// ToInt64
//
function TTokenBuffer.ToInt64 : Int64;
function ComplexToInt64(var buffer : TTokenBuffer) : Int64;
begin
Result := StrToInt64(buffer.ToStr);
end;
var
i, i2 : Integer;
begin
case Len of
1 : begin
i:=Ord(Buffer[0])-Ord('0');
if Cardinal(i)<Cardinal(10) then Exit(i);
end;
2 : begin
i:=Ord(Buffer[0])-Ord('0');
if Cardinal(i)<Cardinal(10) then begin
i2:=Ord(Buffer[1])-Ord('0');
if Cardinal(i2)<Cardinal(10) then
Exit(i*10+i2);
end;
end;
end;
Result:=ComplexToInt64(Self);
end;
// ToFloat
//
function TTokenBuffer.ToFloat : Double;
var
buf : Extended;
begin
AppendChar(#0);
if not TryTextToFloat(PChar(@Buffer[0]), buf, cFormatSettings) then
raise EConvertError.Create('');
Result:=buf;
end;
// ToType
//
function TTokenBuffer.ToType : TTokenType;
begin
Result := ttNAME;
if Len=0 then Exit;
case Buffer[0] of
'/':
if Len=1 then
Result := ttDIVIDE
else if Len=2 then
if Buffer[1]='=' then
Result := ttDIVIDE_ASSIGN; // '/='
'*':
if Len=1 then
Result := ttTIMES
else if Len=2 then
if Buffer[1]='=' then
Result := ttTIMES_ASSIGN; // '*='
'+':
if Len=1 then
Result := ttPLUS
else if Len=2 then
if Buffer[1]='=' then
Result := ttPLUS_ASSIGN; // '+='
'-':
if Len=1 then
Result := ttMINUS
else if Len=2 then
if Buffer[1]='=' then
Result := ttMINUS_ASSIGN; // '-='
'@':
if Len=1 then
Result := ttAT
else if Len=2 then
if Buffer[1]='=' then
Result := ttAT_ASSIGN; // '@='
'%':
if Len=1 then
Result := ttPERCENT
else if Len=2 then
if Buffer[1]='=' then
Result := ttPERCENT_ASSIGN; // '%='
'^':
if Len=1 then
Result := ttCARET
else if Len=2 then
if Buffer[1]='=' then
Result := ttCARET_ASSIGN; // '^='
'~':
if Len=1 then
Result := ttTILDE
else if Len=2 then
if Buffer[1]='=' then
Result := ttTILDE_ASSIGN; // '~='
';': Result := ttSEMI;
'(': Result := ttBLEFT;
')': Result := ttBRIGHT;
'[': Result := ttALEFT;
']': Result := ttARIGHT;
'!': Result := ttEXCLAMATION;
'?':
if Len=1 then
Result := ttQUESTION
else if Len=2 then case Buffer[1] of
'?' : Result:= ttQUESTIONQUESTION; // ??
'.' : Result:= ttQUESTIONDOT; // ?.
end;
'=':
if Len=1 then
Result := ttEQ
else if Len=2 then
if Buffer[1]='>' then
Result := ttEQGTR;
'<':
if Len=1 then // '<'
Result := ttLESS
else if Len=2 then case Buffer[1] of
'=' : Result := ttLESSEQ; // '<='
'>' : Result := ttNOTEQ; // '<>'
'<' : Result := ttLESSLESS; // '<<'
end;
'>':
if Len=1 then // '>'
Result := ttGTR
else if Len=2 then case Buffer[1] of
'=' : Result := ttGTREQ; // '>='
'>' : Result := ttGTRGTR; // '>>'
end;
':':
if Len=1 then // ':'
Result := ttCOLON
else if Len=2 then
if Buffer[1]='=' then
Result := ttASSIGN; // ':='
',': Result := ttCOMMA;
'{': Result := ttCLEFT;
'}': Result := ttCRIGHT;
'.':
if Len=1 then
Result := ttDOT;
'$':
if Len=1 then
Result := ttDOLLAR;
'|':
if Len=1 then
Result := ttPIPE
else if Len=2 then
if Buffer[1]='|' then
Result := ttPIPEPIPE;
else
if CaseSensitive then
Result:=ToAlphaTypeCaseSensitive
else Result:=ToAlphaType;
end;
end;
// ToAlphaType
//
const
cAlphaTypeTokens : TTokenTypes = [
ttAND, ttARRAY, ttABSTRACT, ttAS, ttASM,
ttBEGIN, ttBREAK,
ttCONST, ttCLASS, ttCONSTRUCTOR, ttCASE, ttCDECL, ttCONTINUE,
ttDO, ttDOWNTO, ttDIV, ttDEFAULT, ttDESTRUCTOR, ttDEPRECATED,
ttELSE, ttEMPTY, ttEND, ttENSURE, ttENUM, ttEXCEPT, ttEXIT, ttEXTERNAL, ttEXPORT,
ttFALSE, ttFINAL, ttFINALIZATION, ttFINALLY, ttFLAGS, ttFOR,
ttFORWARD, ttFUNCTION, ttHELPER,
ttIF, ttIMPLIES, ttIMPLEMENTATION, ttIMPLEMENTS, ttIMPLICIT,
ttIN, ttINITIALIZATION, ttINLINE, ttINVARIANTS,
ttINHERITED, ttINDEX, ttINTERFACE, ttIS,
ttLAMBDA, ttLAZY, ttLIBRARY,
ttMETHOD, ttMOD,
ttNAMESPACE, ttNEW, ttNIL, ttNOT,
ttOBJECT, ttOF, ttOLD, ttON, ttOPERATOR, ttOR, ttOVERLOAD, ttOVERRIDE,
ttPARTIAL, ttPROCEDURE, ttPROPERTY, ttPASCAL, ttPROGRAM,
ttPRIVATE, ttPROTECTED, ttPUBLIC, ttPUBLISHED,
ttRECORD, ttREAD, ttRAISE, ttREINTRODUCE, ttREFERENCE, ttREGISTER,
ttREPEAT, ttREQUIRE, ttRESOURCESTRING,
ttSAFECALL, ttSAR, ttSEALED, ttSET, ttSHL, ttSHR, ttSTATIC, ttSTDCALL, ttSTRICT,
ttTHEN, ttTO, ttTRUE, ttTRY, ttTYPE,
ttUNIT, ttUNTIL, ttUSES,
ttVAR, ttVIRTUAL,
ttWHILE, ttWITH, ttWRITE,
ttXOR ];
type
TTokenAlphaLookup = record
Alpha : String;
Token : TTokenType;
end;
TTokenAlphaLookups = array of TTokenAlphaLookup;
PTokenAlphaLookups = ^TTokenAlphaLookups;
var
vAlphaToTokenType : array [2..14] of array ['A'..'X'] of TTokenAlphaLookups;
procedure PrepareAlphaToTokenType;
var
n, len : Integer;
tokenName : String;
tt : TTokenType;
begin
for tt in cAlphaTypeTokens do begin
tokenName := GetEnumName(TypeInfo(TTokenType), Ord(tt));
len:=Length(tokenName)-2;
Assert(len<=14);
n:=Length(vAlphaToTokenType[len][tokenName[3]]);
SetLength(vAlphaToTokenType[len][tokenName[3]], n+1);
with vAlphaToTokenType[len][tokenName[3]][n] do begin
Alpha := StrDeleteLeft(tokenName, 2);
Token := tt;
end;
end;
end;
// ------------------
// ------------------ TTokenBuffer ------------------
// ------------------
// UpperMatchLen
//
function TTokenBuffer.UpperMatchLen(const str : String) : Boolean;
var
i : Integer;
p : PChar;
ch : Char;
begin
p:=PChar(Pointer(str));
for i:=1 to Len-1 do begin
ch:=Buffer[i];
case ch of
'a'..'z' : if Char(Word(ch) xor $0020)<>p[i] then Exit(False);
else
if ch<>p[i] then Exit(False);
end;
end;
Result:=True;
end;
// ToAlphaType
//
function TTokenBuffer.ToAlphaType : TTokenType;
var
ch : Char;
i : Integer;
lookups : PTokenAlphaLookups;
begin
if (Len<2) or (Len>14) then Exit(ttNAME);
ch:=Buffer[0];
case ch of
'a'..'x' : lookups:=@vAlphaToTokenType[Len][Char(Word(ch) xor $0020)];
'A'..'X' : lookups:=@vAlphaToTokenType[Len][ch];
else
Exit(ttNAME);
end;
for i:=0 to High(lookups^) do begin
if UpperMatchLen(lookups^[i].Alpha) then
Exit(lookups^[i].Token);
end;
Result:=ttNAME;
end;
// MatchLen
//
function TTokenBuffer.MatchLen(const str : String) : Boolean;
var
i : Integer;
p : PChar;
ch : Char;
begin
p:=PChar(Pointer(str));
for i:=1 to Len-1 do begin
ch:=Buffer[i];
case ch of
'a'..'z' : if Char(Word(ch) xor $0020)<>p[i] then Exit(False);
else
Exit(False);
end;
end;
Result:=True;
end;
// ToAlphaTypeCaseSensitive
//
function TTokenBuffer.ToAlphaTypeCaseSensitive : TTokenType;
var
ch : Char;
i : Integer;
lookups : PTokenAlphaLookups;
begin
if (Len<2) or (Len>14) then Exit(ttNAME);
ch:=Buffer[0];
case ch of
'a'..'x' : lookups:=@vAlphaToTokenType[Len][Char(Word(ch) xor $0020)];
else
Exit(ttNAME);
end;
for i:=0 to High(lookups^) do begin
if MatchLen(lookups^[i].Alpha) then
Exit(lookups^[i].Token);
end;
Result:=ttNAME;
end;
// StringToTokenType
//
class function TTokenBuffer.StringToTokenType(const str : String) : TTokenType;
var
c : Char;
buffer : TTokenBuffer;
begin
if str='' then Exit(ttNone);
buffer.Capacity:=0;
buffer.Len:=0;
for c in str do
buffer.AppendChar(c);
Result:=buffer.ToType;
end;
// ------------------
// ------------------ TState ------------------
// ------------------
// Destroy
//
destructor TState.Destroy;
begin
FOwnedTransitions.Clean;
inherited Destroy;
end;
// FindTransition
//
function TState.FindTransition(c : Char) : TTransition;
begin
if c>#127 then
c:=#127;
Result:=FTransitions[c];
end;
// AddTransition
//
procedure TState.AddTransition(const chrs : TCharsType; o : TTransition);
var
c : AnsiChar;
begin
for c:=#0 to #127 do
if c in chrs then begin
if FTransitions[c]=nil then
SetTransition(c, o);
end;
FOwnedTransitions.Add(o);
end;
// AddEOFTransition
//
procedure TState.AddEOFTransition(o : TTransition);
begin
SetTransition(#0, o);
FOwnedTransitions.Add(o);
end;
// SetTransition
//
procedure TState.SetTransition(c : AnsiChar; o : TTransition);
begin
FTransitions[c]:=o;
end;
// SetElse
//
procedure TState.SetElse(o : TTransition);
var
c : AnsiChar;
begin
for c:=#1 to #127 do
if FTransitions[c]=nil then
SetTransition(c, o);
FOwnedTransitions.Add(o);
end;
// ------------------
// ------------------ TTransition ------------------
// ------------------
// Create
//
constructor TTransition.Create;