%{
/*
 * Copyright (c) 1995, 1996, 1997, 1998 Kungliga Tekniska Högskolan
 * (Royal Institute of Technology, Stockholm, Sweden).
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by the Kungliga Tekniska
 *      Högskolan and its contributors.
 * 
 * 4. Neither the name of the Institute nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
RCSID("$Id: lex.l,v 1.13 1998/02/23 08:40:46 assar Exp $");
#endif

#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <roken.h>
#include "sym.h"
#include "types.h"
#include "parse.h"
#include "lex.h"
#include "output.h"

static unsigned lineno;

static char filename[256];

static void parse_filename (char *s);
static void parse_lineno (char *s);
%}

%%
^\#[ ][0-9]+[ ]\"[^\n\"]*\".*$	{ parse_filename (yytext); }
^\#[ ][0-9]+			{ parse_lineno (yytext); }
^\#ident.*$			{ }
const				{ return CONST; }
enum				{ return ENUM; }
struct				{ return STRUCT; }
typedef				{ return TYPEDEF; }
unsigned			{ return UNSIGNED; }
long				{ return LONG; }
int32_t				{ return LONG; }
u_long				{ return ULONG; }
u_int32_t			{ return ULONG; }
short				{ return SHORT; }
int16_t				{ return SHORT; }
u_short				{ return USHORT; }
u_int16_t			{ return USHORT; }
int				{ return INT; }
u_char				{ return UCHAR; }
char				{ return CHAR; }
string				{ return STRING; }
opaque				{ return OPAQUE; }
package				{ return PACKAGE; }
split				{ return SPLIT; }
multi				{ return MULTI; }
IN				{ return IN; }
OUT				{ return OUT; }
INOUT				{ return INOUT; }
ASIS				{ return ASIS; }
"["|"]"|[,;=()<>]|"{"|"}"|"*"	{ return *yytext; }
^\%[^\n]*$			{ yylval.name = strdup (yytext+1); return VERBATIM; }
-?[0-9]+			{ yylval.constant = atoi(yytext); return CONSTANT; }
0[Xx][0-9a-fA-F]+		{ yylval.constant = (int)strtol(yytext+2, NULL, 0x10); return CONSTANT; }
[A-Za-z][A-Za-z0-9_]*		{
Symbol *sym;

sym = findsym(yytext);
yylval.sym = sym;
if (sym == NULL) {
    yylval.name = strdup(yytext);
    return IDENTIFIER;
} else if (sym->type == TCONST)
    return IDCONST;
else if (sym->type == TTYPEDEF || sym->type == TENUM || sym->type == TSTRUCT)
    return IDTYPE;
else
    error_message ("Ignoring \"%s\"\n", yytext);
}
[ \t]				;
\n				{ lineno++; }
.				{ error_message("Ignoring char(%c)\n", *yytext); }
%%

#ifndef yywrap
int
yywrap (void)
{
     return 1;
}
#endif /* !yywrap */

void
error_message (char *format, ...)
{
     va_list args;

     va_start (args, format);
     fprintf (stderr, "%s:%d: ", filename, lineno);
     vfprintf (stderr, format, args);
     va_end (args);
}

static void
parse_filename (char *s)
{
     char *d1, *d2;

     lineno = atoi (s + 2);
     d1 = strchr (s, '"') + 1;
     d2 = strchr (d1, '"');
     *d2 = '\0';
     if (strcmp (d1, "") != 0)
	 strcpy (filename, d1);
}

static void
parse_lineno (char *s)
{
     lineno = atoi (s + 2);
}
