%{
static	char	ami_lexer_l[] = "$Header: ami_lexer.l,v 1.2 89/02/02 15:57:25 dillon Exp $";
/**********************************************************************

  ami_lexer. 

  a lexical scanner for postgres' amiint
  recent extensions include $def for macro definitions
  and ${id} for macro evaluation

 **********************************************************************/

#include "ami.h"

int	yylval;
int yycolumn,yyline;
#define NEWLINE() {yycolumn=0;yyline++;}

%}

%a 5000
%o 10000

D	[0-9]
oct     \\{D}{D}{D}
Exp	[Ee][-+]?{D}+
id      ([A-Za-z0-9_]|{oct}|\-)+

%%

o |
open       	return(OPEN);

c |
close		return(CLOSE);

C |
create		return(CREATE);

p |
print		return(P_RELN);

in		return(IN);
as		return(AS);
to		return(TO);
OID             return(OBJ_ID);
i |
insert		return(INSERT_TUPLE);

q |
quit		return(QUIT);

\%pstr          {printstrtable();EMITPROMPT;}

\%phash     	{printhashtable();EMITPROMPT;}

".D" |
destroy		{return(DESTROY);}

".R" |
rename		{return(RENAME);}
attribute	{return(ATTR);}
relation 	{return(RELATION); }
","     	{return(COMMA);}
":"		{return(COLON);}
"."		{return(DOT);}
"="		{return(EQUALS);}
"("		{return(LPAREN);}
")"		{return(RPAREN);}
add		{return(ADD);}


[\n]      	NEWLINE();
" "		; 

^\#[^\n]* ; /*drop everything after "#" for comments */


"define"	{ /* macro definition */
		  return(DEFINE);
		}
"macro"		{return(MACRO);}
"display"	{return(DISPLAY);}
"$"{id}		{ 
		  char *in, *out;
		  for(in=out=yytext; *out = *in; out++)
		    if(*in++ == '\\') *out = (unsigned char)MapEscape(&in);
		  *(out+1) = '\000';
		  yylval=LookUpMacro(&(yytext[1]));
		  return(ID);}
{id}	 	 { 
		  char *in, *out;
		  for(in = out = yytext; *out = *in; out++)
		    if(*in++ == '\\') *out = (unsigned char)MapEscape(&in);
		   *(out+1) = '\000';
		   yylval=EnterString(yytext); return(ID);}


(-)?{D}+"."{D}*({Exp})?	|
(-)?{D}*"."{D}+({Exp})?	|
(-)?{D}+{Exp}		;


(-)?{D}+	;


.	printf("syntax error : -> %s\n",yytext);



%%

yywrap()
{
	StartTransactionCommand();
	startmmgr(0);
	cleanup();
	endmmgr(0);
	CommitTransactionCommand();
	return;
}

yyerror(str)
	char *str;
{
	fprintf(stderr,"\tsyntax error : %s",str);
}

