2012-03-18 18:24:12 +02:00
|
|
|
%{
|
|
|
|
/*
|
|
|
|
* 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>
|
2012-05-01 21:04:24 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2012-03-18 18:24:12 +02:00
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
#include "param.h"
|
2012-04-25 23:14:39 +03:00
|
|
|
#include "chr.h"
|
2012-05-23 22:05:24 +03:00
|
|
|
#include "bom.h"
|
2012-03-18 18:24:12 +02:00
|
|
|
#include "y.tab.h"
|
2012-04-26 09:02:48 +03:00
|
|
|
#include "lang.h"
|
2012-03-18 18:24:12 +02:00
|
|
|
|
|
|
|
|
2012-04-26 09:02:48 +03:00
|
|
|
extern int yyparse(void);
|
|
|
|
|
2012-05-30 18:11:53 +03:00
|
|
|
const char *file_name_override;
|
|
|
|
|
2012-04-26 09:02:48 +03:00
|
|
|
static int start_token;
|
2012-05-23 22:05:24 +03:00
|
|
|
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 */
|
2012-05-01 21:07:38 +03:00
|
|
|
static const char *file_name;
|
2012-04-28 17:51:47 +03:00
|
|
|
static int lineno;
|
2012-03-18 18:24:12 +02:00
|
|
|
|
2012-04-26 09:02:48 +03:00
|
|
|
|
2012-05-01 21:04:24 +03:00
|
|
|
static void open_stdin(const char *name)
|
2012-04-26 09:02:48 +03:00
|
|
|
{
|
2012-05-01 21:04:24 +03:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = open(name, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror(name);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (dup2(fd, 0) < 0) {
|
|
|
|
perror("dup2");
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-04-26 09:02:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-20 17:18:18 +03:00
|
|
|
static void do_parse(const char *name, int start, int nl, int pat)
|
2012-04-26 09:02:48 +03:00
|
|
|
{
|
2012-05-01 21:04:24 +03:00
|
|
|
open_stdin(name);
|
2012-05-22 03:25:29 +03:00
|
|
|
|
2012-05-01 21:04:24 +03:00
|
|
|
start_token = start;
|
|
|
|
expose_nl = nl;
|
2012-05-20 17:18:18 +03:00
|
|
|
pattern = pat;
|
2012-05-30 18:11:53 +03:00
|
|
|
file_name = file_name_override ? file_name_override : unique(name);
|
|
|
|
file_name_override = NULL;
|
2012-04-28 17:51:47 +03:00
|
|
|
lineno = 1;
|
2012-04-26 09:02:48 +03:00
|
|
|
yyparse();
|
|
|
|
}
|
|
|
|
|
2012-05-01 02:03:10 +03:00
|
|
|
|
2012-05-01 21:04:24 +03:00
|
|
|
void parse_hierarchy(const char *name)
|
2012-05-01 02:03:10 +03:00
|
|
|
{
|
2012-05-20 17:18:18 +03:00
|
|
|
do_parse(name, START_HIERARCHY, 0, 0);
|
2012-05-01 02:03:10 +03:00
|
|
|
}
|
|
|
|
|
2012-05-01 20:57:12 +03:00
|
|
|
|
2012-05-01 21:04:24 +03:00
|
|
|
void parse_characteristics(const char *name)
|
2012-05-01 04:49:33 +03:00
|
|
|
{
|
2012-05-20 17:18:18 +03:00
|
|
|
do_parse(name, START_CHAR, 1, 0);
|
2012-05-01 04:49:33 +03:00
|
|
|
}
|
|
|
|
|
2012-05-01 20:57:12 +03:00
|
|
|
|
2012-05-01 21:04:24 +03:00
|
|
|
void parse_inventory(const char *name)
|
2012-05-01 20:57:12 +03:00
|
|
|
{
|
2012-05-20 17:18:18 +03:00
|
|
|
do_parse(name, START_INVENTORY, 1, 0);
|
2012-05-01 21:04:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void parse_currencies(const char *name)
|
|
|
|
{
|
2012-05-20 17:18:18 +03:00
|
|
|
do_parse(name, START_EXCHANGE, 1, 0);
|
2012-05-01 21:04:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void parse_providers(const char *name)
|
|
|
|
{
|
2012-05-20 17:18:18 +03:00
|
|
|
do_parse(name, START_PROVIDERS, 1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void parse_substitutions(const char *name)
|
|
|
|
{
|
|
|
|
do_parse(name, START_SUBST, 0, 1);
|
2012-05-01 20:57:12 +03:00
|
|
|
}
|
|
|
|
|
2012-05-23 23:26:35 +03:00
|
|
|
|
|
|
|
void parse_symbols(const char *name)
|
|
|
|
{
|
|
|
|
do_parse(name, START_SYMBOLS, 1, 0);
|
|
|
|
}
|
|
|
|
|
2012-05-24 00:13:26 +03:00
|
|
|
|
|
|
|
static void process_bom_line(const char *s)
|
|
|
|
{
|
|
|
|
struct bom *b;
|
|
|
|
|
|
|
|
b = bom_parse_line(s);
|
|
|
|
bom_subst(b, substitutions);
|
2012-05-24 00:26:27 +03:00
|
|
|
bom_dump(stderr, b);
|
2012-05-24 00:13:26 +03:00
|
|
|
}
|
|
|
|
|
2012-03-18 18:24:12 +02:00
|
|
|
%}
|
|
|
|
|
2012-04-28 17:57:47 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We use ID for a bit of a hack: let %... be recognized as '%' WORD but treat
|
|
|
|
* ...% still as a single WORD.
|
|
|
|
*/
|
|
|
|
|
2012-05-24 05:25:33 +03:00
|
|
|
ID [-_A-Za-z0-9()+./,]
|
2012-05-20 17:18:18 +03:00
|
|
|
PAT "\\"[^\t\n]|[^ \t\n\\]
|
|
|
|
|
|
|
|
%s ID PAT
|
2012-05-23 22:05:24 +03:00
|
|
|
%x BOM
|
2012-04-28 17:57:47 +03:00
|
|
|
|
2012-03-18 18:24:12 +02:00
|
|
|
%%
|
|
|
|
|
2012-04-26 09:02:48 +03:00
|
|
|
%{
|
|
|
|
if (start_token) {
|
|
|
|
int tmp = start_token;
|
|
|
|
|
|
|
|
start_token = 0;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
%}
|
|
|
|
|
2012-05-21 05:16:51 +03:00
|
|
|
<INITIAL,ID>{ID}({ID}|"%")* { yylval.s = unique(yytext);
|
2012-03-18 18:24:12 +02:00
|
|
|
return WORD; }
|
|
|
|
|
2012-05-20 17:18:18 +03:00
|
|
|
<PAT>{PAT}* { BEGIN(ID);
|
|
|
|
yylval.s = stralloc(yytext);
|
|
|
|
return PATTERN; }
|
|
|
|
|
2012-05-23 22:05:24 +03:00
|
|
|
<BOM>"eeschema \("[^\n]* { hash = 0;
|
|
|
|
return BOM_EESCHEMA; }
|
|
|
|
<BOM>"|"[^\n]* { if (hash == 1)
|
2012-05-24 00:13:26 +03:00
|
|
|
process_bom_line(yytext); }
|
2012-05-23 22:05:24 +03:00
|
|
|
<BOM>"#End Cmp" { YY_FLUSH_BUFFER;
|
|
|
|
return 0; }
|
|
|
|
<BOM>#[^\n]* hash++;
|
|
|
|
<BOM>\n lineno++;
|
|
|
|
<BOM>. return *yytext;
|
|
|
|
|
2012-05-21 05:16:51 +03:00
|
|
|
[<>=] { if (pattern)
|
2012-05-20 17:18:18 +03:00
|
|
|
BEGIN(PAT);
|
2012-05-21 05:16:51 +03:00
|
|
|
return *yytext; }
|
2012-05-20 17:18:18 +03:00
|
|
|
|
2012-05-21 05:16:51 +03:00
|
|
|
"<=" { if (pattern)
|
|
|
|
BEGIN(PAT);
|
|
|
|
return TOK_LE; }
|
|
|
|
">=" { if (pattern)
|
|
|
|
BEGIN(PAT);
|
|
|
|
return TOK_GE; }
|
2012-03-18 18:24:12 +02:00
|
|
|
|
2012-04-26 01:37:53 +03:00
|
|
|
"//"[^\n]* ;
|
2012-04-28 17:51:47 +03:00
|
|
|
"/*"([^*]|("*"+([^*/])))*"*"+"/" { const char *s = yytext;
|
|
|
|
while (*s)
|
|
|
|
lineno += *s++ == '\n'; }
|
2012-04-26 01:37:53 +03:00
|
|
|
|
2012-03-18 18:24:12 +02:00
|
|
|
[ \t] ;
|
2012-04-28 17:46:42 +03:00
|
|
|
\n { lineno++;
|
|
|
|
if (expose_nl)
|
|
|
|
return TOK_NL; }
|
2012-03-18 18:24:12 +02:00
|
|
|
^#[^n]*\n lineno++;
|
|
|
|
|
|
|
|
. return *yytext;
|
|
|
|
|
|
|
|
%%
|
|
|
|
|
|
|
|
|
2012-04-29 04:38:06 +03:00
|
|
|
void yywarnf(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2012-05-01 21:07:38 +03:00
|
|
|
fprintf(stderr, "%s:%d: warning: ", file_name, lineno);
|
2012-04-29 04:38:06 +03:00
|
|
|
vfprintf(stderr, fmt, ap) ;
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-31 18:13:06 +03:00
|
|
|
void yywarn(const char *s)
|
|
|
|
{
|
|
|
|
yywarnf("%s", s);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-20 17:18:18 +03:00
|
|
|
void __attribute__((noreturn)) yyerrorf(const char *fmt, ...)
|
2012-03-18 18:24:12 +02:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2012-05-01 21:07:38 +03:00
|
|
|
fprintf(stderr, "%s:%d: ", file_name, lineno);
|
2012-03-18 18:24:12 +02:00
|
|
|
vfprintf(stderr, fmt, ap) ;
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
va_end(ap);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-20 17:18:18 +03:00
|
|
|
void __attribute__((noreturn)) yyerror(const char *s)
|
2012-03-18 18:24:12 +02:00
|
|
|
{
|
2012-05-31 18:13:06 +03:00
|
|
|
yyerrorf("%s", s);
|
2012-03-18 18:24:12 +02:00
|
|
|
}
|
2012-05-23 22:05:24 +03:00
|
|
|
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
}
|