]> mj.ucw.cz Git - eval.git/blobdiff - judge/token.c
Judge: Added function get_nl() for checking of an expected end of line.
[eval.git] / judge / token.c
index 2f43ddc99a45adcefeb4de462ceccb0512a1c30d..4606395483935493c2d8715099174c389254b820 100644 (file)
@@ -4,7 +4,9 @@
  *     (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>
@@ -17,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;
@@ -28,9 +30,15 @@ void tok_cleanup(struct tokenizer *t)
   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);
+  printf("%s:%d: ", t->stream->name, t->line);
+  vprintf(msg, args);
+  putchar('\n');
+  va_end(args);
+  exit(1);
 }
 
 static inline int is_white(int c)
@@ -68,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);
@@ -93,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)
 
@@ -153,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");
+}