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.
7 * Licensed under the Open Software License version 1.1
24 int input_stream_nr
= 0;
25 struct stream
*input_streams
;
26 static int input_streams_allocated
;
28 #define BUFSIZE (8192)
33 struct token
**tokenlist
;
35 unsigned char *buffer
;
39 const char *show_special(int val
)
41 static const char *combinations
[] = COMBINATION_STRINGS
;
42 static char buffer
[4];
46 if (val
>= SPECIAL_BASE
)
47 strcpy(buffer
, combinations
[val
- SPECIAL_BASE
]);
51 const char *show_ident(const struct ident
*ident
)
53 static char buffer
[256];
56 sprintf(buffer
, "%.*s", ident
->len
, ident
->name
);
60 char *charstr(char *ptr
, unsigned char c
, unsigned char escape
, unsigned char next
)
63 if (c
== escape
|| c
== '\\')
78 return ptr
+ sprintf(ptr
, "%o", c
);
80 return ptr
+ sprintf(ptr
, "%03o", c
);
83 const char *show_string(const struct string
*string
)
85 static char buffer
[256];
91 for (i
= 0; i
< string
->length
-1; i
++) {
92 const unsigned char *p
= string
->data
+ i
;
93 ptr
= charstr(ptr
, p
[0], '"', p
[1]);
100 const char *show_token(const struct token
*token
)
102 static char buffer
[256];
106 switch (token_type(token
)) {
108 return "syntax error";
111 return "end-of-input";
114 return show_ident(token
->ident
);
117 return show_string(token
->string
);
119 case TOKEN_INTEGER
: {
120 const char *p
= token
->integer
;
125 strcpy(buffer
+1, p
+1);
136 return show_special(token
->special
);
140 int c
= token
->character
;
142 ptr
= charstr(ptr
, c
, '\'', 0);
148 case TOKEN_STREAMBEGIN
:
149 sprintf(buffer
, "<beginning of '%s'>", (input_streams
+ token
->pos
.stream
)->name
);
152 case TOKEN_STREAMEND
:
153 sprintf(buffer
, "<end of '%s'>", (input_streams
+ token
->pos
.stream
)->name
);
161 int init_stream(const char *name
, int fd
)
163 int stream
= input_stream_nr
;
164 struct stream
*current
;
166 if (stream
>= input_streams_allocated
) {
167 int newalloc
= stream
* 4 / 3 + 10;
168 input_streams
= realloc(input_streams
, newalloc
* sizeof(struct stream
));
170 die("Unable to allocate more streams space");
171 input_streams_allocated
= newalloc
;
173 current
= input_streams
+ stream
;
174 memset(current
, 0, sizeof(*current
));
175 current
->name
= name
;
177 current
->constant
= -1; // "unknown"
183 current
->dev
= st
.st_dev
;
184 current
->ino
= st
.st_ino
;
185 for (i
= 0; i
< stream
; i
++) {
186 struct stream
*s
= input_streams
+ i
;
187 if (s
->dev
== st
.st_dev
&& s
->ino
== st
.st_ino
) {
188 if (s
->constant
> 0 && lookup_symbol(s
->protect
, NS_PREPROCESSOR
))
193 input_stream_nr
= stream
+1;
197 static struct token
* alloc_token(stream_t
*stream
)
199 struct token
*token
= __alloc_token(0);
200 token
->pos
= stream
->pos
;
204 static int nextchar(stream_t
*stream
)
206 int offset
= stream
->offset
;
207 int size
= stream
->size
;
210 if (offset
>= size
) {
211 size
= read(stream
->fd
, stream
->buffer
, BUFSIZE
);
218 c
= stream
->buffer
[offset
];
219 stream
->offset
= offset
+ 1;
223 stream
->pos
.newline
= 1;
229 struct token eof_token_entry
;
231 static void mark_eof(stream_t
*stream
, struct token
*end_token
)
235 end
= alloc_token(stream
);
236 token_type(end
) = TOKEN_STREAMEND
;
237 end
->pos
.newline
= 1;
239 eof_token_entry
.next
= &eof_token_entry
;
240 eof_token_entry
.pos
.newline
= 1;
243 end_token
= &eof_token_entry
;
244 end
->next
= end_token
;
245 *stream
->tokenlist
= end
;
246 stream
->tokenlist
= NULL
;
249 static void add_token(stream_t
*stream
)
251 struct token
*token
= stream
->token
;
253 stream
->token
= NULL
;
255 *stream
->tokenlist
= token
;
256 stream
->tokenlist
= &token
->next
;
259 static void drop_token(stream_t
*stream
)
261 stream
->pos
.newline
|= stream
->token
->pos
.newline
;
262 stream
->pos
.whitespace
|= stream
->token
->pos
.whitespace
;
263 stream
->token
= NULL
;
266 static int get_base_number(unsigned int base
, char **p
, int next
, stream_t
*stream
)
273 next
= nextchar(stream
);
283 static int do_integer(char *buffer
, int len
, int next
, stream_t
*stream
)
285 struct token
*token
= stream
->token
;
288 while (next
== 'u' || next
== 'U' || next
== 'l' || next
== 'L') {
289 buffer
[len
++] = next
;
290 next
= nextchar(stream
);
292 buffer
[len
++] = '\0';
293 buf
= __alloc_bytes(len
);
294 memcpy(buf
, buffer
, len
);
295 token_type(token
) = TOKEN_INTEGER
;
296 token
->integer
= buf
;
301 static int get_one_number(int c
, stream_t
*stream
)
303 static char buffer
[256];
304 int next
= nextchar(stream
);
312 next
= get_base_number(8, &p
, next
, stream
);
317 next
= get_base_number(10, &p
, next
, stream
);
322 next
= get_base_number(16, &p
, next
, stream
);
325 return do_integer(buffer
, p
- buffer
, next
, stream
);
328 static int escapechar(int first
, int type
, stream_t
*stream
, int *valp
)
332 next
= nextchar(stream
);
336 warn(stream
->pos
, "Newline in string or character constant");
338 if (first
== '\\' && next
!= EOF
) {
340 next
= nextchar(stream
);
358 while (next
>= '0' && next
<= '9') {
359 value
= (value
<< 3) + (next
-'0');
360 next
= nextchar(stream
);
368 int hex
= hexval(next
);
371 next
= nextchar(stream
);
372 while ((hex
= hexval(next
)) < 16) {
373 value
= (value
<< 4) + hex
;
374 next
= nextchar(stream
);
382 warn(stream
->pos
, "Unknown escape '%c'", value
);
385 /* Mark it as escaped */
392 static int get_char_token(int next
, stream_t
*stream
)
397 next
= escapechar(next
, '\'', stream
, &value
);
398 if (value
== '\'' || next
!= '\'') {
399 warn(stream
->pos
, "Bad character constant");
404 token
= stream
->token
;
405 token_type(token
) = TOKEN_CHAR
;
406 token
->character
= value
& 0xff;
409 return nextchar(stream
);
412 static int get_string_token(int next
, stream_t
*stream
)
414 static char buffer
[512];
415 struct string
*string
;
421 next
= escapechar(next
, '"', stream
, &val
);
425 warn(stream
->pos
, "Enf of file in middle of string");
428 if (len
< sizeof(buffer
)) {
436 warn(stream
->pos
, "String too long");
438 string
= __alloc_string(len
+1);
439 memcpy(string
->data
, buffer
, len
);
440 string
->data
[len
] = '\0';
441 string
->length
= len
+1;
444 token
= stream
->token
;
445 token_type(token
) = TOKEN_STRING
;
446 token
->string
= string
;
452 static int drop_stream_eoln(stream_t
*stream
)
454 int next
= nextchar(stream
);
460 next
= nextchar(stream
);
466 static int drop_stream_comment(stream_t
*stream
)
468 int next
= nextchar(stream
);
473 warn(stream
->pos
, "End of file in the middle of a comment");
476 next
= nextchar(stream
);
477 if (curr
== '*' && next
== '/')
480 return nextchar(stream
);
483 unsigned char combinations
[][3] = COMBINATION_STRINGS
;
485 #define NR_COMBINATIONS (sizeof(combinations)/3)
487 static int get_one_special(int c
, stream_t
*stream
)
490 unsigned char c1
, c2
, c3
;
494 next
= nextchar(stream
);
497 * Check for strings, character constants, and comments
501 return get_string_token(next
, stream
);
503 return get_char_token(next
, stream
);
506 return drop_stream_eoln(stream
);
508 return drop_stream_comment(stream
);
512 * Check for combinations
515 comb
= combinations
[0];
516 c1
= c
; c2
= next
; c3
= 0;
517 for (i
= 0; i
< NR_COMBINATIONS
; i
++) {
518 if (comb
[0] == c1
&& comb
[1] == c2
&& comb
[2] == c3
) {
519 value
= i
+ SPECIAL_BASE
;
520 next
= nextchar(stream
);
529 token
= stream
->token
;
530 token_type(token
) = TOKEN_SPECIAL
;
531 token
->special
= value
;
536 #define IDENT_HASH_BITS (10)
537 #define IDENT_HASH_SIZE (1<<IDENT_HASH_BITS)
538 #define IDENT_HASH_MASK (IDENT_HASH_SIZE-1)
540 #define ident_hash_init(c) (c)
541 #define ident_hash_add(oldhash,c) ((oldhash)*11 + (c))
542 #define ident_hash_end(hash) ((((hash) >> IDENT_HASH_BITS) + (hash)) & IDENT_HASH_MASK)
544 static struct ident
*hash_table
[IDENT_HASH_SIZE
];
545 int ident_hit
, ident_miss
;
547 void show_identifier_stats(void)
550 int distribution
[100];
552 fprintf(stderr
, "identifiers: %d hits, %d misses\n",
553 ident_hit
, ident_miss
);
555 for (i
= 0; i
< 100; i
++)
558 for (i
= 0; i
< IDENT_HASH_SIZE
; i
++) {
559 struct ident
* ident
= hash_table
[i
];
568 distribution
[count
]++;
571 for (i
= 0; i
< 100; i
++) {
573 fprintf(stderr
, "%2d: %d buckets\n", i
, distribution
[i
]);
577 static struct ident
*alloc_ident(const char *name
, int len
)
579 struct ident
*ident
= __alloc_ident(len
);
580 ident
->symbols
= NULL
;
582 memcpy(ident
->name
, name
, len
);
586 static struct ident
* insert_hash(struct ident
*ident
, unsigned long hash
)
588 ident
->next
= hash_table
[hash
];
589 hash_table
[hash
] = ident
;
594 static struct ident
*create_hashed_ident(const char *name
, int len
, unsigned long hash
)
598 ident
= hash_table
[hash
];
600 if (ident
->len
== len
&& !memcmp(ident
->name
, name
, len
)) {
607 return insert_hash(alloc_ident(name
, len
), hash
);
610 static unsigned long hash_name(const char *name
, int len
)
613 const unsigned char *p
= (const unsigned char *)name
;
615 hash
= ident_hash_init(*p
++);
617 unsigned int i
= *p
++;
618 hash
= ident_hash_add(hash
, i
);
620 return ident_hash_end(hash
);
623 struct ident
*hash_ident(struct ident
*ident
)
625 return insert_hash(ident
, hash_name(ident
->name
, ident
->len
));
628 struct ident
*built_in_ident(const char *name
)
630 int len
= strlen(name
);
631 return create_hashed_ident(name
, len
, hash_name(name
, len
));
634 struct token
*built_in_token(int stream
, const char *name
)
638 token
= __alloc_token(0);
639 token
->pos
.stream
= stream
;
640 token_type(token
) = TOKEN_IDENT
;
641 token
->ident
= built_in_ident(name
);
645 static int get_one_identifier(int c
, stream_t
*stream
)
654 hash
= ident_hash_init(c
);
657 next
= nextchar(stream
);
663 if (len
< sizeof(buf
)) {
664 hash
= ident_hash_add(hash
, next
);
672 hash
= ident_hash_end(hash
);
674 ident
= create_hashed_ident(buf
, len
, hash
);
677 token
= stream
->token
;
678 token_type(token
) = TOKEN_IDENT
;
679 token
->ident
= ident
;
684 static int get_one_token(int c
, stream_t
*stream
)
688 return get_one_number(c
, stream
);
692 return get_one_identifier(c
, stream
);
694 return get_one_special(c
, stream
);
698 static struct token
*setup_stream(stream_t
*stream
, int idx
, int fd
,
699 unsigned char *buf
, unsigned int buf_size
)
703 stream
->pos
.stream
= idx
;
704 stream
->pos
.line
= 1;
705 stream
->pos
.newline
= 1;
706 stream
->pos
.whitespace
= 0;
709 stream
->token
= NULL
;
712 stream
->size
= buf_size
;
713 stream
->buffer
= buf
;
715 begin
= alloc_token(stream
);
716 token_type(begin
) = TOKEN_STREAMBEGIN
;
717 stream
->tokenlist
= &begin
->next
;
721 static void tokenize_stream(stream_t
*stream
, struct token
*endtoken
)
723 int c
= nextchar(stream
);
726 c
= nextchar(stream
);
727 stream
->pos
.newline
= 0;
728 stream
->pos
.whitespace
= 1;
732 struct token
*token
= alloc_token(stream
);
733 stream
->token
= token
;
734 stream
->pos
.newline
= 0;
735 stream
->pos
.whitespace
= 0;
736 c
= get_one_token(c
, stream
);
739 stream
->pos
.whitespace
= 1;
740 c
= nextchar(stream
);
742 mark_eof(stream
, endtoken
);
745 struct token
* tokenize_buffer(unsigned char *buffer
, unsigned long size
, struct token
*endtoken
)
750 begin
= setup_stream(&stream
, 0, -1, buffer
, size
);
751 tokenize_stream(&stream
, endtoken
);
755 struct token
* tokenize(const char *name
, int fd
, struct token
*endtoken
)
759 unsigned char buffer
[BUFSIZE
];
762 idx
= init_stream(name
, fd
);
766 begin
= setup_stream(&stream
, idx
, fd
, buffer
, 0);
767 tokenize_stream(&stream
, endtoken
);