-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscan.c
1097 lines (1000 loc) · 26.3 KB
/
scan.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
#include <assert.h>
#include <stdio.h>
#include <memory.h>
#include <sys/types.h>
#include "c2ada.h"
#include "y.tab.h"
#undef NULL
#define NULL 0
#define FN file_name(yypos)
#define LN line_number(yypos)
#define TRACE() printf("\n%s:%d", __FILE__, __LINE__)
/* This is an otherwise unused character that's been hijacked
* to represent the start of a macro body in a special call
* to the parser.
*/
#define MARKER_CHAR '\033'
file_pos_t yypos;
static int yyc;
static int skipping_compound_statements;
static bool at_line_start = true;
static node_t* last_ident;
/* When this flag is true, a typedef-name token will be returned
* as an IDENTIFIER; otherwise as a TYPEDEF_NAME
*/
static bool typedef_name_as_id;
void yield_typedef(bool flag)
{
typedef_name_as_id = !flag;
}
void yyerror(char* msg)
{
error(FN, LN, msg);
}
void td(void)
{
yield_typedef(true);
}
void yylex_init()
{
yyc = cpp_getc();
}
static void end_line()
{
yypos++;
last_ident = NULL;
at_line_start = true;
}
static int magnitude(int c, host_int_t* val)
{
host_int_t m;
int sign;
if(!is_magnitude(c))
{
*val = 1;
return c;
}
c = cpp_getc();
sign = 1;
if(c == '-')
{
sign = -1;
c = cpp_getc();
}
else if(c == '+')
{
c = cpp_getc();
}
for(m = 0; is_digit(c);)
{
m = m * 10 + (c - '0');
c = cpp_getc();
}
*val = m * sign;
return c;
}
static int grok_number(bool fraction)
/* fraction is true if the character previous to yyc was '.' */
{
host_int_t val;
int base;
if(fraction)
{
val = 0;
base = 10;
}
else
{
/* scan integer part of number */
if(yyc == '0')
{
val = 0;
yyc = cpp_getc();
if(yyc == 'x' || yyc == 'X')
{
base = 16;
for(;;)
{
yyc = cpp_getc();
if(is_digit(yyc))
{
val = (val << 4) + (yyc - '0');
}
else if(yyc >= 'A' && yyc <= 'F')
{
val = (val << 4) + 10 + (yyc - 'A');
}
else if(yyc >= 'a' && yyc <= 'f')
{
val = (val << 4) + 10 + (yyc - 'a');
}
else
{
break;
}
}
}
else
{
base = is_octal_digit(yyc) ? 8 : 10;
while(is_octal_digit(yyc))
{
val = (val << 3) + (yyc - '0');
yyc = cpp_getc();
}
}
}
else
{
val = yyc - '0';
base = 10;
for(;;)
{
yyc = cpp_getc();
if(!is_digit(yyc))
break;
val = val * 10 + (yyc - '0');
}
}
if(int_modifier(yyc))
{
yyc = cpp_getc();
}
}
if(fraction || yyc == '.')
{
host_float_t dval, d;
dval = (host_float_t)val;
d = 0.1;
/* If fraction, yyc is already the first following digit */
if(!fraction)
yyc = cpp_getc();
while(is_digit(yyc))
{
int tmp;
tmp = yyc - '0';
dval += (d * (host_float_t)tmp);
d *= 0.1;
yyc = cpp_getc();
}
/* handle magnitude (i.e, exponent) */
if(is_magnitude(yyc))
{
host_int_t vm;
host_float_t m;
yyc = magnitude(yyc, &vm);
if(vm > 0)
{
for(m = 1.0; vm; vm--)
{
m *= 10.0;
}
}
else
{
for(m = 1.0; vm; vm++)
{
m /= 10.0;
}
}
dval *= m;
}
if(float_modifier(yyc))
{
yyc = cpp_getc();
}
yylval.nod = new_node(_FP_Number, dval);
return FLOATING_CONSTANT;
}
while(int_modifier(yyc))
{
yyc = cpp_getc();
}
yylval.nod = new_node(_Int_Number, val, base);
return INTEGER_CONSTANT;
}
struct resword
{
char* name;
short token;
};
extern struct resword* in_word_set(char*, int);
static int grok_ident(void)
{
char id[1024], *p;
struct resword* r;
symbol_t* sym;
for(p = id; is_alpha_numeric(yyc); yyc = cpp_getc())
{
*p++ = yyc;
}
*p = 0;
r = in_word_set(id, (int)(p - id));
if(r != NULL)
{
return r->token;
}
if(!macro_find(id) && (sym = find_sym(id)))
{
if(!typedef_name_as_id && is_typedef(sym))
{
yylval.typ = copy_type(sym->sym_type);
yylval.typ->type_base = sym;
yylval.typ->is_typedef = 0;
return TYPEDEF_NAME;
}
}
yylval.nod = new_node(_Ident, new_string(id));
last_ident = yylval.nod;
return IDENTIFIER;
}
static int escaped_char(int c)
/*
* Interpret an escape sequence. {c} is the first character
* following the backslash, and the global {yyc} holds the
* first character after that. escaped_char will consume
* more characters (with calls to cpp_getc) if {c} is '0' or 'x',
* indicating the start of a numeric sequence.
* On exit, yyc holds the first character following the
* escape sequence.
*/
{
int val, i;
switch(c)
{
case 'n':
return '\n';
case 't':
return '\t';
case 'v':
return '\v';
case 'b':
return '\b';
case 'r':
return '\r';
case 'f':
return '\f';
case 'a':
return '\a';
case '?':
return '\?';
case '\'':
return '\'';
case '\"':
return '\"';
case '\\':
return '\\';
case 0:
for(i = 0, val = 0; is_octal_digit(yyc) && i < 3; i++, yyc = cpp_getc())
{
val = val * 8 + (yyc - '0');
}
return val;
case 'x':
for(i = 0, val = 0; is_hex_digit(yyc) && i < 2; i++, yyc = cpp_getc())
{
val *= 16;
if(is_digit(yyc))
{
val += (yyc - '0');
}
else if(yyc <= 'F')
{
val += (yyc - ('A' - 10));
}
else
{
val += (yyc - ('a' - 10));
}
}
return val;
default:
break;
}
return c;
}
static int scan_char_const()
{
host_int_t cval = 0;
int c;
for(;;)
{
if(is_eof(yyc))
{
error(FN, LN, "End of file while scanning char constant");
goto end_char_const;
}
if(is_eol(yyc))
{
error(FN, LN, "End of line while scanning char constant");
goto end_char_const;
}
switch(yyc)
{
case '\'':
yyc = cpp_getc();
goto end_char_const;
case '\\':
yyc = cpp_getc();
switch(yyc)
{
case '\n':
yypos++;
yyc = cpp_getc();
break;
case '0':
case 'x':
case 'n':
case 't':
case 'v':
case 'b':
case 'r':
case 'f':
case 'a':
case '?':
case '\\':
case '\'':
case '\"':
c = yyc;
yyc = cpp_getc();
cval <<= 8;
cval |= escaped_char(c);
break;
default:
cval <<= 8;
cval |= yyc;
yyc = cpp_getc();
break;
}
break;
default:
cval <<= 8;
cval |= yyc;
yyc = cpp_getc();
break;
}
}
end_char_const:
yylval.nod = new_node(_Int_Number, cval, 10);
yylval.nod->char_lit = true;
return CHARACTER_CONSTANT;
}
static int scan_string()
{
buffer_t buf;
int c, len;
char* s;
buf_init(&buf);
for(;;)
{
if(is_eof(yyc))
{
error(FN, LN, "End of file while scanning string constant");
goto end_of_string;
}
if(is_eol(yyc))
{
error(FN, LN, "End of line while scanning string constant");
goto end_of_string;
}
switch(yyc)
{
case '"':
yyc = cpp_getc();
goto end_of_string;
case '\\':
yyc = cpp_getc();
switch(yyc)
{
case '\n':
yypos++;
yyc = cpp_getc();
break;
case '0':
case 'x':
case 'n':
case 't':
case 'v':
case 'b':
case 'r':
case 'f':
case 'a':
case '?':
case '\\':
case '\'':
case '\"':
c = yyc;
yyc = cpp_getc();
buf_add(&buf, escaped_char(c));
break;
default:
buf_add(&buf, yyc);
yyc = cpp_getc();
break;
}
break;
default:
buf_add(&buf, yyc);
yyc = cpp_getc();
break;
}
}
end_of_string:
len = buf_count(&buf);
s = buf_get_str(&buf);
yylval.nod = new_node(_String, s, len);
set_cur_unit_has_const_string();
return STRING;
}
static int skip_to_end(int c)
{
for(;;)
{
if(is_eof(c) || is_eol(c))
break;
c = cpp_getc();
}
return c;
}
int skip_white(int c)
{
while(is_white(c))
{
c = cpp_getc();
}
return c;
}
static int skip_c_comment(int c)
{
int tmp;
for(;;)
{
switch(classof(c))
{
case END_INPUT:
return 0;
case END_OF_LINE:
yypos++;
c = cpp_getc();
break;
case DIGIT | XDIGIT:
case ALPHA:
case ALPHA | XDIGIT:
case MSTART:
case WHITE:
c = cpp_getc();
break;
case PUNCT:
tmp = c;
c = cpp_getc();
switch(tmp)
{
case '*':
if(c == '/')
{
return cpp_getc();
}
break;
}
break;
default:
assert(0);
break;
}
}
}
static int skip_cpp_comment(int c)
{
for(;;)
{
switch(classof(c))
{
case END_INPUT:
return 0;
case END_OF_LINE:
yypos++;
return cpp_getc();
case DIGIT | XDIGIT:
case ALPHA:
case ALPHA | XDIGIT:
case MSTART:
case WHITE:
case PUNCT:
c = cpp_getc();
break;
default:
assert(0);
break;
}
}
}
/******************** saving comments **************/
static struct comment_block* comment_accum;
static void save_comment_line(char* str)
{
if(!comment_accum)
comment_accum = new_comment_block();
add_comment_line(comment_accum, str);
}
struct comment_block* fetch_comment_block()
{
struct comment_block* tmp;
tmp = comment_accum;
comment_accum = 0;
return tmp;
}
/**************************************************/
static int save_c_comment(int c)
{
int tmp, result;
buffer_t buf;
buf_init(&buf);
for(;;)
{
switch(classof(c))
{
case END_INPUT:
result = 0;
goto end_of_subp;
case END_OF_LINE:
buf_add(&buf, c);
yypos++;
c = cpp_getc();
break;
case DIGIT | XDIGIT:
case ALPHA:
case ALPHA | XDIGIT:
case MSTART:
case WHITE:
buf_add(&buf, c);
c = cpp_getc();
break;
case PUNCT:
tmp = c;
c = cpp_getc();
switch(tmp)
{
case '*':
if(c == '/')
{
result = cpp_getc();
goto end_of_subp;
}
break;
}
buf_add(&buf, tmp);
break;
default:
assert(0);
break;
}
}
end_of_subp:
if(buf_count(&buf) > 0)
{
if(at_line_start)
{
save_comment_line(buf_get_str(&buf));
}
else
{
assert(last_ident->node_kind == _Ident);
last_ident->node.id.cmnt = buf_get_str(&buf);
}
}
last_ident = NULL;
return result;
}
int save_cpp_comment(int c)
{
int result;
buffer_t buf;
buf_init(&buf);
for(;;)
{
switch(classof(c))
{
case END_INPUT:
result = 0;
goto end_of_subp;
case END_OF_LINE:
buf_add(&buf, c);
yypos++;
result = cpp_getc();
goto end_of_subp;
case DIGIT | XDIGIT:
case ALPHA:
case ALPHA | XDIGIT:
case MSTART:
case WHITE:
case PUNCT:
buf_add(&buf, c);
c = cpp_getc();
break;
default:
assert(0);
break;
}
}
end_of_subp:
if(buf_count(&buf) > 0)
{
assert(last_ident->node_kind == _Ident);
last_ident->node.id.cmnt = buf_get_str(&buf);
}
last_ident = NULL;
return result;
}
static int scan_c_comment(int c)
{
if(last_ident || at_line_start)
{
return save_c_comment(c);
}
return skip_c_comment(c);
}
static int scan_cpp_comment(int c)
{
if(last_ident)
{
return save_cpp_comment(c);
}
return skip_cpp_comment(c);
}
static void grok_directive(void)
/*
* The only directive expected at this point (post-preprocessing)
* is of the form
* # <linenumber> <filename> <nesting level>
* where <nesting level> is an extension to the standard C directive,
* showing how many include levels deep this file is.
*/
{
char fname[256];
int i, line, nest;
file_pos_t old_pos;
/* scan the line number */
yyc = skip_white(cpp_getc());
if(!is_digit(yyc))
{
yyc = skip_to_end(yyc);
return;
}
for(line = 0; is_digit(yyc); yyc = cpp_getc())
{
line = line * 10 + (yyc - '0');
}
/* scan the file name */
yyc = skip_white(yyc);
if(yyc != '"')
{
yyc = skip_to_end(yyc);
return;
}
for(i = 0;; i++)
{
yyc = cpp_getc();
if(yyc == '"' || is_eof(yyc) || is_eol(yyc))
break;
fname[i] = yyc;
}
fname[i] = 0;
if(i == 0)
{
yyc = skip_to_end(yyc);
return;
}
/* change the file position */
old_pos = yypos;
yypos = set_file_pos(fname, line);
if(at_file_start && pos_file(old_pos) != pos_file(yypos))
{
/* Grab initial comments in current file before
* opening up next one.
*/
if(!cur_unit_header_comment_set())
{
set_cur_unit_header_comment(fetch_comment_block());
}
}
init_unit(yypos);
if(yyc != '"')
{
yyc = skip_to_end(yyc);
return;
}
/* scan nesting level number */
yyc = skip_white(cpp_getc());
if(!is_digit(yyc))
{
yyc = skip_to_end(yyc);
return;
}
for(nest = 0; is_digit(yyc); yyc = cpp_getc())
{
nest = nest * 10 + (yyc - '0');
}
unit_included(yypos, nest);
yyc = skip_to_end(yyc);
if(is_eol(yyc))
{
/* DON'T increment yypos */
yyc = cpp_getc();
}
}
static int skip()
{
int token;
int yylex();
token = yylex();
for(;;)
{
switch(token)
{
case '}':
case 0:
goto done;
case '{':
token = skip();
if(token == '}')
{
token = yylex();
}
break;
default:
token = yylex();
break;
}
}
done:
return token;
}
static int next_token(void); /* forward ref */
int yylex(void)
{
int result = next_token();
if(at_file_start)
{
set_cur_unit_header_comment(fetch_comment_block());
}
at_file_start = false;
return result;
}
static int next_token(void)
{
int token;
if(skipping_compound_statements)
{
skipping_compound_statements = 0;
token = skip();
return token;
}
for(;;)
{
switch(classof(yyc))
{
case END_INPUT:
return 0;
case WHITE:
do
{
yyc = cpp_getc();
} while(is_white(yyc));
break;
case END_OF_LINE:
end_line();
yyc = cpp_getc();
break;
case DIGIT | XDIGIT:
at_line_start = false;
return grok_number(false);
case ALPHA:
case ALPHA | XDIGIT:
at_line_start = false;
return grok_ident();
case MSTART:
grok_directive();
break;
case PUNCT:
token = yyc;
yyc = cpp_getc();
switch(token)
{
case '.':
if(yyc == '.')
{
yyc = cpp_getc();
if(yyc == '.')
{
yyc = cpp_getc();
at_line_start = false;
return ELLIPSIS;
}
at_line_start = false;
return DOTDOT;
}
else if(is_digit(yyc))
{
/* This is a floating point number starting with '.' */
return grok_number(true);
}
break;
case '\"':
at_line_start = false;
return scan_string();
case '\'':
at_line_start = false;
return scan_char_const();
case '&':
switch(yyc)
{
case '&':
yyc = cpp_getc();
at_line_start = false;
return AND_OP;
case '=':
yyc = cpp_getc();
at_line_start = false;
return AND_ASSIGN;
}
break;
case '^':
if(yyc == '=')
{
yyc = cpp_getc();
at_line_start = false;
return XOR_ASSIGN;
}
break;
case '|':
switch(yyc)
{
case '|':
yyc = cpp_getc();
at_line_start = false;
return OR_OP;
case '=':
yyc = cpp_getc();
at_line_start = false;
return OR_ASSIGN;
}
break;
case '*':
if(yyc == '=')
{
yyc = cpp_getc();
at_line_start = false;
return MUL_ASSIGN;
}
break;
case '/':
switch(yyc)
{
case '=':
yyc = cpp_getc();
at_line_start = false;
return DIV_ASSIGN;
case '*':
yyc = scan_c_comment(cpp_getc());
continue;
case '/':
yyc = scan_cpp_comment(cpp_getc());
continue;
}
break;
case '%':
if(yyc == '=')
{
yyc = cpp_getc();
at_line_start = false;
return MOD_ASSIGN;
}
break;
case '<':
switch(yyc)
{
case '<':
yyc = cpp_getc();
if(yyc == '=')
{
yyc = cpp_getc();
at_line_start = false;
return LEFT_ASSIGN;
}
at_line_start = false;
return LEFT_OP;
case '=':