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
) {
218 size
= read(stream
->fd
, stream
->buffer
, BUFSIZE
);
222 stream
->offset
= offset
= 0;
225 c
= stream
->buffer
[offset
++];
227 if (had_cr
&& c
!= '\n')
242 if (!had_backslash
) {
252 warning(stream_pos(stream
), "non-ASCII data stream");
262 stream
->offset
= offset
;
264 warning(stream_pos(stream
), "non-ASCII data stream");
274 warning(stream_pos(stream
), "no newline at end of file");
276 warning(stream_pos(stream
), "non-ASCII data stream");
278 warning(stream_pos(stream
), "backslash-newline at end of file");
283 * We want that as light as possible while covering all normal cases.
284 * Slow path (including the logics with line-splicing and EOF sanity
285 * checks) is in nextchar_slow().
287 static inline int nextchar(stream_t
*stream
)
289 int offset
= stream
->offset
;
291 if (offset
< stream
->size
) {
292 int c
= stream
->buffer
[offset
++];
293 static const char special
[256] = {
294 ['\r'] = 1, ['\n'] = 1, ['\\'] = 1
297 stream
->offset
= offset
;
302 return nextchar_slow(stream
);
305 struct token eof_token_entry
;
307 static void mark_eof(stream_t
*stream
, struct token
*end_token
)
311 end
= alloc_token(stream
);
312 token_type(end
) = TOKEN_STREAMEND
;
313 end
->pos
.newline
= 1;
315 eof_token_entry
.next
= &eof_token_entry
;
316 eof_token_entry
.pos
.newline
= 1;
319 end_token
= &eof_token_entry
;
320 end
->next
= end_token
;
321 *stream
->tokenlist
= end
;
322 stream
->tokenlist
= NULL
;
325 static void add_token(stream_t
*stream
)
327 struct token
*token
= stream
->token
;
329 stream
->token
= NULL
;
331 *stream
->tokenlist
= token
;
332 stream
->tokenlist
= &token
->next
;
335 static void drop_token(stream_t
*stream
)
337 stream
->newline
|= stream
->token
->pos
.newline
;
338 stream
->whitespace
|= stream
->token
->pos
.whitespace
;
339 stream
->token
= NULL
;
351 static const long cclass
[257] = {
352 ['0' + 1 ... '9' + 1] = Digit
| Hex
,
353 ['A' + 1 ... 'D' + 1] = Letter
| Hex
,
354 ['E' + 1] = Letter
| Hex
| Exp
,
355 ['F' + 1] = Letter
| Hex
,
356 ['G' + 1 ... 'O' + 1] = Letter
,
357 ['P' + 1] = Letter
| Exp
,
358 ['Q' + 1 ... 'Z' + 1] = Letter
,
359 ['a' + 1 ... 'd' + 1] = Letter
| Hex
,
360 ['e' + 1] = Letter
| Hex
| Exp
,
361 ['f' + 1] = Letter
| Hex
,
362 ['g' + 1 ... 'o' + 1] = Letter
,
363 ['p' + 1] = Letter
| Exp
,
364 ['q' + 1 ... 'z' + 1] = Letter
,
366 ['.' + 1] = Dot
| ValidSecond
,
367 ['=' + 1] = ValidSecond
,
368 ['+' + 1] = ValidSecond
,
369 ['-' + 1] = ValidSecond
,
370 ['>' + 1] = ValidSecond
,
371 ['<' + 1] = ValidSecond
,
372 ['&' + 1] = ValidSecond
,
373 ['|' + 1] = ValidSecond
,
374 ['#' + 1] = ValidSecond
,
382 * pp-number identifier-nodigit
389 static int get_one_number(int c
, int next
, stream_t
*stream
)
392 static char buffer
[4095];
393 char *p
= buffer
, *buf
, *buffer_end
= buffer
+ sizeof (buffer
);
398 long class = cclass
[next
+ 1];
399 if (!(class & (Dot
| Digit
| Letter
)))
403 next
= nextchar(stream
);
405 if (next
== '-' || next
== '+') {
408 next
= nextchar(stream
);
413 if (p
== buffer_end
) {
414 sparse_error(stream_pos(stream
), "number token exceeds %td characters",
415 buffer_end
- buffer
);
416 // Pretend we saw just "1".
423 buf
= __alloc_bytes(len
);
424 memcpy(buf
, buffer
, len
);
426 token
= stream
->token
;
427 token_type(token
) = TOKEN_NUMBER
;
434 static int escapechar(int first
, int type
, stream_t
*stream
, int *valp
)
438 next
= nextchar(stream
);
442 warning(stream_pos(stream
), "Newline in string or character constant");
444 if (first
== '\\' && next
!= EOF
) {
446 next
= nextchar(stream
);
482 warning(stream_pos(stream
), "Newline in string or character constant");
487 while (next
>= '0' && next
<= '9') {
488 value
= (value
<< 3) + (next
-'0');
489 next
= nextchar(stream
);
497 int hex
= hexval(next
);
500 next
= nextchar(stream
);
501 while ((hex
= hexval(next
)) < 16) {
502 value
= (value
<< 4) + hex
;
503 next
= nextchar(stream
);
511 warning(stream_pos(stream
), "Unknown escape '%c'", value
);
514 /* Mark it as escaped */
521 static int get_char_token(int next
, stream_t
*stream
)
526 next
= escapechar(next
, '\'', stream
, &value
);
527 if (value
== '\'' || next
!= '\'') {
528 sparse_error(stream_pos(stream
), "Bad character constant");
533 token
= stream
->token
;
534 token_type(token
) = TOKEN_CHAR
;
535 token
->character
= value
& 0xff;
538 return nextchar(stream
);
541 static int get_string_token(int next
, stream_t
*stream
)
543 static char buffer
[MAX_STRING
];
544 struct string
*string
;
550 next
= escapechar(next
, '"', stream
, &val
);
554 warning(stream_pos(stream
), "End of file in middle of string");
557 if (len
< MAX_STRING
)
562 if (len
> MAX_STRING
) {
563 warning(stream_pos(stream
), "string too long (%d bytes, %d bytes max)", len
, MAX_STRING
);
567 string
= __alloc_string(len
+1);
568 memcpy(string
->data
, buffer
, len
);
569 string
->data
[len
] = '\0';
570 string
->length
= len
+1;
573 token
= stream
->token
;
574 token_type(token
) = TOKEN_STRING
;
575 token
->string
= string
;
581 static int drop_stream_eoln(stream_t
*stream
)
585 switch (nextchar(stream
)) {
589 return nextchar(stream
);
594 static int drop_stream_comment(stream_t
*stream
)
599 newline
= stream
->newline
;
601 next
= nextchar(stream
);
605 warning(stream_pos(stream
), "End of file in the middle of a comment");
608 next
= nextchar(stream
);
609 if (curr
== '*' && next
== '/')
612 stream
->newline
= newline
;
613 return nextchar(stream
);
616 unsigned char combinations
[][4] = COMBINATION_STRINGS
;
618 #define NR_COMBINATIONS (SPECIAL_ARG_SEPARATOR - SPECIAL_BASE)
620 /* hash function for two-character punctuators - all give unique values */
621 #define special_hash(c0, c1) (((c0*8+c1*2)+((c0*8+c1*2)>>5))&31)
624 * note that we won't get false positives - special_hash(0,0) is 0 and
625 * entry 0 is filled (by +=), so all the missing ones are OK.
627 static unsigned char hash_results
[32][2] = {
628 #define RES(c0, c1) [special_hash(c0, c1)] = {c0, c1}
629 RES('+', '='), /* 00 */
630 RES('/', '='), /* 01 */
631 RES('^', '='), /* 05 */
632 RES('&', '&'), /* 07 */
633 RES('#', '#'), /* 08 */
634 RES('<', '<'), /* 0a */
635 RES('<', '='), /* 0c */
636 RES('!', '='), /* 0e */
637 RES('%', '='), /* 0f */
638 RES('-', '-'), /* 10 */
639 RES('-', '='), /* 11 */
640 RES('-', '>'), /* 13 */
641 RES('=', '='), /* 15 */
642 RES('&', '='), /* 17 */
643 RES('*', '='), /* 18 */
644 RES('.', '.'), /* 1a */
645 RES('+', '+'), /* 1b */
646 RES('|', '='), /* 1c */
647 RES('>', '='), /* 1d */
648 RES('|', '|'), /* 1e */
649 RES('>', '>') /* 1f */
652 static int code
[32] = {
653 #define CODE(c0, c1, value) [special_hash(c0, c1)] = value
654 CODE('+', '=', SPECIAL_ADD_ASSIGN
), /* 00 */
655 CODE('/', '=', SPECIAL_DIV_ASSIGN
), /* 01 */
656 CODE('^', '=', SPECIAL_XOR_ASSIGN
), /* 05 */
657 CODE('&', '&', SPECIAL_LOGICAL_AND
), /* 07 */
658 CODE('#', '#', SPECIAL_HASHHASH
), /* 08 */
659 CODE('<', '<', SPECIAL_LEFTSHIFT
), /* 0a */
660 CODE('<', '=', SPECIAL_LTE
), /* 0c */
661 CODE('!', '=', SPECIAL_NOTEQUAL
), /* 0e */
662 CODE('%', '=', SPECIAL_MOD_ASSIGN
), /* 0f */
663 CODE('-', '-', SPECIAL_DECREMENT
), /* 10 */
664 CODE('-', '=', SPECIAL_SUB_ASSIGN
), /* 11 */
665 CODE('-', '>', SPECIAL_DEREFERENCE
), /* 13 */
666 CODE('=', '=', SPECIAL_EQUAL
), /* 15 */
667 CODE('&', '=', SPECIAL_AND_ASSIGN
), /* 17 */
668 CODE('*', '=', SPECIAL_MUL_ASSIGN
), /* 18 */
669 CODE('.', '.', SPECIAL_DOTDOT
), /* 1a */
670 CODE('+', '+', SPECIAL_INCREMENT
), /* 1b */
671 CODE('|', '=', SPECIAL_OR_ASSIGN
), /* 1c */
672 CODE('>', '=', SPECIAL_GTE
), /* 1d */
673 CODE('|', '|', SPECIAL_LOGICAL_OR
), /* 1e */
674 CODE('>', '>', SPECIAL_RIGHTSHIFT
) /* 1f */
678 static int get_one_special(int c
, stream_t
*stream
)
683 next
= nextchar(stream
);
686 * Check for numbers, strings, character constants, and comments
690 if (next
>= '0' && next
<= '9')
691 return get_one_number(c
, next
, stream
);
694 return get_string_token(next
, stream
);
696 return get_char_token(next
, stream
);
699 return drop_stream_eoln(stream
);
701 return drop_stream_comment(stream
);
705 * Check for combinations
708 if (cclass
[next
+ 1] & ValidSecond
) {
709 i
= special_hash(c
, next
);
710 if (hash_results
[i
][0] == c
&& hash_results
[i
][1] == next
) {
712 next
= nextchar(stream
);
713 if (value
>= SPECIAL_LEFTSHIFT
&&
714 next
== "==."[value
- SPECIAL_LEFTSHIFT
]) {
716 next
= nextchar(stream
);
722 token
= stream
->token
;
723 token_type(token
) = TOKEN_SPECIAL
;
724 token
->special
= value
;
729 #define IDENT_HASH_BITS (13)
730 #define IDENT_HASH_SIZE (1<<IDENT_HASH_BITS)
731 #define IDENT_HASH_MASK (IDENT_HASH_SIZE-1)
733 #define ident_hash_init(c) (c)
734 #define ident_hash_add(oldhash,c) ((oldhash)*11 + (c))
735 #define ident_hash_end(hash) ((((hash) >> IDENT_HASH_BITS) + (hash)) & IDENT_HASH_MASK)
737 static struct ident
*hash_table
[IDENT_HASH_SIZE
];
738 static int ident_hit
, ident_miss
, idents
;
740 void show_identifier_stats(void)
743 int distribution
[100];
745 fprintf(stderr
, "identifiers: %d hits, %d misses\n",
746 ident_hit
, ident_miss
);
748 for (i
= 0; i
< 100; i
++)
751 for (i
= 0; i
< IDENT_HASH_SIZE
; i
++) {
752 struct ident
* ident
= hash_table
[i
];
761 distribution
[count
]++;
764 for (i
= 0; i
< 100; i
++) {
766 fprintf(stderr
, "%2d: %d buckets\n", i
, distribution
[i
]);
770 static struct ident
*alloc_ident(const char *name
, int len
)
772 struct ident
*ident
= __alloc_ident(len
);
773 ident
->symbols
= NULL
;
776 memcpy(ident
->name
, name
, len
);
780 static struct ident
* insert_hash(struct ident
*ident
, unsigned long hash
)
782 ident
->next
= hash_table
[hash
];
783 hash_table
[hash
] = ident
;
788 static struct ident
*create_hashed_ident(const char *name
, int len
, unsigned long hash
)
793 p
= &hash_table
[hash
];
794 while ((ident
= *p
) != NULL
) {
795 if (ident
->len
== (unsigned char) len
) {
796 if (strncmp(name
, ident
->name
, len
) != 0)
806 ident
= alloc_ident(name
, len
);
814 static unsigned long hash_name(const char *name
, int len
)
817 const unsigned char *p
= (const unsigned char *)name
;
819 hash
= ident_hash_init(*p
++);
821 unsigned int i
= *p
++;
822 hash
= ident_hash_add(hash
, i
);
824 return ident_hash_end(hash
);
827 struct ident
*hash_ident(struct ident
*ident
)
829 return insert_hash(ident
, hash_name(ident
->name
, ident
->len
));
832 struct ident
*built_in_ident(const char *name
)
834 int len
= strlen(name
);
835 return create_hashed_ident(name
, len
, hash_name(name
, len
));
838 struct token
*built_in_token(int stream
, const char *name
)
842 token
= __alloc_token(0);
843 token
->pos
.stream
= stream
;
844 token_type(token
) = TOKEN_IDENT
;
845 token
->ident
= built_in_ident(name
);
849 static int get_one_identifier(int c
, stream_t
*stream
)
858 hash
= ident_hash_init(c
);
861 next
= nextchar(stream
);
862 if (!(cclass
[next
+ 1] & (Letter
| Digit
)))
864 if (len
>= sizeof(buf
))
866 hash
= ident_hash_add(hash
, next
);
870 hash
= ident_hash_end(hash
);
872 ident
= create_hashed_ident(buf
, len
, hash
);
875 token
= stream
->token
;
876 token_type(token
) = TOKEN_IDENT
;
877 token
->ident
= ident
;
882 static int get_one_token(int c
, stream_t
*stream
)
884 long class = cclass
[c
+ 1];
886 return get_one_number(c
, nextchar(stream
), stream
);
888 return get_one_identifier(c
, stream
);
889 return get_one_special(c
, stream
);
892 static struct token
*setup_stream(stream_t
*stream
, int idx
, int fd
,
893 unsigned char *buf
, unsigned int buf_size
)
900 stream
->whitespace
= 0;
903 stream
->token
= NULL
;
906 stream
->size
= buf_size
;
907 stream
->buffer
= buf
;
909 begin
= alloc_token(stream
);
910 token_type(begin
) = TOKEN_STREAMBEGIN
;
911 stream
->tokenlist
= &begin
->next
;
915 static void tokenize_stream(stream_t
*stream
, struct token
*endtoken
)
917 int c
= nextchar(stream
);
920 struct token
*token
= alloc_token(stream
);
921 stream
->token
= token
;
923 stream
->whitespace
= 0;
924 c
= get_one_token(c
, stream
);
927 stream
->whitespace
= 1;
928 c
= nextchar(stream
);
930 mark_eof(stream
, endtoken
);
933 struct token
* tokenize_buffer(void *buffer
, unsigned long size
, struct token
*endtoken
)
938 begin
= setup_stream(&stream
, 0, -1, buffer
, size
);
939 tokenize_stream(&stream
, endtoken
);
943 struct token
* tokenize(const char *name
, int fd
, struct token
*endtoken
, const char **next_path
)
947 unsigned char buffer
[BUFSIZE
];
950 idx
= init_stream(name
, fd
, next_path
);
952 // info(endtoken->pos, "File %s is const", name);
956 begin
= setup_stream(&stream
, idx
, fd
, buffer
, 0);
957 tokenize_stream(&stream
, endtoken
);