%{

#include <stdio.h>

#include "squid.h"
#include "y.tab.h"

%}


%%

"=="				return EQEQ;
"<="				return LTEQ;
">="				return GTEQ;  
"!="				return NOTEQ; 
"||"				return '|';  
"&&"				return '&';  
[A-Za-z_][0-9A-Za-z_]*		return iden_or_keyword(yytext);  
"-"?[1-9][0-9]*			{ yylval.number = atoi(yytext); return INT_CONST; }
0[0-9]*				{ yylval.number = octtoi(yytext+1); return INT_CONST; }
0x[0-9a-fA-F]+			{ yylval.number = hextoi(yytext+2); return INT_CONST; }
[ \t\n]+			;
["]				return read_strconst();  
"/*"				skip_comment();
.				return yytext[0];  

%%
 
struct keyword {
  char *keyword;
  int code;
};

static struct keyword keywords[] = {
  "bitmap", BITMAP,
  "call", CALL,
  "end", END,
  "kill", KILL,
  "return", RETURN,
  "sequence", SEQUENCE,
  "show", SHOW,
  "start", START,
  "tick", TICK,
  "ticks", TICKS,
  "while", WHILE,
  "wait", WAIT,
  "if", IF,
  "then", THEN,
  "else", ELSE,
  "", 0,
};

iden_or_keyword (text)
     char *text;
{
  struct keyword *k;

  for (k = keywords; k->keyword[0]; k++) 
    if (!strcmp(k->keyword, text))
      return k->code;
  yylval.string = (char *) malloc(strlen(text) + 1);
  strcpy (yylval.string, text);
  return IDEN;
}

int read_strconst()			/* Assume first " has been read */
{
  char ch, buf[4096];
  int n = 0;

  while ((ch = input()) && (ch != '\n')) {
    switch (ch) {

    case '"':
      buf[n] = 0;
      yylval.string = (char *) malloc(n + 1);
      strcpy (yylval.string, buf);
      return STR_CONST;
      
    case '\\':
      switch (ch = input()) {
      case '\n': continue;
      case 'n': ch = '\n'; break;
      case 'e': ch = 27;   break;
      case 't': ch = '\t'; break;
      case 'r': ch = '\r'; break;
      }
    }

    if (n < sizeof(buf))
      buf[n++] = ch;
  }    
      
  fprintf (stderr, "Unterminated string constant, line %d\n", yylineno);
  yylval.string = (char *) malloc(1);
  yylval.string[0] = 0;
  return STR_CONST;
}


void free_token(tok)
     int tok;
{
  if (tok == IDEN || tok == STR_CONST)
    free (yylval.string);
}


skip_comment ()
{
  char ch;	

  while (ch = input())
    while (ch == '*') 
      if ((ch = input()) == '/') return;
  fprintf (stderr, "Unterminated comment\n");
}

int octtoi (c)
    char *c;
{
  int t = 0;
  while (*c)
    t = (t << 3) + *c++ - '0';
  return t;
}

int hextoi (c)
     char *c;
{
  int t = 0;
  int ch;
  while (ch = *c++)
    t = (t << 4) + (ch <= '9' ? ch - '0' :
		    (ch <= 'F' ? ch - 'A' + 10 : ch - 'a' + 10));
  return t;
}

  
  
