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 struct token
**tokenlist
;
36 unsigned char *buffer
;
40 const char *show_special(int val
)
42 static const char *combinations
[] = COMBINATION_STRINGS
;
43 static char buffer
[4];
47 if (val
>= SPECIAL_BASE
)
48 strcpy(buffer
, combinations
[val
- SPECIAL_BASE
]);
52 const char *show_ident(const struct ident
*ident
)
54 static char buffer
[256];
57 sprintf(buffer
, "%.*s", ident
->len
, ident
->name
);
61 char *charstr(char *ptr
, unsigned char c
, unsigned char escape
, unsigned char next
)
64 if (c
== escape
|| c
== '\\')
79 return ptr
+ sprintf(ptr
, "%o", c
);
81 return ptr
+ sprintf(ptr
, "%03o", c
);
84 const char *show_string(const struct string
*string
)
86 static char buffer
[256];
92 for (i
= 0; i
< string
->length
-1; i
++) {
93 const unsigned char *p
= string
->data
+ i
;
94 ptr
= charstr(ptr
, p
[0], '"', p
[1]);
101 const char *show_token(const struct token
*token
)
103 static char buffer
[256];
107 switch (token_type(token
)) {
109 return "syntax error";
112 return "end-of-input";
115 return show_ident(token
->ident
);
118 return show_string(token
->string
);
121 return token
->number
;
124 return show_special(token
->special
);
128 int c
= token
->character
;
130 ptr
= charstr(ptr
, c
, '\'', 0);
136 case TOKEN_STREAMBEGIN
:
137 sprintf(buffer
, "<beginning of '%s'>", (input_streams
+ token
->pos
.stream
)->name
);
140 case TOKEN_STREAMEND
:
141 sprintf(buffer
, "<end of '%s'>", (input_streams
+ token
->pos
.stream
)->name
);
149 int init_stream(const char *name
, int fd
)
151 int stream
= input_stream_nr
;
152 struct stream
*current
;
154 if (stream
>= input_streams_allocated
) {
155 int newalloc
= stream
* 4 / 3 + 10;
156 input_streams
= realloc(input_streams
, newalloc
* sizeof(struct stream
));
158 die("Unable to allocate more streams space");
159 input_streams_allocated
= newalloc
;
161 current
= input_streams
+ stream
;
162 memset(current
, 0, sizeof(*current
));
163 current
->name
= name
;
165 current
->constant
= -1; // "unknown"
171 current
->dev
= st
.st_dev
;
172 current
->ino
= st
.st_ino
;
173 for (i
= 0; i
< stream
; i
++) {
174 struct stream
*s
= input_streams
+ i
;
175 if (s
->dev
== st
.st_dev
&& s
->ino
== st
.st_ino
) {
176 if (s
->constant
> 0 && lookup_symbol(s
->protect
, NS_PREPROCESSOR
))
181 input_stream_nr
= stream
+1;
185 static struct token
* alloc_token(stream_t
*stream
)
187 struct token
*token
= __alloc_token(0);
188 token
->pos
= stream
->pos
;
193 * Argh... That was surprisingly messy - handling '\r' complicates the
196 static int nextchar_slow(stream_t
*stream
)
198 int offset
= stream
->offset
;
199 int size
= stream
->size
;
201 int spliced
= 0, had_cr
, had_backslash
, complain
;
204 had_cr
= had_backslash
= complain
= 0;
207 if (offset
>= size
) {
208 size
= read(stream
->fd
, stream
->buffer
, BUFSIZE
);
212 stream
->offset
= offset
= 0;
215 c
= stream
->buffer
[offset
++];
217 if (had_cr
&& c
!= '\n')
232 if (!had_backslash
) {
238 stream
->pos
.newline
= 1;
242 warn(stream
->pos
, "non-ASCII data stream");
252 stream
->offset
= offset
;
254 warn(stream
->pos
, "non-ASCII data stream");
264 warn(stream
->pos
, "no newline at end of file");
266 warn(stream
->pos
, "non-ASCII data stream");
268 warn(stream
->pos
, "backslash-newline at end of file");
273 * We want that as light as possible while covering all normal cases.
274 * Slow path (including the logics with line-splicing and EOF sanity
275 * checks) is in nextchar_slow().
277 static inline int nextchar(stream_t
*stream
)
279 int offset
= stream
->offset
;
281 if (offset
< stream
->size
) {
282 int c
= stream
->buffer
[offset
++];
288 stream
->offset
= offset
;
290 stream
->pos
.newline
= 1;
294 if (offset
>= stream
->size
)
296 next
= stream
->buffer
[offset
];
297 if (next
== '\n' || next
== '\r')
301 stream
->offset
= offset
;
306 return nextchar_slow(stream
);
309 struct token eof_token_entry
;
311 static void mark_eof(stream_t
*stream
, struct token
*end_token
)
315 end
= alloc_token(stream
);
316 token_type(end
) = TOKEN_STREAMEND
;
317 end
->pos
.newline
= 1;
319 eof_token_entry
.next
= &eof_token_entry
;
320 eof_token_entry
.pos
.newline
= 1;
323 end_token
= &eof_token_entry
;
324 end
->next
= end_token
;
325 *stream
->tokenlist
= end
;
326 stream
->tokenlist
= NULL
;
329 static void add_token(stream_t
*stream
)
331 struct token
*token
= stream
->token
;
333 stream
->token
= NULL
;
335 *stream
->tokenlist
= token
;
336 stream
->tokenlist
= &token
->next
;
339 static void drop_token(stream_t
*stream
)
341 stream
->pos
.newline
|= stream
->token
->pos
.newline
;
342 stream
->pos
.whitespace
|= stream
->token
->pos
.whitespace
;
343 stream
->token
= NULL
;
352 * pp-number identifier-nodigit
359 static int get_one_number(int c
, int next
, stream_t
*stream
)
362 static char buffer
[256];
363 char *p
= buffer
, *buf
;
372 next
= nextchar(stream
);
373 if (next
!= '-' && next
!= '+')
375 /* Fallthrough for sign of 'e'/'p' */
378 case 'a'...'d': case 'A'...'D':
379 case 'f'...'o': case 'F'...'O':
380 case 'q'...'z': case 'Q'...'Z':
382 next
= nextchar(stream
);
389 buf
= __alloc_bytes(len
);
390 memcpy(buf
, buffer
, len
);
392 token
= stream
->token
;
393 token_type(token
) = TOKEN_NUMBER
;
400 static int escapechar(int first
, int type
, stream_t
*stream
, int *valp
)
404 next
= nextchar(stream
);
408 warn(stream
->pos
, "Newline in string or character constant");
410 if (first
== '\\' && next
!= EOF
) {
412 next
= nextchar(stream
);
446 warn(stream
->pos
, "Newline in string or character constant");
451 while (next
>= '0' && next
<= '9') {
452 value
= (value
<< 3) + (next
-'0');
453 next
= nextchar(stream
);
461 int hex
= hexval(next
);
464 next
= nextchar(stream
);
465 while ((hex
= hexval(next
)) < 16) {
466 value
= (value
<< 4) + hex
;
467 next
= nextchar(stream
);
475 warn(stream
->pos
, "Unknown escape '%c'", value
);
478 /* Mark it as escaped */
485 static int get_char_token(int next
, stream_t
*stream
)
490 next
= escapechar(next
, '\'', stream
, &value
);
491 if (value
== '\'' || next
!= '\'') {
492 warn(stream
->pos
, "Bad character constant");
497 token
= stream
->token
;
498 token_type(token
) = TOKEN_CHAR
;
499 token
->character
= value
& 0xff;
502 return nextchar(stream
);
505 static int get_string_token(int next
, stream_t
*stream
)
507 static char buffer
[512];
508 struct string
*string
;
514 next
= escapechar(next
, '"', stream
, &val
);
518 warn(stream
->pos
, "End of file in middle of string");
521 if (len
< sizeof(buffer
)) {
529 warn(stream
->pos
, "String too long");
531 string
= __alloc_string(len
+1);
532 memcpy(string
->data
, buffer
, len
);
533 string
->data
[len
] = '\0';
534 string
->length
= len
+1;
537 token
= stream
->token
;
538 token_type(token
) = TOKEN_STRING
;
539 token
->string
= string
;
545 static int drop_stream_eoln(stream_t
*stream
)
547 int next
= nextchar(stream
);
553 next
= nextchar(stream
);
559 static int drop_stream_comment(stream_t
*stream
)
564 newline
= stream
->pos
.newline
;
566 next
= nextchar(stream
);
570 warn(stream
->pos
, "End of file in the middle of a comment");
573 next
= nextchar(stream
);
574 if (curr
== '*' && next
== '/')
577 stream
->pos
.newline
= newline
;
578 return nextchar(stream
);
581 unsigned char combinations
[][3] = COMBINATION_STRINGS
;
583 #define NR_COMBINATIONS (sizeof(combinations)/3)
585 static int get_one_special(int c
, stream_t
*stream
)
588 unsigned char c1
, c2
, c3
;
592 next
= nextchar(stream
);
595 * Check for numbers, strings, character constants, and comments
599 if (next
>= '0' && next
<= '9')
600 return get_one_number(c
, next
, stream
);
603 return get_string_token(next
, stream
);
605 return get_char_token(next
, stream
);
608 return drop_stream_eoln(stream
);
610 return drop_stream_comment(stream
);
614 * Check for combinations
618 comb
= combinations
[0];
619 c1
= c
; c2
= next
; c3
= 0;
620 for (i
= 0; i
< NR_COMBINATIONS
; i
++) {
621 if (comb
[0] == c1
&& comb
[1] == c2
&& comb
[2] == c3
) {
622 value
= i
+ SPECIAL_BASE
;
623 next
= nextchar(stream
);
633 token
= stream
->token
;
634 token_type(token
) = TOKEN_SPECIAL
;
635 token
->special
= value
;
640 #define IDENT_HASH_BITS (10)
641 #define IDENT_HASH_SIZE (1<<IDENT_HASH_BITS)
642 #define IDENT_HASH_MASK (IDENT_HASH_SIZE-1)
644 #define ident_hash_init(c) (c)
645 #define ident_hash_add(oldhash,c) ((oldhash)*11 + (c))
646 #define ident_hash_end(hash) ((((hash) >> IDENT_HASH_BITS) + (hash)) & IDENT_HASH_MASK)
648 static struct ident
*hash_table
[IDENT_HASH_SIZE
];
649 int ident_hit
, ident_miss
;
651 void show_identifier_stats(void)
654 int distribution
[100];
656 fprintf(stderr
, "identifiers: %d hits, %d misses\n",
657 ident_hit
, ident_miss
);
659 for (i
= 0; i
< 100; i
++)
662 for (i
= 0; i
< IDENT_HASH_SIZE
; i
++) {
663 struct ident
* ident
= hash_table
[i
];
672 distribution
[count
]++;
675 for (i
= 0; i
< 100; i
++) {
677 fprintf(stderr
, "%2d: %d buckets\n", i
, distribution
[i
]);
681 static struct ident
*alloc_ident(const char *name
, int len
)
683 struct ident
*ident
= __alloc_ident(len
);
684 ident
->symbols
= NULL
;
687 memcpy(ident
->name
, name
, len
);
691 static struct ident
* insert_hash(struct ident
*ident
, unsigned long hash
)
693 ident
->next
= hash_table
[hash
];
694 hash_table
[hash
] = ident
;
699 static struct ident
*create_hashed_ident(const char *name
, int len
, unsigned long hash
)
703 ident
= hash_table
[hash
];
705 if (ident
->len
== len
&& !memcmp(ident
->name
, name
, len
)) {
712 return insert_hash(alloc_ident(name
, len
), hash
);
715 static unsigned long hash_name(const char *name
, int len
)
718 const unsigned char *p
= (const unsigned char *)name
;
720 hash
= ident_hash_init(*p
++);
722 unsigned int i
= *p
++;
723 hash
= ident_hash_add(hash
, i
);
725 return ident_hash_end(hash
);
728 struct ident
*hash_ident(struct ident
*ident
)
730 return insert_hash(ident
, hash_name(ident
->name
, ident
->len
));
733 struct ident
*built_in_ident(const char *name
)
735 int len
= strlen(name
);
736 return create_hashed_ident(name
, len
, hash_name(name
, len
));
739 struct token
*built_in_token(int stream
, const char *name
)
743 token
= __alloc_token(0);
744 token
->pos
.stream
= stream
;
745 token_type(token
) = TOKEN_IDENT
;
746 token
->ident
= built_in_ident(name
);
750 static int get_one_identifier(int c
, stream_t
*stream
)
759 hash
= ident_hash_init(c
);
762 next
= nextchar(stream
);
768 if (len
< sizeof(buf
)) {
769 hash
= ident_hash_add(hash
, next
);
777 hash
= ident_hash_end(hash
);
779 ident
= create_hashed_ident(buf
, len
, hash
);
782 token
= stream
->token
;
783 token_type(token
) = TOKEN_IDENT
;
784 token
->ident
= ident
;
789 static int get_one_token(int c
, stream_t
*stream
)
793 return get_one_number(c
, nextchar(stream
), stream
);
797 return get_one_identifier(c
, stream
);
799 return get_one_special(c
, stream
);
803 static struct token
*setup_stream(stream_t
*stream
, int idx
, int fd
,
804 unsigned char *buf
, unsigned int buf_size
)
808 stream
->pos
.stream
= idx
;
809 stream
->pos
.line
= 1;
810 stream
->pos
.newline
= 1;
811 stream
->pos
.whitespace
= 0;
813 stream
->pos
.noexpand
= 0;
815 stream
->token
= NULL
;
818 stream
->size
= buf_size
;
819 stream
->buffer
= buf
;
821 begin
= alloc_token(stream
);
822 token_type(begin
) = TOKEN_STREAMBEGIN
;
823 stream
->tokenlist
= &begin
->next
;
827 static void tokenize_stream(stream_t
*stream
, struct token
*endtoken
)
829 int c
= nextchar(stream
);
832 struct token
*token
= alloc_token(stream
);
833 stream
->token
= token
;
834 stream
->pos
.newline
= 0;
835 stream
->pos
.whitespace
= 0;
836 c
= get_one_token(c
, stream
);
839 stream
->pos
.whitespace
= 1;
840 c
= nextchar(stream
);
842 mark_eof(stream
, endtoken
);
845 struct token
* tokenize_buffer(unsigned char *buffer
, unsigned long size
, struct token
*endtoken
)
850 begin
= setup_stream(&stream
, 0, -1, buffer
, size
);
851 tokenize_stream(&stream
, endtoken
);
855 struct token
* tokenize(const char *name
, int fd
, struct token
*endtoken
)
859 unsigned char buffer
[BUFSIZE
];
862 idx
= init_stream(name
, fd
);
866 begin
= setup_stream(&stream
, idx
, fd
, buffer
, 0);
867 tokenize_stream(&stream
, endtoken
);