2 * This is a really stupid C tokenizer. It doesn't do any include
3 * files or anything complex at all. That's the preprocessor.
5 * Copyright (C) 2003 Transmeta Corp.
8 * Licensed under the Open Software License version 1.1
25 int input_stream_nr
= 0;
26 struct stream
*input_streams
;
27 static int input_streams_allocated
;
29 #define BUFSIZE (8192)
34 int newline
, whitespace
;
35 struct token
**tokenlist
;
37 unsigned char *buffer
;
40 const char *stream_name(int stream
)
42 if (stream
< 0 || stream
> input_stream_nr
)
43 return "<bad stream>";
44 return input_streams
[stream
].name
;
47 static struct position
stream_pos(stream_t
*stream
)
51 pos
.stream
= stream
->nr
;
52 pos
.newline
= stream
->newline
;
53 pos
.whitespace
= stream
->whitespace
;
54 pos
.pos
= stream
->pos
;
55 pos
.line
= stream
->line
;
60 const char *show_special(int val
)
62 static char buffer
[4];
66 if (val
>= SPECIAL_BASE
)
67 strcpy(buffer
, (char *) combinations
[val
- SPECIAL_BASE
]);
71 const char *show_ident(const struct ident
*ident
)
73 static char buffer
[256];
76 sprintf(buffer
, "%.*s", ident
->len
, ident
->name
);
80 static char *charstr(char *ptr
, unsigned char c
, unsigned char escape
, unsigned char next
)
83 if (c
== escape
|| c
== '\\')
98 return ptr
+ sprintf(ptr
, "%o", c
);
100 return ptr
+ sprintf(ptr
, "%03o", c
);
103 const char *show_string(const struct string
*string
)
105 static char buffer
[4 * MAX_STRING
+ 3];
110 return "<bad_string>";
113 for (i
= 0; i
< string
->length
-1; i
++) {
114 const char *p
= string
->data
+ i
;
115 ptr
= charstr(ptr
, p
[0], '"', p
[1]);
122 const char *show_token(const struct token
*token
)
124 static char buffer
[256];
128 switch (token_type(token
)) {
130 return "syntax error";
133 return "end-of-input";
136 return show_ident(token
->ident
);
139 return show_string(token
->string
);
142 return token
->number
;
145 return show_special(token
->special
);
149 int c
= token
->character
;
151 ptr
= charstr(ptr
, c
, '\'', 0);
157 case TOKEN_STREAMBEGIN
:
158 sprintf(buffer
, "<beginning of '%s'>", stream_name(token
->pos
.stream
));
161 case TOKEN_STREAMEND
:
162 sprintf(buffer
, "<end of '%s'>", stream_name(token
->pos
.stream
));
170 int init_stream(const char *name
, int fd
, const char **next_path
)
172 int stream
= input_stream_nr
;
173 struct stream
*current
;
175 if (stream
>= input_streams_allocated
) {
176 int newalloc
= stream
* 4 / 3 + 10;
177 input_streams
= realloc(input_streams
, newalloc
* sizeof(struct stream
));
179 die("Unable to allocate more streams space");
180 input_streams_allocated
= newalloc
;
182 current
= input_streams
+ stream
;
183 memset(current
, 0, sizeof(*current
));
184 current
->name
= name
;
186 current
->next_path
= next_path
;
187 current
->path
= NULL
;
188 current
->constant
= CONSTANT_FILE_MAYBE
;
189 input_stream_nr
= stream
+1;
193 static struct token
* alloc_token(stream_t
*stream
)
195 struct token
*token
= __alloc_token(0);
196 token
->pos
= stream_pos(stream
);
201 * Argh... That was surprisingly messy - handling '\r' complicates the
204 static int nextchar_slow(stream_t
*stream
)
206 int offset
= stream
->offset
;
207 int size
= stream
->size
;
209 int spliced
= 0, had_cr
, had_backslash
, complain
;
212 had_cr
= had_backslash
= complain
= 0;
215 if (offset
>= size
) {
216 size
= read(stream
->fd
, stream
->buffer
, BUFSIZE
);
220 stream
->offset
= offset
= 0;
223 c
= stream
->buffer
[offset
++];
225 if (had_cr
&& c
!= '\n')
240 if (!had_backslash
) {
250 warning(stream_pos(stream
), "non-ASCII data stream");
260 stream
->offset
= offset
;
262 warning(stream_pos(stream
), "non-ASCII data stream");
272 warning(stream_pos(stream
), "no newline at end of file");
274 warning(stream_pos(stream
), "non-ASCII data stream");
276 warning(stream_pos(stream
), "backslash-newline at end of file");
281 * We want that as light as possible while covering all normal cases.
282 * Slow path (including the logics with line-splicing and EOF sanity
283 * checks) is in nextchar_slow().
285 static inline int nextchar(stream_t
*stream
)
287 int offset
= stream
->offset
;
289 if (offset
< stream
->size
) {
290 int c
= stream
->buffer
[offset
++];
291 static const char special
[256] = {
292 ['\r'] = 1, ['\n'] = 1, ['\\'] = 1
295 stream
->offset
= offset
;
300 return nextchar_slow(stream
);
303 struct token eof_token_entry
;
305 static void mark_eof(stream_t
*stream
, struct token
*end_token
)
309 end
= alloc_token(stream
);
310 token_type(end
) = TOKEN_STREAMEND
;
311 end
->pos
.newline
= 1;
313 eof_token_entry
.next
= &eof_token_entry
;
314 eof_token_entry
.pos
.newline
= 1;
317 end_token
= &eof_token_entry
;
318 end
->next
= end_token
;
319 *stream
->tokenlist
= end
;
320 stream
->tokenlist
= NULL
;
323 static void add_token(stream_t
*stream
)
325 struct token
*token
= stream
->token
;
327 stream
->token
= NULL
;
329 *stream
->tokenlist
= token
;
330 stream
->tokenlist
= &token
->next
;
333 static void drop_token(stream_t
*stream
)
335 stream
->newline
|= stream
->token
->pos
.newline
;
336 stream
->whitespace
|= stream
->token
->pos
.whitespace
;
337 stream
->token
= NULL
;
349 static const long cclass
[257] = {
350 ['0' + 1 ... '9' + 1] = Digit
| Hex
,
351 ['A' + 1 ... 'D' + 1] = Letter
| Hex
,
352 ['E' + 1] = Letter
| Hex
| Exp
,
353 ['F' + 1] = Letter
| Hex
,
354 ['G' + 1 ... 'O' + 1] = Letter
,
355 ['P' + 1] = Letter
| Exp
,
356 ['Q' + 1 ... 'Z' + 1] = Letter
,
357 ['a' + 1 ... 'd' + 1] = Letter
| Hex
,
358 ['e' + 1] = Letter
| Hex
| Exp
,
359 ['f' + 1] = Letter
| Hex
,
360 ['g' + 1 ... 'o' + 1] = Letter
,
361 ['p' + 1] = Letter
| Exp
,
362 ['q' + 1 ... 'z' + 1] = Letter
,
364 ['.' + 1] = Dot
| ValidSecond
,
365 ['=' + 1] = ValidSecond
,
366 ['+' + 1] = ValidSecond
,
367 ['-' + 1] = ValidSecond
,
368 ['>' + 1] = ValidSecond
,
369 ['<' + 1] = ValidSecond
,
370 ['&' + 1] = ValidSecond
,
371 ['|' + 1] = ValidSecond
,
372 ['#' + 1] = ValidSecond
,
380 * pp-number identifier-nodigit
387 static int get_one_number(int c
, int next
, stream_t
*stream
)
390 static char buffer
[4095];
391 char *p
= buffer
, *buf
, *buffer_end
= buffer
+ sizeof (buffer
);
396 long class = cclass
[next
+ 1];
397 if (!(class & (Dot
| Digit
| Letter
)))
401 next
= nextchar(stream
);
403 if (next
== '-' || next
== '+') {
406 next
= nextchar(stream
);
411 if (p
== buffer_end
) {
412 sparse_error(stream_pos(stream
), "number token exceeds %td characters",
413 buffer_end
- buffer
);
414 // Pretend we saw just "1".
421 buf
= __alloc_bytes(len
);
422 memcpy(buf
, buffer
, len
);
424 token
= stream
->token
;
425 token_type(token
) = TOKEN_NUMBER
;
432 static int escapechar(int first
, int type
, stream_t
*stream
, int *valp
)
436 next
= nextchar(stream
);
440 warning(stream_pos(stream
), "Newline in string or character constant");
442 if (first
== '\\' && next
!= EOF
) {
444 next
= nextchar(stream
);
480 warning(stream_pos(stream
), "Newline in string or character constant");
485 while (next
>= '0' && next
<= '9') {
486 value
= (value
<< 3) + (next
-'0');
487 next
= nextchar(stream
);
495 int hex
= hexval(next
);
498 next
= nextchar(stream
);
499 while ((hex
= hexval(next
)) < 16) {
500 value
= (value
<< 4) + hex
;
501 next
= nextchar(stream
);
509 warning(stream_pos(stream
), "Unknown escape '%c'", value
);
512 /* Mark it as escaped */
519 static int get_char_token(int next
, stream_t
*stream
)
524 next
= escapechar(next
, '\'', stream
, &value
);
525 if (value
== '\'' || next
!= '\'') {
526 sparse_error(stream_pos(stream
), "Bad character constant");
531 token
= stream
->token
;
532 token_type(token
) = TOKEN_CHAR
;
533 token
->character
= value
& 0xff;
536 return nextchar(stream
);
539 static int get_string_token(int next
, stream_t
*stream
)
541 static char buffer
[MAX_STRING
];
542 struct string
*string
;
548 next
= escapechar(next
, '"', stream
, &val
);
552 warning(stream_pos(stream
), "End of file in middle of string");
555 if (len
< MAX_STRING
)
560 if (len
> MAX_STRING
) {
561 warning(stream_pos(stream
), "string too long (%d bytes, %d bytes max)", len
, MAX_STRING
);
565 string
= __alloc_string(len
+1);
566 memcpy(string
->data
, buffer
, len
);
567 string
->data
[len
] = '\0';
568 string
->length
= len
+1;
571 token
= stream
->token
;
572 token_type(token
) = TOKEN_STRING
;
573 token
->string
= string
;
579 static int drop_stream_eoln(stream_t
*stream
)
581 int next
= nextchar(stream
);
587 next
= nextchar(stream
);
593 static int drop_stream_comment(stream_t
*stream
)
598 newline
= stream
->newline
;
600 next
= nextchar(stream
);
604 warning(stream_pos(stream
), "End of file in the middle of a comment");
607 next
= nextchar(stream
);
608 if (curr
== '*' && next
== '/')
611 stream
->newline
= newline
;
612 return nextchar(stream
);
615 unsigned char combinations
[][4] = COMBINATION_STRINGS
;
617 #define NR_COMBINATIONS (SPECIAL_ARG_SEPARATOR - SPECIAL_BASE)
619 /* hash function for two-character punctuators - all give unique values */
620 #define special_hash(c0, c1) (((c0*8+c1*2)+((c0*8+c1*2)>>5))&31)
623 * note that we won't get false positives - special_hash(0,0) is 0 and
624 * entry 0 is filled (by +=), so all the missing ones are OK.
626 static unsigned char hash_results
[32][2] = {
627 #define RES(c0, c1) [special_hash(c0, c1)] = {c0, c1}
628 RES('+', '='), /* 00 */
629 RES('/', '='), /* 01 */
630 RES('^', '='), /* 05 */
631 RES('&', '&'), /* 07 */
632 RES('#', '#'), /* 08 */
633 RES('<', '<'), /* 0a */
634 RES('<', '='), /* 0c */
635 RES('!', '='), /* 0e */
636 RES('%', '='), /* 0f */
637 RES('-', '-'), /* 10 */
638 RES('-', '='), /* 11 */
639 RES('-', '>'), /* 13 */
640 RES('=', '='), /* 15 */
641 RES('&', '='), /* 17 */
642 RES('*', '='), /* 18 */
643 RES('.', '.'), /* 1a */
644 RES('+', '+'), /* 1b */
645 RES('|', '='), /* 1c */
646 RES('>', '='), /* 1d */
647 RES('|', '|'), /* 1e */
648 RES('>', '>') /* 1f */
651 static int code
[32] = {
652 #define CODE(c0, c1, value) [special_hash(c0, c1)] = value
653 CODE('+', '=', SPECIAL_ADD_ASSIGN
), /* 00 */
654 CODE('/', '=', SPECIAL_DIV_ASSIGN
), /* 01 */
655 CODE('^', '=', SPECIAL_XOR_ASSIGN
), /* 05 */
656 CODE('&', '&', SPECIAL_LOGICAL_AND
), /* 07 */
657 CODE('#', '#', SPECIAL_HASHHASH
), /* 08 */
658 CODE('<', '<', SPECIAL_LEFTSHIFT
), /* 0a */
659 CODE('<', '=', SPECIAL_LTE
), /* 0c */
660 CODE('!', '=', SPECIAL_NOTEQUAL
), /* 0e */
661 CODE('%', '=', SPECIAL_MOD_ASSIGN
), /* 0f */
662 CODE('-', '-', SPECIAL_DECREMENT
), /* 10 */
663 CODE('-', '=', SPECIAL_SUB_ASSIGN
), /* 11 */
664 CODE('-', '>', SPECIAL_DEREFERENCE
), /* 13 */
665 CODE('=', '=', SPECIAL_EQUAL
), /* 15 */
666 CODE('&', '=', SPECIAL_AND_ASSIGN
), /* 17 */
667 CODE('*', '=', SPECIAL_MUL_ASSIGN
), /* 18 */
668 CODE('.', '.', SPECIAL_DOTDOT
), /* 1a */
669 CODE('+', '+', SPECIAL_INCREMENT
), /* 1b */
670 CODE('|', '=', SPECIAL_OR_ASSIGN
), /* 1c */
671 CODE('>', '=', SPECIAL_GTE
), /* 1d */
672 CODE('|', '|', SPECIAL_LOGICAL_OR
), /* 1e */
673 CODE('>', '>', SPECIAL_RIGHTSHIFT
) /* 1f */
677 static int get_one_special(int c
, stream_t
*stream
)
682 next
= nextchar(stream
);
685 * Check for numbers, strings, character constants, and comments
689 if (next
>= '0' && next
<= '9')
690 return get_one_number(c
, next
, stream
);
693 return get_string_token(next
, stream
);
695 return get_char_token(next
, stream
);
698 return drop_stream_eoln(stream
);
700 return drop_stream_comment(stream
);
704 * Check for combinations
707 if (cclass
[next
+ 1] & ValidSecond
) {
708 i
= special_hash(c
, next
);
709 if (hash_results
[i
][0] == c
&& hash_results
[i
][1] == next
) {
711 next
= nextchar(stream
);
712 if (value
>= SPECIAL_LEFTSHIFT
&&
713 next
== "==."[value
- SPECIAL_LEFTSHIFT
]) {
715 next
= nextchar(stream
);
721 token
= stream
->token
;
722 token_type(token
) = TOKEN_SPECIAL
;
723 token
->special
= value
;
728 #define IDENT_HASH_BITS (13)
729 #define IDENT_HASH_SIZE (1<<IDENT_HASH_BITS)
730 #define IDENT_HASH_MASK (IDENT_HASH_SIZE-1)
732 #define ident_hash_init(c) (c)
733 #define ident_hash_add(oldhash,c) ((oldhash)*11 + (c))
734 #define ident_hash_end(hash) ((((hash) >> IDENT_HASH_BITS) + (hash)) & IDENT_HASH_MASK)
736 static struct ident
*hash_table
[IDENT_HASH_SIZE
];
737 static int ident_hit
, ident_miss
, idents
;
739 void show_identifier_stats(void)
742 int distribution
[100];
744 fprintf(stderr
, "identifiers: %d hits, %d misses\n",
745 ident_hit
, ident_miss
);
747 for (i
= 0; i
< 100; i
++)
750 for (i
= 0; i
< IDENT_HASH_SIZE
; i
++) {
751 struct ident
* ident
= hash_table
[i
];
760 distribution
[count
]++;
763 for (i
= 0; i
< 100; i
++) {
765 fprintf(stderr
, "%2d: %d buckets\n", i
, distribution
[i
]);
769 static struct ident
*alloc_ident(const char *name
, int len
)
771 struct ident
*ident
= __alloc_ident(len
);
772 ident
->symbols
= NULL
;
775 memcpy(ident
->name
, name
, len
);
779 static struct ident
* insert_hash(struct ident
*ident
, unsigned long hash
)
781 ident
->next
= hash_table
[hash
];
782 hash_table
[hash
] = ident
;
787 static struct ident
*create_hashed_ident(const char *name
, int len
, unsigned long hash
)
792 p
= &hash_table
[hash
];
793 while ((ident
= *p
) != NULL
) {
794 if (ident
->len
== (unsigned char) len
) {
795 const char *n
= name
;
796 const char *m
= ident
->name
;
812 ident
= alloc_ident(name
, len
);
820 static unsigned long hash_name(const char *name
, int len
)
823 const unsigned char *p
= (const unsigned char *)name
;
825 hash
= ident_hash_init(*p
++);
827 unsigned int i
= *p
++;
828 hash
= ident_hash_add(hash
, i
);
830 return ident_hash_end(hash
);
833 struct ident
*hash_ident(struct ident
*ident
)
835 return insert_hash(ident
, hash_name(ident
->name
, ident
->len
));
838 struct ident
*built_in_ident(const char *name
)
840 int len
= strlen(name
);
841 return create_hashed_ident(name
, len
, hash_name(name
, len
));
844 struct token
*built_in_token(int stream
, const char *name
)
848 token
= __alloc_token(0);
849 token
->pos
.stream
= stream
;
850 token_type(token
) = TOKEN_IDENT
;
851 token
->ident
= built_in_ident(name
);
855 static int get_one_identifier(int c
, stream_t
*stream
)
864 hash
= ident_hash_init(c
);
867 next
= nextchar(stream
);
868 if (!(cclass
[next
+ 1] & (Letter
| Digit
)))
870 if (len
>= sizeof(buf
))
872 hash
= ident_hash_add(hash
, next
);
876 hash
= ident_hash_end(hash
);
878 ident
= create_hashed_ident(buf
, len
, hash
);
881 token
= stream
->token
;
882 token_type(token
) = TOKEN_IDENT
;
883 token
->ident
= ident
;
888 static int get_one_token(int c
, stream_t
*stream
)
890 long class = cclass
[c
+ 1];
892 return get_one_number(c
, nextchar(stream
), stream
);
894 return get_one_identifier(c
, stream
);
895 return get_one_special(c
, stream
);
898 static struct token
*setup_stream(stream_t
*stream
, int idx
, int fd
,
899 unsigned char *buf
, unsigned int buf_size
)
906 stream
->whitespace
= 0;
909 stream
->token
= NULL
;
912 stream
->size
= buf_size
;
913 stream
->buffer
= buf
;
915 begin
= alloc_token(stream
);
916 token_type(begin
) = TOKEN_STREAMBEGIN
;
917 stream
->tokenlist
= &begin
->next
;
921 static void tokenize_stream(stream_t
*stream
, struct token
*endtoken
)
923 int c
= nextchar(stream
);
926 struct token
*token
= alloc_token(stream
);
927 stream
->token
= token
;
929 stream
->whitespace
= 0;
930 c
= get_one_token(c
, stream
);
933 stream
->whitespace
= 1;
934 c
= nextchar(stream
);
936 mark_eof(stream
, endtoken
);
939 struct token
* tokenize_buffer(void *buffer
, unsigned long size
, struct token
*endtoken
)
944 begin
= setup_stream(&stream
, 0, -1, buffer
, size
);
945 tokenize_stream(&stream
, endtoken
);
949 struct token
* tokenize(const char *name
, int fd
, struct token
*endtoken
, const char **next_path
)
953 unsigned char buffer
[BUFSIZE
];
956 idx
= init_stream(name
, fd
, next_path
);
958 // info(endtoken->pos, "File %s is const", name);
962 begin
= setup_stream(&stream
, idx
, fd
, buffer
, 0);
963 tokenize_stream(&stream
, endtoken
);