%{
/*
 * lang.l - BOOM syntax
 *
 *  Copyright 2012 by Werner Almesberger
 *
 * This program 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 of the License, or
 * (at your option) any later version.
 */

#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

#include "util.h"
#include "param.h"
#include "chr.h"
#include "bom.h"
#include "y.tab.h"
#include "lang.h"


extern int yyparse(void);

const char *file_name_override;

static int start_token;
static int expose_nl;	/* 0: ignore \n; 1: return TOK_NL */
static int pattern;	/* 0: = relops are normal; 1: relops switch to PAT */
static int hash;	/* number of hashes seen in BOM mode */
static const char *file_name;
static int lineno;


static void open_stdin(const char *name)
{
	int fd;

	fd = open(name, O_RDONLY);
	if (fd < 0) {
		perror(name);
		exit(1);
	}
	if (dup2(fd, 0) < 0) {
		perror("dup2");
		exit(1);
	}
}


static void do_parse(const char *name, int start, int nl, int pat)
{
	open_stdin(name);

	start_token = start;
	expose_nl = nl;
	pattern = pat;
	file_name = file_name_override ? file_name_override : unique(name);
	file_name_override = NULL;
	lineno = 1;
	yyparse();
}


void parse_hierarchy(const char *name)
{
	do_parse(name, START_HIERARCHY, 0, 0);
}


void parse_characteristics(const char *name)
{
	do_parse(name, START_CHAR, 1, 0);
}


void parse_inventory(const char *name)
{
	do_parse(name, START_INVENTORY, 1, 0);
}


void parse_currencies(const char *name)
{
	do_parse(name, START_EXCHANGE, 1, 0);
}


void parse_providers(const char *name)
{
	do_parse(name, START_PROVIDERS, 1, 0);
}


void parse_substitutions(const char *name)
{
	do_parse(name, START_SUBST, 0, 1);
}


void parse_symbols(const char *name)
{
	do_parse(name, START_SYMBOLS, 1, 0);
}


static void process_bom_line(const char *s)
{
	struct bom *b;

	b = bom_parse_line(s);
	bom_subst(b, substitutions);
	bom_dump(stderr, b);
}

%}


/*
 * We use ID for a bit of a hack: let %... be recognized as '%' WORD but treat
 * ...% still as a single WORD.
 */

ID	[-_A-Za-z0-9()+./,]
PAT	"\\"[^\t\n]|[^ \t\n\\]

%s ID PAT
%x BOM

%%

%{
	if (start_token) {
		int tmp = start_token;

		start_token = 0;
		return tmp;
	}
%}

<INITIAL,ID>{ID}({ID}|"%")*	{ yylval.s = unique(yytext);
				  return WORD; }

<PAT>{PAT}*			{ BEGIN(ID);
				  yylval.s = stralloc(yytext);
				  return PATTERN; }

<BOM>"eeschema \("[^\n]*	{ hash = 0;
				  return BOM_EESCHEMA; }
<BOM>"|"[^\n]*			{ if (hash == 1)
					process_bom_line(yytext); }
<BOM>"#End Cmp"			{ YY_FLUSH_BUFFER;
				  return 0; }
<BOM>#[^\n]*			hash++;
<BOM>\n				lineno++;
<BOM>.				return *yytext;

[<>=]				{ if (pattern)
					BEGIN(PAT);
				  return *yytext; }

"<="				{ if (pattern)
					BEGIN(PAT);
				  return TOK_LE; }
">="				{ if (pattern)
					BEGIN(PAT);
				  return TOK_GE; }

"//"[^\n]*			;
"/*"([^*]|("*"+([^*/])))*"*"+"/" { const char *s = yytext;
				  while (*s)
					lineno += *s++ == '\n'; }

[ \t]				;
\n				{ lineno++;
				  if (expose_nl)
					return TOK_NL; }
^#[^n]*\n			lineno++;

.				return *yytext;

%%


void yywarnf(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	fprintf(stderr, "%s:%d: warning: ", file_name, lineno);
	vfprintf(stderr, fmt, ap) ;
	fprintf(stderr, "\n");
	va_end(ap);
}


void yywarn(const char *s)
{
	yywarnf("%s", s);
}


void __attribute__((noreturn)) yyerrorf(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	fprintf(stderr, "%s:%d: ", file_name, lineno);
	vfprintf(stderr, fmt, ap) ;
	fprintf(stderr, "\n");
	va_end(ap);
	exit(1);
}


void __attribute__((noreturn)) yyerror(const char *s)
{
	yyerrorf("%s", s);
}


/* Define parse_kicad_bom here, so that we have access to BOM and INITIAL */

void parse_kicad_bom(const char *name)
{
	BEGIN(BOM);
	do_parse(name, START_BOM, 1, 0);
	BEGIN(INITIAL);
}