%{

/* Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.

This file is part of GLD, the Gnu Linker.

GLD is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GLD is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GLD; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

/*
This was written by steve chamberlain
                    sac@cygnus.com
*/


#include <ansidecl.h>
#include <stdio.h>
#include "bfd.h"
#include "sysdep.h"
#include "ld.h"
#include "ldgram.h"
#include "ldmisc.h"
#include "ldexp.h"
#include "ldlang.h"
#include "ldfile.h"
#include "ldlex.h"
#include "ldmain.h"

/* The type of top-level parser input.
   yylex and yyparse (indirectly) both check this.  */
input_type parser_input;

/* Radix to use for bfd_scan_vma -- 0 (default to base 10) or 16.  */
int hex_mode;

/* Line number in the current input file.
   (FIXME Actually, it doesn't appear to get reset for each file?)  */
unsigned int lineno = 1;

/* Support for flex reading from more than one input file (stream).
   `include_stack' is flex's input state for each open file;
   `file_name_stack' is the file names.

   If `include_stack_ptr' is 0, we haven't started reading anything yet.
   Otherwise, stack elements 0 through `include_stack_ptr - 1' are valid.  */

#undef YY_INPUT
#define YY_INPUT(buf,result,max_size) yy_input(buf, &result, max_size)

#define MAX_INCLUDE_DEPTH 10
static YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
static char *file_name_stack[MAX_INCLUDE_DEPTH];
static unsigned int include_stack_ptr = 0;

static YY_BUFFER_STATE yy_create_string_buffer PARAMS ((const char *string,
							size_t size));
static void yy_input PARAMS ((char *, int *result, int max_size));

static void comment PARAMS ((void));
static void lex_warn_invalid PARAMS ((char *where, char *what));

/* STATES 
	EXPRESSION	definitely in an expression
	SCRIPT		definitely in a script
	BOTH		either EXPRESSION or SCRIPT
	DEFSYMEXP	in an argument to -defsym
        MRI             in an MRI script
*/
#define RTOKEN(x)  {  yylval.token = x; return x; }

/* Some versions of flex want this.  */
#ifndef yywrap
int yywrap () { return 1; }
#endif
%}

%a 4000
%o 5000

CMDFILENAMECHAR   [_a-zA-Z0-9\/\.\\_\+\$\:\[\]\\\,\=\&\!\<\>\-\~]
CMDFILENAMECHAR1  [_a-zA-Z0-9\/\.\\_\+\$\:\[\]\\\,\=\&\!\<\>\~]
FILENAMECHAR1	[_a-zA-Z\/\.\\\$\_\~]
SYMBOLCHARN     [_a-zA-Z\/\.\\0-9]
FILENAMECHAR	[_a-zA-Z0-9\/\.\-\_\+\=\$\:\[\]\\\,\~]
FILENAME	{FILENAMECHAR}+
WHITE		[ \t\n]+ 

NOCFILENAMECHAR	[_a-zA-Z0-9\/\.\-\_\+\$\:\[\]\\\~]


%s SCRIPT
%s EXPRESSION
%s BOTH
%s DEFSYMEXP
%s MRI
%%

  if (parser_input != input_selected)
    {
      /* The first token of the input determines the initial parser state.  */
      input_type t = parser_input;
      parser_input = input_selected;
      switch (t)
	{
	case input_script: return INPUT_SCRIPT; break;
	case input_mri_script: return INPUT_MRI_SCRIPT; break;
	case input_defsym: return INPUT_DEFSYM; break;
	default: abort ();
	}
    }

<BOTH,SCRIPT,EXPRESSION>"/*"	{ comment(); }


<DEFSYMEXP>"-"                  { RTOKEN('-');}
<DEFSYMEXP>"+"                  { RTOKEN('+');}
<DEFSYMEXP>{FILENAMECHAR1}{SYMBOLCHARN}*   { yylval.name = buystring(yytext); return NAME; }
<DEFSYMEXP>"="                  { RTOKEN('='); }

<MRI,EXPRESSION>"$"([0-9A-Fa-f])+ {
  				yylval.integer = bfd_scan_vma (yytext+1, 0,16);
				return INT;
			}

<MRI,EXPRESSION>([0-9A-Fa-f])+(H|X|B|O|D) {
				   int ibase ;
				   switch (yytext[yyleng-1]) {
				    case 'X': 
				    case 'H':
				     ibase = 16;
				     break;
				    case 'O':
				     ibase = 8;
				     break;
				    case 'B':
				     ibase = 2;
				     break;
				    default:
				     ibase = 10;
				   }
				   yylval.integer = bfd_scan_vma (yytext+1, 0,
								  ibase);
				   return INT;
				 }
<SCRIPT,DEFSYMEXP,MRI,BOTH,EXPRESSION>"$"?"0x"?([0-9A-Fa-f])+(M|K|m|k)? {
				  yylval.integer = bfd_scan_vma (yytext, 0,
								 hex_mode);
				  if (yytext[yyleng-1]=='M'
					|| yytext[yyleng-1] == 'm') {
				      yylval.integer *= 1024*1024;
				    }	
				  if (yytext[yyleng-1]=='K' 
				|| yytext[yyleng-1]=='k') {
				      yylval.integer *= 1024;
				    }		
				  return INT;
				}
<BOTH,SCRIPT,EXPRESSION>"]"		{ RTOKEN(']');}
<BOTH,SCRIPT,EXPRESSION>"["		{ RTOKEN('[');}
<BOTH,SCRIPT,EXPRESSION>"<<="	{ RTOKEN(LSHIFTEQ);}
<BOTH,SCRIPT,EXPRESSION>">>="	{ RTOKEN(RSHIFTEQ);}
<BOTH,SCRIPT,EXPRESSION>"||"	{ RTOKEN(OROR);}
<BOTH,SCRIPT,EXPRESSION>"=="	{ RTOKEN(EQ);}
<BOTH,SCRIPT,EXPRESSION>"!="	{ RTOKEN(NE);}
<BOTH,SCRIPT,EXPRESSION>">="	{ RTOKEN(GE);}
<BOTH,SCRIPT,EXPRESSION>"<="	{ RTOKEN(LE);}
<BOTH,SCRIPT,EXPRESSION>"<<"	{ RTOKEN(LSHIFT);}
<BOTH,SCRIPT,EXPRESSION>">>"	{ RTOKEN(RSHIFT);}
<BOTH,SCRIPT,EXPRESSION>"+="	{ RTOKEN(PLUSEQ);}
<BOTH,SCRIPT,EXPRESSION>"-="	{ RTOKEN(MINUSEQ);}
<BOTH,SCRIPT,EXPRESSION>"*="	{ RTOKEN(MULTEQ);}
<BOTH,SCRIPT,EXPRESSION>"/="	{ RTOKEN(DIVEQ);}
<BOTH,SCRIPT,EXPRESSION>"&="	{ RTOKEN(ANDEQ);}
<BOTH,SCRIPT,EXPRESSION>"|="	{ RTOKEN(OREQ);}
<BOTH,SCRIPT,EXPRESSION>"&&"	{ RTOKEN(ANDAND);}
<BOTH,SCRIPT,EXPRESSION>">"		{ RTOKEN('>');}
<MRI,BOTH,SCRIPT,EXPRESSION>","		{ RTOKEN(',');}
<BOTH,SCRIPT,EXPRESSION>"&"		{ RTOKEN('&');}
<BOTH,SCRIPT,EXPRESSION>"|"		{ RTOKEN('|');}
<BOTH,SCRIPT,EXPRESSION>"~"		{ RTOKEN('~');}
<BOTH,SCRIPT,EXPRESSION>"!"		{ RTOKEN('!');}
<BOTH,SCRIPT,EXPRESSION>"?"		{ RTOKEN('?');}
<BOTH,SCRIPT,EXPRESSION>"*"		{ RTOKEN('*');}
<BOTH,SCRIPT,EXPRESSION>"+"		{ RTOKEN('+');}
<BOTH,SCRIPT,EXPRESSION>"-"		{ RTOKEN('-');}
<BOTH,SCRIPT,EXPRESSION>"/"		{ RTOKEN('/');}
<BOTH,SCRIPT,EXPRESSION>"%"		{ RTOKEN('%');}
<BOTH,SCRIPT,EXPRESSION>"<"		{ RTOKEN('<');}
<MRI,BOTH,SCRIPT,EXPRESSION>"="          { RTOKEN('=');}
<BOTH,SCRIPT,EXPRESSION>"}"			{ RTOKEN('}') ; }
<BOTH,SCRIPT,EXPRESSION>"{"			{ RTOKEN('{'); }
<BOTH,SCRIPT,EXPRESSION>")"			{ RTOKEN(')');}
<BOTH,SCRIPT,EXPRESSION>"("			{ RTOKEN('(');}
<BOTH,SCRIPT,EXPRESSION>":"		{ RTOKEN(':'); }
<BOTH,SCRIPT,EXPRESSION>";"		{ RTOKEN(';');}
<BOTH,SCRIPT>"MEMORY"		{ RTOKEN(MEMORY);}
<BOTH,SCRIPT>"ORIGIN"		{ RTOKEN(ORIGIN);}
<BOTH,SCRIPT>"BLOCK"			{ RTOKEN(BLOCK);}
<BOTH,SCRIPT>"LENGTH"		{ RTOKEN(LENGTH);}
<EXPRESSION,BOTH,SCRIPT>"ALIGN"			{ RTOKEN(ALIGN_K);}
<EXPRESSION,BOTH,SCRIPT>"ADDR"			{ RTOKEN(ADDR);}
<BOTH,SCRIPT>"ENTRY"			{ RTOKEN(ENTRY);}
<EXPRESSION,BOTH,SCRIPT>"NEXT"			{ RTOKEN(NEXT);}
<EXPRESSION,BOTH,SCRIPT>"sizeof_headers"	{ RTOKEN(SIZEOF_HEADERS);}
<EXPRESSION,BOTH,SCRIPT>"SIZEOF_HEADERS"	{ RTOKEN(SIZEOF_HEADERS);}
<BOTH,SCRIPT>"MAP"			{ RTOKEN(MAP);}
<EXPRESSION,BOTH,SCRIPT>"SIZEOF"		{ RTOKEN(SIZEOF);}
<BOTH,SCRIPT>"TARGET"		{ RTOKEN(TARGET_K);}
<BOTH,SCRIPT>"SEARCH_DIR"		{ RTOKEN(SEARCH_DIR);}
<BOTH,SCRIPT>"OUTPUT"		{ RTOKEN(OUTPUT);}
<BOTH,SCRIPT>"INPUT"			{ RTOKEN(INPUT);}
<EXPRESSION,BOTH,SCRIPT>"DEFINED"		{ RTOKEN(DEFINED);}
<BOTH,SCRIPT>"CREATE_OBJECT_SYMBOLS"	{ RTOKEN(CREATE_OBJECT_SYMBOLS);}
<BOTH,SCRIPT>"CONSTRUCTORS"		{ RTOKEN( CONSTRUCTORS);}
<BOTH,SCRIPT>"FORCE_COMMON_ALLOCATION" { RTOKEN(FORCE_COMMON_ALLOCATION);}
<BOTH,SCRIPT>"SECTIONS"		{ RTOKEN(SECTIONS);}
<BOTH,SCRIPT>"FILL"			{ RTOKEN(FILL);}
<BOTH,SCRIPT>"STARTUP"		{ RTOKEN(STARTUP);}
<BOTH,SCRIPT>"OUTPUT_FORMAT"		{ RTOKEN(OUTPUT_FORMAT);}
<BOTH,SCRIPT>"OUTPUT_ARCH"		{ RTOKEN( OUTPUT_ARCH);}
<BOTH,SCRIPT>"HLL"			{ RTOKEN(HLL);}
<BOTH,SCRIPT>"SYSLIB"		{ RTOKEN(SYSLIB);}
<BOTH,SCRIPT>"FLOAT"			{ RTOKEN(FLOAT);}
<BOTH,SCRIPT>"QUAD"			{ RTOKEN( QUAD);}
<BOTH,SCRIPT>"LONG"			{ RTOKEN( LONG);}
<BOTH,SCRIPT>"SHORT"			{ RTOKEN( SHORT);}
<BOTH,SCRIPT>"BYTE"			{ RTOKEN( BYTE);}
<BOTH,SCRIPT>"NOFLOAT"		{ RTOKEN(NOFLOAT);}
<EXPRESSION,BOTH,SCRIPT>"NOLOAD"		{ RTOKEN(NOLOAD);}
<BOTH,SCRIPT>"DSECT"			{ RTOKEN(DSECT);}
<BOTH,SCRIPT>"COPY"			{ RTOKEN(COPY);}
<BOTH,SCRIPT>"INFO"			{ RTOKEN(INFO);}
<BOTH,SCRIPT>"OVERLAY"		{ RTOKEN(OVERLAY);}
<BOTH,SCRIPT>"o"			{ RTOKEN(ORIGIN);}
<BOTH,SCRIPT>"org"			{ RTOKEN(ORIGIN);}
<BOTH,SCRIPT>"l"			{ RTOKEN( LENGTH);}
<BOTH,SCRIPT>"len"			{ RTOKEN( LENGTH);}
<BOTH,SCRIPT>"INCLUDE"			{ RTOKEN(INCLUDE);}
<EXPRESSION,BOTH,SCRIPT>"AT"			{ RTOKEN(AT);}
<MRI>"\n"	                { ++ lineno;  RTOKEN(NEWLINE); }
<MRI>"*".*			{ /* Mri comment line */ }
<MRI>"END"                      { RTOKEN(ENDWORD); }
<MRI>"ALIGNMOD"		{ RTOKEN(ALIGNMOD);}
<MRI>"ALIGN"		{ RTOKEN(ALIGN_K);}

<MRI>"CHIP"                     { RTOKEN(CHIP); }
<MRI>"BASE"                     { RTOKEN(BASE); }
<MRI>"ALIAS"                     { RTOKEN(ALIAS); }
<MRI>"TRUNCATE"                     { RTOKEN(TRUNCATE); }
<MRI>"LOAD"                     { RTOKEN(LOAD); }
<MRI>"PUBLIC"                   { RTOKEN(PUBLIC); }
<MRI>"ORDER"                    { RTOKEN(ORDER); }
<MRI>"NAME"                     { RTOKEN(NAMEWORD); }
<MRI>"FORMAT"                   { RTOKEN(FORMAT); }
<MRI>"LIST".*                   { RTOKEN(LIST); /* LIST and ignore to end of line */ }
<MRI>"SECT"			{ RTOKEN(SECT); }
<EXPRESSION,BOTH,SCRIPT,MRI>"ABSOLUTE"			{ RTOKEN(ABSOLUTE); }
<MRI>"end"                      { RTOKEN(ENDWORD); }
<MRI>"chip"                     { RTOKEN(CHIP); }
<MRI>"load"                     { RTOKEN(LOAD); }
<MRI>"order"                    { RTOKEN(ORDER); }
<MRI>"name"                     { RTOKEN(NAMEWORD); }
<MRI>"format"                   { RTOKEN(FORMAT); }
<MRI>"list".*                   { RTOKEN(LIST); /* LIST and ignore to end of line */ }
<MRI>"sect"			{ RTOKEN(SECT); }
<EXPRESSION,BOTH,SCRIPT,MRI>"absolute"			{ RTOKEN(ABSOLUTE); }

<MRI>{FILENAMECHAR1}{NOCFILENAMECHAR}*	{
/* Filename without commas, needed to parse mri stuff */
				 yylval.name = buystring(yytext); 
				  return NAME;
				}


<BOTH,EXPRESSION>{FILENAMECHAR1}{FILENAMECHAR}*	{
				 yylval.name = buystring(yytext); 
				  return NAME;
				}
<SCRIPT>{FILENAMECHAR}* { yylval.name = buystring(yytext); 
				  return NAME;
				}

<EXPRESSION,BOTH,SCRIPT>"\""[^\"]*"\"" {
					/* No matter the state, quotes
					   give what's inside */
					yylval.name = buystring(yytext+1);
					yylval.name[yyleng-2] = 0;
					return NAME;
				}
<BOTH,SCRIPT,EXPRESSION>"\n"		{ lineno++;}
<MRI,BOTH,SCRIPT,EXPRESSION>[ \t]

<<EOF>> {
  include_stack_ptr--;
    
  if (include_stack_ptr == 0) 
  {
    yyterminate();
  }
  else 
  {
    yy_switch_to_buffer(include_stack[include_stack_ptr]);

  }
  BEGIN(SCRIPT);
  ldfile_input_filename = file_name_stack[include_stack_ptr-1];

  return END;
}

<SCRIPT,MRI>.		lex_warn_invalid(" in script", yytext);
<EXPRESSION,DEFSYMEXP,BOTH>.	lex_warn_invalid(" in expression", yytext);
    
%%


/* Switch flex to reading script file NAME, open on FILE,
   saving the current input info on the include stack.  */

void
lex_push_file (file, name)
     FILE *file;
     char *name;
{
  if (include_stack_ptr >= MAX_INCLUDE_DEPTH) 
    {
      einfo("%F:includes nested too deeply\n");
    }
  file_name_stack[include_stack_ptr] = name;
  include_stack[include_stack_ptr] = YY_CURRENT_BUFFER;

  include_stack_ptr++;
  yyin = file;
  yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
  BEGIN (SCRIPT);
}

/* Return a newly created flex input buffer containing STRING,
   which is SIZE bytes long.  */

static YY_BUFFER_STATE 
yy_create_string_buffer (string, size)
     CONST char *string;
     size_t size;
{
  YY_BUFFER_STATE b;

  /* Calls to m-alloc get turned by sed into xm-alloc.  */
  b = (YY_BUFFER_STATE) malloc (sizeof (struct yy_buffer_state));
  b->yy_input_file = 0;
  b->yy_buf_size = size;

  /* yy_ch_buf has to be 2 characters longer than the size given because
     we need to put in 2 end-of-buffer characters.  */
  b->yy_ch_buf = (YY_CHAR *) malloc ((unsigned) (b->yy_buf_size + 3));

  b->yy_ch_buf[0] = '\n';
  strcpy (b->yy_ch_buf+1, string);
  b->yy_ch_buf[size+1] = YY_END_OF_BUFFER_CHAR;
  b->yy_ch_buf[size+2] = YY_END_OF_BUFFER_CHAR;
  b->yy_n_chars = size+1;
  b->yy_buf_pos = &b->yy_ch_buf[1];
  b->yy_eof_status = EOF_NOT_SEEN;

  return b;
}

/* Switch flex to reading from STRING, saving the current input info
   on the include stack.  */

void
lex_redirect (string)
     CONST char *string;
{
  YY_BUFFER_STATE tmp;

  yy_init = 0;
  if (include_stack_ptr >= MAX_INCLUDE_DEPTH) 
    {
      einfo("%F: macros nested too deeply\n");
    }
  file_name_stack[include_stack_ptr] = "redirect";
  include_stack[include_stack_ptr] = YY_CURRENT_BUFFER;
  include_stack_ptr++;
  tmp = yy_create_string_buffer (string, strlen (string));
  yy_switch_to_buffer (tmp);
  BEGIN (SCRIPT);
}

/* Functions to switch to a different flex start condition,
   saving the current start condition on `state_stack'.  */

static int state_stack[MAX_INCLUDE_DEPTH * 2];
static int *state_stack_p = state_stack;

void
ldlex_script ()
{
  *(state_stack_p)++ = yy_start;
  BEGIN (SCRIPT);
}

void
ldlex_mri_script ()
{
  *(state_stack_p)++ = yy_start;
  BEGIN (MRI);
}

void
ldlex_defsym ()
{
  *(state_stack_p)++ = yy_start;
  BEGIN (DEFSYMEXP);
}
	   
void
ldlex_expression ()
{
  *(state_stack_p)++ = yy_start;
  BEGIN (EXPRESSION);
}

void
ldlex_both ()
{
  *(state_stack_p)++ = yy_start;
  BEGIN (BOTH);
}

void
ldlex_popstate ()
{
  yy_start = *(--state_stack_p);
}


/* Place up to MAX_SIZE characters in BUF and return in *RESULT
   either the number of characters read, or 0 to indicate EOF.  */

static void
yy_input (buf, result, max_size)
     char *buf;
     int *result;
     int max_size;
{
  *result = 0; 
  if (yy_current_buffer->yy_input_file)
    {
      if (yyin)
	{
	  *result = read (fileno (yyin), (char *) buf, max_size);
	  if (*result < 0) 
	    einfo ("%F%P: read in flex scanner failed");
	}
    }
}

/* Eat the rest of a C-style comment.  */

static void
comment ()
{
  int c;

  while (1)
  {
    c = input();
    while (c != '*' && c != EOF) 
    {
      if (c == '\n')
	lineno++;
      c = input();
    }

    if (c == '*')
    {
      c = input();
      while (c == '*')
       c = input();
      if (c == '/')
       break;			/* found the end */
    }

    if (c == '\n')
      lineno++;

    if (c == EOF)
    {
      einfo( "%F%P: EOF in comment\n");
      break;
    }
  }
}

/* Warn the user about a garbage character WHAT in the input
   in context WHERE.  */

static void
lex_warn_invalid (where, what)
     char *where, *what;
{
  fprintf(stderr,
	  "%s: ignoring invalid character `%s'%s\n",
	  program_name, what, where);
}
