]> mj.ucw.cz Git - moe.git/blobdiff - judge/token.c
Added syntax for config files (to be reviewed)
[moe.git] / judge / token.c
index 69b9398395b89fb0e3c1a6ae1fd37dfd068fec6c..85f5069420e96755f2f98e7299f3fa4469055f12 100644 (file)
@@ -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");
+}