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)
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];
54 sprintf(buffer
, "%.*s", ident
->len
, ident
->name
);
58 char *charstr(char *ptr
, unsigned char c
, unsigned char escape
, unsigned char next
)
61 if (c
== escape
|| c
== '\\')
76 return ptr
+ sprintf(ptr
, "%o", c
);
78 return ptr
+ sprintf(ptr
, "%03o", c
);
81 const char *show_string(const struct string
*string
)
83 static char buffer
[256];
89 for (i
= 0; i
< string
->length
-1; i
++) {
90 const unsigned char *p
= string
->data
+ i
;
91 ptr
= charstr(ptr
, p
[0], '"', p
[1]);
98 const char *show_token(const struct token
*token
)
100 static char buffer
[256];
104 switch (token_type(token
)) {
106 return "syntax error";
109 return "end-of-input";
112 return show_ident(token
->ident
);
115 return show_string(token
->string
);
117 case TOKEN_INTEGER
: {
118 const char *p
= token
->integer
;
123 strcpy(buffer
+1, p
+1);
134 return show_special(token
->special
);
138 int c
= token
->character
;
140 ptr
= charstr(ptr
, c
, '\'', 0);
146 case TOKEN_STREAMBEGIN
:
147 sprintf(buffer
, "<beginning of '%s'>", (input_streams
+ token
->pos
.stream
)->name
);
150 case TOKEN_STREAMEND
:
151 sprintf(buffer
, "<end of '%s'>", (input_streams
+ token
->pos
.stream
)->name
);
159 int init_stream(const char *name
, int fd
)
161 int stream
= input_stream_nr
;
162 struct stream
*current
;
164 if (stream
>= input_streams_allocated
) {
165 int newalloc
= stream
* 4 / 3 + 10;
166 input_streams
= realloc(input_streams
, newalloc
* sizeof(struct stream
));
168 die("Unable to allocate more streams space");
169 input_streams_allocated
= newalloc
;
171 current
= input_streams
+ stream
;
172 memset(current
, 0, sizeof(*current
));
173 current
->name
= name
;
175 current
->constant
= -1; // "unknown"
181 current
->dev
= st
.st_dev
;
182 current
->ino
= st
.st_ino
;
183 for (i
= 0; i
< stream
; i
++) {
184 struct stream
*s
= input_streams
+ i
;
185 if (s
->dev
== st
.st_dev
&& s
->ino
== st
.st_ino
) {
186 if (s
->constant
> 0 && lookup_symbol(s
->protect
, NS_PREPROCESSOR
))
191 input_stream_nr
= stream
+1;
195 static struct token
* alloc_token(stream_t
*stream
)
197 struct token
*token
= __alloc_token(0);
198 token
->pos
= stream
->pos
;
202 static int nextchar(stream_t
*stream
)
204 int offset
= stream
->offset
;
205 int size
= stream
->size
;
208 if (offset
>= size
) {
209 size
= read(stream
->fd
, stream
->buffer
, BUFSIZE
);
216 c
= stream
->buffer
[offset
];
217 stream
->offset
= offset
+ 1;
221 stream
->pos
.newline
= 1;
227 struct token eof_token_entry
;
229 static void mark_eof(stream_t
*stream
, struct token
*end_token
)
233 end
= alloc_token(stream
);
234 token_type(end
) = TOKEN_STREAMEND
;
235 end
->pos
.newline
= 1;
237 eof_token_entry
.next
= &eof_token_entry
;
238 eof_token_entry
.pos
.newline
= 1;
241 end_token
= &eof_token_entry
;
242 end
->next
= end_token
;
243 *stream
->tokenlist
= end
;
244 stream
->tokenlist
= NULL
;
247 static void add_token(stream_t
*stream
)
249 struct token
*token
= stream
->token
;
251 stream
->token
= NULL
;
253 *stream
->tokenlist
= token
;
254 stream
->tokenlist
= &token
->next
;
257 static void drop_token(stream_t
*stream
)
259 stream
->pos
.newline
|= stream
->token
->pos
.newline
;
260 stream
->pos
.whitespace
|= stream
->token
->pos
.whitespace
;
261 stream
->token
= NULL
;
264 static int get_base_number(unsigned int base
, char **p
, int next
, stream_t
*stream
)
271 next
= nextchar(stream
);
281 static int do_integer(char *buffer
, int len
, int next
, stream_t
*stream
)
283 struct token
*token
= stream
->token
;
286 while (next
== 'u' || next
== 'U' || next
== 'l' || next
== 'L') {
287 buffer
[len
++] = next
;
288 next
= nextchar(stream
);
290 buffer
[len
++] = '\0';
291 buf
= __alloc_bytes(len
);
292 memcpy(buf
, buffer
, len
);
293 token_type(token
) = TOKEN_INTEGER
;
294 token
->integer
= buf
;
299 static int get_one_number(int c
, stream_t
*stream
)
301 static char buffer
[256];
302 int next
= nextchar(stream
);
310 next
= get_base_number(8, &p
, next
, stream
);
315 next
= get_base_number(10, &p
, next
, stream
);
320 next
= get_base_number(16, &p
, next
, stream
);
323 return do_integer(buffer
, p
- buffer
, next
, stream
);
326 static int escapechar(int first
, int type
, stream_t
*stream
, int *valp
)
330 next
= nextchar(stream
);
334 warn(stream
->pos
, "Newline in string or character constant");
336 if (first
== '\\' && next
!= EOF
) {
338 next
= nextchar(stream
);
356 while (next
>= '0' && next
<= '9') {
357 value
= (value
<< 3) + (next
-'0');
358 next
= nextchar(stream
);
366 int hex
= hexval(next
);
369 next
= nextchar(stream
);
370 while ((hex
= hexval(next
)) < 16) {
371 value
= (value
<< 4) + hex
;
372 next
= nextchar(stream
);
380 warn(stream
->pos
, "Unknown escape '%c'", value
);
383 /* Mark it as escaped */
390 static int get_char_token(int next
, stream_t
*stream
)
395 next
= escapechar(next
, '\'', stream
, &value
);
396 if (value
== '\'' || next
!= '\'') {
397 warn(stream
->pos
, "Bad character constant");
402 token
= stream
->token
;
403 token_type(token
) = TOKEN_CHAR
;
404 token
->character
= value
& 0xff;
407 return nextchar(stream
);
410 static int get_string_token(int next
, stream_t
*stream
)
412 static char buffer
[512];
413 struct string
*string
;
419 next
= escapechar(next
, '"', stream
, &val
);
423 warn(stream
->pos
, "Enf of file in middle of string");
426 if (len
< sizeof(buffer
)) {
434 warn(stream
->pos
, "String too long");
436 string
= __alloc_string(len
+1);
437 memcpy(string
->data
, buffer
, len
);
438 string
->data
[len
] = '\0';
439 string
->length
= len
+1;
442 token
= stream
->token
;
443 token_type(token
) = TOKEN_STRING
;
444 token
->string
= string
;
450 static int drop_stream_eoln(stream_t
*stream
)
452 int next
= nextchar(stream
);
458 next
= nextchar(stream
);
464 static int drop_stream_comment(stream_t
*stream
)
466 int next
= nextchar(stream
);
471 warn(stream
->pos
, "End of file in the middle of a comment");
474 next
= nextchar(stream
);
475 if (curr
== '*' && next
== '/')
478 return nextchar(stream
);
481 unsigned char combinations
[][3] = COMBINATION_STRINGS
;
483 #define NR_COMBINATIONS (sizeof(combinations)/3)
485 static int get_one_special(int c
, stream_t
*stream
)
488 unsigned char c1
, c2
, c3
;
492 next
= nextchar(stream
);
495 * Check for strings, character constants, and comments
499 return get_string_token(next
, stream
);
501 return get_char_token(next
, stream
);
504 return drop_stream_eoln(stream
);
506 return drop_stream_comment(stream
);
510 * Check for combinations
513 comb
= combinations
[0];
514 c1
= c
; c2
= next
; c3
= 0;
515 for (i
= 0; i
< NR_COMBINATIONS
; i
++) {
516 if (comb
[0] == c1
&& comb
[1] == c2
&& comb
[2] == c3
) {
517 value
= i
+ SPECIAL_BASE
;
518 next
= nextchar(stream
);
527 token
= stream
->token
;
528 token_type(token
) = TOKEN_SPECIAL
;
529 token
->special
= value
;
534 #define IDENT_HASH_BITS (10)
535 #define IDENT_HASH_SIZE (1<<IDENT_HASH_BITS)
536 #define IDENT_HASH_MASK (IDENT_HASH_SIZE-1)
538 #define ident_hash_init(c) (c)
539 #define ident_hash_add(oldhash,c) ((oldhash)*11 + (c))
540 #define ident_hash_end(hash) ((((hash) >> IDENT_HASH_BITS) + (hash)) & IDENT_HASH_MASK)
542 static struct ident
*hash_table
[IDENT_HASH_SIZE
];
543 int ident_hit
, ident_miss
;
545 void show_identifier_stats(void)
548 int distribution
[100];
550 fprintf(stderr
, "identifiers: %d hits, %d misses\n",
551 ident_hit
, ident_miss
);
553 for (i
= 0; i
< 100; i
++)
556 for (i
= 0; i
< IDENT_HASH_SIZE
; i
++) {
557 struct ident
* ident
= hash_table
[i
];
566 distribution
[count
]++;
569 for (i
= 0; i
< 100; i
++) {
571 fprintf(stderr
, "%2d: %d buckets\n", i
, distribution
[i
]);
575 static struct ident
*alloc_ident(const char *name
, int len
)
577 struct ident
*ident
= __alloc_ident(len
);
578 ident
->symbols
= NULL
;
580 memcpy(ident
->name
, name
, len
);
584 static struct ident
* insert_hash(struct ident
*ident
, unsigned long hash
)
586 ident
->next
= hash_table
[hash
];
587 hash_table
[hash
] = ident
;
592 static struct ident
*create_hashed_ident(const char *name
, int len
, unsigned long hash
)
596 ident
= hash_table
[hash
];
598 if (ident
->len
== len
&& !memcmp(ident
->name
, name
, len
)) {
605 return insert_hash(alloc_ident(name
, len
), hash
);
608 static unsigned long hash_name(const char *name
, int len
)
611 const unsigned char *p
= (const unsigned char *)name
;
613 hash
= ident_hash_init(*p
++);
615 unsigned int i
= *p
++;
616 hash
= ident_hash_add(hash
, i
);
618 return ident_hash_end(hash
);
621 struct ident
*hash_ident(struct ident
*ident
)
623 return insert_hash(ident
, hash_name(ident
->name
, ident
->len
));
626 struct ident
*built_in_ident(const char *name
)
628 int len
= strlen(name
);
629 return create_hashed_ident(name
, len
, hash_name(name
, len
));
632 struct token
*built_in_token(int stream
, const char *name
)
636 token
= __alloc_token(0);
637 token
->pos
.stream
= stream
;
638 token_type(token
) = TOKEN_IDENT
;
639 token
->ident
= built_in_ident(name
);
643 static int get_one_identifier(int c
, stream_t
*stream
)
652 hash
= ident_hash_init(c
);
655 next
= nextchar(stream
);
661 if (len
< sizeof(buf
)) {
662 hash
= ident_hash_add(hash
, next
);
670 hash
= ident_hash_end(hash
);
672 ident
= create_hashed_ident(buf
, len
, hash
);
675 token
= stream
->token
;
676 token_type(token
) = TOKEN_IDENT
;
677 token
->ident
= ident
;
682 static int get_one_token(int c
, stream_t
*stream
)
686 return get_one_number(c
, stream
);
690 return get_one_identifier(c
, stream
);
692 return get_one_special(c
, stream
);
696 static struct token
*setup_stream(stream_t
*stream
, int idx
, int fd
,
697 unsigned char *buf
, unsigned int buf_size
)
701 stream
->pos
.stream
= idx
;
702 stream
->pos
.line
= 1;
703 stream
->pos
.newline
= 1;
704 stream
->pos
.whitespace
= 0;
707 stream
->token
= NULL
;
710 stream
->size
= buf_size
;
711 stream
->buffer
= buf
;
713 begin
= alloc_token(stream
);
714 token_type(begin
) = TOKEN_STREAMBEGIN
;
715 stream
->tokenlist
= &begin
->next
;
719 static void tokenize_stream(stream_t
*stream
, struct token
*endtoken
)
721 int c
= nextchar(stream
);
724 c
= nextchar(stream
);
725 stream
->pos
.newline
= 0;
726 stream
->pos
.whitespace
= 1;
730 struct token
*token
= alloc_token(stream
);
731 stream
->token
= token
;
732 stream
->pos
.newline
= 0;
733 stream
->pos
.whitespace
= 0;
734 c
= get_one_token(c
, stream
);
737 stream
->pos
.whitespace
= 1;
738 c
= nextchar(stream
);
740 mark_eof(stream
, endtoken
);
743 struct token
* tokenize_buffer(unsigned char *buffer
, unsigned long size
, struct token
*endtoken
)
748 begin
= setup_stream(&stream
, 0, -1, buffer
, size
);
749 tokenize_stream(&stream
, endtoken
);
753 struct token
* tokenize(const char *name
, int fd
, struct token
*endtoken
)
757 unsigned char buffer
[BUFSIZE
];
760 idx
= init_stream(name
, fd
);
764 begin
= setup_stream(&stream
, idx
, fd
, buffer
, 0);
765 tokenize_stream(&stream
, endtoken
);