-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlex.c
1440 lines (1303 loc) · 45.9 KB
/
lex.c
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
/*ident "@(#)cls4:src/lex.c 1.14" */
/*******************************************************************************
C++ source for the C++ Language System, Release 3.0. This product
is a new release of the original cfront developed in the computer
science research center of AT&T Bell Laboratories.
Copyright (c) 1993 UNIX System Laboratories, Inc.
Copyright (c) 1991, 1992 AT&T and UNIX System Laboratories, Inc.
Copyright (c) 1984, 1989, 1990 AT&T. All Rights Reserved.
THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE of AT&T and UNIX System
Laboratories, Inc. The copyright notice above does not evidence
any actual or intended publication of such source code.
lex.c:
lexical analyser based on pcc's and cpre's scanners
modified to handle classes:
new keywords: class
public
call
etc.
names are not entered in the symbol table by lex()
names can be of arbitrary length
error() is used to report errors
{} and () must match
numeric constants are not converted into internal representation
but stored as strings
****************************************************************************/
#include "cfront.h"
#include "yystype.h"
#include "size.h"
#include "tqueue.h"
#include "template.h"
#include "Block.h"
#define CCTRANS(x) x
Blockdeclare(short)
// static data members definition
bool templ_compilation::in_progress = false;
char *strdup(const char *s1)
/* string duplication
returns pointer to a new string which is the duplicate of string
pointed to by s1
return 0 if new string can't be created
*/
{
char *s2;
s2 = new char[strlen(s1) + 1];
return (s2 == 0 ? 0 : strcpy(s2, s1));
}
#define copy_if_need_be(s) ((templp->in_progress || templp->parameters_in_progress) ? strdup(s) : (s))
/* lexical actions */
#define A_ERR 0 /* illegal character */
#define A_LET 1 /* saw a letter */
#define A_DIG 2 /* saw a digit */
#define A_1C 3 /* return a single character */
#define A_STR 4 /* string */
#define A_CC 5 /* character constant */
#define A_BCD 6 /* GCOS BCD constant */
#define A_SL 7 /* saw a / */
#define A_DOT 8 /* saw a . */
#define A_2C 9 /* possible two character symbol */
#define A_WS 10 /* whitespace (not \n) */
#define A_NL 11 /* \n */
#define A_LC 12 /* { */
#define A_RC 13 /* } */
#define A_L 14 /* ( */
#define A_R 15 /* ) */
#define A_EOF 16
#define A_ASS 17
#define A_LT 18
#define A_GT 19 /* > */
#define A_ER 20
#define A_OR 21
#define A_AND 22
#define A_MOD 23
#define A_NOT 24
#define A_MIN 25
#define A_MUL 26
#define A_PL 27
#define A_COL 28 /* : */
#define A_SHARP 29 /* # */
#define A_DOLL 30 /* $ */
/* character classes */
#define LEXLET 01
#define LEXDIG 02
/* no LEXOCT because 8 and 9 used to be octal digits */
#define LEXHEX 010
#define LEXWS 020
#define LEXDOT 040
const int FIRSTCHUNK = 8 * 1024 - 8;
const int BUFCHUNK = 4 * 1024 - 8;
/* text buffer */
static char inbuf[FIRSTCHUNK /*TBUFSZ*/];
static char *txtmax = &inbuf[FIRSTCHUNK /*TBUFSZ*/ - 1];
static char *txtstart = 0;
static char *txtfree = 0;
static struct buf *bufhead;
static buf *freebuf;
// static bufs;
static struct loc tloc;
struct buf {
buf *next;
char chars[BUFCHUNK];
// buf() { next=bufhead; bufhead=this; }
};
int new_buf(char c) {
// fprintf(stderr,"new_buf %d\n",bufs++);
buf *pbuf;
if (freebuf) {
pbuf = freebuf;
freebuf = freebuf->next;
} else
pbuf = new buf; // allocate and register new chunk
pbuf->next = bufhead;
bufhead = pbuf;
if (BUFCHUNK < txtmax - txtstart)
error('l', &tloc, "lexical token too long");
// copy current token:
char *p = txtstart;
txtstart = txtfree = &pbuf->chars[0];
while (p < txtmax)
*txtfree++ = *p++;
*txtfree++ = c;
txtmax = &pbuf->chars[BUFCHUNK - 1];
return 0;
}
#define pch(c) ((txtmax <= txtfree) ? new_buf(c) : (*txtfree++ = c))
#define start_txt() txtstart = txtfree
#define del_txt() txtfree = txtstart
static int Nfile; // = 1;
static Block(Pchar) file_name; // source file names
// file_name[0] == src_file_name
// file_name[0] == 0 means stdin
static int tcurr_file; // current index in file_name
Linkage linkage = linkage_default; // linkage is default C++
const int LINKMAX = 10;
static Linkage lvec[LINKMAX] = { linkage_default };
static int lcount = 0;
void set_linkage(char *p) {
if (p == 0 || *p == 0) { // resume previous linkage
if (lcount > 0)
linkage = lvec[--lcount];
} else {
if (LINKMAX <= ++lcount) {
error('l', "linkage directive nested too deep");
--lcount;
} else if (strcmp(p, "C") == 0)
lvec[lcount] = linkage = linkage_C;
else if (strcmp(p, "C++") == 0)
lvec[lcount] = linkage = linkage_Cplusplus;
else {
error("%s linkage", p);
--lcount;
}
}
}
FILE *out_file = stdout;
static FILE *in_file = stdin;
FILE *pt_file;
FILE *dtpt_file;
static bit doneflag = 0;
int first_file = 0;
static Ptable keyword_table;
static int p_level = 0; /* number of unmatched ``(''s */
static int b_level = 0; /* number of unmatched ``{''s */
#ifdef ibm
#define CSMASK 0377
#define CSSZ 256
#else
#define CSMASK 0177
#define CSSZ 128
#endif
static short lxmask[CSSZ + 1];
int saved = 0; /* putback character, avoid ungetchar */
static int lxtitle();
// overload rt;
inline YYSTYPE rt(char *x) {
YYSTYPE y;
y.s = x;
return y;
}
inline YYSTYPE rt(int x) {
YYSTYPE y;
y.t = x;
return y;
}
inline YYSTYPE rt(loc x) {
YYSTYPE y;
y.l = x;
return y;
}
// inline YYSTYPE rt(void* x) { YYSTYPE y; y.pn = Pname(x); return y; }
#ifdef DBG
#define get(zzz) \
(saved ? error('i', &tloc, "getting twice: %d %c, lex.c line %d", saved, saved, __LINE__) : 0, \
zzz = getc(in_file))
#else
#define get(c) (c = getc(in_file))
#endif
#define get1(c) (saved ? (c = saved, saved = 0, c) : (c = getc(in_file)))
#define unget(zzz) \
{ \
if (saved) \
error('i', &tloc, "unget: saved==%c", saved); \
saved = zzz; \
}
#define backup() (ungetc(saved, in_file), saved = 0)
#define reti(a, b) \
{ \
addtok(a, rt(b), tloc); \
return a; \
}
#define retn(a, b) \
{ \
addtok(a, rt((Pnode) b), tloc); \
return a; \
}
#define rets(a, b) \
{ \
addtok(a, rt(b), tloc); \
return a; \
}
#define retl(a) \
{ \
addtok(a, rt(tloc), tloc); \
return a; \
}
// keys[] holds the external form for tokens with fixed representation
// illegal tokens and those with variable representation have 0 entries
char *keys[MAXTOK + 1];
static void new_key(char *s, TOK toknum, TOK yyclass)
/*
make "s" a new keyword with the representation (token) "toknum"
"yyclass" is the yacc token (for example new_key("int",INT,TYPE); )
"yyclass==0" means yyclass=toknum;
*/
{
Pname n = new name(s);
keys[(toknum == LOC) ? yyclass : toknum] = s;
// n = new name(s);
Pname nn = keyword_table->insert(n, 0);
// if (Nold) error('i',&tloc,"keyword %sD twice",s);
nn->base = toknum;
nn->syn_class = (yyclass) ? yyclass : toknum;
delete n;
}
const int keyword_count = 67;
static void ktbl_init()
/*
enter keywords into keyword table for use by lex()
and into keyword representation table used for output
*/
{
keyword_table = new table(keyword_count, 0, 0);
new_key("asm", ASM, 0);
new_key("auto", AUTO, TYPE);
new_key("break", LOC, BREAK);
new_key("case", LOC, CASE);
new_key("continue", LOC, CONTINUE);
new_key("char", CHAR, TYPE);
new_key("do", LOC, DO);
new_key("double", DOUBLE, TYPE);
new_key("default", LOC, DEFAULT);
new_key("enum", ENUM, 0);
new_key("else", LOC, ELSE);
new_key("extern", EXTERN, TYPE);
new_key("float", FLOAT, TYPE);
new_key("for", LOC, FOR);
new_key("goto", LOC, GOTO);
new_key("if", LOC, IF);
new_key("int", INT, TYPE);
new_key("long", LONG, TYPE);
new_key("return", LOC, RETURN);
new_key("register", REGISTER, TYPE);
new_key("static", STATIC, TYPE);
new_key("struct", STRUCT, AGGR);
new_key("sizeof", SIZEOF, 0);
new_key("short", SHORT, TYPE);
new_key("switch", LOC, SWITCH);
new_key("typedef", TYPEDEF, TYPE);
new_key("unsigned", UNSIGNED, TYPE);
new_key("union", UNION, AGGR);
new_key("void", VOID, TYPE);
new_key("while", LOC, WHILE);
new_key("catch", CATCH, CATCH);
new_key("class", CLASS, AGGR);
new_key("const", CONST, TYPE);
new_key("delete", LOC, DELETE);
new_key("friend", FRIEND, TYPE);
new_key("inline", INLINE, TYPE);
new_key("namespace", NAMESPACE, NAMESPACE);
new_key("mutable", MUTABLE, MUTABLE);
new_key("new", NEW, 0);
new_key("operator", OPERATOR, 0);
new_key("overload", OVERLOAD, TYPE);
new_key("private", PRIVATE, PR);
new_key("protected", PROTECTED, PR);
new_key("public", PUBLIC, PR);
new_key("signed", SIGNED, TYPE);
new_key("template", TEMPLATE, 0);
new_key("this", THIS, 0);
new_key("throw", THROW, THROW);
new_key("try", TRY, TRY);
new_key("using", USING, USING);
new_key("virtual", VIRTUAL, TYPE);
new_key("volatile", VOLATILE, TYPE);
}
loc last_line;
loc noloc = { 0, 0 };
void loc::putline() {
if (file == 0 && line == 0)
return;
if (0 <= file && file <= Nfile) {
char *f = file_name[file];
if (f == 0)
f = src_file_name;
fprintf(out_file, line_format, line, f);
last_line = *this;
}
}
void loc::put(FILE *p) {
if (0 <= file && file <= Nfile) {
char *f = file_name[file];
if (f == 0)
f = src_file_name;
fprintf(p, "\"%s\", line %d: ", f, line);
}
}
char *curr_filename() {
return (file_name[curloc.file]);
}
void lxenter(register char *s, short m)
/* enter a mask into lxmask */
{
register int c;
while (c = *s++)
lxmask[c + 1] |= m;
}
void lxget(register int c, register int m)
/*
put 'c' back then scan for members of character class 'm'
terminate the string read with \0
txtfree points to the character position after that \0
*/
{
pch(c);
if ((get1(c), lxmask[c + 1] & m)) {
pch(c);
while ((get(c), lxmask[c + 1] & m))
pch(c);
}
unget(c);
pch('\0');
}
static struct LXDOPE {
short lxch; /* the character */
short lxact; /* the action to be performed */
TOK lxtok; /* the token number to be returned */
} lxdope[] = {
#ifdef apollo
'@', A_ERR, 0, /* illegal characters go here... */
#else
'$', A_DOLL, 0,
// '$', A_ERR, 0, /* illegal characters go here... */
#endif
'_', A_LET, 0, /* letters point here */
'0', A_DIG, 0, /* digits point here */
' ', A_WS, 0, /* whitespace goes here */
'\n', A_NL, 0, '"', A_STR, 0, /* character string */
'\'', A_CC, 0, /* ASCII character constant */
'`', A_BCD, 0, /* 'foreign' character constant, e.g. BCD */
'(', A_L, LP, ')', A_R, RP, '{', A_LC, LC, '}', A_RC, RC, '[',
A_1C, LB, ']', A_1C, RB, '*', A_MUL, MUL, '?', A_1C, QUEST, ':', A_COL,
COLON, '+', A_PL, PLUS, '-', A_MIN, MINUS, '/', A_SL, DIV, '%', A_MOD, MOD,
'&', A_AND, AND, '|', A_OR, OR, '^', A_ER, ER, '!', A_NOT, NOT, '~',
A_1C, COMPL, ',', A_1C, CM, ';', A_1C, SM, '.', A_DOT, DOT, '<', A_LT,
LT, '>', A_GT, GT, '=', A_ASS, ASSIGN, '#', A_SHARP, 0, EOF, A_EOF, EOFTOK
};
/* note: EOF is used as sentinel, so must be <=0 and last entry in table */
static struct LXDOPE *lxcp[CSSZ + 1];
void lex_init() {
register struct LXDOPE *p;
register int i;
register char *cp;
/* set up character classes */
/* first clear lexmask */
for (i = 0; i <= CSSZ; i++)
lxmask[i] = 0;
#ifdef apollo
lxenter("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$", LEXLET);
#else
lxenter("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_", LEXLET);
#endif
lxenter("0123456789", LEXDIG);
lxenter("0123456789abcdefABCDEF", LEXHEX);
/* \013 should become \v someday; \013 is OK for ASCII and EBCDIC */
lxenter(" \t\r\b\f\013", LEXWS);
lxmask['.' + 1] |= LEXDOT;
/* make lxcp point to appropriate lxdope entry for each character */
/* initialize error entries */
for (i = 0; i <= CSSZ; ++i)
lxcp[i] = lxdope;
/* make unique entries */
for (p = lxdope;; ++p) {
lxcp[p->lxch + 1] = p;
if (p->lxch < 0)
break;
}
/* handle letters, digits, and whitespace */
/* by convention, first, second, and third places */
cp = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
while (*cp)
lxcp[*cp++ + 1] = &lxdope[1];
cp = "123456789";
while (*cp)
lxcp[*cp++ + 1] = &lxdope[2];
cp = "\t\b\r\f\013";
while (*cp)
lxcp[*cp++ + 1] = &lxdope[3];
file_name.reserve(0);
file_name[0] = src_file_name;
// set both curloc and tloc so curloc is valid at program startup
// curloc.file = tloc.file = 0;
curloc.line = tloc.line = 1;
ktbl_init();
lex_clear();
saved = lxtitle();
}
void lex_clear() {
// delete extra buffers:
buf *p = bufhead;
bufhead = 0;
// if (p) {
// fprintf(stderr,"lex_clear\n");
// bufs=0;
//}
while (p) {
buf *pp = p;
p = p->next;
pp->next = freebuf;
freebuf = pp;
}
// re-set to static buffer:
txtstart = txtfree = inbuf;
txtmax = &inbuf[FIRSTCHUNK /*TBUFSZ*/ - 1];
}
int int_val(char hex) {
switch (hex) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return hex - '0';
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
return hex - 'a' + 10;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
return hex - 'A' + 10;
}
error('i', "fall off end of int_val()");
return 0;
}
void hex_to_oct()
/*
\x has been seen on input (in char const or string) and \ printed
read the following hexadecimal integer and replace it with an octal
*/
{
int i = 0;
int c;
get1(c);
if (lxmask[c + 1] & LEXHEX) {
i = int_val(c);
get(c); // try for two
if (lxmask[c + 1] & LEXHEX) {
i = (i << 4) + int_val(c);
get(c); // try for three
if (lxmask[c + 1] & LEXHEX)
i = (i << 4) + int_val(c);
else
unget(c);
} else
unget(c);
} else {
error(&tloc, "hexadecimal digitE after \\x");
unget(c);
}
// if (0377 < i) error('l',&tloc,"hexadecimal constant too large");
i &= 0377;
pch(('0' + (i >> 6)));
pch(('0' + ((i & 070) >> 3)));
pch(('0' + (i & 7)));
}
char *chconst()
/*
read a character constant into inbuf
*/
{
register int c;
int nch = 0;
pch('\'');
for (;;) {
char *p;
char cc = 0;
get1(c);
switch (c) {
case '\'':
goto ex;
case EOF:
error(&tloc, "eof in char constant");
goto ex;
case '\n':
error(&tloc, "newline in char constant");
goto ex;
case '\\':
if (SZ_INT == nch++)
error('l', &tloc, "char constant too long");
pch(c);
switch (get(c)) {
case '\n':
++tloc.line;
default:
pch(c);
break;
case '4':
case '5':
case '6':
case '7': // octal
p = txtfree;
cc = c - 4;
case '0':
case '1':
case '2':
case '3':
pch(c);
get(c); /* try for 2 */
if (lxmask[c + 1] & LEXDIG && c < '8') {
pch(c);
get(c); /* try for 3 */
if (lxmask[c + 1] & LEXDIG && c < '8') {
if (cc)
*p = cc; // zap high bit
pch(c);
} else
unget(c);
} else
unget(c);
break;
case 'x': // hexadecimal
hex_to_oct();
break;
};
break;
default:
if (SZ_INT == nch++)
error('l', &tloc, "char constant too long");
pch(c);
}
}
ex:
if (nch == 0)
error(&tloc, "empty char constant");
pch('\'');
pch('\0');
return txtstart;
}
void lxcom()
/* process a "block comment" */
{
register int c;
get1(c);
for (;; get(c)) {
xx:
switch (c) {
case EOF:
error('w', &tloc, "eof in comment");
return;
case '\n':
tloc.line++;
// Nline++;
break;
case '*':
if (get(c) == '/')
return;
goto xx;
case '/':
if (get(c) == '*') {
error('w', &tloc, "``/*'' in comment");
if (get(c) == '/')
return;
}
goto xx;
}
}
}
void linecom()
// process a "line comment"
{
register int c;
get1(c);
#ifdef DBG
if (c == '@' && get(c) == '!') {
while (get(c) != '\n' && c != EOF)
pch(c);
pch('\0');
process_debug_flags(txtstart);
del_txt();
}
#endif
for (;; get(c))
switch (c) {
case EOF:
error('w', &tloc, "eof in comment");
return;
case '\n':
tloc.line++;
// Nline++;
saved = lxtitle();
return;
}
}
char eat_whitespace() {
register int c;
get1(c);
for (;;) {
lx:
switch (c) {
case EOF:
error('w', &tloc, "unexpected comment");
return EOF;
case '/':
switch (get(c)) {
case '*':
lxcom();
break;
case '/':
linecom();
break;
default:
if (c == EOF)
error(&tloc, "eof after /");
else {
unget(c);
backup();
}
return '/';
}
get1(c);
goto lx;
case '\n':
++tloc.line;
c = lxtitle();
goto lx;
case ' ':
case '\t':
break;
default:
return c;
}
get(c);
}
}
void get_string() {
int lxchar;
for (;;)
switch (get1(lxchar)) {
case '\\':
pch('\\');
switch (get(lxchar)) {
case '\n':
++tloc.line;
default:
pch(lxchar);
break;
case 'x': // hexadecimal
hex_to_oct();
break;
};
break;
case '"': {
char *p = txtstart; // eat_whitespace() moves txtstart
if ((lxchar = eat_whitespace()) == '"') {
// string catenation, break with
// newline to avoid merging characters
// (e.g. "\xAB" "C")
pch('\\');
pch('\n');
continue; // eat '\"' and carry on
};
txtstart = p;
unget(lxchar);
pch(0);
return;
}
case '\n':
error(&tloc, "newline in string");
pch(0);
return;
case EOF:
error(&tloc, "eof in string");
pch(0);
return;
default:
pch(lxchar);
}
}
TOK tlex() {
TOK ret;
Pname n;
// Ntoken++;
for (;;) {
register int lxchar;
register struct LXDOPE *p;
start_txt();
get1(lxchar);
if (lxchar + 1 >= CSSZ)
error("illegal input character encountered: %d", lxchar);
switch ((p = lxcp[lxchar + 1])->lxact) {
case A_1C: // eat up a single character, and return an opcode
reti(p->lxtok, p->lxtok);
case A_EOF:
if (p_level || b_level + lcount)
error(&tloc, "'%s' missing at end of input", (b_level + lcount) ? "}" : ")");
reti(EOFTOK, 0);
case A_SHARP:
// cope with header file not ended with '\n'
unget('#');
saved = lxtitle();
continue;
case A_ERR: {
if (' ' <= lxchar && lxchar <= '~') // ASCII printable
error(&tloc, "illegal character '%c' (ignored)", lxchar);
else
error(&tloc, "illegal character%o (ignored)", lxchar);
continue;
}
case A_DOLL: { // lex a name of the for $id for template tree formals
/* SBL: can remove all this: restore to previous */
// Pname fn ;
// lxget( lxchar, LEXLET|LEXDIG ) ;
// if (!templp->in_progress || !txtstart[1]) {
// no name string immediately follows, treat it
// like an illegal character
error(&tloc, "illegal character%o (ignored)", lxchar);
continue;
// }
/* SBL: remove
txtstart++ ;
if(fn=templ_compilation::tree_parameter(txtstart)) {
switch (fn->n_template_arg) {
case template_expr_tree_formal:
// retain the $ in the name
retn(ID, strdup(--txtstart)) ;
case template_stmt_tree_formal:
retn(SM_PARAM, fn) ;
}
}
error(&tloc,"%s wasn't a statement or expression formal",
txtstart); rets(ID, copy_if_need_be(txtstart));
*/
}
case A_LET: // collect an identifier and check for keyword
{
char ll;
switch (ll = lxchar) {
// case 'l':
case 'L':
switch (get(lxchar)) {
case '\'':
error('s', &tloc, "wide character constant");
unget(lxchar);
continue;
case '"':
error('s', &tloc, "wide character string");
unget(lxchar);
continue;
}
unget(lxchar);
lxchar = ll;
}
}
lxget(lxchar, LEXLET | LEXDIG);
/* look for a keyword or a global type */
if (n = keyword_table->look(txtstart, 0)) /* keyword */
{
TOK x;
switch (x = n->base) {
case TNAME:
del_txt();
error('i', &tloc, "TN%n in keyword_table", n);
break;
// rets(ID,n->string);
case LOC:
del_txt();
retl(n->syn_class);
case EXTERN:
del_txt();
if ((lxchar = eat_whitespace()) == '\"') {
// linkage directive
get_string();
rets(LINKAGE, txtstart);
}
unget(lxchar);
reti(TYPE, EXTERN);
case CATCH:
del_txt();
{
static bit warned = 0;
if (warned == 0) {
error('s', &tloc, "%k", x);
warned = 1;
}
}
reti(n->syn_class, x);
case THROW:
del_txt();
{
static bit warned = 0;
if (warned == 0) {
error('s', &tloc, "%k", x);
warned = 1;
}
}
reti(n->syn_class, x);
case TRY:
del_txt();
{
static bit warned = 0;
if (warned == 0) {
error('s', &tloc, "%k", x);
warned = 1;
}
}
reti(n->syn_class, x);
case MUTABLE: {
static bit warned = 0;
if (warned == 0) {
error('w', &tloc, "%k is a future reserved keyword", n->syn_class);
warned = 1;
}
}
rets(ID, copy_if_need_be(txtstart));
case NAMESPACE: {
static bit warned = 0;
if (warned == 0) {
error('w', &tloc, "%k is a future reserved keyword", n->syn_class);
warned = 1;
}
}
rets(ID, copy_if_need_be(txtstart));
case USING: {
static bit warned = 0;
if (warned == 0) {
error('w', &tloc, "%k is a future reserved keyword", n->syn_class);
warned = 1;
}
}
rets(ID, copy_if_need_be(txtstart));
default:
del_txt();
reti(n->syn_class, x);
}
}
// rets(ID,txtstart);
rets(ID, copy_if_need_be(txtstart));
case A_DIG:
ret = ICON;
if (lxchar == '0') {
int pkchar;
get(pkchar);
if (pkchar == 'x' || pkchar == 'X') { // hex
pch(lxchar);
lxget(pkchar, LEXHEX);
txtfree--;
if (txtfree - txtstart < 3) // minimum "0Xd\0"
error(&tloc, "hex digitX after \"0x\"");
get1(lxchar);
goto getsuffix;
}
unget(pkchar);
}
lxget(lxchar, LEXDIG);