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 Linus Torvalds, all rights reserved.
22 int input_stream_nr
= 0;
23 struct stream
*input_streams
;
24 static int input_streams_allocated
;
26 #define BUFSIZE (8192)
29 int fd
, stream
, line
, pos
, offset
, size
;
30 unsigned int newline
:1, whitespace
:1;
31 struct token
**tokenlist
;
33 unsigned char *buffer
;
37 const char *show_special(int val
)
39 static const char *combinations
[] = COMBINATION_STRINGS
;
40 static char buffer
[4];
44 if (val
>= SPECIAL_BASE
)
45 strcpy(buffer
, combinations
[val
- SPECIAL_BASE
]);
49 const char *show_ident(const struct ident
*ident
)
51 static char buffer
[256];
52 sprintf(buffer
, "%.*s", ident
->len
, ident
->name
);
56 char *charstr(char *ptr
, unsigned char c
, unsigned char escape
, unsigned char next
)
59 if (c
== escape
|| c
== '\\')
74 return ptr
+ sprintf(ptr
, "%o", c
);
76 return ptr
+ sprintf(ptr
, "%03o", c
);
79 const char *show_token(const struct token
*token
)
81 static char buffer
[256];
85 switch (token
->type
) {
87 return "syntax error";
90 return "end-of-input";
93 return show_ident(token
->ident
);
98 struct string
*string
= token
->string
;
102 for (i
= 0; i
< string
->length
-1; i
++) {
103 unsigned char *p
= string
->data
+ i
;
104 ptr
= charstr(ptr
, p
[0], '"', p
[1]);
111 case TOKEN_INTEGER
: {
112 const char *p
= token
->integer
;
117 strcpy(buffer
+1, p
+1);
128 return show_special(token
->special
);
132 int c
= token
->character
;
134 ptr
= charstr(ptr
, c
, '\'', 0);
140 case TOKEN_STREAMBEGIN
:
141 sprintf(buffer
, "<beginning of '%s'>", (input_streams
+ token
->stream
)->name
);
144 case TOKEN_STREAMEND
:
145 sprintf(buffer
, "<end of '%s'>", (input_streams
+ token
->stream
)->name
);
153 int init_stream(const char *name
, int fd
)
155 int stream
= input_stream_nr
;
156 struct stream
*current
;
158 if (stream
>= input_streams_allocated
) {
159 int newalloc
= stream
* 4 / 3 + 10;
160 input_streams
= realloc(input_streams
, newalloc
* sizeof(struct stream
));
162 die("Unable to allocate more streams space");
163 input_streams_allocated
= newalloc
;
165 current
= input_streams
+ stream
;
166 memset(current
, 0, sizeof(*current
));
167 current
->name
= name
;
169 current
->constant
= -1; // "unknown"
175 current
->dev
= st
.st_dev
;
176 current
->ino
= st
.st_ino
;
177 for (i
= 0; i
< stream
; i
++) {
178 struct stream
*s
= input_streams
+ i
;
179 if (s
->dev
== st
.st_dev
&& s
->ino
== st
.st_ino
) {
180 if (s
->constant
> 0 && lookup_symbol(s
->protect
, NS_PREPROCESSOR
))
185 input_stream_nr
= stream
+1;
189 static struct token
* alloc_token(stream_t
*stream
)
191 struct token
*token
= __alloc_token(0);
192 token
->line
= stream
->line
;
193 token
->pos
= stream
->pos
;
194 token
->stream
= stream
->stream
;
195 token
->newline
= stream
->newline
;
196 token
->whitespace
= stream
->whitespace
;
200 static int nextchar(stream_t
*stream
)
202 int offset
= stream
->offset
;
203 int size
= stream
->size
;
206 if (offset
>= size
) {
207 size
= read(stream
->fd
, stream
->buffer
, BUFSIZE
);
214 c
= stream
->buffer
[offset
];
215 stream
->offset
= offset
+ 1;
225 struct token eof_token_entry
;
227 static void mark_eof(stream_t
*stream
, struct token
*end_token
)
231 end
= alloc_token(stream
);
232 end
->type
= TOKEN_STREAMEND
;
235 eof_token_entry
.next
= &eof_token_entry
;
236 eof_token_entry
.newline
= 1;
239 end_token
= &eof_token_entry
;
240 end
->next
= end_token
;
241 *stream
->tokenlist
= end
;
242 stream
->tokenlist
= NULL
;
245 static void add_token(stream_t
*stream
)
247 struct token
*token
= stream
->token
;
249 stream
->token
= NULL
;
251 *stream
->tokenlist
= token
;
252 stream
->tokenlist
= &token
->next
;
255 static void drop_token(stream_t
*stream
)
257 stream
->newline
|= stream
->token
->newline
;
258 stream
->whitespace
|= stream
->token
->whitespace
;
259 stream
->token
= NULL
;
262 static int get_base_number(unsigned int base
, char **p
, int next
, stream_t
*stream
)
269 next
= nextchar(stream
);
279 static int do_integer(char *buffer
, int len
, int next
, stream_t
*stream
)
281 struct token
*token
= stream
->token
;
284 while (next
== 'u' || next
== 'U' || next
== 'l' || next
== 'L') {
285 buffer
[len
++] = next
;
286 next
= nextchar(stream
);
288 buffer
[len
++] = '\0';
289 buf
= __alloc_bytes(len
);
290 memcpy(buf
, buffer
, len
);
291 token
->type
= TOKEN_INTEGER
;
292 token
->integer
= buf
;
297 static int get_one_number(int c
, stream_t
*stream
)
299 static char buffer
[256];
300 int next
= nextchar(stream
);
308 next
= get_base_number(8, &p
, next
, stream
);
313 next
= get_base_number(10, &p
, next
, stream
);
318 next
= get_base_number(16, &p
, next
, stream
);
321 return do_integer(buffer
, p
- buffer
, next
, stream
);
324 static int escapechar(int first
, int type
, stream_t
*stream
, int *valp
)
328 next
= nextchar(stream
);
332 warn(stream
->token
, "Newline in string or character constant");
334 if (first
== '\\' && next
!= EOF
) {
336 next
= nextchar(stream
);
354 while (next
>= '0' && next
<= '9') {
355 value
= (value
<< 3) + (next
-'0');
356 next
= nextchar(stream
);
364 int hex
= hexval(next
);
367 next
= nextchar(stream
);
368 while ((hex
= hexval(next
)) < 16) {
369 value
= (value
<< 4) + hex
;
370 next
= nextchar(stream
);
378 warn(stream
->token
, "Unknown escape '%c'", value
);
381 /* Mark it as escaped */
388 static int get_char_token(int next
, stream_t
*stream
)
393 next
= escapechar(next
, '\'', stream
, &value
);
394 if (value
== '\'' || next
!= '\'') {
395 warn(stream
->token
, "Bad character constant");
400 token
= stream
->token
;
401 token
->type
= TOKEN_CHAR
;
402 token
->character
= value
& 0xff;
405 return nextchar(stream
);
408 static int get_string_token(int next
, stream_t
*stream
)
410 static char buffer
[512];
411 struct string
*string
;
417 next
= escapechar(next
, '"', stream
, &val
);
421 warn(stream
->token
, "Enf of file in middle of string");
424 if (len
< sizeof(buffer
)) {
432 warn(stream
->token
, "String too long");
434 string
= __alloc_string(len
+1);
435 memcpy(string
->data
, buffer
, len
);
436 string
->data
[len
] = '\0';
437 string
->length
= len
+1;
440 token
= stream
->token
;
441 token
->type
= TOKEN_STRING
;
442 token
->string
= string
;
448 static int drop_stream_eoln(stream_t
*stream
)
450 int next
= nextchar(stream
);
456 next
= nextchar(stream
);
462 static int drop_stream_comment(stream_t
*stream
)
464 int next
= nextchar(stream
);
469 warn(stream
->token
, "End of file in the middle of a comment");
472 next
= nextchar(stream
);
473 if (curr
== '*' && next
== '/')
476 return nextchar(stream
);
479 unsigned char combinations
[][3] = COMBINATION_STRINGS
;
481 #define NR_COMBINATIONS (sizeof(combinations)/3)
483 static int get_one_special(int c
, stream_t
*stream
)
486 unsigned char c1
, c2
, c3
;
490 next
= nextchar(stream
);
493 * Check for strings, character constants, and comments
497 return get_string_token(next
, stream
);
499 return get_char_token(next
, stream
);
502 return drop_stream_eoln(stream
);
504 return drop_stream_comment(stream
);
508 * Check for combinations
511 comb
= combinations
[0];
512 c1
= c
; c2
= next
; c3
= 0;
513 for (i
= 0; i
< NR_COMBINATIONS
; i
++) {
514 if (comb
[0] == c1
&& comb
[1] == c2
&& comb
[2] == c3
) {
515 value
= i
+ SPECIAL_BASE
;
516 next
= nextchar(stream
);
525 token
= stream
->token
;
526 token
->type
= TOKEN_SPECIAL
;
527 token
->special
= value
;
532 #define IDENT_HASH_BITS (10)
533 #define IDENT_HASH_SIZE (1<<IDENT_HASH_BITS)
534 #define IDENT_HASH_MASK (IDENT_HASH_SIZE-1)
536 #define ident_hash_init(c) (c)
537 #define ident_hash_add(oldhash,c) ((oldhash)*11 + (c))
538 #define ident_hash_end(hash) ((((hash) >> IDENT_HASH_BITS) + (hash)) & IDENT_HASH_MASK)
540 static struct ident
*hash_table
[IDENT_HASH_SIZE
];
541 int ident_hit
, ident_miss
;
543 void show_identifier_stats(void)
546 int distribution
[100];
548 fprintf(stderr
, "identifiers: %d hits, %d misses\n",
549 ident_hit
, ident_miss
);
551 for (i
= 0; i
< 100; i
++)
554 for (i
= 0; i
< IDENT_HASH_SIZE
; i
++) {
555 struct ident
* ident
= hash_table
[i
];
564 distribution
[count
]++;
567 for (i
= 0; i
< 100; i
++) {
569 fprintf(stderr
, "%2d: %d buckets\n", i
, distribution
[i
]);
573 static struct ident
*alloc_ident(const char *name
, int len
)
575 struct ident
*ident
= __alloc_ident(len
);
576 ident
->symbols
= NULL
;
578 memcpy(ident
->name
, name
, len
);
582 static struct ident
* insert_hash(struct ident
*ident
, unsigned long hash
)
584 ident
->next
= hash_table
[hash
];
585 hash_table
[hash
] = ident
;
590 static struct ident
*create_hashed_ident(const char *name
, int len
, unsigned long hash
)
594 ident
= hash_table
[hash
];
596 if (ident
->len
== len
&& !memcmp(ident
->name
, name
, len
)) {
603 return insert_hash(alloc_ident(name
, len
), hash
);
606 static unsigned long hash_name(const char *name
, int len
)
609 const unsigned char *p
= (const unsigned char *)name
;
611 hash
= ident_hash_init(*p
++);
613 unsigned int i
= *p
++;
614 hash
= ident_hash_add(hash
, i
);
616 return ident_hash_end(hash
);
619 struct ident
*hash_ident(struct ident
*ident
)
621 return insert_hash(ident
, hash_name(ident
->name
, ident
->len
));
624 struct ident
*built_in_ident(const char *name
)
626 int len
= strlen(name
);
627 return create_hashed_ident(name
, len
, hash_name(name
, len
));
630 struct token
*built_in_token(int stream
, const char *name
)
634 token
= __alloc_token(0);
635 token
->stream
= stream
;
636 token
->type
= TOKEN_IDENT
;
637 token
->ident
= built_in_ident(name
);
641 static int get_one_identifier(int c
, stream_t
*stream
)
650 hash
= ident_hash_init(c
);
653 next
= nextchar(stream
);
659 if (len
< sizeof(buf
)) {
660 hash
= ident_hash_add(hash
, next
);
668 hash
= ident_hash_end(hash
);
670 ident
= create_hashed_ident(buf
, len
, hash
);
673 token
= stream
->token
;
674 token
->type
= TOKEN_IDENT
;
675 token
->ident
= ident
;
680 static int get_one_token(int c
, stream_t
*stream
)
684 return get_one_number(c
, stream
);
688 return get_one_identifier(c
, stream
);
690 return get_one_special(c
, stream
);
694 static struct token
*setup_stream(stream_t
*stream
, int idx
, int fd
,
695 unsigned char *buf
, unsigned int buf_size
)
699 stream
->stream
= idx
;
700 stream
->token
= NULL
;
703 stream
->whitespace
= 0;
707 stream
->size
= buf_size
;
708 stream
->buffer
= buf
;
710 begin
= alloc_token(stream
);
711 begin
->type
= TOKEN_STREAMBEGIN
;
712 stream
->tokenlist
= &begin
->next
;
716 static void tokenize_stream(stream_t
*stream
, struct token
*endtoken
)
718 int c
= nextchar(stream
);
721 c
= nextchar(stream
);
723 stream
->whitespace
= 1;
727 struct token
*token
= alloc_token(stream
);
728 token
->newline
= stream
->newline
;
729 token
->whitespace
= stream
->whitespace
;
731 stream
->whitespace
= 0;
732 stream
->token
= token
;
733 c
= get_one_token(c
, stream
);
736 stream
->whitespace
= 1;
737 c
= nextchar(stream
);
739 mark_eof(stream
, endtoken
);
742 struct token
* tokenize_buffer(unsigned char *buffer
, unsigned long size
, struct token
*endtoken
)
747 begin
= setup_stream(&stream
, 0, -1, buffer
, size
);
748 tokenize_stream(&stream
, endtoken
);
752 struct token
* tokenize(const char *name
, int fd
, struct token
*endtoken
)
756 unsigned char buffer
[BUFSIZE
];
759 idx
= init_stream(name
, fd
);
763 begin
= setup_stream(&stream
, idx
, fd
, buffer
, 0);
764 tokenize_stream(&stream
, endtoken
);