/* Surface
 * (C) Copyright 1995 by Ashton Mason (amason@cs.uct.ac.za)
 *
 * Permission to use, modify, copy and distribute this source code for
 * any purpose and without fee is granted, provided that this copyright
 * notice appears in all copies and supporting documentation, and that
 * credit is given where due. This source code is provided "as is" with
 * no express or implied warranty.
 */

#include "headers.h"


	// constructor


lexical::lexical(void)
{
  file = NULL;
  numnames = 0;
}


	// deconstructor


lexical::~lexical(void)
{
  if (file)
    fclose(file);
}


	// open a file to be analysed


int lexical::openFile(char *name)
{
  file = fopen(name, "r");
  if (!file)
  {
    error("can't open colour expression file");
    return 0;
  }
  return 1;
}


int lexical::closeFile()
{
  fclose(file);
  return 1;
}


	// gets the next token from the input file
        // requires whitespace between *all* tokens
        // returns 0 on EOF


int lexical::getToken(char *token)
{
  int result;

  result = fscanf(file, " %s", token);
  if (result && result != EOF)
  {
    return 1;
  }

  return 0;
}


	// get the next symbol from the file


symbol lexical::getSymbol()
{
  char token[50], mssg[50];
  int result, sym;
  value val;

  if (getToken(token))				// get next token
  {
    result = sscanf(token, " %f", &val.value1d);
    if (result && result != EOF)
    {
      lastvalue = val;
      lastvalue.type = v1d;
      return CONST;				// constant v1d
    }

    result = sscanf(token, " <%f,%f,%f>", &val.value3d.x, &val.value3d.y, &val.value3d.z);
    if (result && result != EOF)
    {
      lastvalue = val;
      lastvalue.type = v3d;
      return CONST;				// constant v3d
    }

    for (sym = 0; sym < SYMBOLS; sym++)
    {
      if (!strcmp(keywords[sym], token))
      {
        return (symbol) sym;			// keyword
      }
    }

    for (sym = 0; sym < numnames; sym++)
    {
      if (!strcmp(names[sym], token))
      {
	lastname = sym;
        return NAME;				// declared name
      }
    }

    sprintf(mssg, "unrecognised token '%s' in file", token);
    error(mssg);
  }

  return NOP;				// end of file
}


	// get the text from the expected #define statement


int lexical::getDefine(char *name)
{
  if (fscanf(file, " #define %s", name))
  {
    return 1;
  }
  error("#define statement expected in colour expression file");
  return 0;
}


	// get the text from the next #include statement, if any



int lexical::getInclude(char *name)
{
  if (fscanf(file, " #include %s", name))
  {
    if (name[0] != '"')
    {
      error("missing quote around name of included expression file");
      return 0;
    }

    char temp[50];			// cut out start quote
    strcpy(temp, name + 1);
    strcpy(name, temp);

    if (name[strlen(name) - 1] != '"')
    {
      error("missing quote around name of included expression file");
      return 0;
    }

    name[strlen(name) - 1] = '\0';	// clobber end quote
    return 1;
  }
  return 0;
}


	// add an included expression name to the lexical
	// analyser. allows that expression to be recognised
	// as a valid symbol


int lexical::include(char *incname)
{
  if (numnames == MAXINCS)
  {
    printf("too many included names - maximum of %d", MAXINCS);
    return 0;
  }

  strcpy(names[numnames], incname);
  numnames++;
  return 1;
}


