forked from vtereshkov/xdpw
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScanner.pas
More file actions
773 lines (609 loc) · 16.6 KB
/
Copy pathScanner.pas
File metadata and controls
773 lines (609 loc) · 16.6 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
// XD Pascal - a 32-bit compiler for Windows
// Copyright (c) 2009-2010, 2019-2020, Vasiliy Tereshkov
// VERSION 0.14;0
{$I-}
{$H-}
unit Scanner;
interface
uses Common,SysUtils;
var
Tok: TToken;
procedure InitializeScanner(const Name: TString);
function SaveScanner: Boolean;
function RestoreScanner: Boolean;
procedure FinalizeScanner;
procedure NextTok;
procedure CheckTok(ExpectedTokKind: TTokenKind);
procedure EatTok(ExpectedTokKind: TTokenKind);
procedure AssertIdent;
function ScannerFileName: TString;
function ScannerLine: Integer;
function ScannerPos: Integer;
implementation
type
TBuffer = record
Ptr: PCharacter;
Size, Pos: Integer;
end;
TScannerState = record
Token: TToken;
CommentFile,
FileName: TString;
CommentPoint,
CommentLine,
Position,
Line: Integer;
Buffer: TBuffer;
ch, ch2: TCharacter;
EndOfUnit: Boolean;
end;
const
SCANNERSTACKSIZE = 10;
var
ScannerState: TScannerState;
ScannerStack: array [1..SCANNERSTACKSIZE] of TScannerState;
ScannerStackTop: Integer = 0;
const
Digits: set of TCharacter = ['0'..'9'];
HexDigits: set of TCharacter = ['0'..'9', 'A'..'F'];
Spaces: set of TCharacter = [#1..#31, ' '];
AlphaNums: set of TCharacter = ['A'..'Z', 'a'..'z', '0'..'9', '_'];
procedure InitializeScanner(const Name: TString);
var
F: TInFile;
ActualSize: Integer;
FolderIndex: Integer;
begin
ScannerState.Buffer.Ptr := nil;
// First search the source folder, then the units folder, then the folders specified in $UNITPATH
FolderIndex := 1;
repeat
Assign(F, TGenericString(Folders[FolderIndex] + Name));
Reset(F, 1);
if IOResult = 0 then Break;
Inc(FolderIndex);
until FolderIndex > NumFolders;
if FolderIndex > NumFolders then
Error('Unable to open source file ' + Name);
with ScannerState do
begin
FileName := Name;
Line := 1;
Position := 0;
CommentLine := 0;
CommentPoint := 0;
with Buffer do
begin
Size := FileSize(F);
Pos := 0;
GetMem(Ptr, Size);
ActualSize := 0;
BlockRead(F, Ptr^, Size, ActualSize);
Close(F);
if ActualSize <> Size then
Error('Unable to read source file ' + Name);
end;
ch := ' ';
ch2 := ' ';
EndOfUnit := FALSE;
end;
end;
function SaveScanner: Boolean;
begin
Result := FALSE;
if ScannerStackTop < SCANNERSTACKSIZE then
begin
Inc(ScannerStackTop);
ScannerStack[ScannerStackTop] := ScannerState;
Result := TRUE;
end;
end;
function RestoreScanner: Boolean;
begin
Result := FALSE;
if ScannerStackTop > 0 then
begin
ScannerState := ScannerStack[ScannerStackTop];
Dec(ScannerStackTop);
Tok := ScannerState.Token;
Result := TRUE;
end;
end;
procedure FinalizeScanner;
begin
ScannerState.EndOfUnit := TRUE;
with ScannerState.Buffer do
if Ptr <> nil then
begin
FreeMem(Ptr);
Ptr := nil;
end;
end;
procedure AppendStrSafe(var s: TString; ch: TCharacter);
begin
if Length(s) >= MAXSTRLENGTH - 1 then
Error('String is too long');
s := s + ch;
end;
procedure ReadChar(var ch: TCharacter);
begin
if ScannerState.ch = #10 then // End of line found
begin
Inc(ScannerState.Line);
Scannerstate.Position := 0;
end;
ch := #0;
with ScannerState.Buffer do
if Pos < Size then
begin
ch := PCharacter(Integer(Ptr) + Pos)^;
Inc(Pos);
Inc(ScannerState.Position);
end
else
ScannerState.EndOfUnit := TRUE;
end;
procedure ReadUppercaseChar(var ch: TCharacter);
begin
ReadChar(ch);
ch := UpCase(ch);
end;
procedure ReadLiteralChar(var ch: TCharacter);
begin
ReadChar(ch);
if (ch = #0) or (ch = #10) then
Error('Unterminated string');
end;
procedure ReadSingleLineComment;
begin
with ScannerState do
while (ch <> #10) and not EndOfUnit do
ReadChar(ch);
end;
procedure ReadMultiLineComment(Oldschool: Boolean);
Procedure CommentEof; // eof in middle of a comment
begin // provide an alternative error message for "runaway" comment
with ScannerState do
begin
Notice(ScannerFileName + ' (' + IntToStr(ScannerLine) +
':' + IntToStr(ScannerPos) + ') Error: End of file inside comment');
Notice('Attention: Your last comment began at ('+
IntToStr(CommentLine)+':'+IntToStr(CommentPoint)+
') and it might be helpful to look there.');
repeat FinalizeScanner until not RestoreScanner;
FinalizeCommon;
Halt(1);
end
end;
begin
with ScannerState do
if not oldschool then
begin
while (ch <> '}') and not EndOfUnit do
ReadChar(ch);
if EndofUnit then
CommentEof;
end
else // older comment starting with (* must end with *)
repeat
readchar(ch);
if endofunit then CommentEof;
if ch='*' then // check for close of comment
begin
readchar(ch);
if endofunit then CommentEof;
if ch=')' then
exit; // it is closed
end
until EndOfUnit;
end;
procedure ReadDirective(OldSchool:boolean);
var
Text: TString;
begin
with ScannerState do
begin
Text := '';
repeat
AppendStrSafe(Text, ch);
ReadUppercaseChar(ch);
until not (ch in AlphaNums);
if Text = '$APPTYPE' then // Console/GUI application type directive
begin
Text := '';
ReadChar(ch);
while (ch <> '}') and not EndOfUnit do
begin
if (ch = #0) or (ch > ' ') then
AppendStrSafe(Text, UpCase(ch));
ReadChar(ch);
end;
if Text = 'CONSOLE' then
IsConsoleProgram := TRUE
else if Text = 'GUI' then
IsConsoleProgram := FALSE
else
Error('Unknown application type ' + Text);
end
else if Text = '$UNITPATH' then // Unit path directive
begin
Text := '';
ReadChar(ch);
while (ch <> '}') and not EndOfUnit do
begin
if (ch = #0) or (ch > ' ') then
AppendStrSafe(Text, UpCase(ch));
ReadChar(ch);
end;
Inc(NumFolders);
if NumFolders > MAXFOLDERS then
Error('Maximum number of unit paths exceeded');
Folders[NumFolders] := Folders[1] + Text;
end
else // All other directives are ignored
ReadMultiLineComment(OldSchool);
end;
end;
// hexadecimal numbers
procedure ReadHexadecimalNumber;
var
Num, Digit: Integer;
NumFound: Boolean;
begin
with ScannerState do
begin
Num := 0;
NumFound := FALSE;
while ch in HexDigits do
begin
if Num and $F0000000 <> 0 then
Error('Numeric constant is too large');
if ch in Digits then
Digit := Ord(ch) - Ord('0')
else
Digit := Ord(ch) - Ord('A') + 10;
Num := Num shl 4 or Digit;
NumFound := TRUE;
ReadUppercaseChar(ch);
end;
if not NumFound then
Error('Hexadecimal constant is not found');
Token.Kind := INTNUMBERTOK;
Token.OrdValue := Num;
end;
end;
// integers and floating-point numbers
procedure ReadDecimalNumber;
var
Num, Expon, Digit: Integer;
Frac, FracWeight: Double;
NegExpon, RangeFound, ExponFound: Boolean;
begin
with ScannerState do
begin
Num := 0;
Frac := 0;
Expon := 0;
NegExpon := FALSE;
while ch in Digits do
begin
Digit := Ord(ch) - Ord('0');
if Num > (HighBound(INTEGERTYPEINDEX) - Digit) div 10 then
Error('Numeric constant is too large');
Num := 10 * Num + Digit;
ReadUppercaseChar(ch);
end;
if (ch <> '.') and (ch <> 'E') then // Integer number
begin
Token.Kind := INTNUMBERTOK;
Token.OrdValue := Num;
end
else
begin
// Check for '..' token
RangeFound := FALSE;
if ch = '.' then
begin
ReadUppercaseChar(ch2);
if ch2 = '.' then // Integer number followed by '..' token
begin
Token.Kind := INTNUMBERTOK;
Token.OrdValue := Num;
RangeFound := TRUE;
end;
if not EndOfUnit then Dec(Buffer.Pos);
end; // if ch = '.'
if not RangeFound then // Fractional number
begin
// Check for fractional part
if ch = '.' then
begin
FracWeight := 0.1;
ReadUppercaseChar(ch);
while ch in Digits do
begin
Digit := Ord(ch) - Ord('0');
Frac := Frac + FracWeight * Digit;
FracWeight := FracWeight / 10;
ReadUppercaseChar(ch);
end;
end; // if ch = '.'
// Check for exponent
if ch = 'E' then
begin
ReadUppercaseChar(ch);
// Check for exponent sign
if ch = '+' then
ReadUppercaseChar(ch)
else if ch = '-' then
begin
NegExpon := TRUE;
ReadUppercaseChar(ch);
end;
ExponFound := FALSE;
while ch in Digits do
begin
Digit := Ord(ch) - Ord('0');
Expon := 10 * Expon + Digit;
ReadUppercaseChar(ch);
ExponFound := TRUE;
end;
if not ExponFound then
Error('Exponent is not found');
if NegExpon then Expon := -Expon;
end; // if ch = 'E'
Token.Kind := REALNUMBERTOK;
Token.RealValue := (Num + Frac) * exp(Expon * ln(10));
end; // if not RangeFound
end; // else
end;
end;
// Determine type of number
procedure ReadNumber;
begin
with ScannerState do
if ch = '$' then
begin
ReadUppercaseChar(ch);
ReadHexadecimalNumber;
end
else
ReadDecimalNumber;
end;
// red #nnn or #$nn char
procedure ReadCharCode;
begin
with ScannerState do
begin
ReadUppercaseChar(ch);
if not (ch in Digits + ['$']) then
Error('Character code is not found');
ReadNumber;
if (Token.Kind = REALNUMBERTOK) or (Token.OrdValue < 0) or (Token.OrdValue > 255) then
Error('Illegal character code');
Token.Kind := CHARLITERALTOK;
end;
end;
// Is it a keyword or an identifier? Read more and find out!!
procedure ReadKeywordOrIdentifier;
var
Text, NonUppercaseText: TString;
CurToken: TTokenKind;
begin
with ScannerState do
begin
Text := '';
NonUppercaseText := '';
repeat
AppendStrSafe(NonUppercaseText, ch);
ch := UpCase(ch);
AppendStrSafe(Text, ch);
ReadChar(ch);
until not (ch in AlphaNums);
CurToken := GetKeyword(Text);
if CurToken <> EMPTYTOK then // Keyword found
Token.Kind := CurToken
else
begin // Identifier found
Token.Kind := IDENTTOK;
Token.Name := Text;
Token.NonUppercaseName := NonUppercaseText;
end;
end;
end;
// get one character or a string
procedure ReadCharOrStringLiteral;
var
Text: TString;
EndOfLiteral: Boolean;
begin
with ScannerState do
begin
Text := '';
EndOfLiteral := FALSE;
repeat
ReadLiteralChar(ch);
if ch <> '''' then
AppendStrSafe(Text, ch)
else
begin
ReadChar(ch2);
if ch2 = '''' then // Apostrophe character found
AppendStrSafe(Text, ch)
else
begin
if not EndOfUnit then Dec(Buffer.Pos); // Discard ch2
EndOfLiteral := TRUE;
end;
end;
until EndOfLiteral;
if Length(Text) = 1 then
begin
Token.Kind := CHARLITERALTOK;
Token.OrdValue := Ord(Text[1]);
end
else
begin
Token.Kind := STRINGLITERALTOK;
Token.Name := Text;
Token.StrLength := Length(Text);
DefineStaticString(Text, Token.StrAddress);
end;
ReadUppercaseChar(ch);
end;
end;
// read the next noken in sequence
procedure NextTok;
begin
with ScannerState do
begin
Token.Kind := EMPTYTOK;
// Skip spaces, comments, directives
while (ch in Spaces) or (ch = '{') or
(ch = '/') or (ch='(') do
begin
if ch = '{' then // handle mewer { comments } // Multi-line comment or directive
begin
Commentpoint := Position ; // for "runaway" comments
CommentLine := Line;
ReadChar(ch);
if ch = '$' then ReadDirective(FALSE) else ReadMultiLineComment(FALSE);
end
else if ch = '/' then
begin
ReadChar(ch2);
if ch2 = '/' then
ReadSingleLineComment // Double-line comment
else
begin
if not EndOfUnit then Dec(Buffer.Pos); // put the character back // Discard ch2
Break;
end;
end
else if ch = '(' then // handle old-school (* comments *)
begin
ReadChar(ch2);
if ch2 = '*' then
begin
Commentpoint := Position -1; // for "runaway" comments
CommentLine := Line;
ReadChar(ch);
if ch = '$' then ReadDirective(TRUE) else ReadMultiLineComment(TRUE);
end
else
begin
if not EndOfUnit then Dec(Buffer.Pos); // return borrowed char // Discard ch2
Break;
end;
end;
ReadChar(ch);
end;
// Read token
case ch of
'0'..'9', '$':
ReadNumber;
'#':
ReadCharCode;
'A'..'Z', 'a'..'z', '_':
ReadKeywordOrIdentifier;
'''':
ReadCharOrStringLiteral;
':': // Single- or double-character tokens
begin
Token.Kind := COLONTOK;
ReadUppercaseChar(ch);
if ch = '=' then
begin
Token.Kind := ASSIGNTOK;
ReadUppercaseChar(ch);
end;
end;
'>':
begin
Token.Kind := GTTOK;
ReadUppercaseChar(ch);
if ch = '=' then
begin
Token.Kind := GETOK;
ReadUppercaseChar(ch);
end;
end;
'<':
begin
Token.Kind := LTTOK;
ReadUppercaseChar(ch);
if ch = '=' then
begin
Token.Kind := LETOK;
ReadUppercaseChar(ch);
end
else if ch = '>' then
begin
Token.Kind := NETOK;
ReadUppercaseChar(ch);
end;
end;
'.':
begin
Token.Kind := PERIODTOK;
ReadUppercaseChar(ch);
if ch = '.' then
begin
Token.Kind := RANGETOK;
ReadUppercaseChar(ch);
end;
end
else // Double-character tokens
case ch of
'=': Token.Kind := EQTOK;
',': Token.Kind := COMMATOK;
';': Token.Kind := SEMICOLONTOK;
'(': Token.Kind := OPARTOK;
')': Token.Kind := CPARTOK;
'*': Token.Kind := MULTOK;
'/': Token.Kind := DIVTOK;
'+': Token.Kind := PLUSTOK;
'-': Token.Kind := MINUSTOK;
'^': Token.Kind := DEREFERENCETOK;
'@': Token.Kind := ADDRESSTOK;
'[': Token.Kind := OBRACKETTOK;
']': Token.Kind := CBRACKETTOK;
'}': Error('Closing comment } character found without opening {');
else
Error('Unexpected character "'+ch+'" ($'+Hex(ord(ch))+') found or end of file');
end; // case
ReadChar(ch);
end; // case
end;
Tok := ScannerState.Token;
end; // NextTok
procedure CheckTok(ExpectedTokKind: TTokenKind);
begin
with ScannerState do
if Token.Kind <> ExpectedTokKind then
Error(GetTokSpelling(ExpectedTokKind) + ' expected but ' + GetTokSpelling(Token.Kind) + ' found');
end;
procedure EatTok(ExpectedTokKind: TTokenKind);
begin
CheckTok(ExpectedTokKind);
NextTok;
end;
procedure AssertIdent;
begin
with ScannerState do
if Token.Kind <> IDENTTOK then
Error('Identifier expected but ' + GetTokSpelling(Token.Kind) + ' found');
end;
function ScannerFileName: TString;
begin
Result := ScannerState.FileName;
end;
function ScannerLine: Integer;
begin
Result := ScannerState.Line;
end;
function ScannerPos: Integer;
begin
Result := ScannerState.Position;
end;
end.