summaryrefslogtreecommitdiff
path: root/AltParser/compiler/pcomponentpascal.pas
blob: d329b902158c895493e6602638dea70db3fecd98 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
{
    Copyright (c) 1998-2010 by Florian Klaempfl and Dr. Diettrich

    Implements the Component Pascal language (not strictly)
}
unit PComponentPascal;

{$i fpcdefs.inc}

{$DEFINE StdExprs} //use OPL expressions and literals?

interface

uses
  pbase,node,symdef,symconst;

type
  TPreDeclared = (
    pdNone,
    pdAbs, pdAnyPtr, pdAnyRec, pdASH, pdAssert,
    pdBits, pdBoolean, pdByte,
    pdCap, pdChar, pdChr,
    pdDec,
    pdEntier, pdExcl,
    pdFalse,
    pdHalt,
    pdInc, pdIncl, pdInf, pdInteger,
    pdLen, pdLong, pdLongInt,
    pdMax, pdMin,
    pdNew,
    pdOdd, pdOrd,
    pdReal,
    pdSet, pdShort, pdShortChar, pdShortInt, pdShortReal, pdSize,
    pdTrue
  );
  TComponentPascal = class(TParser)
  protected //in grammar order
    //procedure Module;
    procedure ImportList;
    procedure DeclSeq;
    procedure ConstDecl;
    procedure TypeDecl;
    procedure VarDecl;
    procedure ProcDecl;
      //procedure ForwardDecl; - part of ProcDecl
      //procedure Receiver;
      procedure MethAttributes;
    procedure FormalPars;
    procedure FPSection;
    procedure Type_;
    procedure FieldList;
    procedure StatementSeq;
    function  Statement: tnode; override;
    //procedure Case_;
    //procedure CaseLabels;
    procedure Guard;
    procedure ConstExpr;
    function  Expr(dotypecheck: boolean = false): tnode; override;
  {$IFDEF StdExprs}
    //using expr.pas
  {$ELSE}
    procedure SimpleExpr;
    procedure Term;
    procedure Factor;
    procedure Set_;
    procedure Element;
    //procedure Relation;
    //procedure AddOp;
    //procedure MulOp;
  {$ENDIF}
    procedure Designator;
    procedure ExprList;
    procedure IdentList;
    procedure QualIdent;
    procedure IdentDef; //function?
    procedure Ident;  //function?
    function  UnitID: boolean;
  public
    procedure Compile; override;
    procedure SkipWhite; override;
    procedure ScanToken; override;
  end;

var
  predeclared: TPreDeclared;

implementation

uses
  globals,symsym,verbose,
  globtype,
  SModules,fmodule,
{$IFDEF StdExprs}
  pexpr,
{$ELSE}
{$ENDIF}
  scanner, tokens;

//some token substitutions, for now
const
//keywords
  _CLOSE  = _FINALIZATION; //same meaning
  _IMPORT = _USES;
  _MODULE = _UNIT;
  _BY     = _DOWNTO; //mutually exlucsive
  _ELSEIF = _OTHERWISE;
  _EMPTY  = _DEFAULT;
  _EXTENSIBLE = _VIRTUAL;
  _LIMITED= _PRIVATE;
  _LOOP   = _LABEL;
  _POINTER= _GOTO;
//tokens
  _LBRACE = _LSHARPBRACKET;
  _RBRACE = _RSHARPBRACKET;
  _DOLLAR = _KLAMMERAFFE;
  _TILDE  = _NOT;

  Relation = [_EQUAL,_UNEQUAL,_LT,_LTE,_GT,_GTE,_IN,_IS];
            //"=" | "#" | "<" | "<=" | ">" | ">=" | IN | IS.
  AddOp = [_PLUS,_MINUS,_OR]; // "+" | "-" | OR.
  MulOp = [_STAR,_SLASH,_DIV,_MOD,_AMPERSAND];  // " * " | "/" | DIV | MOD | "&".
  LogOp = [_OR, _AMPERSAND, _TILDE];  // logical OR, AND, NOT

const
  CPcpas: TParserListItem = (
    ext: '.cpas'; cls: TComponentPascal;
    alt:nil; next:nil;
  );

{ TComponentPascal }

procedure TComponentPascal.ScanToken;
{
The representation of (terminal) symbols in terms of characters is defined using
ISO 8859-1, i.e., the Latin-1 extension of the ASCII character set.
Unicode (16 bit) characters are allowed in string constants only.

Braces, $ and # are tokens!

Strings in single or double quotes, no duplication allowed.

IMO the C-ish number format is stupid, but to translate real life code...
}
var
  i: integer;
  delim: char;
const //for Latin-1 charset in identifiers
  IDfirst = ['A'..'Z','a'..'z','_'];
  IDnext = ['A'..'Z','a'..'z','_', '0'..'9', //'À'..'Ö', 'Ø'..'ö', 'ø'..'ÿ',#0];
    #$C0..#$D6, #$D8..#$F6, #$F8..#$FF, #0];
begin
  predeclared :=pdNone;
{ Check first for an identifier/keyword, this is 20+% faster (PFV) }
  if c in IDfirst then begin
  { Only keywords should have token<>_ID,
    other words have idtoken set differently.
    idtoken always should equal token, except for _ID (easy parse)
  }
    //readstring;
    i := 1; //length+1
    while c in IDnext do begin
      if c=#0 then begin
        reload;
        continue; //???
      end else if i <= 255 then begin
        orgpattern[i]:=c;
        pattern[i]:=c; //the language is case sensitive
        inc(i); //>255 is overflow
      end;
      c := inputpointer^;
      inc(inputpointer);
    end;
  //check length - allow for overflows (ignore excess chars?)
    if i > 255 then begin
      Message(scan_e_string_exceeds_255_chars);
      i := 255;
    end else
      dec(i); //real length
    SetLength(orgpattern,i);
    SetLength(pattern,i);
    token:=_ID;
    idtoken:=_ID; //new: predeclared:TPreDeclared !!!
  { keyword or any other known word? }
    case pattern[1] of
    'A':  if (i=8) and (pattern='ABSTRACT') then token:=_ABSTRACT else
          if (i=5) and (pattern='ARRAY')    then token:=_ARRAY else
          if (i=3) then begin
            if (pattern='ABS')    then predeclared:=pdAbs else
            if (pattern='ASH')    then predeclared:=pdASH end else
          if (i=6) then begin
            if (pattern='ANYPTR')    then predeclared:=pdAnyPtr else
            if (pattern='ANYREC')    then predeclared:=pdAnyRec else
            if (pattern='ASSERT')    then predeclared:=pdAssert end;
    'B':  if (i=5) and (pattern='BEGIN')    then token:=_BEGIN else
          if (i=2) and (pattern[2]='Y')     then token:=_BY else
          if (i=4) then begin
            if (pattern='BITS')    then predeclared:=pdBits else
            if (pattern='BYTE')    then predeclared:=pdByte end else
          if (i=7) and (pattern='BOOLEAN')  then predeclared:=pdBoolean;
    'C':  if (i=4) then begin
                    if (pattern='CASE')     then token:=_CASE else
                    if (pattern='CHAR') then predeclared:=pdChar end else
          if (i=5) then begin
                    if pattern='CLOSE'      then token:=_close else
                    if pattern='CONST'      then token:=_CONST end else
          if (i=3) then begin
            if (pattern='CAP')    then predeclared:=pdCap else
            if (pattern='CHR')    then predeclared:=pdChr end;
    'D':  if (i=2) and (pattern[2]='O')        then token:=_DO else
          if (i=3) then begin
            if (pattern='DIV')      then token:=_DIV else
            if (pattern='DEC')      then predeclared:=pdDec end;
    'E':  if (i=4) then begin
                    if pattern='ELSE'       then token:=_ELSE else
                    if pattern='EXIT'       then token:=_EXIT else
                    if pattern='EXCL'     then predeclared:=pdExcl end else
          if (i=3) and (pattern='END')      then token:=_END else
          if (i=5) then begin
                    if pattern='ELSIF'      then token:=_ELSEIF else
                    if pattern='EMPTY'      then token:=_EMPTY; end else
          if (i=10) and (pattern='EXTENSIBLE') then token:=_EXTENSIBLE else
          if (i=6) and (pattern='ENTIER') then predeclared:=pdEntier;
    'F':  if (i=3) and (pattern='FOR')      then token:=_FOR else
          if (i=5) and (pattern='FALSE')  then predeclared:=pdFalse;
    'H':  if (i=4) and (pattern='HALT')   then predeclared:=pdHalt;
    'I':  if (i=2) then begin
            case pattern[2] of
              'F': token:=_IF;
              'N': token:=_IN;
              'S': token:=_IS;
            end
          end else if (i=6) and (pattern='IMPORT') then token:=_IMPORT else
          if (i=7) and (pattern='INTEGER')  then predeclared:=pdInteger else
          if (i=3) then begin
            if (pattern='INC')    then predeclared:=pdInc else
            if (pattern='INF')    then predeclared:=pdInf end else
          if (i=4) and (pattern='INCL') then predeclared:=pdIncl;
    'L':  if (i=7) then begin
            if (pattern='LIMITED')    then token:=_LIMITED else
            if (pattern='LONGINT')  then predeclared:=pdLongInt end else
          if (i=3) and (pattern='LEN')  then predeclared:=pdLen else
          if (i=4) then begin
            if (pattern='LONG')     then predeclared:=pdLong else
            if (pattern='LOOP')       then token:=_LOOP end;
    'M':  if (i=3) then begin
            if (pattern='MOD')      then token:=_MOD else
            if (pattern='MAX')  then predeclared:=pdMax else
            if (pattern='MIN')  then predeclared:=pdMin end else
          if (i=6) and (pattern='MODULE')   then token:=_module;
    'N':  if (i=3) then begin
                    if (pattern='NIL')      then token:=_NIL else
                    if (pattern='NEW')     then predeclared:=pdNew; end;
    'O':  if (i=3) then begin
            if (pattern='OUT')      then token:=_OUT else
            if (pattern='ODD')     then predeclared:=pdOdd else
            if (pattern='ORD')     then predeclared:=pdOrd end else
          if (i=2) then
            case pattern[2] of
            'F':  token:=_OF;
            'R':  token:=_OR;
            end;
    'P':  if (i=7) and (pattern='POINTER')  then token:=_POINTER else
          if (i=9) and (pattern='PROCEDURE')then token:=_PROCEDURE;
    'R':  if (i=6) then begin
            if pattern='RECORD'             then token:=_RECORD else
            if pattern='REPEAT'             then token:=_REPEAT else
            if pattern='RETURN'             then token:=_RETURN end else
          if (i=4) and (pattern='REAL')     then predeclared:=pdReal;
    'S':  if (i=4) and (pattern='SIZE')     then predeclared:=pdSize else
          if (i=3) and (pattern='SET')      then predeclared:=pdSet else
          if (i=5) and (pattern='SHORT')    then predeclared:=pdShort else
          if (i=8) and (pattern='SHORTINT') then predeclared:=pdShortInt else
          if (i=9) then begin
            if (pattern='SHORTCHAR')        then predeclared:=pdShortChar else
            if (pattern='SHORTREAL')        then predeclared:=pdShortReal end;
    'T':  if (i=2) and (pattern[2]='O')     then token:=_TO else
          if (i=4) then begin
            if (pattern='THEN')             then token:=_THEN else
            if (pattern='TRUE')            then predeclared:=pdTrue else
            if (pattern='TYPE')             then token:=_TYPE end;
    'U':  if (i=5) and (pattern='UNTIL')    then token:=_UNTIL;
    'V':  if (i=3) and (pattern='VAR')      then token:=_VAR;
    'W':  if (i=4) and (pattern='WITH')     then token:=_WITH else
          if (i=5) and (pattern='WHILE')    then token:=_WHILE;
    end;
  { identifiers may be macros }
    if token=_ID then
      CheckMacro;
  end else begin
    idtoken:=_NOID;
    case c of
    '''', '"': //todo: strings deserve more handling
      begin
        delim:=c;
        SetLength(cstringpattern,256);
        i := 0;
        repeat
          readchar; //handles #0
          case c of
          #26:  end_of_file;
          #10,#13: Message(scan_f_string_exceeds_line);
          else //case
          //todo: only handle Ansi, for now
            if c = delim then
              break;
            inc(i);
            if i > Length(cstringpattern) then
              SetLength(cstringpattern, i*2);
            cstringpattern[i] := c;
          end;
        until False;
        readchar; //skip delimiter(?)
        SetLength(cstringpattern,i);
        if i = 1 then begin
          token := _CCHAR;  //really???
          pattern:=cstringpattern;
        end else
          token := _CSTRING;
      end;
    '#': begin readchar; token:=_UNEQUAL; end;
    '$':  //token (AsString), but also allow for hex numbers(?)
      if (m_fpc in current_settings.modeswitches) then begin
        readnumber;
        if length(pattern) = 1 then
          token:=_dollar
        else
          token:=_INTCONST;
      end else begin
        readchar;
        token:=_dollar;
      end;
    '%':  //allow for binary number?
      if (m_fpc in current_settings.modeswitches) then begin
        readnumber;
        token:=_INTCONST;
      end else
        Illegal_Char(c);
    '&': //_AMPERSAND, also allow for escaped ID, octal number?
      if [m_fpc,m_delphi] * current_settings.modeswitches <> [] then begin
        readnumber;
        if length(pattern)=1 then begin
            readstring;
            token:=_ID;
            idtoken:=_ID;
        end else
          token:=_INTCONST;
      end else begin
        readchar;
        token:=_AMPERSAND;
      end;
//    '(':  begin readchar; token:=_LKLAMMER end; //comment ---------------
    ')':  begin readchar; token:=_RKLAMMER end;
//    '*':  begin readchar; token:=_STAR; end;  //**? *=?
//    '+':  begin readchar; token:=_PLUS; end;
    ',':  begin readchar; token:=_COMMA; end;
//    '-':  begin readchar; token:=_MINUS; end;
//    '.':  begin readchar; token:=_POINT; end; //.. ----------------------
//    '/':  begin readchar; token:=_SLASH; end; // /=? comment??? --------------
    '0'..'9' : //--------------------------------------------------------
       begin
         readnumber;
         if (c in ['.','e','E']) then begin
          { first check for a . }
            if c='.' then begin
               cachenexttokenpos;
               readchar;
               { is it a .. from a range? }
               case c of
                 '.' :
                   begin
                     readchar;
                     token:=_INTCONST;
                     nexttoken:=_POINTPOINT;
                     exit;
                   end;
                 ')' :
                   begin
                     readchar;
                     token:=_INTCONST;
                     nexttoken:=_RECKKLAMMER;
                     exit;
                   end;
                end;
            end;
             { insert the number after the . }
             pattern:=pattern+'.';
             while c in ['0'..'9'] do
              begin
                pattern:=pattern+c;
                readchar;
              end;
          { E can also follow after a point is scanned }
            if c in ['e','E'] then
             begin
               pattern:=pattern+'E';
               readchar;
               if c in ['-','+'] then
                begin
                  pattern:=pattern+c;
                  readchar;
                end;
               if not(c in ['0'..'9']) then
                Illegal_Char(c);
               while c in ['0'..'9'] do
                begin
                  pattern:=pattern+c;
                  readchar;
                end;
             end;
            token:=_REALNUMBER;
            exit;
          end;
         token:=_INTCONST;
       end;
//    ':':  begin readchar; token:=_COLON; end; //:= -----------------
    ';':  begin readchar; token:=_SEMICOLON; end;
//    '<':  begin readchar; token:=_LT; end;  // < <= generics? <= -----------
    '=':  begin readchar; token:=_EQUAL; end;
//    '>':  begin readchar; token:=_GT; end;  //>= ----------------------
    '[':  begin readchar; token:=_LECKKLAMMER; end;
    ']':  begin readchar; token:=_RECKKLAMMER; end;
    '^':  begin readchar; token:=_CARET; end;
    '{':  begin readchar; token:=_LBRACE; end;
    '|':  begin readchar; token:=_PIPE; end;
    '}':  begin readchar; token:=_RBRACE; end;
    '~':  begin readchar; token:=_TILDE; end;

     '(' :  // ( or (. or (* ?
       begin
         readchar;
         case c of
           '*' :
             begin
               c:=#0;{Signal skipoldtpcomment to reload a char }
               skipoldtpcomment;
               readtoken(false);
             end;
           '.' :
             begin
               readchar;
               token:=_LECKKLAMMER;
             end;
         else
               token:=_LKLAMMER;
         end;
       end;

     '+' : // + or +=
       begin
         readchar;
         if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then begin
            readchar;
            token:=_PLUSASN;
         end else
           token:=_PLUS;
       end;

     '-' :  // - or -=
       begin
         readchar;
         if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then begin
            readchar;
            token:=_MINUSASN;
         end else
           token:=_MINUS;
       end;

     ':' :  // : or :=
       begin
         readchar;
         if c='=' then begin
            readchar;
            token:=_ASSIGNMENT;
         end else
           token:=_COLON;
       end;

     '*' :  // * or *= or ** ???
       begin
         readchar;
         if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then begin
            readchar;
            token:=_STARASN;
         end else if c='*' then begin
             readchar;
             token:=_STARSTAR;
         end else
            token:=_STAR;
       end;

     '/' : // / or /= or // ???
       begin
         readchar;
         case c of
         '=' :
           begin
             if (cs_support_c_operators in current_settings.moduleswitches) then begin
                readchar;
                token:=_SLASHASN;
              end;
           end;
         '/' :
           begin
             skipdelphicomment;
             readtoken(false);
           end;
         else
           token:=_SLASH;
         end;
       end;

     '.' : // . or .. or .) or ... ???
       begin
         readchar;
         case c of
           '.' :
             begin
               readchar;
               case c of
               '.' :
                 begin
                   readchar;
                   token:=_POINTPOINTPOINT;
                 end;
               else
                   token:=_POINTPOINT;
               end;
             end;
           ')' :
             begin
               readchar;
               token:=_RECKKLAMMER;
             end;
         else //case
           token:=_POINT;
         end;
       end;
{$IFDEF OPL}
     '@' :
       begin
         readchar;
         token:=_KLAMMERAFFE;
       end;

     '''','#','^' :
       begin
         len:=0;
         cstringpattern:='';
         iswidestring:=false;
         if c='^' then begin
            readchar;
            c:=upcase(c);
            if (block_type in [bt_type,bt_const_type,bt_var_type]) or
               (lasttoken=_ID) or (lasttoken=_NIL) or (lasttoken=_OPERATOR) or
               (lasttoken=_RKLAMMER) or (lasttoken=_RECKKLAMMER) or (lasttoken=_CARET) then
            begin
               token:=_CARET;
               exit;  //goto exit_label;
            end else begin
               inc(len);
               setlength(cstringpattern,256);
               if c<#64 then
                 cstringpattern[len]:=chr(ord(c)+64)
               else
                 cstringpattern[len]:=chr(ord(c)-64);
               readchar;
            end;
         end;
         repeat
           case c of
             '#' :
               begin
                 readchar; { read # }
                 case c of
                   '$':
                     begin
                       readchar; { read leading $ }
                       asciinr:='$';
                       while (upcase(c) in ['A'..'F','0'..'9']) and (length(asciinr)<=5) do
                         begin
                           asciinr:=asciinr+c;
                           readchar;
                         end;
                     end;
                   '&':
                     begin
                       readchar; { read leading $ }
                       asciinr:='&';
                       while (upcase(c) in ['0'..'7']) and (length(asciinr)<=7) do
                         begin
                           asciinr:=asciinr+c;
                           readchar;
                         end;
                     end;
                   '%':
                     begin
                       readchar; { read leading $ }
                       asciinr:='%';
                       while (upcase(c) in ['0','1']) and (length(asciinr)<=17) do
                         begin
                           asciinr:=asciinr+c;
                           readchar;
                         end;
                     end;
                   else
                     begin
                       asciinr:='';
                       while (c in ['0'..'9']) and (length(asciinr)<=5) do
                         begin
                           asciinr:=asciinr+c;
                           readchar;
                         end;
                     end;
                 end;
                 val(asciinr,m,code);
                 if (asciinr='') or (code<>0) then
                   Message(scan_e_illegal_char_const)
                 else if (m<0) or (m>255) or (length(asciinr)>3) then
                   begin
                      if (m>=0) and (m<=65535) then
                        begin
                          if not iswidestring then
                           begin
                             if len>0 then
                               ascii2unicode(@cstringpattern[1],len,patternw)
                             else
                               ascii2unicode(nil,len,patternw);
                             iswidestring:=true;
                             len:=0;
                           end;
                          concatwidestringchar(patternw,tcompilerwidechar(m));
                        end
                      else
                        Message(scan_e_illegal_char_const)
                   end
                 else if iswidestring then
                   concatwidestringchar(patternw,asciichar2unicode(char(m)))
                 else
                   begin
                     if len>=length(cstringpattern) then
                       setlength(cstringpattern,length(cstringpattern)+256);
                      inc(len);
                      cstringpattern[len]:=chr(m);
                   end;
               end;
             '''' :
               begin
                 repeat
                   readchar;
                   case c of
                     #26 :
                       end_of_file;
                     #10,#13 :
                       Message(scan_f_string_exceeds_line);
                     '''' :
                       begin
                         readchar;
                         if c<>'''' then
                          break;
                       end;
                   end;
                   { interpret as utf-8 string? }
                   if (ord(c)>=$80) and (current_settings.sourcecodepage='utf8') then
                     begin
                       { convert existing string to an utf-8 string }
                       if not iswidestring then
                         begin
                           if len>0 then
                             ascii2unicode(@cstringpattern[1],len,patternw)
                           else
                             ascii2unicode(nil,len,patternw);
                           iswidestring:=true;
                           len:=0;
                         end;
                       { four or more chars aren't handled }
                       if (ord(c) and $f0)=$f0 then
                         message(scan_e_utf8_bigger_than_65535)
                       { three chars }
                       else if (ord(c) and $e0)=$e0 then
                         begin
                           w:=ord(c) and $f;
                           readchar;
                           if (ord(c) and $c0)<>$80 then
                             message(scan_e_utf8_malformed);
                           w:=(w shl 6) or (ord(c) and $3f);
                           readchar;
                           if (ord(c) and $c0)<>$80 then
                             message(scan_e_utf8_malformed);
                           w:=(w shl 6) or (ord(c) and $3f);
                           concatwidestringchar(patternw,w);
                         end
                       { two chars }
                       else if (ord(c) and $c0)<>0 then
                         begin
                           w:=ord(c) and $1f;
                           readchar;
                           if (ord(c) and $c0)<>$80 then
                             message(scan_e_utf8_malformed);
                           w:=(w shl 6) or (ord(c) and $3f);
                           concatwidestringchar(patternw,w);
                         end
                       { illegal }
                       else if (ord(c) and $80)<>0 then
                         message(scan_e_utf8_malformed)
                       else
                         concatwidestringchar(patternw,tcompilerwidechar(c))
                     end
                   else if iswidestring then
                     begin
                       if current_settings.sourcecodepage='utf8' then
                         concatwidestringchar(patternw,ord(c))
                       else
                         concatwidestringchar(patternw,asciichar2unicode(c))
                     end
                   else
                     begin
                       if len>=length(cstringpattern) then
                         setlength(cstringpattern,length(cstringpattern)+256);
                        inc(len);
                        cstringpattern[len]:=c;
                     end;
                 until false;
               end;
             '^' :
               begin
                 readchar;
                 c:=upcase(c);
                 if c<#64 then
                  c:=chr(ord(c)+64)
                 else
                  c:=chr(ord(c)-64);

                 if iswidestring then
                   concatwidestringchar(patternw,asciichar2unicode(c))
                 else
                   begin
                     if len>=length(cstringpattern) then
                       setlength(cstringpattern,length(cstringpattern)+256);
                      inc(len);
                      cstringpattern[len]:=c;
                   end;

                 readchar;
               end;
             else
              break;
           end;
         until false;
         { strings with length 1 become const chars }
         if iswidestring then
           begin
             if patternw^.len=1 then
               token:=_CWCHAR
             else
               token:=_CWSTRING;
           end
         else
           begin
             setlength(cstringpattern,len);
             if length(cstringpattern)=1 then
               begin
                 token:=_CCHAR;
                 pattern:=cstringpattern;
               end
             else
               token:=_CSTRING;
           end;
       end;
{$ELSE}
{$ENDIF}

     '>' :  // > or >= or >> or >< ???
       begin
         readchar;
       {$IFDEF generics}
         if (block_type in [bt_type,bt_var_type,bt_const_type]) then
           token:=_RSHARPBRACKET
         else
       {$ELSE}
       {$ENDIF}
           begin
             case c of
               '=' :
                 begin
                   readchar;
                   token:=_GTE;
                 end;
               '>' :
                 begin
                   readchar;
                   token:=_OP_SHR;
                 end;
               '<' :
                 begin { >< is for a symetric diff for sets }
                   readchar;
                   token:=_SYMDIF;
                 end;
             else
               token:=_GT;
             end;
           end;
       end;

     '<' :  // < or <= or << ???
       begin
         readchar;
       {$IFDEF generics}
         if (block_type in [bt_type,bt_var_type,bt_const_type]) then
           token:=_LSHARPBRACKET
         else
       {$ELSE}
       {$ENDIF}
           begin
             case c of
               '>' :
                 begin
                   readchar;
                   token:=_UNEQUAL;
                 end;
               '=' :
                 begin
                   readchar;
                   token:=_LTE;
                 end;
               '<' :
                 begin
                   readchar;
                   token:=_OP_SHL;
                 end;
             else
               token:=_LT;
             end;
           end;
       end;

     #26 : // EOF
       begin
         token:=_EOF;
         checkpreprocstack;
       end;
     else //case
       Illegal_Char(c);
    end;
  end;
end;

procedure TComponentPascal.SkipWhite;
{ comments only in (* *), braces are tokens!
}
begin
{ Skip all spaces and comments }
  repeat
    case c of
      #26 :
        begin
          reload;
          if (c=#26) and not assigned(inputfile.next) then
            break;
        end;
      ' ',#9..#13 :
        begin
{$ifdef PREPROCWRITE}
          if parapreprocess then
           begin
             if c in [#10,#13] then
              preprocfile.eolfound:=true
             else
              preprocfile.spacefound:=true;
           end;
{$endif PREPROCWRITE}
          skipspace;
        end
      else
        break;
    end;
  until false;
end;

procedure TComponentPascal.Compile;
(*
  Module = MODULE ident ";" [ImportList] DeclarationSequence
  [BEGIN StatementSequence]
  [CLOSE StatementSequence] END ident ".".
*)
var
  sm: TSemModule;
begin
{ read the first token }
  current_scanner.readtoken(false);
//Module
  if (token=_MODULE) or (compile_level>main_program_level) then begin
  //non main-level modules can be nothing but units
    current_module.is_unit:=true;
    sm.SModuleInitUnit;  //is this applicable???
  end else begin //assume program
    sm.SModuleInitProgLib(False);
  end;
  if (token=_PROGRAM) or (token=_MODULE) then
    consume(token);
  sm.SProgName; //expect _ID
  consume(_ID);
  consume(_SEMICOLON);
//enter implementation body
  sm.SProgLibImplInit;
//imports/uses?
  if (token=_IMPORT) then begin
    consume(token);
    ImportList;
  end;
//body
  sm.SProgLibBodyInit;
  DeclSeq;
  if try_to_consume(_BEGIN) then
    StatementSeq;
{ save file pos for debuginfo }
  current_module.mainfilepos:=sm.main_procinfo.entrypos;
  sm.SProgLibBodyDone;
//finalization?
  if (token=_CLOSE) then begin
    consume(token);
    sm.SProgLibFinalInit(True);
    //sm.finalize_procinfo.parse_body;
    StatementSeq;
  end else
    sm.SProgLibFinalInit(False);
  sm.SPrgLibFinalDone;
  consume(_END);
  if token = _ID then begin //DoDi: made this ID optional
    consume(_ID); //must match the module name
  end;
  //sm.SProgLibBodyDone;
  consume(_POINT);
  sm.SPrgLibDone;
end;

procedure TComponentPascal.DeclSeq;
(* DeclSeq = { CONST {ConstDecl ";" } | TYPE {TypeDecl ";"} |
    VAR {VarDecl ";"}} {ProcDecl ";" | ForwardDecl ";"}.
  ProcDecl = PROCEDURE [Receiver] IdentDef [FormalPars] MethAttributes
    [";" DeclSeq [BEGIN StatementSeq] END ident].
  ForwardDecl = PROCEDURE " ^ " [Receiver] IdentDef [FormalPars] MethAttributes.
*)
  procedure ConstDecls;
  begin
    consume(_CONST);
    while token = _ID do begin
      ConstDecl;
      consume(_SEMICOLON);
    end;
  end;

  procedure TypeDecls;
  begin
    consume(_TYPE);
    while token = _ID do begin
      TypeDecl;
      consume(_SEMICOLON);
    end;
  end;

  procedure VarDecls;
  begin
    consume(_VAR);
    while token = _ID do begin
      VarDecl;
      consume(_SEMICOLON);
    end;
  end;

begin
  while token in [_CONST,_TYPE,_VAR,_PROCEDURE] do begin
    case token of
    _CONST: ConstDecls;
    _TYPE:  TypeDecls;
    _VAR:   VarDecls;
    _PROCEDURE: ProcDecl; // ProcOrForwardDecl;
    end;
  end;
end;

procedure TComponentPascal.ImportList;
(*
  ImportList = IMPORT] Import {"," Import} ";".
  Import = [ident ":="] ident.
*)
begin
  repeat  //while token = _ID do begin
    consume(_ID);
    if try_to_consume(_ASSIGNMENT) then begin
    //preceding ID is the alias name
    //ID is the real name
      consume(_ID);
    end else begin
    //ID is the real name
    end;
  until not try_to_consume(_COMMA);
  consume(_SEMICOLON);
end;

procedure TComponentPascal.ConstDecl;
(* ConstDecl = IdentDef "=" ConstExpr.
*)
begin
  IdentDef;
  consume(_EQUAL);
  ConstExpr;
end;

procedure TComponentPascal.TypeDecl;
(* TypeDecl = IdentDef "=" Type.
  IdentDef = ident [" * " | "-"].
*)
begin
  IdentDef;
  consume(_EQUAL);
  Type_;
end;

procedure TComponentPascal.VarDecl;
(* VarDecl = IdentList ":" Type.
*)
begin
  IdentList;
  consume(_COLON);
  Type_;
//allow for initializer???
end;

procedure TComponentPascal.StatementSeq;
(* StatementSeq = Statement {";" Statement}.
*)
begin
  repeat
    Statement;
  until not try_to_consume(_SEMICOLON);
end;

function TComponentPascal.Statement: tnode;
(* Statement =
[ Designator ":=" Expr
| Designator ["(" [ExprList] ")"]
| IF Expr THEN StatementSeq
  {ELSIF Expr THEN StatementSeq}
  [ELSE StatementSeq] END
| CASE Expr OF Case {"|" Case}
    [ELSE StatementSeq] END
| WHILE Expr DO StatementSeq END
| REPEAT StatementSeq UNTIL Expr
| FOR ident ":=" Expr TO Expr [BY ConstExpr]
  DO StatementSeq END
| LOOP StatementSeq END
| WITH [ Guard DO StatementSeq ]
  {"|" [ Guard DO StatementSeq ] }
  [ELSE StatementSeq] END
| EXIT
| RETURN [Expr]
].
*)
  procedure LoopStmt;
  (*  LOOP StatementSeq END
  *)
  begin
    consume(token); //_loop
    StatementSeq;
    consume(_END);
  end;

  procedure AssignOrCallStmt;
  begin //got ID
  //DoDi: until we have a _LOOP token...
    if pattern = 'LOOP' then begin
      LoopStmt;
      exit;
    end;
    Designator; //return what?
    if try_to_consume(_ASSIGNMENT) then begin
      Expr;
    end else begin
      if try_to_consume(_LKLAMMER) then begin
      //FormalPars;
        if token<>_RKLAMMER then
          ExprList;
        consume(_RKLAMMER);
      end;
      //Call...
    end;
  end;

  procedure CaseStmt;
  (* CASE Expr OF Case {"|" Case}
      [ELSE StatementSeq] END
    Case = [CaseLabels {"," CaseLabels} ":" StatementSeq].
    CaseLabels = ConstExpr [".." ConstExpr].
  *)
    procedure CaseLabels;
    (* CaseLabels = ConstExpr [".." ConstExpr].
    *)
    begin
      repeat
        ConstExpr;
      until not try_to_consume(_POINTPOINT);
    end;

    procedure Case_;
    (* Case = [CaseLabels {"," CaseLabels} ":" StatementSeq].
    *)
    begin
      repeat
        CaseLabels;
      until not try_to_consume(_COMMA);
      consume(_COLON);
      StatementSeq;
    end;

  begin
    consume(_CASE);
    Expr;
    consume(_OF);
    repeat
      Case_;
    until not try_to_consume(_PIPE);
    if try_to_consume(_ELSE) then
      StatementSeq;
    consume(_END);
  end;

  procedure IfStmt;
  (*  IF Expr THEN StatementSeq
  {ELSIF Expr THEN StatementSeq}
  [ELSE StatementSeq] END
  *)
  begin
    consume(_IF);
    Expr;
    consume(_THEN);
    StatementSeq;
    if (token=_ID) and (pattern='ELSEIF') then begin
      consume(token);
      Expr;
      consume(_THEN);
      StatementSeq;
    end;
    if try_to_consume(_ELSE) then begin
      StatementSeq;
    end;
    consume(_END);
  end;

  procedure WhileStmt;
  (* WHILE Expr DO StatementSeq END
  *)
  begin
    consume(_WHILE);
    Expr;
    consume(_DO);
    StatementSeq;
    consume(_END);
  end;

  procedure RepeatStmt;
  (* REPEAT StatementSeq UNTIL Expr
  *)
  begin
    consume(_REPEAT);
    StatementSeq;
    consume(_UNTIL);
    Expr;
  end;

  procedure ForStmt;
  (* FOR ident ":=" Expr TO Expr [BY ConstExpr]
      DO StatementSeq END
  *)
  begin
    consume(_FOR);
    consume(_ID);
    consume(_ASSIGNMENT);
    Expr;
    consume(_TO);
    Expr;
    if (token=_ID) and (pattern='BY') then begin
      consume(token);
      Expr;
    end;
    consume(_DO);
    StatementSeq;
    consume(_END);
  end;

  procedure WithStmt;
  (* WITH [ Guard DO StatementSeq ]
    {"|" [ Guard DO StatementSeq ] }
    [ELSE StatementSeq] END.
    Guard = Qualident ":" Qualident.
  *)
  begin
    consume(_WITH);
    repeat
      Guard;
    until not try_to_consume(_PIPE);
    if try_to_consume(_ELSE) then begin
      StatementSeq;
    end;
    consume(_END);
  end;

  procedure ExitStmt;
  begin
    consume(_EXIT);
  end;

  procedure ReturnStmt;
  (* RETURN [Expr]
  *)
  begin
    consume(_RETURN);
    if token <> _SEMICOLON then begin //what?
      Expr;
    end;
  end;

begin
  Result := nil;  //to come!
  case token of
  _ID:    AssignOrCallStmt;
  _IF:    IfStmt;
  _Case:  CaseStmt;
  _WHILE: WhileStmt;
  _REPEAT:  RepeatStmt;
  _FOR:   ForStmt;
  //_loop:  LoopStmt; //as _ID
  _WITH:  WithStmt;
  _EXIT:  ExitStmt;
  _RETURN:  ReturnStmt;
  end;
end;

procedure TComponentPascal.ConstExpr;
begin
  Expr;
end;

procedure TComponentPascal.Designator;
(* Designator = Qualident
{"." ident
| "[" ExprList "]"
| " ^ "
| "(" Qualident ")" --- type guard ???
| "(" [ExprList] ")"} --- ActualParameters ???
[ "$" ].  --- string from char[] ???
*)
begin
  QualIdent;
  while token in [_POINT,_LECKKLAMMER,_CARET,_LKLAMMER] do begin
    case token of
    _POINT: //member selector
      begin
        consume(_POINT);
        Ident;  //how?
      end;
    _LECKKLAMMER: //array selector
      begin
        consume(_LECKKLAMMER);
        ExprList;
        consume(_RECKKLAMMER);
      end;
    _CARET: //pointer dereference
      begin
        consume(_CARET);
      end;
    _LKLAMMER: //???
      begin
        consume(_LKLAMMER);
        if token=_ID then begin //type guard?
          QualIdent;
        end else begin //procedure call?
          ExprList;
        end;
        consume(_RKLAMMER);
      end;
    else
      break;
    end;
  end;
//unhandled: "$"
end;

function TComponentPascal.Expr(dotypecheck: boolean): tnode;
begin
{$IFDEF StdExprs}
  Result := pexpr.expr(dotypecheck);  //???
{$ELSE}
...
  SimpleExpr;
  if token in Relation then begin
    consume(token);
    SimpleExpr;
  end;
{$ENDIF}
end;

procedure TComponentPascal.ExprList;
(* ExprList = Expr {"," Expr}.
*)
begin
  repeat
    Expr;
  until not try_to_consume(_COMMA);
end;

{$IFDEF StdExprs}
{$ELSE}
procedure TComponentPascal.Element;
begin
  Expr;
  if try_to_consume(_POINTPOINT) then begin
    Expr;
  end;
end;

procedure TComponentPascal.Factor;
(* Factor = Designator
| number
| character
| string
| NIL
| Set
|"(" Expr ")"
| " ~ " Factor.
*)
begin
...
end;

procedure TComponentPascal.SimpleExpr;
begin
  if token in [_PLUS,_MINUS] then begin
    consume(token);
  end;
  Term;
  if token in AddOp then begin
    consume(token);
  end;
  Term;
end;

procedure TComponentPascal.Term;
(* Term = Factor {MulOp Factor}.
*)
begin
  Factor;
  if token in MulOp then begin
    consume(token);
    Factor;
  end;
end;

procedure TComponentPascal.Set_;
(* Set = "{" [Element {"," Element}] "}".
*)
begin
  consume(_LBRACE);
  while token <> _RBRACE do begin
    Element;
    if not try_to_consume(_COMMA) then
      break;
  end;
  consume(_RBRACE);
end;

{$ENDIF}

procedure TComponentPascal.FieldList;
(* FieldList = [IdentList ":" Type].
*)
begin
  //if ???
  IdentList;
  consume(_COLON);
  Type_;
end;

procedure TComponentPascal.FormalPars;
(* FormalPars = "(" [FPSection {";" FPSection}] ")" [":" Type].
*)
begin
  //if not try_to_consume(_LKLAMMER) then exit;
  consume(_LKLAMMER);

  if token <> _RKLAMMER then begin
    repeat
      FPSection;
    until not try_to_consume(_SEMICOLON) ;
  end;
  consume(_RKLAMMER);

  if try_to_consume(_COLON) then begin
    Type_;
  end;
end;

procedure TComponentPascal.FPSection;
(* FPSection = [VAR | IN | OUT] ident {"," ident} ":" Type.
*)
begin
  if token in [_VAR,_IN,_OUT] then begin
    consume(token);
  end;
  repeat
    Ident;
  until not try_to_consume(_COMMA);
  Type_;
end;

procedure TComponentPascal.Guard;
(* Guard = Qualident ":" Qualident.
*)
begin
  if token=_ID then begin
    QualIdent;
    consume(_COLON);
    QualIdent;
  end;
end;

procedure TComponentPascal.IdentDef;
(* IdentDef = ident [" * " | "-"].
*)
begin
  consume(_ID);
  if token=_STAR then begin
  //???
    consume(_STAR);
  end else if token=_MINUS then begin
    consume(_MINUS);
  end;
end;

procedure TComponentPascal.IdentList;
(* IdentList = IdentDef {"," IdentDef}.
*)
begin
  repeat
    IdentDef;
  until not try_to_consume(_COMMA);
end;

procedure TComponentPascal.MethAttributes;
(* MethAttributes = ["," NEW] ["," (ABSTRACT | EMPTY | EXTENSIBLE)].
*)
begin
{ The leading comma already has been consumed }
  //if try_to_consume(_COMMA) then begin
    if (predeclared=pdNew) then begin
      consume(token);
      if not try_to_consume(_COMMA) then
        exit; //only NEW
    end;
    if try_to_consume(_ABSTRACT) then begin
    //???
    end else if token=_EMPTY then begin
      //???
      consume(token);
    end else if token=_EXTENSIBLE then begin
      //???
      consume(token);
    end;
  //end;
end;

procedure TComponentPascal.ProcDecl;
(* ProcDecl = PROCEDURE [Receiver] IdentDef [FormalPars] MethAttributes
[";" DeclSeq [BEGIN StatementSeq] END ident].
*)
(* ForwardDecl = PROCEDURE " ^ " [Receiver] IdentDef [FormalPars] MethAttributes.
*)
    procedure Receiver;
    (* Receiver = "(" [VAR | IN] ident ":" ident ")".
    *)
    begin
      if try_to_consume(_VAR) then begin
      //???
      end else if try_to_consume(_IN) then begin
      //???
      end;
      consume(_ID);
      consume(_COLON);
      consume(_ID);
    end;

var
  fwd: boolean;
begin
  consume(_PROCEDURE);
  fwd := try_to_consume(_CARET);
  if try_to_consume(_LKLAMMER) then begin
    Receiver; //opt.
  end;
  IdentDef;
  if try_to_consume(_LKLAMMER) then begin
    FormalPars; //opt
  end;
  if try_to_consume(_COMMA) then begin
    MethAttributes;
  end;
  if fwd then begin
    //...
  end else begin
    if try_to_consume(_SEMICOLON) then begin
      DeclSeq;
      if try_to_consume(_BEGIN) then begin
        StatementSeq;
        consume(_END);
        if token=_ID then begin //DoDi: made this conditional
        //ident must match procedure name
          consume(_ID);
        end;
        consume(_SEMICOLON);
      end;
    end;
    //...
  end;
end;

procedure TComponentPascal.QualIdent;
(* Qualident = [ident "."] ident.
*)
begin
  if UnitID then begin
  //unit.ident
    consume(_ID);
    consume(_POINT);
    consume(_ID);
  end else begin
    consume(_ID);
  end;
end;

procedure TComponentPascal.Ident;
begin
  consume(_ID);
end;

procedure TComponentPascal.Type_;
(* Type = Qualident
| ARRAY [ConstExpr {"," ConstExpr}] OF Type
| [ABSTRACT | EXTENSIBLE | LIMITED]
    RECORD ["("Qualident")"] FieldList {";" FieldList} END
| POINTER TO Type
| PROCEDURE [FormalPars].
*)
type
  TTypeKind = (tkNone, tkRec, tkPtr, tkTypeName); //disambiguate _ID
var
  kind: TTypeKind;
begin
  case token of
  _ARRAY:
    begin
      consume(_ARRAY);
      if token <> _OF then begin
        repeat
          ConstExpr;  //one dimension
        until not try_to_consume(_COMMA);
      end; //else open array
      consume(_OF);
      Type_;
    end;
  _ABSTRACT, _ID, _RECORD:
    begin
      kind:=tkNone;
      if token=_ABSTRACT then begin
        kind:=tkRec;
        consume(_ABSTRACT);
      end else if token=_RECORD then begin
        kind := tkRec;
        //consume later
      end else if token=_ID then begin
        if pattern='EXTENSIBLE' then begin
          kind := tkRec;
          consume(_ID);
        end else if pattern='LIMITED' then begin
          kind := tkRec;
          consume(_ID);
        end else if pattern = 'POINTER' then begin
          kind:=tkPtr;
          consume(_ID);
        //... handle pointer
          try_to_consume(_TO);  //DoDi: made this optional
          Type_;
        end else begin
        //assume/test typename
          kind := tkTypeName;
          QualIdent;
          //...
        end;
      end;
    //handle the detected type
      case kind of
      tkRec:
        begin
          consume(_RECORD);
          if try_to_consume(_LKLAMMER) then begin
          //inherit from
            QualIdent;
            consume(_RKLAMMER);
          end;
        //members
          repeat
            FieldList;
          until not try_to_consume(_SEMICOLON);
          consume(_END);
        end;
      //tkPtr, tkTypeName: finish?
      //tkNone: error?
      end;
    end;
  _PROCEDURE:
    begin
      consume(_PROCEDURE);
      FormalPars;
    end;
  //else Message...
  end;
end;

function TComponentPascal.UnitID: boolean;
begin
//todo: check if current token is a unit identifier
  //Result := (token=_ID) and ???;
  Result := False;  //for now
end;

initialization
  RegisterParser(@CPcpas);
end.