Let void have sizeof 1
[smatch.git] / tokenize.c
blob7c41a56df6556c293bd1d6b5f99102d60e59b821
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;
29 #define BUFSIZE (8192)
31 typedef struct {
32 int fd, offset, size;
33 int pos, line, nr;
34 int newline, whitespace;
35 struct token **tokenlist;
36 struct token *token;
37 unsigned char *buffer;
38 } stream_t;
40 const char *stream_name(int stream)
42 if (stream < 0 || stream > input_stream_nr)
43 return "<bad stream>";
44 return input_streams[stream].name;
47 static struct position stream_pos(stream_t *stream)
49 struct position pos;
50 pos.type = 0;
51 pos.stream = stream->nr;
52 pos.newline = stream->newline;
53 pos.whitespace = stream->whitespace;
54 pos.pos = stream->pos;
55 pos.line = stream->line;
56 pos.noexpand = 0;
57 return pos;
60 const char *show_special(int val)
62 static char buffer[4];
64 buffer[0] = val;
65 buffer[1] = 0;
66 if (val >= SPECIAL_BASE)
67 strcpy(buffer, (char *) combinations[val - SPECIAL_BASE]);
68 return buffer;
71 const char *show_ident(const struct ident *ident)
73 static char buffer[256];
74 if (!ident)
75 return "<noident>";
76 sprintf(buffer, "%.*s", ident->len, ident->name);
77 return buffer;
80 static char *charstr(char *ptr, unsigned char c, unsigned char escape, unsigned char next)
82 if (isprint(c)) {
83 if (c == escape || c == '\\')
84 *ptr++ = '\\';
85 *ptr++ = c;
86 return ptr;
88 *ptr++ = '\\';
89 switch (c) {
90 case '\n':
91 *ptr++ = 'n';
92 return ptr;
93 case '\t':
94 *ptr++ = 't';
95 return ptr;
97 if (!isdigit(next))
98 return ptr + sprintf(ptr, "%o", c);
100 return ptr + sprintf(ptr, "%03o", c);
103 const char *show_string(const struct string *string)
105 static char buffer[4 * MAX_STRING + 3];
106 char *ptr;
107 int i;
109 if (!string->length)
110 return "<bad_string>";
111 ptr = buffer;
112 *ptr++ = '"';
113 for (i = 0; i < string->length-1; i++) {
114 const char *p = string->data + i;
115 ptr = charstr(ptr, p[0], '"', p[1]);
117 *ptr++ = '"';
118 *ptr = '\0';
119 return buffer;
122 const char *show_token(const struct token *token)
124 static char buffer[256];
126 if (!token)
127 return "<no token>";
128 switch (token_type(token)) {
129 case TOKEN_ERROR:
130 return "syntax error";
132 case TOKEN_EOF:
133 return "end-of-input";
135 case TOKEN_IDENT:
136 return show_ident(token->ident);
138 case TOKEN_STRING:
139 return show_string(token->string);
141 case TOKEN_NUMBER:
142 return token->number;
144 case TOKEN_SPECIAL:
145 return show_special(token->special);
147 case TOKEN_CHAR: {
148 char *ptr = buffer;
149 int c = token->character;
150 *ptr++ = '\'';
151 ptr = charstr(ptr, c, '\'', 0);
152 *ptr++ = '\'';
153 *ptr++ = '\0';
154 return buffer;
157 case TOKEN_STREAMBEGIN:
158 sprintf(buffer, "<beginning of '%s'>", stream_name(token->pos.stream));
159 return buffer;
161 case TOKEN_STREAMEND:
162 sprintf(buffer, "<end of '%s'>", stream_name(token->pos.stream));
163 return buffer;
165 default:
166 return "WTF???";
170 int init_stream(const char *name, int fd, const char **next_path)
172 int stream = input_stream_nr;
173 struct stream *current;
175 if (stream >= input_streams_allocated) {
176 int newalloc = stream * 4 / 3 + 10;
177 input_streams = realloc(input_streams, newalloc * sizeof(struct stream));
178 if (!input_streams)
179 die("Unable to allocate more streams space");
180 input_streams_allocated = newalloc;
182 current = input_streams + stream;
183 memset(current, 0, sizeof(*current));
184 current->name = name;
185 current->fd = fd;
186 current->next_path = next_path;
187 current->path = NULL;
188 current->constant = CONSTANT_FILE_MAYBE;
189 input_stream_nr = stream+1;
190 return stream;
193 static struct token * alloc_token(stream_t *stream)
195 struct token *token = __alloc_token(0);
196 token->pos = stream_pos(stream);
197 return token;
201 * Argh... That was surprisingly messy - handling '\r' complicates the
202 * things a _lot_.
204 static int nextchar_slow(stream_t *stream)
206 int offset = stream->offset;
207 int size = stream->size;
208 int c;
209 int spliced = 0, had_cr, had_backslash, complain;
211 restart:
212 had_cr = had_backslash = complain = 0;
214 repeat:
215 if (offset >= size) {
216 if (stream->fd < 0)
217 goto got_eof;
218 size = read(stream->fd, stream->buffer, BUFSIZE);
219 if (size <= 0)
220 goto got_eof;
221 stream->size = size;
222 stream->offset = offset = 0;
225 c = stream->buffer[offset++];
227 if (had_cr && c != '\n')
228 complain = 1;
230 if (c == '\r') {
231 had_cr = 1;
232 goto repeat;
235 stream->pos++;
237 if (c == '\n') {
238 stream->line++;
239 stream->pos = 0;
242 if (!had_backslash) {
243 if (c == '\\') {
244 had_backslash = 1;
245 goto repeat;
247 if (c == '\n')
248 stream->newline = 1;
249 } else {
250 if (c == '\n') {
251 if (complain)
252 warning(stream_pos(stream), "non-ASCII data stream");
253 spliced = 1;
254 goto restart;
256 stream->pos--;
257 offset--;
258 c = '\\';
261 out:
262 stream->offset = offset;
263 if (complain)
264 warning(stream_pos(stream), "non-ASCII data stream");
266 return c;
268 got_eof:
269 if (had_backslash) {
270 c = '\\';
271 goto out;
273 if (stream->pos)
274 warning(stream_pos(stream), "no newline at end of file");
275 else if (had_cr)
276 warning(stream_pos(stream), "non-ASCII data stream");
277 else if (spliced)
278 warning(stream_pos(stream), "backslash-newline at end of file");
279 return EOF;
283 * We want that as light as possible while covering all normal cases.
284 * Slow path (including the logics with line-splicing and EOF sanity
285 * checks) is in nextchar_slow().
287 static inline int nextchar(stream_t *stream)
289 int offset = stream->offset;
291 if (offset < stream->size) {
292 int c = stream->buffer[offset++];
293 static const char special[256] = {
294 ['\r'] = 1, ['\n'] = 1, ['\\'] = 1
296 if (!special[c]) {
297 stream->offset = offset;
298 stream->pos++;
299 return c;
302 return nextchar_slow(stream);
305 struct token eof_token_entry;
307 static struct token *mark_eof(stream_t *stream)
309 struct token *end;
311 end = alloc_token(stream);
312 token_type(end) = TOKEN_STREAMEND;
313 end->pos.newline = 1;
315 eof_token_entry.next = &eof_token_entry;
316 eof_token_entry.pos.newline = 1;
318 end->next = &eof_token_entry;
319 *stream->tokenlist = end;
320 stream->tokenlist = NULL;
321 return end;
324 static void add_token(stream_t *stream)
326 struct token *token = stream->token;
328 stream->token = NULL;
329 token->next = NULL;
330 *stream->tokenlist = token;
331 stream->tokenlist = &token->next;
334 static void drop_token(stream_t *stream)
336 stream->newline |= stream->token->pos.newline;
337 stream->whitespace |= stream->token->pos.whitespace;
338 stream->token = NULL;
341 enum {
342 Letter = 1,
343 Digit = 2,
344 Hex = 4,
345 Exp = 8,
346 Dot = 16,
347 ValidSecond = 32,
350 static const long cclass[257] = {
351 ['0' + 1 ... '9' + 1] = Digit | Hex,
352 ['A' + 1 ... 'D' + 1] = Letter | Hex,
353 ['E' + 1] = Letter | Hex | Exp,
354 ['F' + 1] = Letter | Hex,
355 ['G' + 1 ... 'O' + 1] = Letter,
356 ['P' + 1] = Letter | Exp,
357 ['Q' + 1 ... 'Z' + 1] = Letter,
358 ['a' + 1 ... 'd' + 1] = Letter | Hex,
359 ['e' + 1] = Letter | Hex | Exp,
360 ['f' + 1] = Letter | Hex,
361 ['g' + 1 ... 'o' + 1] = Letter,
362 ['p' + 1] = Letter | Exp,
363 ['q' + 1 ... 'z' + 1] = Letter,
364 ['_' + 1] = Letter,
365 ['.' + 1] = Dot | ValidSecond,
366 ['=' + 1] = ValidSecond,
367 ['+' + 1] = ValidSecond,
368 ['-' + 1] = ValidSecond,
369 ['>' + 1] = ValidSecond,
370 ['<' + 1] = ValidSecond,
371 ['&' + 1] = ValidSecond,
372 ['|' + 1] = ValidSecond,
373 ['#' + 1] = ValidSecond,
377 * pp-number:
378 * digit
379 * . digit
380 * pp-number digit
381 * pp-number identifier-nodigit
382 * pp-number e sign
383 * pp-number E sign
384 * pp-number p sign
385 * pp-number P sign
386 * pp-number .
388 static int get_one_number(int c, int next, stream_t *stream)
390 struct token *token;
391 static char buffer[4095];
392 char *p = buffer, *buf, *buffer_end = buffer + sizeof (buffer);
393 int len;
395 *p++ = c;
396 for (;;) {
397 long class = cclass[next + 1];
398 if (!(class & (Dot | Digit | Letter)))
399 break;
400 if (p != buffer_end)
401 *p++ = next;
402 next = nextchar(stream);
403 if (class & Exp) {
404 if (next == '-' || next == '+') {
405 if (p != buffer_end)
406 *p++ = next;
407 next = nextchar(stream);
412 if (p == buffer_end) {
413 sparse_error(stream_pos(stream), "number token exceeds %td characters",
414 buffer_end - buffer);
415 // Pretend we saw just "1".
416 buffer[0] = '1';
417 p = buffer + 1;
420 *p++ = 0;
421 len = p - buffer;
422 buf = __alloc_bytes(len);
423 memcpy(buf, buffer, len);
425 token = stream->token;
426 token_type(token) = TOKEN_NUMBER;
427 token->number = buf;
428 add_token(stream);
430 return next;
433 static int escapechar(int first, int type, stream_t *stream, int *valp)
435 int next, value;
437 next = nextchar(stream);
438 value = first;
440 if (first == '\n')
441 warning(stream_pos(stream), "Newline in string or character constant");
443 if (first == '\\' && next != EOF) {
444 value = next;
445 next = nextchar(stream);
446 if (value != type) {
447 switch (value) {
448 case 'a':
449 value = '\a';
450 break;
451 case 'b':
452 value = '\b';
453 break;
454 case 't':
455 value = '\t';
456 break;
457 case 'n':
458 value = '\n';
459 break;
460 case 'v':
461 value = '\v';
462 break;
463 case 'f':
464 value = '\f';
465 break;
466 case 'r':
467 value = '\r';
468 break;
469 case 'e':
470 value = '\e';
471 break;
472 case '\\':
473 break;
474 case '?':
475 break;
476 case '\'':
477 break;
478 case '"':
479 break;
480 case '\n':
481 warning(stream_pos(stream), "Newline in string or character constant");
482 break;
483 case '0'...'7': {
484 int nr = 2;
485 value -= '0';
486 while (next >= '0' && next <= '9') {
487 value = (value << 3) + (next-'0');
488 next = nextchar(stream);
489 if (!--nr)
490 break;
492 value &= 0xff;
493 break;
495 case 'x': {
496 int hex = hexval(next);
497 if (hex < 16) {
498 value = hex;
499 next = nextchar(stream);
500 while ((hex = hexval(next)) < 16) {
501 value = (value << 4) + hex;
502 next = nextchar(stream);
504 value &= 0xff;
505 break;
508 /* Fall through */
509 default:
510 warning(stream_pos(stream), "Unknown escape '%c'", value);
513 /* Mark it as escaped */
514 value |= 0x100;
516 *valp = value;
517 return next;
520 static int get_char_token(int next, stream_t *stream)
522 int value;
523 struct token *token;
525 next = escapechar(next, '\'', stream, &value);
526 if (value == '\'' || next != '\'') {
527 sparse_error(stream_pos(stream), "Bad character constant");
528 drop_token(stream);
529 return next;
532 token = stream->token;
533 token_type(token) = TOKEN_CHAR;
534 token->character = value & 0xff;
536 add_token(stream);
537 return nextchar(stream);
540 static int get_string_token(int next, stream_t *stream)
542 static char buffer[MAX_STRING];
543 struct string *string;
544 struct token *token;
545 int len = 0;
547 for (;;) {
548 int val;
549 next = escapechar(next, '"', stream, &val);
550 if (val == '"')
551 break;
552 if (next == EOF) {
553 warning(stream_pos(stream), "End of file in middle of string");
554 return next;
556 if (len < MAX_STRING)
557 buffer[len] = val;
558 len++;
561 if (len > MAX_STRING) {
562 warning(stream_pos(stream), "string too long (%d bytes, %d bytes max)", len, MAX_STRING);
563 len = MAX_STRING;
566 string = __alloc_string(len+1);
567 memcpy(string->data, buffer, len);
568 string->data[len] = '\0';
569 string->length = len+1;
571 /* Pass it on.. */
572 token = stream->token;
573 token_type(token) = TOKEN_STRING;
574 token->string = string;
575 add_token(stream);
577 return next;
580 static int drop_stream_eoln(stream_t *stream)
582 drop_token(stream);
583 for (;;) {
584 switch (nextchar(stream)) {
585 case EOF:
586 return EOF;
587 case '\n':
588 return nextchar(stream);
593 static int drop_stream_comment(stream_t *stream)
595 int newline;
596 int next;
597 drop_token(stream);
598 newline = stream->newline;
600 next = nextchar(stream);
601 for (;;) {
602 int curr = next;
603 if (curr == EOF) {
604 warning(stream_pos(stream), "End of file in the middle of a comment");
605 return curr;
607 next = nextchar(stream);
608 if (curr == '*' && next == '/')
609 break;
611 stream->newline = newline;
612 return nextchar(stream);
615 unsigned char combinations[][4] = COMBINATION_STRINGS;
617 #define NR_COMBINATIONS (SPECIAL_ARG_SEPARATOR - SPECIAL_BASE)
619 /* hash function for two-character punctuators - all give unique values */
620 #define special_hash(c0, c1) (((c0*8+c1*2)+((c0*8+c1*2)>>5))&31)
623 * note that we won't get false positives - special_hash(0,0) is 0 and
624 * entry 0 is filled (by +=), so all the missing ones are OK.
626 static unsigned char hash_results[32][2] = {
627 #define RES(c0, c1) [special_hash(c0, c1)] = {c0, c1}
628 RES('+', '='), /* 00 */
629 RES('/', '='), /* 01 */
630 RES('^', '='), /* 05 */
631 RES('&', '&'), /* 07 */
632 RES('#', '#'), /* 08 */
633 RES('<', '<'), /* 0a */
634 RES('<', '='), /* 0c */
635 RES('!', '='), /* 0e */
636 RES('%', '='), /* 0f */
637 RES('-', '-'), /* 10 */
638 RES('-', '='), /* 11 */
639 RES('-', '>'), /* 13 */
640 RES('=', '='), /* 15 */
641 RES('&', '='), /* 17 */
642 RES('*', '='), /* 18 */
643 RES('.', '.'), /* 1a */
644 RES('+', '+'), /* 1b */
645 RES('|', '='), /* 1c */
646 RES('>', '='), /* 1d */
647 RES('|', '|'), /* 1e */
648 RES('>', '>') /* 1f */
649 #undef RES
651 static int code[32] = {
652 #define CODE(c0, c1, value) [special_hash(c0, c1)] = value
653 CODE('+', '=', SPECIAL_ADD_ASSIGN), /* 00 */
654 CODE('/', '=', SPECIAL_DIV_ASSIGN), /* 01 */
655 CODE('^', '=', SPECIAL_XOR_ASSIGN), /* 05 */
656 CODE('&', '&', SPECIAL_LOGICAL_AND), /* 07 */
657 CODE('#', '#', SPECIAL_HASHHASH), /* 08 */
658 CODE('<', '<', SPECIAL_LEFTSHIFT), /* 0a */
659 CODE('<', '=', SPECIAL_LTE), /* 0c */
660 CODE('!', '=', SPECIAL_NOTEQUAL), /* 0e */
661 CODE('%', '=', SPECIAL_MOD_ASSIGN), /* 0f */
662 CODE('-', '-', SPECIAL_DECREMENT), /* 10 */
663 CODE('-', '=', SPECIAL_SUB_ASSIGN), /* 11 */
664 CODE('-', '>', SPECIAL_DEREFERENCE), /* 13 */
665 CODE('=', '=', SPECIAL_EQUAL), /* 15 */
666 CODE('&', '=', SPECIAL_AND_ASSIGN), /* 17 */
667 CODE('*', '=', SPECIAL_MUL_ASSIGN), /* 18 */
668 CODE('.', '.', SPECIAL_DOTDOT), /* 1a */
669 CODE('+', '+', SPECIAL_INCREMENT), /* 1b */
670 CODE('|', '=', SPECIAL_OR_ASSIGN), /* 1c */
671 CODE('>', '=', SPECIAL_GTE), /* 1d */
672 CODE('|', '|', SPECIAL_LOGICAL_OR), /* 1e */
673 CODE('>', '>', SPECIAL_RIGHTSHIFT) /* 1f */
674 #undef CODE
677 static int get_one_special(int c, stream_t *stream)
679 struct token *token;
680 int next, value, i;
682 next = nextchar(stream);
685 * Check for numbers, strings, character constants, and comments
687 switch (c) {
688 case '.':
689 if (next >= '0' && next <= '9')
690 return get_one_number(c, next, stream);
691 break;
692 case '"':
693 return get_string_token(next, stream);
694 case '\'':
695 return get_char_token(next, stream);
696 case '/':
697 if (next == '/')
698 return drop_stream_eoln(stream);
699 if (next == '*')
700 return drop_stream_comment(stream);
704 * Check for combinations
706 value = c;
707 if (cclass[next + 1] & ValidSecond) {
708 i = special_hash(c, next);
709 if (hash_results[i][0] == c && hash_results[i][1] == next) {
710 value = code[i];
711 next = nextchar(stream);
712 if (value >= SPECIAL_LEFTSHIFT &&
713 next == "==."[value - SPECIAL_LEFTSHIFT]) {
714 value += 3;
715 next = nextchar(stream);
720 /* Pass it on.. */
721 token = stream->token;
722 token_type(token) = TOKEN_SPECIAL;
723 token->special = value;
724 add_token(stream);
725 return next;
728 #define IDENT_HASH_BITS (13)
729 #define IDENT_HASH_SIZE (1<<IDENT_HASH_BITS)
730 #define IDENT_HASH_MASK (IDENT_HASH_SIZE-1)
732 #define ident_hash_init(c) (c)
733 #define ident_hash_add(oldhash,c) ((oldhash)*11 + (c))
734 #define ident_hash_end(hash) ((((hash) >> IDENT_HASH_BITS) + (hash)) & IDENT_HASH_MASK)
736 static struct ident *hash_table[IDENT_HASH_SIZE];
737 static int ident_hit, ident_miss, idents;
739 void show_identifier_stats(void)
741 int i;
742 int distribution[100];
744 fprintf(stderr, "identifiers: %d hits, %d misses\n",
745 ident_hit, ident_miss);
747 for (i = 0; i < 100; i++)
748 distribution[i] = 0;
750 for (i = 0; i < IDENT_HASH_SIZE; i++) {
751 struct ident * ident = hash_table[i];
752 int count = 0;
754 while (ident) {
755 count++;
756 ident = ident->next;
758 if (count > 99)
759 count = 99;
760 distribution[count]++;
763 for (i = 0; i < 100; i++) {
764 if (distribution[i])
765 fprintf(stderr, "%2d: %d buckets\n", i, distribution[i]);
769 static struct ident *alloc_ident(const char *name, int len)
771 struct ident *ident = __alloc_ident(len);
772 ident->symbols = NULL;
773 ident->len = len;
774 ident->tainted = 0;
775 memcpy(ident->name, name, len);
776 return ident;
779 static struct ident * insert_hash(struct ident *ident, unsigned long hash)
781 ident->next = hash_table[hash];
782 hash_table[hash] = ident;
783 ident_miss++;
784 return ident;
787 static struct ident *create_hashed_ident(const char *name, int len, unsigned long hash)
789 struct ident *ident;
790 struct ident **p;
792 p = &hash_table[hash];
793 while ((ident = *p) != NULL) {
794 if (ident->len == (unsigned char) len) {
795 if (strncmp(name, ident->name, len) != 0)
796 goto next;
798 ident_hit++;
799 return ident;
801 next:
802 //misses++;
803 p = &ident->next;
805 ident = alloc_ident(name, len);
806 *p = ident;
807 ident->next = NULL;
808 ident_miss++;
809 idents++;
810 return ident;
813 static unsigned long hash_name(const char *name, int len)
815 unsigned long hash;
816 const unsigned char *p = (const unsigned char *)name;
818 hash = ident_hash_init(*p++);
819 while (--len) {
820 unsigned int i = *p++;
821 hash = ident_hash_add(hash, i);
823 return ident_hash_end(hash);
826 struct ident *hash_ident(struct ident *ident)
828 return insert_hash(ident, hash_name(ident->name, ident->len));
831 struct ident *built_in_ident(const char *name)
833 int len = strlen(name);
834 return create_hashed_ident(name, len, hash_name(name, len));
837 struct token *built_in_token(int stream, const char *name)
839 struct token *token;
841 token = __alloc_token(0);
842 token->pos.stream = stream;
843 token_type(token) = TOKEN_IDENT;
844 token->ident = built_in_ident(name);
845 return token;
848 static int get_one_identifier(int c, stream_t *stream)
850 struct token *token;
851 struct ident *ident;
852 unsigned long hash;
853 char buf[256];
854 int len = 1;
855 int next;
857 hash = ident_hash_init(c);
858 buf[0] = c;
859 for (;;) {
860 next = nextchar(stream);
861 if (!(cclass[next + 1] & (Letter | Digit)))
862 break;
863 if (len >= sizeof(buf))
864 break;
865 hash = ident_hash_add(hash, next);
866 buf[len] = next;
867 len++;
869 hash = ident_hash_end(hash);
871 ident = create_hashed_ident(buf, len, hash);
873 /* Pass it on.. */
874 token = stream->token;
875 token_type(token) = TOKEN_IDENT;
876 token->ident = ident;
877 add_token(stream);
878 return next;
881 static int get_one_token(int c, stream_t *stream)
883 long class = cclass[c + 1];
884 if (class & Digit)
885 return get_one_number(c, nextchar(stream), stream);
886 if (class & Letter)
887 return get_one_identifier(c, stream);
888 return get_one_special(c, stream);
891 static struct token *setup_stream(stream_t *stream, int idx, int fd,
892 unsigned char *buf, unsigned int buf_size)
894 struct token *begin;
896 stream->nr = idx;
897 stream->line = 1;
898 stream->newline = 1;
899 stream->whitespace = 0;
900 stream->pos = 0;
902 stream->token = NULL;
903 stream->fd = fd;
904 stream->offset = 0;
905 stream->size = buf_size;
906 stream->buffer = buf;
908 begin = alloc_token(stream);
909 token_type(begin) = TOKEN_STREAMBEGIN;
910 stream->tokenlist = &begin->next;
911 return begin;
914 static struct token *tokenize_stream(stream_t *stream)
916 int c = nextchar(stream);
917 while (c != EOF) {
918 if (!isspace(c)) {
919 struct token *token = alloc_token(stream);
920 stream->token = token;
921 stream->newline = 0;
922 stream->whitespace = 0;
923 c = get_one_token(c, stream);
924 continue;
926 stream->whitespace = 1;
927 c = nextchar(stream);
929 return mark_eof(stream);
932 struct token * tokenize_buffer(void *buffer, unsigned long size, struct token **endtoken)
934 stream_t stream;
935 struct token *begin;
937 begin = setup_stream(&stream, 0, -1, buffer, size);
938 *endtoken = tokenize_stream(&stream);
939 return begin;
942 struct token * tokenize(const char *name, int fd, struct token *endtoken, const char **next_path)
944 struct token *begin, *end;
945 stream_t stream;
946 unsigned char buffer[BUFSIZE];
947 int idx;
949 idx = init_stream(name, fd, next_path);
950 if (idx < 0) {
951 // info(endtoken->pos, "File %s is const", name);
952 return endtoken;
955 begin = setup_stream(&stream, idx, fd, buffer, 0);
956 end = tokenize_stream(&stream);
957 if (endtoken)
958 end->next = endtoken;
959 return begin;