X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=judge%2Ftoken.c;h=85f5069420e96755f2f98e7299f3fa4469055f12;hb=16cdbe07d6da578e69dcbc35115abe90ab4b476e;hp=69b9398395b89fb0e3c1a6ae1fd37dfd068fec6c;hpb=cc2f58454e17a6212a23c27e70e6afcf1e054346;p=moe.git diff --git a/judge/token.c b/judge/token.c index 69b9398..85f5069 100644 --- a/judge/token.c +++ b/judge/token.c @@ -19,7 +19,7 @@ void tok_init(struct tokenizer *t, struct stream *s) { 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; @@ -34,9 +34,9 @@ void tok_err(struct tokenizer *t, char *msg, ...) { va_list args; va_start(args, msg); - printf("%s:%d: ", t->stream->name, t->line); - vprintf(msg, args); - putchar('\n'); + 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); } @@ -76,11 +76,11 @@ char *get_token(struct tokenizer *t) 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); @@ -101,6 +101,8 @@ char *get_token(struct tokenizer *t) #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) @@ -161,3 +163,10 @@ GET(long, long int) 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"); +}