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
;
28 unsigned int tabstop
= 8;
30 #define BUFSIZE (8192)
35 int newline
, whitespace
;
36 struct token
**tokenlist
;
38 unsigned char *buffer
;
41 const char *stream_name(int stream
)
43 if (stream
< 0 || stream
> input_stream_nr
)
44 return "<bad stream>";
45 return input_streams
[stream
].name
;
48 static struct position
stream_pos(stream_t
*stream
)
52 pos
.stream
= stream
->nr
;
53 pos
.newline
= stream
->newline
;
54 pos
.whitespace
= stream
->whitespace
;
55 pos
.pos
= stream
->pos
;
56 pos
.line
= stream
->line
;
61 const char *show_special(int val
)
63 static char buffer
[4];
67 if (val
>= SPECIAL_BASE
)
68 strcpy(buffer
, (char *) combinations
[val
- SPECIAL_BASE
]);
72 const char *show_ident(const struct ident
*ident
)
74 static char buffer
[256];
77 sprintf(buffer
, "%.*s", ident
->len
, ident
->name
);
81 static char *charstr(char *ptr
, unsigned char c
, unsigned char escape
, unsigned char next
)
84 if (c
== escape
|| c
== '\\')
99 return ptr
+ sprintf(ptr
, "%o", c
);
101 return ptr
+ sprintf(ptr
, "%03o", c
);
104 const char *show_string(const struct string
*string
)
106 static char buffer
[4 * MAX_STRING
+ 3];
111 return "<bad_string>";
114 for (i
= 0; i
< string
->length
-1; i
++) {
115 const char *p
= string
->data
+ i
;
116 ptr
= charstr(ptr
, p
[0], '"', p
[1]);
123 const char *show_token(const struct token
*token
)
125 static char buffer
[256];
129 switch (token_type(token
)) {
131 return "syntax error";
134 return "end-of-input";
137 return show_ident(token
->ident
);
140 return show_string(token
->string
);
143 return token
->number
;
146 return show_special(token
->special
);
150 int c
= token
->character
;
152 ptr
= charstr(ptr
, c
, '\'', 0);
158 case TOKEN_STREAMBEGIN
:
159 sprintf(buffer
, "<beginning of '%s'>", stream_name(token
->pos
.stream
));
162 case TOKEN_STREAMEND
:
163 sprintf(buffer
, "<end of '%s'>", stream_name(token
->pos
.stream
));
167 sprintf(buffer
, "<untaint>");
170 case TOKEN_ARG_COUNT
:
171 sprintf(buffer
, "<argcnt>");
175 sprintf(buffer
, "unhandled token type '%d' ", token_type(token
));
180 int init_stream(const char *name
, int fd
, const char **next_path
)
182 int stream
= input_stream_nr
;
183 struct stream
*current
;
185 if (stream
>= input_streams_allocated
) {
186 int newalloc
= stream
* 4 / 3 + 10;
187 input_streams
= realloc(input_streams
, newalloc
* sizeof(struct stream
));
189 die("Unable to allocate more streams space");
190 input_streams_allocated
= newalloc
;
192 current
= input_streams
+ stream
;
193 memset(current
, 0, sizeof(*current
));
194 current
->name
= name
;
196 current
->next_path
= next_path
;
197 current
->path
= NULL
;
198 current
->constant
= CONSTANT_FILE_MAYBE
;
199 input_stream_nr
= stream
+1;
203 static struct token
* alloc_token(stream_t
*stream
)
205 struct token
*token
= __alloc_token(0);
206 token
->pos
= stream_pos(stream
);
211 * Argh... That was surprisingly messy - handling '\r' complicates the
214 static int nextchar_slow(stream_t
*stream
)
216 int offset
= stream
->offset
;
217 int size
= stream
->size
;
219 int spliced
= 0, had_cr
, had_backslash
, complain
;
222 had_cr
= had_backslash
= complain
= 0;
225 if (offset
>= size
) {
228 size
= read(stream
->fd
, stream
->buffer
, BUFSIZE
);
232 stream
->offset
= offset
= 0;
235 c
= stream
->buffer
[offset
++];
237 if (had_cr
&& c
!= '\n')
245 stream
->pos
+= (c
== '\t') ? (tabstop
- stream
->pos
% tabstop
) : 1;
252 if (!had_backslash
) {
262 warning(stream_pos(stream
), "non-ASCII data stream");
272 stream
->offset
= offset
;
274 warning(stream_pos(stream
), "non-ASCII data stream");
284 warning(stream_pos(stream
), "no newline at end of file");
286 warning(stream_pos(stream
), "non-ASCII data stream");
288 warning(stream_pos(stream
), "backslash-newline at end of file");
293 * We want that as light as possible while covering all normal cases.
294 * Slow path (including the logics with line-splicing and EOF sanity
295 * checks) is in nextchar_slow().
297 static inline int nextchar(stream_t
*stream
)
299 int offset
= stream
->offset
;
301 if (offset
< stream
->size
) {
302 int c
= stream
->buffer
[offset
++];
303 static const char special
[256] = {
304 ['\t'] = 1, ['\r'] = 1, ['\n'] = 1, ['\\'] = 1
307 stream
->offset
= offset
;
312 return nextchar_slow(stream
);
315 struct token eof_token_entry
;
317 static struct token
*mark_eof(stream_t
*stream
)
321 end
= alloc_token(stream
);
322 token_type(end
) = TOKEN_STREAMEND
;
323 end
->pos
.newline
= 1;
325 eof_token_entry
.next
= &eof_token_entry
;
326 eof_token_entry
.pos
.newline
= 1;
328 end
->next
= &eof_token_entry
;
329 *stream
->tokenlist
= end
;
330 stream
->tokenlist
= NULL
;
334 static void add_token(stream_t
*stream
)
336 struct token
*token
= stream
->token
;
338 stream
->token
= NULL
;
340 *stream
->tokenlist
= token
;
341 stream
->tokenlist
= &token
->next
;
344 static void drop_token(stream_t
*stream
)
346 stream
->newline
|= stream
->token
->pos
.newline
;
347 stream
->whitespace
|= stream
->token
->pos
.whitespace
;
348 stream
->token
= NULL
;
360 static const long cclass
[257] = {
361 ['0' + 1 ... '9' + 1] = Digit
| Hex
,
362 ['A' + 1 ... 'D' + 1] = Letter
| Hex
,
363 ['E' + 1] = Letter
| Hex
| Exp
,
364 ['F' + 1] = Letter
| Hex
,
365 ['G' + 1 ... 'O' + 1] = Letter
,
366 ['P' + 1] = Letter
| Exp
,
367 ['Q' + 1 ... 'Z' + 1] = Letter
,
368 ['a' + 1 ... 'd' + 1] = Letter
| Hex
,
369 ['e' + 1] = Letter
| Hex
| Exp
,
370 ['f' + 1] = Letter
| Hex
,
371 ['g' + 1 ... 'o' + 1] = Letter
,
372 ['p' + 1] = Letter
| Exp
,
373 ['q' + 1 ... 'z' + 1] = Letter
,
375 ['.' + 1] = Dot
| ValidSecond
,
376 ['=' + 1] = ValidSecond
,
377 ['+' + 1] = ValidSecond
,
378 ['-' + 1] = ValidSecond
,
379 ['>' + 1] = ValidSecond
,
380 ['<' + 1] = ValidSecond
,
381 ['&' + 1] = ValidSecond
,
382 ['|' + 1] = ValidSecond
,
383 ['#' + 1] = ValidSecond
,
391 * pp-number identifier-nodigit
398 static int get_one_number(int c
, int next
, stream_t
*stream
)
401 static char buffer
[4095];
402 char *p
= buffer
, *buf
, *buffer_end
= buffer
+ sizeof (buffer
);
407 long class = cclass
[next
+ 1];
408 if (!(class & (Dot
| Digit
| Letter
)))
412 next
= nextchar(stream
);
414 if (next
== '-' || next
== '+') {
417 next
= nextchar(stream
);
422 if (p
== buffer_end
) {
423 sparse_error(stream_pos(stream
), "number token exceeds %td characters",
424 buffer_end
- buffer
);
425 // Pretend we saw just "1".
432 buf
= __alloc_bytes(len
);
433 memcpy(buf
, buffer
, len
);
435 token
= stream
->token
;
436 token_type(token
) = TOKEN_NUMBER
;
443 static int escapechar(int first
, int type
, stream_t
*stream
, int *valp
)
447 next
= nextchar(stream
);
451 warning(stream_pos(stream
), "Newline in string or character constant");
453 if (first
== '\\' && next
!= EOF
) {
455 next
= nextchar(stream
);
491 warning(stream_pos(stream
), "Newline in string or character constant");
496 while (next
>= '0' && next
<= '9') {
497 value
= (value
<< 3) + (next
-'0');
498 next
= nextchar(stream
);
506 int hex
= hexval(next
);
509 next
= nextchar(stream
);
510 while ((hex
= hexval(next
)) < 16) {
511 value
= (value
<< 4) + hex
;
512 next
= nextchar(stream
);
520 warning(stream_pos(stream
), "Unknown escape '%c'", value
);
523 /* Mark it as escaped */
530 static int get_char_token(int next
, stream_t
*stream
)
535 next
= escapechar(next
, '\'', stream
, &value
);
536 if (value
== '\'' || next
!= '\'') {
537 sparse_error(stream_pos(stream
), "Bad character constant");
542 token
= stream
->token
;
543 token_type(token
) = TOKEN_CHAR
;
544 token
->character
= value
& 0xff;
547 return nextchar(stream
);
550 static int get_string_token(int next
, stream_t
*stream
)
552 static char buffer
[MAX_STRING
];
553 struct string
*string
;
559 next
= escapechar(next
, '"', stream
, &val
);
563 warning(stream_pos(stream
), "End of file in middle of string");
566 if (len
< MAX_STRING
)
571 if (len
> MAX_STRING
) {
572 warning(stream_pos(stream
), "string too long (%d bytes, %d bytes max)", len
, MAX_STRING
);
576 string
= __alloc_string(len
+1);
577 memcpy(string
->data
, buffer
, len
);
578 string
->data
[len
] = '\0';
579 string
->length
= len
+1;
582 token
= stream
->token
;
583 token_type(token
) = TOKEN_STRING
;
584 token
->string
= string
;
590 static int drop_stream_eoln(stream_t
*stream
)
594 switch (nextchar(stream
)) {
598 return nextchar(stream
);
603 static int drop_stream_comment(stream_t
*stream
)
608 newline
= stream
->newline
;
610 next
= nextchar(stream
);
614 warning(stream_pos(stream
), "End of file in the middle of a comment");
617 next
= nextchar(stream
);
618 if (curr
== '*' && next
== '/')
621 stream
->newline
= newline
;
622 return nextchar(stream
);
625 unsigned char combinations
[][4] = COMBINATION_STRINGS
;
627 #define NR_COMBINATIONS (SPECIAL_ARG_SEPARATOR - SPECIAL_BASE)
629 /* hash function for two-character punctuators - all give unique values */
630 #define special_hash(c0, c1) (((c0*8+c1*2)+((c0*8+c1*2)>>5))&31)
633 * note that we won't get false positives - special_hash(0,0) is 0 and
634 * entry 0 is filled (by +=), so all the missing ones are OK.
636 static unsigned char hash_results
[32][2] = {
637 #define RES(c0, c1) [special_hash(c0, c1)] = {c0, c1}
638 RES('+', '='), /* 00 */
639 RES('/', '='), /* 01 */
640 RES('^', '='), /* 05 */
641 RES('&', '&'), /* 07 */
642 RES('#', '#'), /* 08 */
643 RES('<', '<'), /* 0a */
644 RES('<', '='), /* 0c */
645 RES('!', '='), /* 0e */
646 RES('%', '='), /* 0f */
647 RES('-', '-'), /* 10 */
648 RES('-', '='), /* 11 */
649 RES('-', '>'), /* 13 */
650 RES('=', '='), /* 15 */
651 RES('&', '='), /* 17 */
652 RES('*', '='), /* 18 */
653 RES('.', '.'), /* 1a */
654 RES('+', '+'), /* 1b */
655 RES('|', '='), /* 1c */
656 RES('>', '='), /* 1d */
657 RES('|', '|'), /* 1e */
658 RES('>', '>') /* 1f */
661 static int code
[32] = {
662 #define CODE(c0, c1, value) [special_hash(c0, c1)] = value
663 CODE('+', '=', SPECIAL_ADD_ASSIGN
), /* 00 */
664 CODE('/', '=', SPECIAL_DIV_ASSIGN
), /* 01 */
665 CODE('^', '=', SPECIAL_XOR_ASSIGN
), /* 05 */
666 CODE('&', '&', SPECIAL_LOGICAL_AND
), /* 07 */
667 CODE('#', '#', SPECIAL_HASHHASH
), /* 08 */
668 CODE('<', '<', SPECIAL_LEFTSHIFT
), /* 0a */
669 CODE('<', '=', SPECIAL_LTE
), /* 0c */
670 CODE('!', '=', SPECIAL_NOTEQUAL
), /* 0e */
671 CODE('%', '=', SPECIAL_MOD_ASSIGN
), /* 0f */
672 CODE('-', '-', SPECIAL_DECREMENT
), /* 10 */
673 CODE('-', '=', SPECIAL_SUB_ASSIGN
), /* 11 */
674 CODE('-', '>', SPECIAL_DEREFERENCE
), /* 13 */
675 CODE('=', '=', SPECIAL_EQUAL
), /* 15 */
676 CODE('&', '=', SPECIAL_AND_ASSIGN
), /* 17 */
677 CODE('*', '=', SPECIAL_MUL_ASSIGN
), /* 18 */
678 CODE('.', '.', SPECIAL_DOTDOT
), /* 1a */
679 CODE('+', '+', SPECIAL_INCREMENT
), /* 1b */
680 CODE('|', '=', SPECIAL_OR_ASSIGN
), /* 1c */
681 CODE('>', '=', SPECIAL_GTE
), /* 1d */
682 CODE('|', '|', SPECIAL_LOGICAL_OR
), /* 1e */
683 CODE('>', '>', SPECIAL_RIGHTSHIFT
) /* 1f */
687 static int get_one_special(int c
, stream_t
*stream
)
692 next
= nextchar(stream
);
695 * Check for numbers, strings, character constants, and comments
699 if (next
>= '0' && next
<= '9')
700 return get_one_number(c
, next
, stream
);
703 return get_string_token(next
, stream
);
705 return get_char_token(next
, stream
);
708 return drop_stream_eoln(stream
);
710 return drop_stream_comment(stream
);
714 * Check for combinations
717 if (cclass
[next
+ 1] & ValidSecond
) {
718 i
= special_hash(c
, next
);
719 if (hash_results
[i
][0] == c
&& hash_results
[i
][1] == next
) {
721 next
= nextchar(stream
);
722 if (value
>= SPECIAL_LEFTSHIFT
&&
723 next
== "==."[value
- SPECIAL_LEFTSHIFT
]) {
725 next
= nextchar(stream
);
731 token
= stream
->token
;
732 token_type(token
) = TOKEN_SPECIAL
;
733 token
->special
= value
;
738 #define IDENT_HASH_BITS (13)
739 #define IDENT_HASH_SIZE (1<<IDENT_HASH_BITS)
740 #define IDENT_HASH_MASK (IDENT_HASH_SIZE-1)
742 #define ident_hash_init(c) (c)
743 #define ident_hash_add(oldhash,c) ((oldhash)*11 + (c))
744 #define ident_hash_end(hash) ((((hash) >> IDENT_HASH_BITS) + (hash)) & IDENT_HASH_MASK)
746 static struct ident
*hash_table
[IDENT_HASH_SIZE
];
747 static int ident_hit
, ident_miss
, idents
;
749 void show_identifier_stats(void)
752 int distribution
[100];
754 fprintf(stderr
, "identifiers: %d hits, %d misses\n",
755 ident_hit
, ident_miss
);
757 for (i
= 0; i
< 100; i
++)
760 for (i
= 0; i
< IDENT_HASH_SIZE
; i
++) {
761 struct ident
* ident
= hash_table
[i
];
770 distribution
[count
]++;
773 for (i
= 0; i
< 100; i
++) {
775 fprintf(stderr
, "%2d: %d buckets\n", i
, distribution
[i
]);
779 static struct ident
*alloc_ident(const char *name
, int len
)
781 struct ident
*ident
= __alloc_ident(len
);
782 ident
->symbols
= NULL
;
785 memcpy(ident
->name
, name
, len
);
789 static struct ident
* insert_hash(struct ident
*ident
, unsigned long hash
)
791 ident
->next
= hash_table
[hash
];
792 hash_table
[hash
] = ident
;
797 static struct ident
*create_hashed_ident(const char *name
, int len
, unsigned long hash
)
802 p
= &hash_table
[hash
];
803 while ((ident
= *p
) != NULL
) {
804 if (ident
->len
== (unsigned char) len
) {
805 if (strncmp(name
, ident
->name
, len
) != 0)
815 ident
= alloc_ident(name
, len
);
823 static unsigned long hash_name(const char *name
, int len
)
826 const unsigned char *p
= (const unsigned char *)name
;
828 hash
= ident_hash_init(*p
++);
830 unsigned int i
= *p
++;
831 hash
= ident_hash_add(hash
, i
);
833 return ident_hash_end(hash
);
836 struct ident
*hash_ident(struct ident
*ident
)
838 return insert_hash(ident
, hash_name(ident
->name
, ident
->len
));
841 struct ident
*built_in_ident(const char *name
)
843 int len
= strlen(name
);
844 return create_hashed_ident(name
, len
, hash_name(name
, len
));
847 struct token
*built_in_token(int stream
, const char *name
)
851 token
= __alloc_token(0);
852 token
->pos
.stream
= stream
;
853 token_type(token
) = TOKEN_IDENT
;
854 token
->ident
= built_in_ident(name
);
858 static int get_one_identifier(int c
, stream_t
*stream
)
867 hash
= ident_hash_init(c
);
870 next
= nextchar(stream
);
871 if (!(cclass
[next
+ 1] & (Letter
| Digit
)))
873 if (len
>= sizeof(buf
))
875 hash
= ident_hash_add(hash
, next
);
879 hash
= ident_hash_end(hash
);
881 ident
= create_hashed_ident(buf
, len
, hash
);
884 token
= stream
->token
;
885 token_type(token
) = TOKEN_IDENT
;
886 token
->ident
= ident
;
891 static int get_one_token(int c
, stream_t
*stream
)
893 long class = cclass
[c
+ 1];
895 return get_one_number(c
, nextchar(stream
), stream
);
897 return get_one_identifier(c
, stream
);
898 return get_one_special(c
, stream
);
901 static struct token
*setup_stream(stream_t
*stream
, int idx
, int fd
,
902 unsigned char *buf
, unsigned int buf_size
)
909 stream
->whitespace
= 0;
912 stream
->token
= NULL
;
915 stream
->size
= buf_size
;
916 stream
->buffer
= buf
;
918 begin
= alloc_token(stream
);
919 token_type(begin
) = TOKEN_STREAMBEGIN
;
920 stream
->tokenlist
= &begin
->next
;
924 static struct token
*tokenize_stream(stream_t
*stream
)
926 int c
= nextchar(stream
);
929 struct token
*token
= alloc_token(stream
);
930 stream
->token
= token
;
932 stream
->whitespace
= 0;
933 c
= get_one_token(c
, stream
);
936 stream
->whitespace
= 1;
937 c
= nextchar(stream
);
939 return mark_eof(stream
);
942 struct token
* tokenize_buffer(void *buffer
, unsigned long size
, struct token
**endtoken
)
947 begin
= setup_stream(&stream
, 0, -1, buffer
, size
);
948 *endtoken
= tokenize_stream(&stream
);
952 struct token
* tokenize(const char *name
, int fd
, struct token
*endtoken
, const char **next_path
)
954 struct token
*begin
, *end
;
956 unsigned char buffer
[BUFSIZE
];
959 idx
= init_stream(name
, fd
, next_path
);
961 // info(endtoken->pos, "File %s is const", name);
965 begin
= setup_stream(&stream
, idx
, fd
, buffer
, 0);
966 end
= tokenize_stream(&stream
);
968 end
->next
= endtoken
;