*new* check_macros: find macro precedence bugs
[smatch.git] / tokenize.c
blobfd2ef7c0c95ccb7a988a8300516e6fca04964649
1 /*
2 * This is a really stupid C tokenizer. It doesn't do any include
3 * files or anything complex at all. That's the preprocessor.
5 * Copyright (C) 2003 Transmeta Corp.
6 * 2003 Linus Torvalds
8 * Licensed under the Open Software License version 1.1
9 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <stdarg.h>
13 #include <stddef.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <unistd.h>
18 #include "lib.h"
19 #include "allocate.h"
20 #include "token.h"
21 #include "symbol.h"
23 #define EOF (-1)
25 int input_stream_nr = 0;
26 struct stream *input_streams;
27 static int input_streams_allocated;
28 unsigned int tabstop = 8;
29 int no_lineno = 0;
31 #define BUFSIZE (8192)
33 typedef struct {
34 int fd, offset, size;
35 int pos, line, nr;
36 int newline, whitespace;
37 struct token **tokenlist;
38 struct token *token;
39 unsigned char *buffer;
40 } stream_t;
42 const char *stream_name(int stream)
44 if (stream < 0 || stream > input_stream_nr)
45 return "<bad stream>";
46 return input_streams[stream].name;
49 static struct position stream_pos(stream_t *stream)
51 struct position pos;
52 pos.type = 0;
53 pos.stream = stream->nr;
54 pos.newline = stream->newline;
55 pos.whitespace = stream->whitespace;
56 pos.pos = stream->pos;
58 pos.line = stream->line;
59 if (no_lineno)
60 pos.line = 123456;
62 pos.noexpand = 0;
63 return pos;
66 const char *show_special(int val)
68 static char buffer[4];
70 buffer[0] = val;
71 buffer[1] = 0;
72 if (val >= SPECIAL_BASE)
73 strcpy(buffer, (char *) combinations[val - SPECIAL_BASE]);
74 return buffer;
77 const char *show_ident(const struct ident *ident)
79 static char buffer[256];
80 if (!ident)
81 return "<noident>";
82 sprintf(buffer, "%.*s", ident->len, ident->name);
83 return buffer;
86 static char *charstr(char *ptr, unsigned char c, unsigned char escape, unsigned char next)
88 if (isprint(c)) {
89 if (c == escape || c == '\\')
90 *ptr++ = '\\';
91 *ptr++ = c;
92 return ptr;
94 *ptr++ = '\\';
95 switch (c) {
96 case '\n':
97 *ptr++ = 'n';
98 return ptr;
99 case '\t':
100 *ptr++ = 't';
101 return ptr;
103 if (!isdigit(next))
104 return ptr + sprintf(ptr, "%o", c);
106 return ptr + sprintf(ptr, "%03o", c);
109 const char *show_string(const struct string *string)
111 static char buffer[4 * MAX_STRING + 3];
112 char *ptr;
113 int i;
115 if (!string->length)
116 return "<bad_string>";
117 ptr = buffer;
118 *ptr++ = '"';
119 for (i = 0; i < string->length-1; i++) {
120 const char *p = string->data + i;
121 ptr = charstr(ptr, p[0], '"', p[1]);
123 *ptr++ = '"';
124 *ptr = '\0';
125 return buffer;
128 const char *show_token(const struct token *token)
130 static char buffer[256];
132 if (!token)
133 return "<no token>";
134 switch (token_type(token)) {
135 case TOKEN_ERROR:
136 return "syntax error";
138 case TOKEN_EOF:
139 return "end-of-input";
141 case TOKEN_IDENT:
142 return show_ident(token->ident);
144 case TOKEN_STRING:
145 return show_string(token->string);
147 case TOKEN_NUMBER:
148 return token->number;
150 case TOKEN_SPECIAL:
151 return show_special(token->special);
153 case TOKEN_CHAR: {
154 char *ptr = buffer;
155 int c = token->character;
156 *ptr++ = '\'';
157 ptr = charstr(ptr, c, '\'', 0);
158 *ptr++ = '\'';
159 *ptr++ = '\0';
160 return buffer;
163 case TOKEN_STREAMBEGIN:
164 sprintf(buffer, "<beginning of '%s'>", stream_name(token->pos.stream));
165 return buffer;
167 case TOKEN_STREAMEND:
168 sprintf(buffer, "<end of '%s'>", stream_name(token->pos.stream));
169 return buffer;
171 case TOKEN_UNTAINT:
172 sprintf(buffer, "<untaint>");
173 return buffer;
175 case TOKEN_ARG_COUNT:
176 sprintf(buffer, "<argcnt>");
177 return buffer;
179 default:
180 sprintf(buffer, "unhandled token type '%d' ", token_type(token));
181 return buffer;
185 int init_stream(const char *name, int fd, const char **next_path)
187 int stream = input_stream_nr;
188 struct stream *current;
190 if (stream >= input_streams_allocated) {
191 int newalloc = stream * 4 / 3 + 10;
192 input_streams = realloc(input_streams, newalloc * sizeof(struct stream));
193 if (!input_streams)
194 die("Unable to allocate more streams space");
195 input_streams_allocated = newalloc;
197 current = input_streams + stream;
198 memset(current, 0, sizeof(*current));
199 current->name = name;
200 current->fd = fd;
201 current->next_path = next_path;
202 current->path = NULL;
203 current->constant = CONSTANT_FILE_MAYBE;
204 input_stream_nr = stream+1;
205 return stream;
208 static struct token * alloc_token(stream_t *stream)
210 struct token *token = __alloc_token(0);
211 token->pos = stream_pos(stream);
212 return token;
216 * Argh... That was surprisingly messy - handling '\r' complicates the
217 * things a _lot_.
219 static int nextchar_slow(stream_t *stream)
221 int offset = stream->offset;
222 int size = stream->size;
223 int c;
224 int spliced = 0, had_cr, had_backslash, complain;
226 restart:
227 had_cr = had_backslash = complain = 0;
229 repeat:
230 if (offset >= size) {
231 if (stream->fd < 0)
232 goto got_eof;
233 size = read(stream->fd, stream->buffer, BUFSIZE);
234 if (size <= 0)
235 goto got_eof;
236 stream->size = size;
237 stream->offset = offset = 0;
240 c = stream->buffer[offset++];
242 if (had_cr && c != '\n')
243 complain = 1;
245 if (c == '\r') {
246 had_cr = 1;
247 goto repeat;
250 stream->pos += (c == '\t') ? (tabstop - stream->pos % tabstop) : 1;
252 if (c == '\n') {
253 stream->line++;
254 stream->pos = 0;
257 if (!had_backslash) {
258 if (c == '\\') {
259 had_backslash = 1;
260 goto repeat;
262 if (c == '\n')
263 stream->newline = 1;
264 } else {
265 if (c == '\n') {
266 if (complain)
267 warning(stream_pos(stream), "non-ASCII data stream");
268 spliced = 1;
269 goto restart;
271 stream->pos--;
272 offset--;
273 c = '\\';
276 out:
277 stream->offset = offset;
278 if (complain)
279 warning(stream_pos(stream), "non-ASCII data stream");
281 return c;
283 got_eof:
284 if (had_backslash) {
285 c = '\\';
286 goto out;
288 if (stream->pos)
289 warning(stream_pos(stream), "no newline at end of file");
290 else if (had_cr)
291 warning(stream_pos(stream), "non-ASCII data stream");
292 else if (spliced)
293 warning(stream_pos(stream), "backslash-newline at end of file");
294 return EOF;
298 * We want that as light as possible while covering all normal cases.
299 * Slow path (including the logics with line-splicing and EOF sanity
300 * checks) is in nextchar_slow().
302 static inline int nextchar(stream_t *stream)
304 int offset = stream->offset;
306 if (offset < stream->size) {
307 int c = stream->buffer[offset++];
308 static const char special[256] = {
309 ['\t'] = 1, ['\r'] = 1, ['\n'] = 1, ['\\'] = 1
311 if (!special[c]) {
312 stream->offset = offset;
313 stream->pos++;
314 return c;
317 return nextchar_slow(stream);
320 struct token eof_token_entry;
322 static struct token *mark_eof(stream_t *stream)
324 struct token *end;
326 end = alloc_token(stream);
327 token_type(end) = TOKEN_STREAMEND;
328 end->pos.newline = 1;
330 eof_token_entry.next = &eof_token_entry;
331 eof_token_entry.pos.newline = 1;
333 end->next = &eof_token_entry;
334 *stream->tokenlist = end;
335 stream->tokenlist = NULL;
336 return end;
339 static void add_token(stream_t *stream)
341 struct token *token = stream->token;
343 stream->token = NULL;
344 token->next = NULL;
345 *stream->tokenlist = token;
346 stream->tokenlist = &token->next;
349 static void drop_token(stream_t *stream)
351 stream->newline |= stream->token->pos.newline;
352 stream->whitespace |= stream->token->pos.whitespace;
353 stream->token = NULL;
356 enum {
357 Letter = 1,
358 Digit = 2,
359 Hex = 4,
360 Exp = 8,
361 Dot = 16,
362 ValidSecond = 32,
365 static const long cclass[257] = {
366 ['0' + 1 ... '9' + 1] = Digit | Hex,
367 ['A' + 1 ... 'D' + 1] = Letter | Hex,
368 ['E' + 1] = Letter | Hex | Exp,
369 ['F' + 1] = Letter | Hex,
370 ['G' + 1 ... 'O' + 1] = Letter,
371 ['P' + 1] = Letter | Exp,
372 ['Q' + 1 ... 'Z' + 1] = Letter,
373 ['a' + 1 ... 'd' + 1] = Letter | Hex,
374 ['e' + 1] = Letter | Hex | Exp,
375 ['f' + 1] = Letter | Hex,
376 ['g' + 1 ... 'o' + 1] = Letter,
377 ['p' + 1] = Letter | Exp,
378 ['q' + 1 ... 'z' + 1] = Letter,
379 ['_' + 1] = Letter,
380 ['.' + 1] = Dot | ValidSecond,
381 ['=' + 1] = ValidSecond,
382 ['+' + 1] = ValidSecond,
383 ['-' + 1] = ValidSecond,
384 ['>' + 1] = ValidSecond,
385 ['<' + 1] = ValidSecond,
386 ['&' + 1] = ValidSecond,
387 ['|' + 1] = ValidSecond,
388 ['#' + 1] = ValidSecond,
392 * pp-number:
393 * digit
394 * . digit
395 * pp-number digit
396 * pp-number identifier-nodigit
397 * pp-number e sign
398 * pp-number E sign
399 * pp-number p sign
400 * pp-number P sign
401 * pp-number .
403 static int get_one_number(int c, int next, stream_t *stream)
405 struct token *token;
406 static char buffer[4095];
407 char *p = buffer, *buf, *buffer_end = buffer + sizeof (buffer);
408 int len;
410 *p++ = c;
411 for (;;) {
412 long class = cclass[next + 1];
413 if (!(class & (Dot | Digit | Letter)))
414 break;
415 if (p != buffer_end)
416 *p++ = next;
417 next = nextchar(stream);
418 if (class & Exp) {
419 if (next == '-' || next == '+') {
420 if (p != buffer_end)
421 *p++ = next;
422 next = nextchar(stream);
427 if (p == buffer_end) {
428 sparse_error(stream_pos(stream), "number token exceeds %td characters",
429 buffer_end - buffer);
430 // Pretend we saw just "1".
431 buffer[0] = '1';
432 p = buffer + 1;
435 *p++ = 0;
436 len = p - buffer;
437 buf = __alloc_bytes(len);
438 memcpy(buf, buffer, len);
440 token = stream->token;
441 token_type(token) = TOKEN_NUMBER;
442 token->number = buf;
443 add_token(stream);
445 return next;
448 static int escapechar(int first, int type, stream_t *stream, int *valp)
450 int next, value;
452 next = nextchar(stream);
453 value = first;
455 if (first == '\n')
456 warning(stream_pos(stream), "Newline in string or character constant");
458 if (first == '\\' && next != EOF) {
459 value = next;
460 next = nextchar(stream);
461 if (value != type) {
462 switch (value) {
463 case 'a':
464 value = '\a';
465 break;
466 case 'b':
467 value = '\b';
468 break;
469 case 't':
470 value = '\t';
471 break;
472 case 'n':
473 value = '\n';
474 break;
475 case 'v':
476 value = '\v';
477 break;
478 case 'f':
479 value = '\f';
480 break;
481 case 'r':
482 value = '\r';
483 break;
484 case 'e':
485 value = '\e';
486 break;
487 case '\\':
488 break;
489 case '?':
490 break;
491 case '\'':
492 break;
493 case '"':
494 break;
495 case '\n':
496 warning(stream_pos(stream), "Newline in string or character constant");
497 break;
498 case '0'...'7': {
499 int nr = 2;
500 value -= '0';
501 while (next >= '0' && next <= '9') {
502 value = (value << 3) + (next-'0');
503 next = nextchar(stream);
504 if (!--nr)
505 break;
507 value &= 0xff;
508 break;
510 case 'x': {
511 int hex = hexval(next);
512 if (hex < 16) {
513 value = hex;
514 next = nextchar(stream);
515 while ((hex = hexval(next)) < 16) {
516 value = (value << 4) + hex;
517 next = nextchar(stream);
519 value &= 0xff;
520 break;
523 /* Fall through */
524 default:
525 warning(stream_pos(stream), "Unknown escape '%c'", value);
528 /* Mark it as escaped */
529 value |= 0x100;
531 *valp = value;
532 return next;
535 static int get_char_token(int next, stream_t *stream)
537 int value;
538 struct token *token;
540 next = escapechar(next, '\'', stream, &value);
541 if (value == '\'' || next != '\'') {
542 sparse_error(stream_pos(stream), "Bad character constant");
543 drop_token(stream);
544 return next;
547 token = stream->token;
548 token_type(token) = TOKEN_CHAR;
549 token->character = value & 0xff;
551 add_token(stream);
552 return nextchar(stream);
555 static int get_string_token(int next, stream_t *stream)
557 static char buffer[MAX_STRING];
558 struct string *string;
559 struct token *token;
560 int len = 0;
562 for (;;) {
563 int val;
564 next = escapechar(next, '"', stream, &val);
565 if (val == '"')
566 break;
567 if (next == EOF) {
568 warning(stream_pos(stream), "End of file in middle of string");
569 return next;
571 if (len < MAX_STRING)
572 buffer[len] = val;
573 len++;
576 if (len > MAX_STRING) {
577 warning(stream_pos(stream), "string too long (%d bytes, %d bytes max)", len, MAX_STRING);
578 len = MAX_STRING;
581 string = __alloc_string(len+1);
582 memcpy(string->data, buffer, len);
583 string->data[len] = '\0';
584 string->length = len+1;
586 /* Pass it on.. */
587 token = stream->token;
588 token_type(token) = TOKEN_STRING;
589 token->string = string;
590 add_token(stream);
592 return next;
595 static int drop_stream_eoln(stream_t *stream)
597 drop_token(stream);
598 for (;;) {
599 switch (nextchar(stream)) {
600 case EOF:
601 return EOF;
602 case '\n':
603 return nextchar(stream);
608 static int drop_stream_comment(stream_t *stream)
610 int newline;
611 int next;
612 drop_token(stream);
613 newline = stream->newline;
615 next = nextchar(stream);
616 for (;;) {
617 int curr = next;
618 if (curr == EOF) {
619 warning(stream_pos(stream), "End of file in the middle of a comment");
620 return curr;
622 next = nextchar(stream);
623 if (curr == '*' && next == '/')
624 break;
626 stream->newline = newline;
627 return nextchar(stream);
630 unsigned char combinations[][4] = COMBINATION_STRINGS;
632 #define NR_COMBINATIONS (SPECIAL_ARG_SEPARATOR - SPECIAL_BASE)
634 /* hash function for two-character punctuators - all give unique values */
635 #define special_hash(c0, c1) (((c0*8+c1*2)+((c0*8+c1*2)>>5))&31)
638 * note that we won't get false positives - special_hash(0,0) is 0 and
639 * entry 0 is filled (by +=), so all the missing ones are OK.
641 static unsigned char hash_results[32][2] = {
642 #define RES(c0, c1) [special_hash(c0, c1)] = {c0, c1}
643 RES('+', '='), /* 00 */
644 RES('/', '='), /* 01 */
645 RES('^', '='), /* 05 */
646 RES('&', '&'), /* 07 */
647 RES('#', '#'), /* 08 */
648 RES('<', '<'), /* 0a */
649 RES('<', '='), /* 0c */
650 RES('!', '='), /* 0e */
651 RES('%', '='), /* 0f */
652 RES('-', '-'), /* 10 */
653 RES('-', '='), /* 11 */
654 RES('-', '>'), /* 13 */
655 RES('=', '='), /* 15 */
656 RES('&', '='), /* 17 */
657 RES('*', '='), /* 18 */
658 RES('.', '.'), /* 1a */
659 RES('+', '+'), /* 1b */
660 RES('|', '='), /* 1c */
661 RES('>', '='), /* 1d */
662 RES('|', '|'), /* 1e */
663 RES('>', '>') /* 1f */
664 #undef RES
666 static int code[32] = {
667 #define CODE(c0, c1, value) [special_hash(c0, c1)] = value
668 CODE('+', '=', SPECIAL_ADD_ASSIGN), /* 00 */
669 CODE('/', '=', SPECIAL_DIV_ASSIGN), /* 01 */
670 CODE('^', '=', SPECIAL_XOR_ASSIGN), /* 05 */
671 CODE('&', '&', SPECIAL_LOGICAL_AND), /* 07 */
672 CODE('#', '#', SPECIAL_HASHHASH), /* 08 */
673 CODE('<', '<', SPECIAL_LEFTSHIFT), /* 0a */
674 CODE('<', '=', SPECIAL_LTE), /* 0c */
675 CODE('!', '=', SPECIAL_NOTEQUAL), /* 0e */
676 CODE('%', '=', SPECIAL_MOD_ASSIGN), /* 0f */
677 CODE('-', '-', SPECIAL_DECREMENT), /* 10 */
678 CODE('-', '=', SPECIAL_SUB_ASSIGN), /* 11 */
679 CODE('-', '>', SPECIAL_DEREFERENCE), /* 13 */
680 CODE('=', '=', SPECIAL_EQUAL), /* 15 */
681 CODE('&', '=', SPECIAL_AND_ASSIGN), /* 17 */
682 CODE('*', '=', SPECIAL_MUL_ASSIGN), /* 18 */
683 CODE('.', '.', SPECIAL_DOTDOT), /* 1a */
684 CODE('+', '+', SPECIAL_INCREMENT), /* 1b */
685 CODE('|', '=', SPECIAL_OR_ASSIGN), /* 1c */
686 CODE('>', '=', SPECIAL_GTE), /* 1d */
687 CODE('|', '|', SPECIAL_LOGICAL_OR), /* 1e */
688 CODE('>', '>', SPECIAL_RIGHTSHIFT) /* 1f */
689 #undef CODE
692 static int get_one_special(int c, stream_t *stream)
694 struct token *token;
695 int next, value, i;
697 next = nextchar(stream);
700 * Check for numbers, strings, character constants, and comments
702 switch (c) {
703 case '.':
704 if (next >= '0' && next <= '9')
705 return get_one_number(c, next, stream);
706 break;
707 case '"':
708 return get_string_token(next, stream);
709 case '\'':
710 return get_char_token(next, stream);
711 case '/':
712 if (next == '/')
713 return drop_stream_eoln(stream);
714 if (next == '*')
715 return drop_stream_comment(stream);
719 * Check for combinations
721 value = c;
722 if (cclass[next + 1] & ValidSecond) {
723 i = special_hash(c, next);
724 if (hash_results[i][0] == c && hash_results[i][1] == next) {
725 value = code[i];
726 next = nextchar(stream);
727 if (value >= SPECIAL_LEFTSHIFT &&
728 next == "==."[value - SPECIAL_LEFTSHIFT]) {
729 value += 3;
730 next = nextchar(stream);
735 /* Pass it on.. */
736 token = stream->token;
737 token_type(token) = TOKEN_SPECIAL;
738 token->special = value;
739 add_token(stream);
740 return next;
743 #define IDENT_HASH_BITS (13)
744 #define IDENT_HASH_SIZE (1<<IDENT_HASH_BITS)
745 #define IDENT_HASH_MASK (IDENT_HASH_SIZE-1)
747 #define ident_hash_init(c) (c)
748 #define ident_hash_add(oldhash,c) ((oldhash)*11 + (c))
749 #define ident_hash_end(hash) ((((hash) >> IDENT_HASH_BITS) + (hash)) & IDENT_HASH_MASK)
751 static struct ident *hash_table[IDENT_HASH_SIZE];
752 static int ident_hit, ident_miss, idents;
754 void show_identifier_stats(void)
756 int i;
757 int distribution[100];
759 fprintf(stderr, "identifiers: %d hits, %d misses\n",
760 ident_hit, ident_miss);
762 for (i = 0; i < 100; i++)
763 distribution[i] = 0;
765 for (i = 0; i < IDENT_HASH_SIZE; i++) {
766 struct ident * ident = hash_table[i];
767 int count = 0;
769 while (ident) {
770 count++;
771 ident = ident->next;
773 if (count > 99)
774 count = 99;
775 distribution[count]++;
778 for (i = 0; i < 100; i++) {
779 if (distribution[i])
780 fprintf(stderr, "%2d: %d buckets\n", i, distribution[i]);
784 static struct ident *alloc_ident(const char *name, int len)
786 struct ident *ident = __alloc_ident(len);
787 ident->symbols = NULL;
788 ident->len = len;
789 ident->tainted = 0;
790 memcpy(ident->name, name, len);
791 return ident;
794 static struct ident * insert_hash(struct ident *ident, unsigned long hash)
796 ident->next = hash_table[hash];
797 hash_table[hash] = ident;
798 ident_miss++;
799 return ident;
802 static struct ident *create_hashed_ident(const char *name, int len, unsigned long hash)
804 struct ident *ident;
805 struct ident **p;
807 p = &hash_table[hash];
808 while ((ident = *p) != NULL) {
809 if (ident->len == (unsigned char) len) {
810 if (strncmp(name, ident->name, len) != 0)
811 goto next;
813 ident_hit++;
814 return ident;
816 next:
817 //misses++;
818 p = &ident->next;
820 ident = alloc_ident(name, len);
821 *p = ident;
822 ident->next = NULL;
823 ident_miss++;
824 idents++;
825 return ident;
828 static unsigned long hash_name(const char *name, int len)
830 unsigned long hash;
831 const unsigned char *p = (const unsigned char *)name;
833 hash = ident_hash_init(*p++);
834 while (--len) {
835 unsigned int i = *p++;
836 hash = ident_hash_add(hash, i);
838 return ident_hash_end(hash);
841 struct ident *hash_ident(struct ident *ident)
843 return insert_hash(ident, hash_name(ident->name, ident->len));
846 struct ident *built_in_ident(const char *name)
848 int len = strlen(name);
849 return create_hashed_ident(name, len, hash_name(name, len));
852 struct token *built_in_token(int stream, const char *name)
854 struct token *token;
856 token = __alloc_token(0);
857 token->pos.stream = stream;
858 token_type(token) = TOKEN_IDENT;
859 token->ident = built_in_ident(name);
860 return token;
863 static int get_one_identifier(int c, stream_t *stream)
865 struct token *token;
866 struct ident *ident;
867 unsigned long hash;
868 char buf[256];
869 int len = 1;
870 int next;
872 hash = ident_hash_init(c);
873 buf[0] = c;
874 for (;;) {
875 next = nextchar(stream);
876 if (!(cclass[next + 1] & (Letter | Digit)))
877 break;
878 if (len >= sizeof(buf))
879 break;
880 hash = ident_hash_add(hash, next);
881 buf[len] = next;
882 len++;
884 hash = ident_hash_end(hash);
886 ident = create_hashed_ident(buf, len, hash);
888 /* Pass it on.. */
889 token = stream->token;
890 token_type(token) = TOKEN_IDENT;
891 token->ident = ident;
892 add_token(stream);
893 return next;
896 static int get_one_token(int c, stream_t *stream)
898 long class = cclass[c + 1];
899 if (class & Digit)
900 return get_one_number(c, nextchar(stream), stream);
901 if (class & Letter)
902 return get_one_identifier(c, stream);
903 return get_one_special(c, stream);
906 static struct token *setup_stream(stream_t *stream, int idx, int fd,
907 unsigned char *buf, unsigned int buf_size)
909 struct token *begin;
911 stream->nr = idx;
912 stream->line = 1;
913 stream->newline = 1;
914 stream->whitespace = 0;
915 stream->pos = 0;
917 stream->token = NULL;
918 stream->fd = fd;
919 stream->offset = 0;
920 stream->size = buf_size;
921 stream->buffer = buf;
923 begin = alloc_token(stream);
924 token_type(begin) = TOKEN_STREAMBEGIN;
925 stream->tokenlist = &begin->next;
926 return begin;
929 static struct token *tokenize_stream(stream_t *stream)
931 int c = nextchar(stream);
932 while (c != EOF) {
933 if (!isspace(c)) {
934 struct token *token = alloc_token(stream);
935 stream->token = token;
936 stream->newline = 0;
937 stream->whitespace = 0;
938 c = get_one_token(c, stream);
939 continue;
941 stream->whitespace = 1;
942 c = nextchar(stream);
944 return mark_eof(stream);
947 struct token * tokenize_buffer(void *buffer, unsigned long size, struct token **endtoken)
949 stream_t stream;
950 struct token *begin;
952 begin = setup_stream(&stream, 0, -1, buffer, size);
953 *endtoken = tokenize_stream(&stream);
954 return begin;
957 struct token * tokenize(const char *name, int fd, struct token *endtoken, const char **next_path)
959 struct token *begin, *end;
960 stream_t stream;
961 unsigned char buffer[BUFSIZE];
962 int idx;
964 idx = init_stream(name, fd, next_path);
965 if (idx < 0) {
966 // info(endtoken->pos, "File %s is const", name);
967 return endtoken;
970 begin = setup_stream(&stream, idx, fd, buffer, 0);
971 end = tokenize_stream(&stream);
972 if (endtoken)
973 end->next = endtoken;
974 return begin;