* (c) 2007 Martin Mares <mj@ucw.cz>
*/
+#include <stdio.h>
#include <stdlib.h>
+#include <stdarg.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
{
memset(t, 0, sizeof(*t));
t->stream = s;
- t->bufsize = 256;
+ t->bufsize = 1;
t->token = xmalloc(t->bufsize);
t->maxsize = DEFAULT_MAX_TOKEN;
t->line = 1;
free(t->token);
}
-void tok_err(struct tokenizer *t, char *msg)
+void tok_err(struct tokenizer *t, char *msg, ...)
{
- die("%s:%d: %s", t->stream->name, t->line, msg);
+ va_list args;
+ va_start(args, msg);
+ fprintf(stderr, "Error at %s line %d:\n", t->stream->name, t->line);
+ vfprintf(stderr, msg, args);
+ fputc('\n', stderr);
+ va_end(args);
+ exit(1);
}
static inline int is_white(int c)
t->token[len++] = c;
if (len >= t->bufsize)
{
- if (len >= t->maxsize)
+ if (len > t->maxsize)
tok_err(t, "Token too long");
t->bufsize *= 2;
if (t->bufsize > t->maxsize)
- t->bufsize = t->maxsize;
+ t->bufsize = t->maxsize+1;
t->token = xrealloc(t->token, t->bufsize);
}
c = sgetc(t->stream);
#define PARSE(f, ...) \
char *end; \
errno = 0; \
+ if (!t->toksize) \
+ return 0; \
*x = f(t->token, &end, ##__VA_ARGS__); \
return !(errno || (unsigned char *) end != t->token + t->toksize)
GET(ulong, unsigned long int)
GET(double, double)
GET(long_double, long double)
+
+void get_nl(struct tokenizer *t)
+{
+ char *tok = get_token(t);
+ if (tok && *tok)
+ tok_err(t, "Expected end of line");
+}