-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdelforengine.pas
More file actions
2481 lines (2375 loc) · 74.3 KB
/
delforengine.pas
File metadata and controls
2481 lines (2375 loc) · 74.3 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
Unit delforengine;
(*
* Modified by Corpsman, to Support Lazarus Linux. 01.09.2009
* Modified by Corpsman, to Support Class, Record, Type Helper 07.10.2020
* Modified by Corpsman, add rudimentary support for C-Operands 04.04.2021
* Modified by Corpsman, Fix Crash, when Expression is nil 04.04.2021
* Modified by Corpsman, to Support Identifier Caseing 21.04.2021
* Modified by Corpsman, to Support "deprecated" as Key word 21.09.2021
* Modified by Corpsman, Fix Crash, when prefix spaces are to long 16.11.2021
*)
{$IFDEF FPC}
{$MODE Delphi}
{$ENDIF}
Interface
Uses SysUtils, oobjects, Classes, delfortypes;
(*
WISHLIST:
- suppress read-only file message
- Extra indent in procedures (brr)
- extra indent in implementation part (brr)
- read capitalization from var const type blocks
- sorting methods
- Is it possible to insert a "user customisable" line or group of lines before each
function/procedure, to allow the developer to comment it. Ex :
{------------Name of the proc------------------------} (Simple)
{***************************
* ...Comment ...
* ...Comment ...
***************************/ (A few lines)}
*)
Type
TPascalWord = Class
private
Procedure SetCase(ACase: TCase); virtual;
Function GetCase: TCase; virtual;
Procedure SetExpression(AExpression: PChar); virtual;
Function GetExpression: PChar; virtual;
public
Constructor Create;
Function WordType: TWordType; virtual;
Procedure GetLength(Var Length: Integer); virtual;
Function Space(SpaceBefore: TSpaceBefore): Boolean; virtual;
Function ReservedType: TReservedType; virtual;
Procedure SetSpace(SpaceBefore: TSpaceBefore; State: Boolean); virtual;
Procedure SetReservedType(AReservedType: TReservedType); virtual;
Function GetEString(Dest: PChar): PChar; virtual; abstract;
Function ChangeComment(commchar: char): boolean;
Property Expression: PChar read GetExpression write SetExpression;
Property ExpressionCase: TCase read GetCase write SetCase;
End;
TLineFeed = Class(TPascalWord)
public
nSpaces: Integer;
oldnSpaces: Integer;
wrapped: Boolean;
Constructor Create(AOldnSpaces: Integer);
Procedure SetIndent(n: Integer);
Procedure IncIndent(n: Integer);
Procedure GetLength(Var Length: Integer); override;
Function ReservedType: TReservedType; override;
Function GetEString(Dest: PChar): PChar; override;
End;
{ TExpression }
TExpression = Class(TPascalWord)
private
Procedure SetCase(ACase: TCase); override;
Function GetCase: TCase; override;
Procedure SetExpression(AExpression: PChar); override;
public
FExpression: PChar;
FWordType: TWordType;
FFormatType: Byte;
FReservedType: TReservedType;
Constructor Create(AType: TWordType; AExpression: PChar);
Procedure CheckReserved;
Procedure SetSpace(SpaceBefore: TSpaceBefore; State: Boolean); override;
Procedure SetReservedType(AReservedType: TReservedType); override;
Function Space(SpaceBefore: TSpaceBefore): Boolean; override;
Function GetEString(Dest: PChar): PChar; override;
Procedure GetLength(Var Length: Integer); override;
Function GetExpression: PChar; override;
Function WordType: TWordType; override;
Function ReservedType: TReservedType; override;
Destructor Destroy; override;
End;
TAlignExpression = Class(TExpression)
public
AlignPos: Byte;
nSpaces: Byte;
Constructor Create(Like: TExpression; Pos: Byte);
Procedure GetLength(Var Length: Integer); override;
Function GetEString(Dest: PChar): PChar; override;
End;
Type
TDelforParser = Class(TObject)
private
FSettings: TSettings;
FileText: TOCollection;
FCurrentText: PChar;
FCapNames: TKeywordColl;
nIndent: Integer;
ProcLevel: Integer;
ReadingAsm: Boolean;
AsmComment: TWordType;
prev: TPascalWord;
PrevLine: TLineFeed;
prevType: TWordType;
FOnProgress: TProgressEvent;
HasAligned: Boolean;
LeftPointBracket: Integer;
Procedure SetFillNewWords(AFillNewWords: TFillMode);
Function AlignExpression(I: Integer; aPos: Integer): TPascalWord;
Procedure checkPrintable(P: PChar);
Procedure ReadAsm(Var Buff: PChar);
Function ReadHalfComment(Dest: PChar; Var Source: PChar): TWordType;
Function ReadWord(Dest: PChar; Var Source: PChar): TWordType;
Procedure SetTextStr(AText: PChar);
Function GetTextStr: PChar;
Procedure CheckWrapping;
Function GetString(Dest: PChar; Var I: Integer): PChar;
public
Constructor Create;
Destructor Destroy; override;
Procedure LoadFromFile(AFileName: PChar);
Procedure LoadFromList(AList: TStringList);
Procedure LoadCapFile(ACapFile: PChar);
Procedure SaveCapFile(ACapFile: PChar);
Function Parse: Boolean;
Procedure Clear;
Procedure WriteToFile(AFileName: PChar);
Procedure Add(Buff: PChar);
Property Text: PChar read GetTextStr write SetTextStr;
Property OnProgress: TProgressEvent read FOnProgress write FOnProgress;
Property CapNames: TKeywordColl read FCapNames write FCapNames;
Property Settings: TSettings read FSettings write FSettings;
Property SpacePerIndent: Integer read FSettings.SpacePerIndent write
FSettings.SpacePerIndent;
Property SpaceOperators: TSpaceBefore read FSettings.SpaceOperators write
FSettings.SpaceOperators;
Property SpaceEqualOper: TSpaceBefore read FSettings.SpaceEqualOper write
FSettings.SpaceEqualOper;
Property SpaceColon: TSpaceBefore read FSettings.SpaceColon write
FSettings.SpaceColon;
Property SpaceComma: TSpaceBefore read FSettings.SpaceComma write
FSettings.SpaceComma;
Property SpaceSemiColon: TSpaceBefore read FSettings.SpaceSemiColon write
FSettings.SpaceSemiColon;
Property SpaceLeftBr: TSpaceBefore read FSettings.SpaceLeftBr write
FSettings.SpaceLeftBr;
Property SpaceRightBr: TSpaceBefore read FSettings.SpaceRightBr write
FSettings.SpaceRightBr;
Property SpaceLeftHook: TSpaceBefore read FSettings.SpaceLeftHook write
FSettings.SpaceLeftHook;
Property SpaceRightHook: TSpaceBefore read FSettings.SpaceRightHook write
FSettings.SpaceRightHook;
Property UpperCompDirectives: Boolean read FSettings.UpperCompDirectives
write FSettings.UpperCompDirectives;
Property UpperNumbers: Boolean read FSettings.UpperNumbers write
FSettings.UpperNumbers;
Property ReservedCase: TCase read FSettings.ReservedCase write
FSettings.ReservedCase;
Property StandDirectivesCase: TCase read FSettings.StandDirectivesCase write
FSettings.StandDirectivesCase;
Property ChangeIndent: Boolean read FSettings.ChangeIndent write
FSettings.ChangeIndent;
Property indentBegin: Boolean read FSettings.indentBegin write
FSettings.indentBegin;
Property NoIndentElseIf: Boolean read FSettings.NoIndentElseIf write
FSettings.NoIndentElseIf;
Property IndentComments: Boolean read FSettings.IndentComments write
FSettings.IndentComments;
Property IndentCompDirectives: Boolean read FSettings.IndentCompDirectives
write FSettings.IndentCompDirectives;
Property IndentTry: Boolean read FSettings.IndentTry write
FSettings.IndentTry;
Property IndentTryElse: Boolean read FSettings.IndentTryElse write
FSettings.IndentTryElse;
Property IndentCaseElse: Boolean read FSettings.IndentCaseElse write
FSettings.IndentCaseElse;
Property BlankProc: Boolean read FSettings.BlankProc write
FSettings.BlankProc;
Property RemoveDoubleBlank: Boolean read FSettings.RemoveDoubleBlank write
FSettings.RemoveDoubleBlank;
Property FeedRoundBegin: TFeedBegin read FSettings.FeedRoundBegin write
FSettings.FeedRoundBegin;
Property FeedAfterThen: Boolean read FSettings.FeedAfterThen write
FSettings.FeedAfterThen;
Property ExceptSingle: Boolean read FSettings.FeedAfterThen write
FSettings.ExceptSingle;
Property NoFeedBeforeThen: Boolean read FSettings.NoFeedBeforeThen write
FSettings.NoFeedBeforeThen;
Property FeedElseIf: Boolean read FSettings.FeedElseIf write
FSettings.FeedElseIf;
Property FeedEachUnit: Boolean read FSettings.FeedEachUnit write
FSettings.FeedEachUnit;
Property FeedAfterVar: Boolean read FSettings.FeedAfterVar write
FSettings.FeedAfterVar;
Property WrapLines: Boolean read FSettings.WrapLines write
FSettings.WrapLines;
Property WrapPosition: Byte read FSettings.WrapPosition write
FSettings.WrapPosition;
Property AlignCommentPos: Byte read FSettings.AlignCommentPos write
FSettings.AlignCommentPos;
Property AlignComments: Boolean read FSettings.AlignComments write
FSettings.AlignComments;
Property AlignVarPos: Byte read FSettings.AlignVarPos write
FSettings.AlignVarPos;
Property AlignVar: Boolean read FSettings.AlignVar write
FSettings.AlignVar;
Property FeedBeforeEnd: Boolean read FSettings.FeedBeforeEnd write
FSettings.FeedBeforeEnd;
Property FillNewWords: TFillMode read FSettings.FillNewWords write
SetFillNewWords;
Property FeedAfterSemiColon: Boolean read FSettings.FeedAfterSemiColon write
FSettings.FeedAfterSemiColon;
Property BlankSubProc: Boolean read FSettings.BlankSubProc write
FSettings.BlankSubProc;
//property CommentFunction: Boolean read FSettings.CommentFunction write
// FSettings.CommentFunction;
//property CommentUnit: Boolean read FSettings.CommentUnit write
// FSettings.CommentUnit;
Property StartCommentOut: TCommentArray read FSettings.StartCommentOut
write FSettings.StartCommentOut;
Property EndCommentOut: TCommentArray read FSettings.EndCommentOut write
FSettings.EndCommentOut;
Property SupportCOperands: Boolean read Fsettings.SupportCOperands
write fsettings.SupportCOperands;
Property IdentifierCaseing: TIdentifierCase read Fsettings.IdentifierCaseing
write fsettings.IdentifierCaseing;
End;
Var
DelforParser: TDelforParser = Nil;
Implementation
Constructor TDelforParser.Create;
Begin
DelforParser := Self;
CapNames := TKeywordColl.Create(10);
Clear;
End;
Function TDelforParser.AlignExpression(I: Integer; aPos: Integer): TPascalWord;
Var
OldExpr: TExpression;
Begin
HasAligned := True;
With FileText Do Begin
OldExpr := TExpression(Items[I]);
Result := TAlignExpression.Create(OldExpr, aPos);
Items[I] := Result;
OldExpr.Free;
End;
End;
Procedure TDelforParser.Clear;
Begin
HasAligned := False;
LeftPointBracket := 0;
nIndent := 0;
ReadingAsm := False;
PrevLine := Nil;
prev := Nil;
prevType := wtNothing;
If FileText = Nil Then
FileText := TOCollection.Create(500)
Else
FileText.FreeAll;
End;
Procedure Nop();
Begin
End;
Procedure TDelforParser.Add(Buff: PChar);
Var
AWord: Array[0..Maxline] Of Char;
Begin
PrevLine := TLineFeed.Create(0); {New(TLineFeed, Create(-1));}
FileText.Add(PrevLine);
If ReadingAsm Then
ReadAsm(Buff);
While (Buff^ <> #0) Do Begin
Case prevType Of
wtHalfComment, wtHalfStarComment,
wtHalfOutComment: prevType := ReadHalfComment(AWord, Buff);
Else
prevType := ReadWord(AWord, Buff);
End;
If Not (prevType = wtSpaces) Then Begin
FileText.Add(TExpression.Create(prevType, AWord));
If ReadingAsm And (Buff^ <> #0) Then
ReadAsm(Buff);
End
Else If (PrevLine <> Nil) And (PrevLine.nSpaces = 0) Then Begin
PrevLine.nSpaces := StrLen(AWord);
PrevLine.oldnSpaces := StrLen(AWord);
End;
End;
End;
Procedure TDelforParser.LoadFromFile(AFileName: PChar);
Var
InFile: TextFile;
Buff: Array[0..Maxline] Of Char;
Begin
If Assigned(OnProgress) Then
OnProgress(Self, 0);
PrevLine := Nil;
ReadingAsm := False;
AssignFile(InFile, AFileName);
Try
Reset(InFile);
Try
While Not Eof(InFile) And (FileText.Count < MaxCollectionSize - 100) Do Begin
Readln(InFile, Buff);
Add(Buff);
End;
If FileText.Count >= MaxCollectionSize - 100 Then
Raise Exception.Create('File to large to reformat')
Finally
CloseFile(InFile);
End;
Finally
End;
If Assigned(OnProgress) Then
OnProgress(Self, 33);
End;
Procedure TDelforParser.LoadFromList(AList: TStringList);
Var
Buff: Array[0..Maxline] Of Char;
I, k: Integer;
Begin
If Assigned(OnProgress) Then
OnProgress(Self, 0);
PrevLine := Nil;
k := 0;
ReadingAsm := False;
With AList Do
If Count = 0 Then
Self.Add(StrCopy(Buff, ''))
Else
For I := 0 To Count - 1 Do Begin
StrCopy(Buff, PChar(Strings[I]));
Self.Add(Buff);
If Assigned(OnProgress) Then Begin
inc(k);
If k = 20 Then Begin
k := 0;
OnProgress(Self, Round(I / Count * 34));
End;
End;
End;
End;
Procedure TDelforParser.ReadAsm(Var Buff: PChar);
Var
P, P1: PChar;
Begin
P := Buff;
P1 := Buff;
While P1^ = ' ' Do
inc(P1);
Repeat
checkPrintable(P);
Case AsmComment Of
wtHalfComment: Begin
If P^ = '}' Then
AsmComment := wtWord;
inc(P);
End;
wtHalfStarComment: Begin
If strLComp(P, '*)', 2) = 0 Then Begin
AsmComment := wtWord;
inc(P);
End;
inc(P);
End;
Else
If (StrLIComp(P, 'end', 3) = 0) And ((P = Buff) Or ((P - 1)^ In [' ',
Tab])
And ((P + 3)^ In [#0, ';', ' ', Tab])) Then Begin
ReadingAsm := False;
If P1 <> P Then Begin
Dec(P);
P^ := #0;
FileText.Add(TExpression.Create(wtAsm, P1));
P^ := ' ';
inc(P);
End;
Buff := P;
Exit;
End
Else If P^ = '{' Then Begin
While (P^ <> '}') And (P^ <> #0) Do Begin
inc(P);
checkPrintable(P);
End;
If (P^ <> '}') Then
AsmComment := wtHalfComment;
End
Else If strLComp(P, '(*', 2) = 0 Then Begin
While (strLComp(P, '*)', 2) <> 0) And (P^ <> #0) Do Begin
inc(P);
checkPrintable(P);
End;
If strLComp(P, '*)', 2) <> 0 Then
AsmComment := wtHalfStarComment;
End
Else If strLComp(P, '//', 2) = 0 Then
While P^ <> #0 Do Begin
checkPrintable(P);
inc(P);
End
Else
inc(P);
End;
Until (P^ = #0);
FileText.Add(TExpression.Create(wtAsm, Buff));
Buff := P;
End;
Procedure TDelforParser.checkPrintable(P: PChar);
Begin
If (P <> Nil) And (P^ In NotPrintable) Then Begin
While (P^ In NotPrintable) And Not (strLComp(P, #13#10, 2) = 0) Do Begin
P^ := ' ';
inc(P);
End;
End;
End;
Function TDelforParser.ReadWord(Dest: PChar; Var Source: PChar): TWordType;
Const
operators = '+-*/=<>[].,():;{}@^';
AllOper = operators + ' {}'''#9;
Var
P: PChar;
Len: Integer;
Procedure ReadString;
Procedure readQuotes;
Begin
While (P^ = '''') And ((P + 1)^ = '''') Do
inc(P, 2);
End;
Begin
Repeat
inc(P);
checkPrintable(P);
If (P^ = '''') Then Begin
readQuotes;
If ((P + 1)^ = '#') Then Begin
inc(P);
While P^ In ['0'..'9', 'A'..'F', 'a'..'f', '$', '#', '^'] Do
inc(P);
If P^ = '''' Then
inc(P)
Else Begin
Dec(P);
Exit;
End;
readQuotes;
End
Else If ((P + 1)^ = '^') Then Begin
inc(P);
While P^ In ['0'..'9', 'A'..'Z', 'a'..'z', '$', '#', '^'] Do
inc(P);
If P^ = '''' Then
inc(P)
Else Begin
Dec(P);
Exit;
End;
readQuotes;
End
{else
readQuotes;}
End;
Until (P^ = '''') Or (P^ = #0);
End;
Procedure ReadIdentifier;
Begin
Result := wtWord;
While (StrScan(AllOper, P^) = Nil) And (P^ <> #0) Do
inc(P);
Dec(P);
End;
Begin
P := Source;
checkPrintable(P);
If P^ In [Tab, ' '] Then Begin
Result := wtSpaces;
While (P^ In [Tab, ' ']) Do
inc(P);
Dec(P);
End
Else If (Settings.StartCommentOut[0] <> #0) And (Settings.EndCommentOut[0] <>
#0) And
(StrLIComp(P, Settings.StartCommentOut, StrLen(Settings.StartCommentOut)) =
0) Then Begin
Result := wtHalfOutComment;
inc(P, StrLen(Settings.StartCommentOut));
Len := StrLen(Settings.EndCommentOut);
While (StrLIComp(P, Settings.EndCommentOut, Len) <> 0) And (P^ <> #0) Do Begin
inc(P);
checkPrintable(P);
End;
If StrLIComp(P, Settings.EndCommentOut, Len) = 0 Then Begin
inc(P, Len - 1);
Result := wtFullOutComment;
End;
End
Else If P^ = '{' Then Begin
Result := wtHalfComment;
While (P^ <> '}') And (P^ <> #0) Do Begin
inc(P);
checkPrintable(P);
End;
If (P^ = '}') Then Begin
Result := wtFullComment;
If (Source + 1)^ = '$' Then
Result := wtCompDirective;
End;
End
Else If strLComp(P, '(*', 2) = 0 Then Begin
Result := wtHalfStarComment;
While (strLComp(P, '*)', 2) <> 0) And (P^ <> #0) Do Begin
inc(P);
checkPrintable(P);
End;
If strLComp(P, '*)', 2) = 0 Then Begin
inc(P);
Result := wtFullComment;
End;
End
Else If strLComp(P, '//', 2) = 0 Then Begin
Result := wtFullComment;
While P^ <> #0 Do Begin
checkPrintable(P);
inc(P);
End
End
Else If P^ = '''' Then Begin
Result := wtString;
ReadString;
If (P^ = #0) Then
Result := wtErrorString;
End
Else If P^ = '^' Then {string starting with ^A or so} Begin
If ((P + 1)^ In ['a'..'z', 'A'..'Z']) And ((P + 2)^ In ['''', '^', '#']) Then Begin
Result := wtString;
While P^ In ['0'..'9', 'A'..'Z', 'a'..'z', '$', '#', '^'] Do
inc(P);
If P^ = '''' Then
ReadString;
End
Else
Result := wtOperator;
End
Else If StrScan(operators, P^) <> Nil Then Begin
Result := wtOperator;
If strLComp(P, '<=', 2) = 0 Then
inc(P);
If strLComp(P, '>=', 2) = 0 Then
inc(P);
If strLComp(P, '<>', 2) = 0 Then
inc(P);
If strLComp(P, ':=', 2) = 0 Then
inc(P);
If SupportCOperands Then Begin
If strLComp(P, '+=', 2) = 0 Then
inc(P);
If strLComp(P, '-=', 2) = 0 Then
inc(P);
If strLComp(P, '*=', 2) = 0 Then
inc(P);
If strLComp(P, '/=', 2) = 0 Then
inc(P);
End;
If strLComp(P, '..', 2) = 0 Then
inc(P);
If strLComp(P, '(.', 2) = 0 Then Begin
inc(LeftPointBracket);
inc(P);
End;
If strLComp(P, '.)', 2) = 0 Then Begin
Dec(LeftPointBracket);
inc(P);
End;
End
Else If P^ = '$' Then Begin
Result := wtHexNumber;
inc(P);
While UpCase(P^) In ['0'..'9', 'A'..'F'] Do
inc(P);
Dec(P);
End
Else If P^ = '#' Then Begin
Result := wtNumber;
While P^ In ['0'..'9', 'A'..'F', 'a'..'f', '$', '#', '^'] Do
inc(P);
If P^ = '''' Then Begin
Result := wtString;
ReadString;
End
Else
Dec(P);
End
Else If P^ In ['0'..'9'] Then Begin
Result := wtNumber;
While (P^ In ['0'..'9', '.']) And Not (strLComp(P, '..', 2) = 0)
And Not ((LeftPointBracket > 0) And (strLComp(P, '.)', 2) = 0)) Do
inc(P);
If UpCase(P^) = 'E' Then
If (P + 1)^ In ['0'..'9', '-', '+'] Then Begin
inc(P, 2);
While (P^ In ['0'..'9']) Do
inc(P);
End;
Dec(P);
End
Else
ReadIdentifier;
StrLCopy(Dest, Source, P - Source + 1);
If (StrIComp(Dest, 'asm') = 0) Then Begin
ReadingAsm := True;
AsmComment := wtWord;
End;
If (P^ = #0) Then
Source := P
Else Begin
If ((P + 1)^ In [Tab, ' ']) Then
inc(P);
Source := P + 1;
End;
{ Readword := Result;}
End;
Function TDelforParser.ReadHalfComment(Dest: PChar; Var Source: PChar):
TWordType;
Var
Len: Integer;
P: PChar;
Begin
P := Source;
While P^ In [Tab, ' '] Do
inc(P);
If (PrevLine <> Nil) And (PrevLine.nSpaces = 0) Then Begin
PrevLine.nSpaces := P - Source;
PrevLine.oldnSpaces := P - Source;
P := StrCopy(Source, P);
End;
ReadHalfComment := prevType;
If prevType = wtHalfComment Then Begin
While (P^ <> '}') And (P^ <> #0) Do
inc(P);
If (P^ = '}') Then Begin
ReadHalfComment := wtFullComment;
inc(P);
End;
End
Else If prevType = wtHalfStarComment Then Begin
While (strLComp(P, '*)', 2) <> 0) And (P^ <> #0) Do
inc(P);
If strLComp(P, '*)', 2) = 0 Then Begin
ReadHalfComment := wtFullComment;
inc(P, 2);
End;
End
Else Begin
Len := StrLen(Settings.EndCommentOut);
While (StrLIComp(P, Settings.EndCommentOut, Len) <> 0) And (P^ <> #0) Do
inc(P);
If StrLIComp(P, Settings.EndCommentOut, Len) = 0 Then Begin
ReadHalfComment := wtFullOutComment;
inc(P, Len);
End;
End;
StrLCopy(Dest, Source, P - Source);
If P^ = #0 Then
Source := P
Else Begin
If (P^ In [Tab, ' ']) Then
inc(P);
Source := P;
End;
End;
Function TDelforParser.Parse: Boolean;
Type
TRec = Record
RT: TReservedType;
nInd: Integer;
End;
Const
MaxStack = 150;
Type
TStackArray = Array[0..MaxStack] Of TRec;
TStackStackRec = Record
stackptr: Integer;
ProcLevel: Integer;
stack: TStackArray;
nIndent: Integer;
End;
Var
Prev1: TPascalWord;
OldWrapIndent: Boolean;
PrevPrevLine: TLineFeed;
stack: TStackArray;
stackptr: Integer;
StackStack: Array[0..2] Of TStackStackRec;
StackStackPtr: Integer;
WrapIndent: Boolean;
interfacePart: Boolean;
NTmp: Integer;
PrevOldNspaces: Integer;
I, J: Integer;
Function GetStackTop: TReservedType;
Begin
If stackptr >= 0 Then
GetStackTop := stack[stackptr].RT
Else
GetStackTop := rtNothing;
End;
Procedure SetSpacing(PascalWord, prev: TPascalWord; I: Integer);
Var
Prev2: TPascalWord;
rtype: TReservedType;
wType: TWordType;
k: Integer;
S: Array[0..Maxline] Of Char;
Found: Boolean;
Begin
If PascalWord <> Nil Then Begin
rtype := PascalWord.ReservedType;
wType := PascalWord.WordType;
{if (rType = rtPrivate) and (prev <> nil) and
(prev.ReservedType <> rtLineFeed) then
begin
PascalWord.SetReservedType(rtNothing);
rType := rtNothing;
end;}
If Not (rtype In NoReservedTypes) Then
PascalWord.ExpressionCase := ReservedCase
Else If rtype In StandardDirectives Then
PascalWord.ExpressionCase := StandDirectivesCase
Else Begin
PascalWord.ExpressionCase := rfUnchanged;
If (wType = wtWord) And (PascalWord.Expression <> Nil) Then Begin
Found := False;
If (FillNewWords In [fmAddNewWord, fmAddUse, fmAddUseExcept])
And (rtype In
(NoReservedTypes - StandardDirectives)) Then Begin
Found := CapNames.Search(PascalWord.Expression, I);
If Not Found Then Begin
StrCopy(S, '*');
StrCat(S, PascalWord.Expression); {comment out}
If Not CapNames.Search(@S, J) Then
CapNames.Insert(I, StrNew(PascalWord.Expression));
End;
End;
If (FillNewWords In [fmUse, fmAddUse])
Or ((FillNewWords In [fmExceptDirect, fmAddUseExcept]) And
Not (rtype In StandardDirectives)) Then Begin
If Not Found Then
Found := CapNames.Search(PascalWord.Expression, I);
If Found Then Begin
PascalWord.Expression := CapNames.Items[I];
PascalWord.ExpressionCase := rfUnchanged;
End;
End;
End;
(* else
if (FillNewWords = fmAddNewWord) and (rType in
(NoReservedTypes - StandardDirectives)) then
begin
StrCopy(S, '*');
StrCat(S, PascalWord.Expression); {comment out}
if not CapNames.Search(@S, I) then
CapNames.Insert(I, (StrNew(PascalWord.Expression));
end; *)
End;
Case rtype Of
rtHelper,
rtThen, rtOf, rtElse, rtDo, rtAsm: PascalWord.SetSpace(spBoth, True);
rtEnd, rtFuncDirective: PascalWord.SetSpace(spBefore, True);
rtIf, rtUntil, rtWhile, rtCase, rtRecord:
PascalWord.SetSpace(spAfter, True);
rtOper, rtMathOper, rtPlus, rtMinus, rtLogOper, rtEquals:
PascalWord.SetSpace(SpaceOperators, True);
rtEqualOper: PascalWord.SetSpace(SpaceEqualOper, True);
rtColon: PascalWord.SetSpace(SpaceColon, True);
rtSemiColon: PascalWord.SetSpace(SpaceSemiColon, True);
rtComma: PascalWord.SetSpace(SpaceComma, True);
rtLeftBr: Begin
PascalWord.SetSpace(SpaceLeftBr, True);
If prev.ReservedType = rtLeftBr Then
PascalWord.SetSpace(spBefore, False);
End;
rtLeftHook: Begin
PascalWord.SetSpace(SpaceLeftHook, True);
If prev.ReservedType = rtLeftHook Then
PascalWord.SetSpace(spBefore, False);
End;
rtRightBr:
PascalWord.SetSpace(SpaceRightBr, True);
rtRightHook:
PascalWord.SetSpace(SpaceRightHook, True);
End;
{append space after : , ;}
If (wType In [wtNumber, wtHexNumber]) And UpperNumbers Then
PascalWord.SetCase(rfUpperCase);
{delimiter between 2 words (necesary)}
If (prev <> Nil) Then Begin
If (SpaceOperators In [spBoth, spBefore, spAfter]) And
(wType In [wtString, wtFullComment,
wtHalfComment, wtHalfStarComment]) And
Not (prev.ReservedType In [rtDotDot, rtLineFeed]) Then
PascalWord.SetSpace(spBefore, True);
If (rtype In [rtMinus, rtPlus]) Then Begin
Prev2 := prev;
k := 0;
While (Prev2 <> Nil) And (Prev2.ReservedType In [rtComment,
rtLineFeed]) Do Begin
inc(k);
If k > I Then
Prev2 := Nil
Else
Prev2 := TPascalword(FileText.Items[I - k]); // -----------------------------------------------------------
End;
If (Prev2 <> Nil) And (Prev2.ReservedType In [rtOper,
rtMathOper, rtPlus, rtMinus, rtSemiColon, rtOf,
rtLogOper, rtEquals, rtEqualOper, rtLeftBr,
rtLeftHook, rtComma, rtDefault]) Then
PascalWord.SetSpace(spAfter, False); {sign operator}
End;
If (rtype = rtLeftHook) Then Begin
If Not (prev.ReservedType In [rtReserved, rtNothing, rtRightBr,
rtRightHook]) Then
// PascalWord.SetSpace(spBefore, False) {array}
// else
PascalWord.SetSpace(spBefore, True);
End;
If PascalWord.Space(spBefore) And
(prev.ReservedType In [rtLeftBr, rtLeftHook,
rtLineFeed]) Then
PascalWord.SetSpace(spBefore, False);
If (prev.WordType In [wtWord, wtNumber, wtHexNumber, wtString]) And
(wType In [wtWord, wtNumber, wtHexNumber]) Then
PascalWord.SetSpace(spBefore, True);
If (prev.ReservedType = rtComment) And
(wType In [wtWord, wtNumber, wtHexNumber]) Then
prev.SetSpace(spAfter, True);
If PascalWord.Space(spBefore) And prev.Space(spAfter) Then
prev.SetSpace(spAfter, False); {avoid double spaces}
End;
End;
End;
Procedure SetPrevIndent;
Begin
If PrevLine <> Nil Then
PrevLine.SetIndent(nIndent + NTmp + ProcLevel);
End;
Procedure Push(R: TReservedType; n, ninc: Integer);
Begin
inc(stackptr);
If stackptr > MaxStack Then
Raise EFormatException.Create('Stack overflow');
With stack[stackptr] Do Begin
RT := R;
nInd := n;
nIndent := n + ninc;
End;
End;
Function HasType(AType: TReservedType): Boolean;
Var
I: Integer;
Begin
HasType := False;
For I := 0 To stackptr Do
If stack[I].RT = AType Then Begin
HasType := True;
Exit;
End;
End;
Function Pop: TReservedType;
Begin
If stackptr >= 0 Then Begin
nIndent := stack[stackptr].nInd;
If (stack[stackptr].RT = rtProcedure) And (ProcLevel > 0) Then
Dec(ProcLevel);
Pop := stack[stackptr].RT;
Dec(stackptr);
End
Else Begin
nIndent := 0;
ProcLevel := 0;
Pop := rtNothing;
End;
End;
Function GetWord(I: Integer): TPascalWord;
Begin
With FileText Do
If (I < Count) And (I >= 0) Then
GetWord := TPascalWord(Items[I])
Else
GetWord := Nil;
End;
Function GetNextNoComment(Var I, k: Integer): TPascalWord;
Begin
k := 0;
Repeat
inc(k);
Result := GetWord(I + k);
Until (Result = Nil) Or (Result.ReservedType <> rtComment);
End;
Function InsertBlankLines(atIndex, NLines: Integer): TLineFeed;
Var
J: Integer;
AfterWord: TPascalWord;
Begin
Result := PrevLine;
For J := 0 To NLines - 1 Do Begin
Result := TLineFeed.Create(0);
Result.SetIndent(nIndent);
AfterWord := TPascalWord(FileText.Items[atIndex]);
If AfterWord.Space(spBefore) Then
AfterWord.SetSpace(spBefore, False);
FileText.Insert(atIndex, Result);
SetSpacing(AfterWord, Result, atIndex);
End;
If atIndex <= I Then
inc(I, NLines);
End;
Procedure CheckBlankProc;
Var
k: Integer;
Prev2: TPascalWord;
Begin
If (prev <> Nil) Then Begin
k := 1;
If prev.ReservedType = rtClass Then Begin
k := 2;
Prev2 := GetWord(I - 2);
End
Else
Prev2 := prev;
If (Prev2 <> Nil) And (Prev2.ReservedType <> rtLineFeed) Then Begin
PrevLine := InsertBlankLines(I - k, 2);
prev := PrevLine;
End
Else Begin
inc(k);
Prev2 := GetWord(I - k);
If (Prev2 <> Nil) And (Prev2.ReservedType <> rtLineFeed) Then Begin
PrevLine := InsertBlankLines(I - k + 1, 1);
prev := PrevLine;
End;
End;
End;
End;
Procedure PutCommentBefore(aComment: PChar);
Var
J: Integer;
P: TPascalWord;
Begin
J := I - 2;