Add test-suite annotations to integer-promotions.c
[smatch.git] / tokenize.c
blobe72c56eddc99500b3c0540553906290829349614
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 size = read(stream->fd, stream->buffer, BUFSIZE);
217 if (size <= 0)
218 goto got_eof;
219 stream->size = size;
220 stream->offset = offset = 0;
223 c = stream->buffer[offset++];
225 if (had_cr && c != '\n')
226 complain = 1;
228 if (c == '\r') {
229 had_cr = 1;
230 goto repeat;
233 stream->pos++;
235 if (c == '\n') {
236 stream->line++;
237 stream->pos = 0;
240 if (!had_backslash) {
241 if (c == '\\') {
242 had_backslash = 1;
243 goto repeat;
245 if (c == '\n')
246 stream->newline = 1;
247 } else {
248 if (c == '\n') {
249 if (complain)
250 warning(stream_pos(stream), "non-ASCII data stream");
251 spliced = 1;
252 goto restart;
254 stream->pos--;
255 offset--;
256 c = '\\';
259 out:
260 stream->offset = offset;
261 if (complain)
262 warning(stream_pos(stream), "non-ASCII data stream");
264 return c;
266 got_eof:
267 if (had_backslash) {
268 c = '\\';
269 goto out;
271 if (stream->pos)
272 warning(stream_pos(stream), "no newline at end of file");
273 else if (had_cr)
274 warning(stream_pos(stream), "non-ASCII data stream");
275 else if (spliced)
276 warning(stream_pos(stream), "backslash-newline at end of file");
277 return EOF;
281 * We want that as light as possible while covering all normal cases.
282 * Slow path (including the logics with line-splicing and EOF sanity
283 * checks) is in nextchar_slow().
285 static inline int nextchar(stream_t *stream)
287 int offset = stream->offset;
289 if (offset < stream->size) {
290 int c = stream->buffer[offset++];
291 static const char special[256] = {
292 ['\r'] = 1, ['\n'] = 1, ['\\'] = 1
294 if (!special[c]) {
295 stream->offset = offset;
296 stream->pos++;
297 return c;
300 return nextchar_slow(stream);
303 struct token eof_token_entry;
305 static void mark_eof(stream_t *stream, struct token *end_token)
307 struct token *end;
309 end = alloc_token(stream);
310 token_type(end) = TOKEN_STREAMEND;
311 end->pos.newline = 1;
313 eof_token_entry.next = &eof_token_entry;
314 eof_token_entry.pos.newline = 1;
316 if (!end_token)
317 end_token = &eof_token_entry;
318 end->next = end_token;
319 *stream->tokenlist = end;
320 stream->tokenlist = NULL;
323 static void add_token(stream_t *stream)
325 struct token *token = stream->token;
327 stream->token = NULL;
328 token->next = NULL;
329 *stream->tokenlist = token;
330 stream->tokenlist = &token->next;
333 static void drop_token(stream_t *stream)
335 stream->newline |= stream->token->pos.newline;
336 stream->whitespace |= stream->token->pos.whitespace;
337 stream->token = NULL;
340 enum {
341 Letter = 1,
342 Digit = 2,
343 Hex = 4,
344 Exp = 8,
345 Dot = 16,
346 ValidSecond = 32,
349 static const long cclass[257] = {
350 ['0' + 1 ... '9' + 1] = Digit | Hex,
351 ['A' + 1 ... 'D' + 1] = Letter | Hex,
352 ['E' + 1] = Letter | Hex | Exp,
353 ['F' + 1] = Letter | Hex,
354 ['G' + 1 ... 'O' + 1] = Letter,
355 ['P' + 1] = Letter | Exp,
356 ['Q' + 1 ... 'Z' + 1] = Letter,
357 ['a' + 1 ... 'd' + 1] = Letter | Hex,
358 ['e' + 1] = Letter | Hex | Exp,
359 ['f' + 1] = Letter | Hex,
360 ['g' + 1 ... 'o' + 1] = Letter,
361 ['p' + 1] = Letter | Exp,
362 ['q' + 1 ... 'z' + 1] = Letter,
363 ['_' + 1] = Letter,
364 ['.' + 1] = Dot | ValidSecond,
365 ['=' + 1] = ValidSecond,
366 ['+' + 1] = ValidSecond,
367 ['-' + 1] = ValidSecond,
368 ['>' + 1] = ValidSecond,
369 ['<' + 1] = ValidSecond,
370 ['&' + 1] = ValidSecond,
371 ['|' + 1] = ValidSecond,
372 ['#' + 1] = ValidSecond,
376 * pp-number:
377 * digit
378 * . digit
379 * pp-number digit
380 * pp-number identifier-nodigit
381 * pp-number e sign
382 * pp-number E sign
383 * pp-number p sign
384 * pp-number P sign
385 * pp-number .
387 static int get_one_number(int c, int next, stream_t *stream)
389 struct token *token;
390 static char buffer[4095];
391 char *p = buffer, *buf, *buffer_end = buffer + sizeof (buffer);
392 int len;
394 *p++ = c;
395 for (;;) {
396 long class = cclass[next + 1];
397 if (!(class & (Dot | Digit | Letter)))
398 break;
399 if (p != buffer_end)
400 *p++ = next;
401 next = nextchar(stream);
402 if (class & Exp) {
403 if (next == '-' || next == '+') {
404 if (p != buffer_end)
405 *p++ = next;
406 next = nextchar(stream);
411 if (p == buffer_end) {
412 sparse_error(stream_pos(stream), "number token exceeds %td characters",
413 buffer_end - buffer);
414 // Pretend we saw just "1".
415 buffer[0] = '1';
416 p = buffer + 1;
419 *p++ = 0;
420 len = p - buffer;
421 buf = __alloc_bytes(len);
422 memcpy(buf, buffer, len);
424 token = stream->token;
425 token_type(token) = TOKEN_NUMBER;
426 token->number = buf;
427 add_token(stream);
429 return next;
432 static int escapechar(int first, int type, stream_t *stream, int *valp)
434 int next, value;
436 next = nextchar(stream);
437 value = first;
439 if (first == '\n')
440 warning(stream_pos(stream), "Newline in string or character constant");
442 if (first == '\\' && next != EOF) {
443 value = next;
444 next = nextchar(stream);
445 if (value != type) {
446 switch (value) {
447 case 'a':
448 value = '\a';
449 break;
450 case 'b':
451 value = '\b';
452 break;
453 case 't':
454 value = '\t';
455 break;
456 case 'n':
457 value = '\n';
458 break;
459 case 'v':
460 value = '\v';
461 break;
462 case 'f':
463 value = '\f';
464 break;
465 case 'r':
466 value = '\r';
467 break;
468 case 'e':
469 value = '\e';
470 break;
471 case '\\':
472 break;
473 case '?':
474 break;
475 case '\'':
476 break;
477 case '"':
478 break;
479 case '\n':
480 warning(stream_pos(stream), "Newline in string or character constant");
481 break;
482 case '0'...'7': {
483 int nr = 2;
484 value -= '0';
485 while (next >= '0' && next <= '9') {
486 value = (value << 3) + (next-'0');
487 next = nextchar(stream);
488 if (!--nr)
489 break;
491 value &= 0xff;
492 break;
494 case 'x': {
495 int hex = hexval(next);
496 if (hex < 16) {
497 value = hex;
498 next = nextchar(stream);
499 while ((hex = hexval(next)) < 16) {
500 value = (value << 4) + hex;
501 next = nextchar(stream);
503 value &= 0xff;
504 break;
507 /* Fall through */
508 default:
509 warning(stream_pos(stream), "Unknown escape '%c'", value);
512 /* Mark it as escaped */
513 value |= 0x100;
515 *valp = value;
516 return next;
519 static int get_char_token(int next, stream_t *stream)
521 int value;
522 struct token *token;
524 next = escapechar(next, '\'', stream, &value);
525 if (value == '\'' || next != '\'') {
526 sparse_error(stream_pos(stream), "Bad character constant");
527 drop_token(stream);
528 return next;
531 token = stream->token;
532 token_type(token) = TOKEN_CHAR;
533 token->character = value & 0xff;
535 add_token(stream);
536 return nextchar(stream);
539 static int get_string_token(int next, stream_t *stream)
541 static char buffer[MAX_STRING];
542 struct string *string;
543 struct token *token;
544 int len = 0;
546 for (;;) {
547 int val;
548 next = escapechar(next, '"', stream, &val);
549 if (val == '"')
550 break;
551 if (next == EOF) {
552 warning(stream_pos(stream), "End of file in middle of string");
553 return next;
555 if (len < MAX_STRING)
556 buffer[len] = val;
557 len++;
560 if (len > MAX_STRING) {
561 warning(stream_pos(stream), "string too long (%d bytes, %d bytes max)", len, MAX_STRING);
562 len = MAX_STRING;
565 string = __alloc_string(len+1);
566 memcpy(string->data, buffer, len);
567 string->data[len] = '\0';
568 string->length = len+1;
570 /* Pass it on.. */
571 token = stream->token;
572 token_type(token) = TOKEN_STRING;
573 token->string = string;
574 add_token(stream);
576 return next;
579 static int drop_stream_eoln(stream_t *stream)
581 drop_token(stream);
582 for (;;) {
583 switch (nextchar(stream)) {
584 case EOF:
585 return EOF;
586 case '\n':
587 return nextchar(stream);
592 static int drop_stream_comment(stream_t *stream)
594 int newline;
595 int next;
596 drop_token(stream);
597 newline = stream->newline;
599 next = nextchar(stream);
600 for (;;) {
601 int curr = next;
602 if (curr == EOF) {
603 warning(stream_pos(stream), "End of file in the middle of a comment");
604 return curr;
606 next = nextchar(stream);
607 if (curr == '*' && next == '/')
608 break;
610 stream->newline = newline;
611 return nextchar(stream);
614 unsigned char combinations[][4] = COMBINATION_STRINGS;
616 #define NR_COMBINATIONS (SPECIAL_ARG_SEPARATOR - SPECIAL_BASE)
618 /* hash function for two-character punctuators - all give unique values */
619 #define special_hash(c0, c1) (((c0*8+c1*2)+((c0*8+c1*2)>>5))&31)
622 * note that we won't get false positives - special_hash(0,0) is 0 and
623 * entry 0 is filled (by +=), so all the missing ones are OK.
625 static unsigned char hash_results[32][2] = {
626 #define RES(c0, c1) [special_hash(c0, c1)] = {c0, c1}
627 RES('+', '='), /* 00 */
628 RES('/', '='), /* 01 */
629 RES('^', '='), /* 05 */
630 RES('&', '&'), /* 07 */
631 RES('#', '#'), /* 08 */
632 RES('<', '<'), /* 0a */
633 RES('<', '='), /* 0c */
634 RES('!', '='), /* 0e */
635 RES('%', '='), /* 0f */
636 RES('-', '-'), /* 10 */
637 RES('-', '='), /* 11 */
638 RES('-', '>'), /* 13 */
639 RES('=', '='), /* 15 */
640 RES('&', '='), /* 17 */
641 RES('*', '='), /* 18 */
642 RES('.', '.'), /* 1a */
643 RES('+', '+'), /* 1b */
644 RES('|', '='), /* 1c */
645 RES('>', '='), /* 1d */
646 RES('|', '|'), /* 1e */
647 RES('>', '>') /* 1f */
648 #undef RES
650 static int code[32] = {
651 #define CODE(c0, c1, value) [special_hash(c0, c1)] = value
652 CODE('+', '=', SPECIAL_ADD_ASSIGN), /* 00 */
653 CODE('/', '=', SPECIAL_DIV_ASSIGN), /* 01 */
654 CODE('^', '=', SPECIAL_XOR_ASSIGN), /* 05 */
655 CODE('&', '&', SPECIAL_LOGICAL_AND), /* 07 */
656 CODE('#', '#', SPECIAL_HASHHASH), /* 08 */
657 CODE('<', '<', SPECIAL_LEFTSHIFT), /* 0a */
658 CODE('<', '=', SPECIAL_LTE), /* 0c */
659 CODE('!', '=', SPECIAL_NOTEQUAL), /* 0e */
660 CODE('%', '=', SPECIAL_MOD_ASSIGN), /* 0f */
661 CODE('-', '-', SPECIAL_DECREMENT), /* 10 */
662 CODE('-', '=', SPECIAL_SUB_ASSIGN), /* 11 */
663 CODE('-', '>', SPECIAL_DEREFERENCE), /* 13 */
664 CODE('=', '=', SPECIAL_EQUAL), /* 15 */
665 CODE('&', '=', SPECIAL_AND_ASSIGN), /* 17 */
666 CODE('*', '=', SPECIAL_MUL_ASSIGN), /* 18 */
667 CODE('.', '.', SPECIAL_DOTDOT), /* 1a */
668 CODE('+', '+', SPECIAL_INCREMENT), /* 1b */
669 CODE('|', '=', SPECIAL_OR_ASSIGN), /* 1c */
670 CODE('>', '=', SPECIAL_GTE), /* 1d */
671 CODE('|', '|', SPECIAL_LOGICAL_OR), /* 1e */
672 CODE('>', '>', SPECIAL_RIGHTSHIFT) /* 1f */
673 #undef CODE
676 static int get_one_special(int c, stream_t *stream)
678 struct token *token;
679 int next, value, i;
681 next = nextchar(stream);
684 * Check for numbers, strings, character constants, and comments
686 switch (c) {
687 case '.':
688 if (next >= '0' && next <= '9')
689 return get_one_number(c, next, stream);
690 break;
691 case '"':
692 return get_string_token(next, stream);
693 case '\'':
694 return get_char_token(next, stream);
695 case '/':
696 if (next == '/')
697 return drop_stream_eoln(stream);
698 if (next == '*')
699 return drop_stream_comment(stream);
703 * Check for combinations
705 value = c;
706 if (cclass[next + 1] & ValidSecond) {
707 i = special_hash(c, next);
708 if (hash_results[i][0] == c && hash_results[i][1] == next) {
709 value = code[i];
710 next = nextchar(stream);
711 if (value >= SPECIAL_LEFTSHIFT &&
712 next == "==."[value - SPECIAL_LEFTSHIFT]) {
713 value += 3;
714 next = nextchar(stream);
719 /* Pass it on.. */
720 token = stream->token;
721 token_type(token) = TOKEN_SPECIAL;
722 token->special = value;
723 add_token(stream);
724 return next;
727 #define IDENT_HASH_BITS (13)
728 #define IDENT_HASH_SIZE (1<<IDENT_HASH_BITS)
729 #define IDENT_HASH_MASK (IDENT_HASH_SIZE-1)
731 #define ident_hash_init(c) (c)
732 #define ident_hash_add(oldhash,c) ((oldhash)*11 + (c))
733 #define ident_hash_end(hash) ((((hash) >> IDENT_HASH_BITS) + (hash)) & IDENT_HASH_MASK)
735 static struct ident *hash_table[IDENT_HASH_SIZE];
736 static int ident_hit, ident_miss, idents;
738 void show_identifier_stats(void)
740 int i;
741 int distribution[100];
743 fprintf(stderr, "identifiers: %d hits, %d misses\n",
744 ident_hit, ident_miss);
746 for (i = 0; i < 100; i++)
747 distribution[i] = 0;
749 for (i = 0; i < IDENT_HASH_SIZE; i++) {
750 struct ident * ident = hash_table[i];
751 int count = 0;
753 while (ident) {
754 count++;
755 ident = ident->next;
757 if (count > 99)
758 count = 99;
759 distribution[count]++;
762 for (i = 0; i < 100; i++) {
763 if (distribution[i])
764 fprintf(stderr, "%2d: %d buckets\n", i, distribution[i]);
768 static struct ident *alloc_ident(const char *name, int len)
770 struct ident *ident = __alloc_ident(len);
771 ident->symbols = NULL;
772 ident->len = len;
773 ident->tainted = 0;
774 memcpy(ident->name, name, len);
775 return ident;
778 static struct ident * insert_hash(struct ident *ident, unsigned long hash)
780 ident->next = hash_table[hash];
781 hash_table[hash] = ident;
782 ident_miss++;
783 return ident;
786 static struct ident *create_hashed_ident(const char *name, int len, unsigned long hash)
788 struct ident *ident;
789 struct ident **p;
791 p = &hash_table[hash];
792 while ((ident = *p) != NULL) {
793 if (ident->len == (unsigned char) len) {
794 if (strncmp(name, ident->name, len) != 0)
795 goto next;
797 ident_hit++;
798 return ident;
800 next:
801 //misses++;
802 p = &ident->next;
804 ident = alloc_ident(name, len);
805 *p = ident;
806 ident->next = NULL;
807 ident_miss++;
808 idents++;
809 return ident;
812 static unsigned long hash_name(const char *name, int len)
814 unsigned long hash;
815 const unsigned char *p = (const unsigned char *)name;
817 hash = ident_hash_init(*p++);
818 while (--len) {
819 unsigned int i = *p++;
820 hash = ident_hash_add(hash, i);
822 return ident_hash_end(hash);
825 struct ident *hash_ident(struct ident *ident)
827 return insert_hash(ident, hash_name(ident->name, ident->len));
830 struct ident *built_in_ident(const char *name)
832 int len = strlen(name);
833 return create_hashed_ident(name, len, hash_name(name, len));
836 struct token *built_in_token(int stream, const char *name)
838 struct token *token;
840 token = __alloc_token(0);
841 token->pos.stream = stream;
842 token_type(token) = TOKEN_IDENT;
843 token->ident = built_in_ident(name);
844 return token;
847 static int get_one_identifier(int c, stream_t *stream)
849 struct token *token;
850 struct ident *ident;
851 unsigned long hash;
852 char buf[256];
853 int len = 1;
854 int next;
856 hash = ident_hash_init(c);
857 buf[0] = c;
858 for (;;) {
859 next = nextchar(stream);
860 if (!(cclass[next + 1] & (Letter | Digit)))
861 break;
862 if (len >= sizeof(buf))
863 break;
864 hash = ident_hash_add(hash, next);
865 buf[len] = next;
866 len++;
868 hash = ident_hash_end(hash);
870 ident = create_hashed_ident(buf, len, hash);
872 /* Pass it on.. */
873 token = stream->token;
874 token_type(token) = TOKEN_IDENT;
875 token->ident = ident;
876 add_token(stream);
877 return next;
880 static int get_one_token(int c, stream_t *stream)
882 long class = cclass[c + 1];
883 if (class & Digit)
884 return get_one_number(c, nextchar(stream), stream);
885 if (class & Letter)
886 return get_one_identifier(c, stream);
887 return get_one_special(c, stream);
890 static struct token *setup_stream(stream_t *stream, int idx, int fd,
891 unsigned char *buf, unsigned int buf_size)
893 struct token *begin;
895 stream->nr = idx;
896 stream->line = 1;
897 stream->newline = 1;
898 stream->whitespace = 0;
899 stream->pos = 0;
901 stream->token = NULL;
902 stream->fd = fd;
903 stream->offset = 0;
904 stream->size = buf_size;
905 stream->buffer = buf;
907 begin = alloc_token(stream);
908 token_type(begin) = TOKEN_STREAMBEGIN;
909 stream->tokenlist = &begin->next;
910 return begin;
913 static void tokenize_stream(stream_t *stream, struct token *endtoken)
915 int c = nextchar(stream);
916 while (c != EOF) {
917 if (!isspace(c)) {
918 struct token *token = alloc_token(stream);
919 stream->token = token;
920 stream->newline = 0;
921 stream->whitespace = 0;
922 c = get_one_token(c, stream);
923 continue;
925 stream->whitespace = 1;
926 c = nextchar(stream);
928 mark_eof(stream, endtoken);
931 struct token * tokenize_buffer(void *buffer, unsigned long size, struct token *endtoken)
933 stream_t stream;
934 struct token *begin;
936 begin = setup_stream(&stream, 0, -1, buffer, size);
937 tokenize_stream(&stream, endtoken);
938 return begin;
941 struct token * tokenize(const char *name, int fd, struct token *endtoken, const char **next_path)
943 struct token *begin;
944 stream_t stream;
945 unsigned char buffer[BUFSIZE];
946 int idx;
948 idx = init_stream(name, fd, next_path);
949 if (idx < 0) {
950 // info(endtoken->pos, "File %s is const", name);
951 return endtoken;
954 begin = setup_stream(&stream, idx, fd, buffer, 0);
955 tokenize_stream(&stream, endtoken);
956 return begin;