%{

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "Attributes.h"
  
Boolean debug = False;

// Function prototypes
int yyerror(char *errorBuf);
int yylex();
int yyparse();
int fileLineNo();

const int eofReturn = -1;
const int errorReturn = 1;
const int okReturn = 0;

Attributes *_attributes;
char _errorBuf[512];

%}

%union 
{
  int integer;
  char *string;
}

%token <string> Word

%%


Attributes: Attribute
{
};


Attributes: Attributes Attribute
{
};


Attribute : Word '=' Word ';'
{
  Attribute::Input *input = new Attribute::Input($1, $3);

  Boolean error = False;

  if (_attributes->parse(input) == -1) {

      // Invalid worksite attribute
      sprintf(_errorBuf, "Invalid attribute: %s = %s",
	      $1, $3);

      error = True;
  }

  delete input;

  if (error)
    return errorReturn;
}




%%

extern char *yytext;

int yyerror(char *errorMsg)
{
  sprintf(_errorBuf, 
	  "Line %d: %s\noffending text: \"%s\"", 
	  fileLineNo(), errorMsg, yytext);
  
  return 0;
}


void parseAttributes(FILE *fp, Attributes *attributes,
		     Boolean *error, char *errorMsg)
{
  _attributes = attributes;

  int ret;
  extern FILE *yyin;
  
  *error = False;
  yyin = fp;
  
  if ((ret = yyparse()) != okReturn)
  {
    /* Error or end-of-file */
    if (ret == errorReturn)
    {
      *error = True;
      strcpy(errorMsg, _errorBuf);
    }
    return;
  }

  return;
}

