#include <stdlib.h>
#include <string.h>

#include "token.h"


#ifndef TRUE
#define TRUE 1
#endif

#ifndef FALSE
#define FALSE 0
#endif


static const char *tokenerror_str[] = {
  "NOERROR", "EOF", "ILLEGAL CHAR", NULL
};

/*
 * Returns a pointer to a string representation of the error number, note the
 * string is in static storage.
 */
const char *token_err2str (tokenerror_t code) {
  return tokenerror_str[code];
}

/*
 * Returns the number of an error string, used for interpreted error code from
 * serialized tokens.
 */
int token_str2err (char *str, tokenerror_t *te) {
  int i;

  /*
   * I'll do a binary search here if there are ever more than 3 strings to
   * deal with.
   */
  for (i = 0; tokenerror_str[i] != NULL; i++) {
    if (strcmp(tokenerror_str[i], str) == 0) {
      *te = (tokenerror_t) i;
      return TRUE;
    }
  }

  return FALSE;
}

