2 * This is a really stupid C tokenizer. It doesn't do any include
3 * files or anything complex at all. That's the pre-processor.
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 const char *combinations
[] = COMBINATION_STRINGS
;
63 static char buffer
[4];
67 if (val
>= SPECIAL_BASE
)
68 strcpy(buffer
, 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
));
171 int init_stream(const char *name
, int fd
, const char **next_path
)
173 int stream
= input_stream_nr
;
174 struct stream
*current
;
176 if (stream
>= input_streams_allocated
) {
177 int newalloc
= stream
* 4 / 3 + 10;
178 input_streams
= realloc(input_streams
, newalloc
* sizeof(struct stream
));
180 die("Unable to allocate more streams space");
181 input_streams_allocated
= newalloc
;
183 current
= input_streams
+ stream
;
184 memset(current
, 0, sizeof(*current
));
185 current
->name
= name
;
187 current
->next_path
= next_path
;
188 current
->path
= NULL
;
189 current
->constant
= CONSTANT_FILE_MAYBE
;
190 input_stream_nr
= stream
+1;
194 static struct token
* alloc_token(stream_t
*stream
)
196 struct token
*token
= __alloc_token(0);
197 token
->pos
= stream_pos(stream
);
202 * Argh... That was surprisingly messy - handling '\r' complicates the
205 static int nextchar_slow(stream_t
*stream
)
207 int offset
= stream
->offset
;
208 int size
= stream
->size
;
210 int spliced
= 0, had_cr
, had_backslash
, complain
;
213 had_cr
= had_backslash
= complain
= 0;
216 if (offset
>= size
) {
217 size
= read(stream
->fd
, stream
->buffer
, BUFSIZE
);
221 stream
->offset
= offset
= 0;
224 c
= stream
->buffer
[offset
++];
226 if (had_cr
&& c
!= '\n')
241 if (!had_backslash
) {
251 warning(stream_pos(stream
), "non-ASCII data stream");
261 stream
->offset
= offset
;
263 warning(stream_pos(stream
), "non-ASCII data stream");
273 warning(stream_pos(stream
), "no newline at end of file");
275 warning(stream_pos(stream
), "non-ASCII data stream");
277 warning(stream_pos(stream
), "backslash-newline at end of file");
282 * We want that as light as possible while covering all normal cases.
283 * Slow path (including the logics with line-splicing and EOF sanity
284 * checks) is in nextchar_slow().
286 static int nextchar(stream_t
*stream
)
288 int offset
= stream
->offset
;
290 if (offset
< stream
->size
) {
291 int c
= stream
->buffer
[offset
++];
292 static const char special
[256] = {
293 ['\r'] = 1, ['\n'] = 1, ['\\'] = 1
296 stream
->offset
= offset
;
301 return nextchar_slow(stream
);
304 struct token eof_token_entry
;
306 static void mark_eof(stream_t
*stream
, struct token
*end_token
)
310 end
= alloc_token(stream
);
311 token_type(end
) = TOKEN_STREAMEND
;
312 end
->pos
.newline
= 1;
314 eof_token_entry
.next
= &eof_token_entry
;
315 eof_token_entry
.pos
.newline
= 1;
318 end_token
= &eof_token_entry
;
319 end
->next
= end_token
;
320 *stream
->tokenlist
= end
;
321 stream
->tokenlist
= NULL
;
324 static void add_token(stream_t
*stream
)
326 struct token
*token
= stream
->token
;
328 stream
->token
= NULL
;
330 *stream
->tokenlist
= token
;
331 stream
->tokenlist
= &token
->next
;
334 static void drop_token(stream_t
*stream
)
336 stream
->newline
|= stream
->token
->pos
.newline
;
337 stream
->whitespace
|= stream
->token
->pos
.whitespace
;
338 stream
->token
= NULL
;
350 static const long cclass
[257] = {
351 ['0' + 1 ... '9' + 1] = Digit
| Hex
,
352 ['A' + 1 ... 'D' + 1] = Letter
| Hex
,
353 ['E' + 1] = Letter
| Hex
| Exp
,
354 ['F' + 1] = Letter
| Hex
,
355 ['G' + 1 ... 'O' + 1] = Letter
,
356 ['P' + 1] = Letter
| Exp
,
357 ['Q' + 1 ... 'Z' + 1] = Letter
,
358 ['a' + 1 ... 'd' + 1] = Letter
| Hex
,
359 ['e' + 1] = Letter
| Hex
| Exp
,
360 ['f' + 1] = Letter
| Hex
,
361 ['g' + 1 ... 'o' + 1] = Letter
,
362 ['p' + 1] = Letter
| Exp
,
363 ['q' + 1 ... 'z' + 1] = Letter
,
365 ['.' + 1] = Dot
| ValidSecond
,
366 ['=' + 1] = ValidSecond
,
367 ['+' + 1] = ValidSecond
,
368 ['-' + 1] = ValidSecond
,
369 ['>' + 1] = ValidSecond
,
370 ['<' + 1] = ValidSecond
,
371 ['&' + 1] = ValidSecond
,
372 ['|' + 1] = ValidSecond
,
373 ['#' + 1] = ValidSecond
,
381 * pp-number identifier-nodigit
388 static int get_one_number(int c
, int next
, stream_t
*stream
)
391 static char buffer
[4095];
392 char *p
= buffer
, *buf
, *buffer_end
= buffer
+ sizeof (buffer
);
397 long class = cclass
[next
+ 1];
398 if (!(class & (Dot
| Digit
| Letter
)))
402 next
= nextchar(stream
);
404 if (next
== '-' || next
== '+') {
407 next
= nextchar(stream
);
412 if (p
== buffer_end
) {
413 error(stream_pos(stream
), "number token exceeds %td characters",
414 buffer_end
- buffer
);
415 // Pretend we saw just "1".
422 buf
= __alloc_bytes(len
);
423 memcpy(buf
, buffer
, len
);
425 token
= stream
->token
;
426 token_type(token
) = TOKEN_NUMBER
;
433 static int escapechar(int first
, int type
, stream_t
*stream
, int *valp
)
437 next
= nextchar(stream
);
441 warning(stream_pos(stream
), "Newline in string or character constant");
443 if (first
== '\\' && next
!= EOF
) {
445 next
= nextchar(stream
);
479 warning(stream_pos(stream
), "Newline in string or character constant");
484 while (next
>= '0' && next
<= '9') {
485 value
= (value
<< 3) + (next
-'0');
486 next
= nextchar(stream
);
494 int hex
= hexval(next
);
497 next
= nextchar(stream
);
498 while ((hex
= hexval(next
)) < 16) {
499 value
= (value
<< 4) + hex
;
500 next
= nextchar(stream
);
508 warning(stream_pos(stream
), "Unknown escape '%c'", value
);
511 /* Mark it as escaped */
518 static int get_char_token(int next
, stream_t
*stream
)
523 next
= escapechar(next
, '\'', stream
, &value
);
524 if (value
== '\'' || next
!= '\'') {
525 warning(stream_pos(stream
), "Bad character constant");
530 token
= stream
->token
;
531 token_type(token
) = TOKEN_CHAR
;
532 token
->character
= value
& 0xff;
535 return nextchar(stream
);
538 static int get_string_token(int next
, stream_t
*stream
)
540 static char buffer
[MAX_STRING
];
541 struct string
*string
;
547 next
= escapechar(next
, '"', stream
, &val
);
551 warning(stream_pos(stream
), "End of file in middle of string");
554 if (len
< MAX_STRING
)
559 if (len
> MAX_STRING
) {
560 warning(stream_pos(stream
), "string too long (%d bytes, %d bytes max)", len
, MAX_STRING
);
564 string
= __alloc_string(len
+1);
565 memcpy(string
->data
, buffer
, len
);
566 string
->data
[len
] = '\0';
567 string
->length
= len
+1;
570 token
= stream
->token
;
571 token_type(token
) = TOKEN_STRING
;
572 token
->string
= string
;
578 static int drop_stream_eoln(stream_t
*stream
)
580 int next
= nextchar(stream
);
586 next
= nextchar(stream
);
592 static int drop_stream_comment(stream_t
*stream
)
597 newline
= stream
->newline
;
599 next
= nextchar(stream
);
603 warning(stream_pos(stream
), "End of file in the middle of a comment");
606 next
= nextchar(stream
);
607 if (curr
== '*' && next
== '/')
610 stream
->newline
= newline
;
611 return nextchar(stream
);
614 unsigned char combinations
[][3] = COMBINATION_STRINGS
;
616 #define NR_COMBINATIONS (SPECIAL_ARG_SEPARATOR - SPECIAL_BASE)
618 static int get_one_special(int c
, stream_t
*stream
)
621 unsigned char c1
, c2
, c3
;
625 next
= nextchar(stream
);
628 * Check for numbers, strings, character constants, and comments
632 if (next
>= '0' && next
<= '9')
633 return get_one_number(c
, next
, stream
);
636 return get_string_token(next
, stream
);
638 return get_char_token(next
, stream
);
641 return drop_stream_eoln(stream
);
643 return drop_stream_comment(stream
);
647 * Check for combinations
650 if (cclass
[next
+ 1] & ValidSecond
) {
651 comb
= combinations
[0];
652 c1
= c
; c2
= next
; c3
= 0;
653 for (i
= 0; i
< NR_COMBINATIONS
; i
++) {
654 if (comb
[0] == c1
&& comb
[1] == c2
&& comb
[2] == c3
) {
655 value
= i
+ SPECIAL_BASE
;
656 next
= nextchar(stream
);
666 token
= stream
->token
;
667 token_type(token
) = TOKEN_SPECIAL
;
668 token
->special
= value
;
673 #define IDENT_HASH_BITS (13)
674 #define IDENT_HASH_SIZE (1<<IDENT_HASH_BITS)
675 #define IDENT_HASH_MASK (IDENT_HASH_SIZE-1)
677 #define ident_hash_init(c) (c)
678 #define ident_hash_add(oldhash,c) ((oldhash)*11 + (c))
679 #define ident_hash_end(hash) ((((hash) >> IDENT_HASH_BITS) + (hash)) & IDENT_HASH_MASK)
681 static struct ident
*hash_table
[IDENT_HASH_SIZE
];
682 static int ident_hit
, ident_miss
, idents
;
684 void show_identifier_stats(void)
687 int distribution
[100];
689 fprintf(stderr
, "identifiers: %d hits, %d misses\n",
690 ident_hit
, ident_miss
);
692 for (i
= 0; i
< 100; i
++)
695 for (i
= 0; i
< IDENT_HASH_SIZE
; i
++) {
696 struct ident
* ident
= hash_table
[i
];
705 distribution
[count
]++;
708 for (i
= 0; i
< 100; i
++) {
710 fprintf(stderr
, "%2d: %d buckets\n", i
, distribution
[i
]);
714 static struct ident
*alloc_ident(const char *name
, int len
)
716 struct ident
*ident
= __alloc_ident(len
);
717 ident
->symbols
= NULL
;
720 memcpy(ident
->name
, name
, len
);
724 static struct ident
* insert_hash(struct ident
*ident
, unsigned long hash
)
726 ident
->next
= hash_table
[hash
];
727 hash_table
[hash
] = ident
;
732 static struct ident
*create_hashed_ident(const char *name
, int len
, unsigned long hash
)
737 p
= &hash_table
[hash
];
738 while ((ident
= *p
) != NULL
) {
739 if (ident
->len
== (unsigned char) len
) {
740 const char *n
= name
;
741 const char *m
= ident
->name
;
757 ident
= alloc_ident(name
, len
);
765 static unsigned long hash_name(const char *name
, int len
)
768 const unsigned char *p
= (const unsigned char *)name
;
770 hash
= ident_hash_init(*p
++);
772 unsigned int i
= *p
++;
773 hash
= ident_hash_add(hash
, i
);
775 return ident_hash_end(hash
);
778 struct ident
*hash_ident(struct ident
*ident
)
780 return insert_hash(ident
, hash_name(ident
->name
, ident
->len
));
783 struct ident
*built_in_ident(const char *name
)
785 int len
= strlen(name
);
786 return create_hashed_ident(name
, len
, hash_name(name
, len
));
789 struct token
*built_in_token(int stream
, const char *name
)
793 token
= __alloc_token(0);
794 token
->pos
.stream
= stream
;
795 token_type(token
) = TOKEN_IDENT
;
796 token
->ident
= built_in_ident(name
);
800 static int get_one_identifier(int c
, stream_t
*stream
)
809 hash
= ident_hash_init(c
);
812 next
= nextchar(stream
);
813 if (!(cclass
[next
+ 1] & (Letter
| Digit
)))
815 if (len
>= sizeof(buf
))
817 hash
= ident_hash_add(hash
, next
);
821 hash
= ident_hash_end(hash
);
823 ident
= create_hashed_ident(buf
, len
, hash
);
826 token
= stream
->token
;
827 token_type(token
) = TOKEN_IDENT
;
828 token
->ident
= ident
;
833 static int get_one_token(int c
, stream_t
*stream
)
835 long class = cclass
[c
+ 1];
837 return get_one_number(c
, nextchar(stream
), stream
);
839 return get_one_identifier(c
, stream
);
840 return get_one_special(c
, stream
);
843 static struct token
*setup_stream(stream_t
*stream
, int idx
, int fd
,
844 unsigned char *buf
, unsigned int buf_size
)
851 stream
->whitespace
= 0;
854 stream
->token
= NULL
;
857 stream
->size
= buf_size
;
858 stream
->buffer
= buf
;
860 begin
= alloc_token(stream
);
861 token_type(begin
) = TOKEN_STREAMBEGIN
;
862 stream
->tokenlist
= &begin
->next
;
866 static void tokenize_stream(stream_t
*stream
, struct token
*endtoken
)
868 int c
= nextchar(stream
);
871 struct token
*token
= alloc_token(stream
);
872 stream
->token
= token
;
874 stream
->whitespace
= 0;
875 c
= get_one_token(c
, stream
);
878 stream
->whitespace
= 1;
879 c
= nextchar(stream
);
881 mark_eof(stream
, endtoken
);
884 struct token
* tokenize_buffer(void *buffer
, unsigned long size
, struct token
*endtoken
)
889 begin
= setup_stream(&stream
, 0, -1, buffer
, size
);
890 tokenize_stream(&stream
, endtoken
);
894 struct token
* tokenize(const char *name
, int fd
, struct token
*endtoken
, const char **next_path
)
898 unsigned char buffer
[BUFSIZE
];
901 idx
= init_stream(name
, fd
, next_path
);
903 // info(endtoken->pos, "File %s is const", name);
907 begin
= setup_stream(&stream
, idx
, fd
, buffer
, 0);
908 tokenize_stream(&stream
, endtoken
);