%{
static	char	ami_lexer_l[] = "$Header: /private/postgres/src/bootstrap/RCS/bootscanner.lex,v 1.2 1991/11/05 05:31:14 mer Exp $";
/**********************************************************************

  ami_lexer. 

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

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

#include "support/bkint.h"
#include "tmp/portal.h" 

#undef BOOTSTRAP
#include "y.tab.h"

int	yylval;
int yycolumn,yyline;
#define NEWLINE() {yycolumn=0;yyline++;}
#if defined(__386BSD__)
#undef yywrap
#endif /* #if defined(__386BSD__) */

%}

%a 5000
%o 10000

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

%%

o |
open       	return(OPEN);

c |
close		return(XCLOSE);

C |
create		return(XCREATE);

p |
print		return(P_RELN);

in		return(XIN);
as		return(AS);
to		return(XTO);
OID             return(OBJ_ID);
bootstrap	return(BOOTSTRAP); 
_null_		return(NULLVAL);

i |
insert		return(INSERT_TUPLE);

q |
quit		return(QUIT);

\%pstr          {printstrtable();EMITPROMPT;}

\%phash     	{printhashtable();EMITPROMPT;}

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

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

[\n]      	NEWLINE();
[\t]		;
" "		; 

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


"define"	{return(XDEFINE);}
"macro"		{return(MACRO);}
"index"		{return(INDEX);}
"on"		{return(ON);}
"using"		{return(USING);}
"display"	{return(DISPLAY);}
"show"		{return(SHOW);}
"$"{id}		{ 
		  unsigned char *in, *out, outbuf[256];
		  int count;
		  out = &outbuf[0];
		  in  = yytext+1;

		  for( count = 0; (*out = *in) && (count < yyleng); count++ )
		    {
		      if(*(in++) == '\\')
		        *out = (unsigned char)MapEscape((char**)(&in));
		      out++;
		    }
		   *(out+1) = '\0';
		  yylval=LookUpMacro((char *)(&outbuf[0]));
		  return(ID);
		}
{id}	 	{ 
		  unsigned char *in, *out, outbuf[256];
		  int count;
		  out = &outbuf[0];
		  in  = yytext;

		  for( count = 0; (*out = *in) && (count < yyleng); count++ )
		    {
		      if(*(in++) == '\\')
		        *out = (unsigned char)MapEscape((char **)(&in));
		      out++;
		    }
		   *(out+1) = '\0';
		   yylval=EnterString((char *)(&outbuf[0])); return(ID);
		}
{sid}		{
		  unsigned char *in, *out, outbuf[256];
		  int count;
		  in  = yytext + 1;
		  out = &outbuf[0];
		  for( count = 0; (*out = *in) && (count < yyleng); count++ )
		    {
		      out++;
		      if( *(++in) == '\"' )
		        break;
		    }
		  *out = '\0';
		  yylval=EnterString((char *)(&outbuf[0]));
		  return(ID);
		}



(-)?{D}+"."{D}*({Exp})?	|
(-)?{D}*"."{D}+({Exp})?	|
(-)?{D}+{Exp}		{
			 yylval=EnterString((char *)yytext);
			 return(FLOAT);
			}


(-)?{D}+	{
		 yylval=EnterString((char *)yytext);
		 return(INT);
		}


.	{
#if defined(__386BSD__)
	printf("syntax error %d : -> <%s>\n", yyline, yytext);
#else
	printf("syntax error %d : -> %s\n", yylineno, yytext);
#endif
	}



%%

yywrap()
{
	StartTransactionCommand();
	cleanup();
	CommitTransactionCommand();
	return;
}

yyerror(str)
	char *str;
{
#if defined(__386BSD__)
	fprintf(stderr,"\terror in line %d : %s\n",yyline, str);
#else /* #if defined(__386BSD__) */
	fprintf(stderr,"\tsyntax error %d : %s",yylineno, str);
#endif /* #if defined(__386BSD__) */
}
