/*******************************************************************************
|
/*******************************************************************************
|
**
|
**
|
** Filename: zpp.l
|
** Filename: zpp.l
|
**
|
**
|
** Project: Zip CPU -- a small, lightweight, RISC CPU core
|
** Project: Zip CPU -- a small, lightweight, RISC CPU core
|
**
|
**
|
** Purpose: The preprocessor for the Zip Assembler.
|
** Purpose: The preprocessor for the Zip Assembler.
|
**
|
**
|
** This routine strips comments, handles #define's, #ifdef's, and
|
** This routine strips comments, handles #define's, #ifdef's, and
|
** #include statements in a C fashion.
|
** #include statements in a C fashion.
|
**
|
**
|
** #define macro's are also defined in the language and therefore
|
** #define macro's are also defined in the language and therefore
|
** supposed to be supported, but the support isn't there yet.
|
** supposed to be supported, but the support isn't there yet.
|
**
|
**
|
** Creator: Dan Gisselquist, Ph.D.
|
** Creator: Dan Gisselquist, Ph.D.
|
** Gisselquist Tecnology, LLC
|
** Gisselquist Technology, LLC
|
**
|
**
|
********************************************************************************
|
********************************************************************************
|
**
|
**
|
** Copyright (C) 2015, Gisselquist Technology, LLC
|
** Copyright (C) 2015, Gisselquist Technology, LLC
|
**
|
**
|
** This program is free software (firmware): you can redistribute it and/or
|
** This program is free software (firmware): you can redistribute it and/or
|
** modify it under the terms of the GNU General Public License as published
|
** modify it under the terms of the GNU General Public License as published
|
** by the Free Software Foundation, either version 3 of the License, or (at
|
** by the Free Software Foundation, either version 3 of the License, or (at
|
** your option) any later version.
|
** your option) any later version.
|
**
|
**
|
** This program is distributed in the hope that it will be useful, but WITHOUT
|
** This program is distributed in the hope that it will be useful, but WITHOUT
|
** ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
** ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
** FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
** FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
** for more details.
|
** for more details.
|
**
|
**
|
** You should have received a copy of the GNU General Public License along
|
** You should have received a copy of the GNU General Public License along
|
** with this program. (It's in the $(ROOT)/doc directory, run make with no
|
** with this program. (It's in the $(ROOT)/doc directory, run make with no
|
** target there if the PDF file isn't present.) If not, see
|
** target there if the PDF file isn't present.) If not, see
|
** for a copy.
|
** for a copy.
|
**
|
**
|
** License: GPL, v3, as defined and found on www.gnu.org,
|
** License: GPL, v3, as defined and found on www.gnu.org,
|
** http://www.gnu.org/licenses/gpl.html
|
** http://www.gnu.org/licenses/gpl.html
|
**
|
**
|
**
|
**
|
*******************************************************************************/
|
*******************************************************************************/
|
|
|
%{
|
%{
|
// #include
|
// #include
|
#include
|
#include
|
#include
|
#include
|
#include
|
#include
|
#include
|
#include
|
#include
|
#include
|
#include
|
#include
|
|
|
using namespace std;
|
using namespace std;
|
|
|
int yylex();
|
int yylex();
|
void mark_line(void);
|
void mark_line(void);
|
int end_of_file(void);
|
int end_of_file(void);
|
void pushf(const char *fname);
|
void pushf(const char *fname);
|
|
|
// #include "zprepr.tab.h"
|
// #include "zprepr.tab.h"
|
int ndef = 0, structno = 0;
|
int ndef = 0, structno = 0;
|
char *structid = NULL;
|
char *structid = NULL;
|
void stb_define(const char *str);
|
void stb_define(const char *str);
|
bool stb_current(const char *str);
|
bool stb_current(const char *str);
|
bool stb_hasargs(const char *str);
|
bool stb_hasargs(const char *str);
|
void stb_expand(FILE *fout, const char *str);
|
void stb_expand(FILE *fout, const char *str);
|
std::string stb_expand(const char *str);
|
std::string stb_expand(const char *str);
|
void stb_args(const char *str);
|
void stb_args(const char *str);
|
void stb_macro(const char *value);
|
void stb_macro(const char *value);
|
void stb_addmacro(const char *value);
|
void stb_addmacro(const char *value);
|
bool stb_isdefined(const char *str);
|
bool stb_isdefined(const char *str);
|
const char *stb_getdefn(const char *str);
|
const char *stb_getdefn(const char *str);
|
%}
|
%}
|
%x DEF DFA DFV DFV_EOL INDEF IFDEFV INNOTDEF NODEF NVRDEF COMMENT INSTRUCT
|
%x DEF DFA DFV DFV_EOL INDEF IFDEFV INNOTDEF NODEF NVRDEF COMMENT INSTRUCT
|
%x INDEF_EOL INNOTDEF_EOL GETSTRUCTID NVRDEF_EOL DONTDEF
|
%x INDEF_EOL INNOTDEF_EOL GETSTRUCTID NVRDEF_EOL DONTDEF
|
%option noyywrap
|
%option noyywrap
|
%option stack
|
%option stack
|
ID [_:A-Za-z][_:A-Za-z0-9]*
|
ID [_:A-Za-z][_:A-Za-z0-9]*
|
IDDOT {ID}("."{ID})*
|
IDDOT {ID}("."{ID})*
|
|
|
%%
|
%%
|
"*/" { yy_pop_state(); }
|
"*/" { yy_pop_state(); }
|
[^*\n]+ { /* Ignore comments */ }
|
[^*\n]+ { /* Ignore comments */ }
|
"*"+[^/] { /* Ignore comments */ }
|
"*"+[^/] { /* Ignore comments */ }
|
^"#include"[ \t]+\"[^\n\"]+\" {
|
^"#include"[ \t]+\"[^\n\"]+\" {
|
char *ptr = &yytext[9], *start, *end, *str;
|
char *ptr = &yytext[9], *start, *end, *str;
|
while(isspace(*ptr))
|
while(isspace(*ptr))
|
ptr++;
|
ptr++;
|
start = ++ptr;
|
start = ++ptr;
|
ptr++;
|
ptr++;
|
while((*ptr)&&(*ptr != '\"'))
|
while((*ptr)&&(*ptr != '\"'))
|
ptr++;
|
ptr++;
|
*ptr = '\0';
|
*ptr = '\0';
|
pushf(start);
|
pushf(start);
|
// push_file_state(yylineno); // and filename ...
|
// push_file_state(yylineno); // and filename ...
|
mark_line();
|
mark_line();
|
}
|
}
|
^"#define"[ \t]+ { yy_push_state(DEF); }
|
^"#define"[ \t]+ { yy_push_state(DEF); }
|
/* <*>^"#line"[ \t]+(0-9)+[ \t]+["][^"]*["][ \t]*\n { } */
|
/* <*>^"#line"[ \t]+(0-9)+[ \t]+["][^"]*["][ \t]*\n { } */
|
{IDDOT}/[^(] {
|
{IDDOT}/[^(] {
|
// fprintf(stderr, "Defining \'%s\'\n", yytext);
|
// fprintf(stderr, "Defining \'%s\'\n", yytext);
|
stb_define(yytext);
|
stb_define(yytext);
|
BEGIN DFV;
|
BEGIN DFV;
|
}
|
}
|
{IDDOT}/[(] { stb_define(yytext); BEGIN DFA; }
|
{IDDOT}/[(] { stb_define(yytext); BEGIN DFA; }
|
"("[^)\n]+")" {
|
"("[^)\n]+")" {
|
/* Process macro arguments */
|
/* Process macro arguments */
|
stb_args(yytext);
|
stb_args(yytext);
|
BEGIN DFV;
|
BEGIN DFV;
|
}
|
}
|
^[ \t]+ { stb_macro(yytext); /* Replicate initial spaces */ }
|
^[ \t]+ { stb_macro(yytext); /* Replicate initial spaces */ }
|
[ \t]+ { stb_macro(" "); /* Ignore all but one internal space */ }
|
[ \t]+ { stb_macro(" "); /* Ignore all but one internal space */ }
|
{ID} {/* Parse to end of line, get value for our define */
|
{ID} {/* Parse to end of line, get value for our define */
|
// fprintf(stderr, "%s may be a macro, line %d\n", yytext, yylineno);
|
// fprintf(stderr, "%s may be a macro, line %d\n", yytext, yylineno);
|
if ((!stb_current(yytext))&&(stb_isdefined(yytext))) {
|
if ((!stb_current(yytext))&&(stb_isdefined(yytext))) {
|
// fprintf(stderr, "Recursive MACRO!\n");
|
// fprintf(stderr, "Recursive MACRO!\n");
|
stb_macro(stb_getdefn(yytext));
|
stb_macro(stb_getdefn(yytext));
|
} else {
|
} else {
|
// fprintf(stderr, "But it is not defined\n");
|
// fprintf(stderr, "But it is not defined\n");
|
stb_macro(yytext);
|
stb_macro(yytext);
|
}
|
}
|
}
|
}
|
{ID}[ \t]*[(][ \t]*{ID}[ \t]*(,[ \t]*{ID}[ \t]*)*[)] {
|
{ID}[ \t]*[(][ \t]*{ID}[ \t]*(,[ \t]*{ID}[ \t]*)*[)] {
|
// fprintf(stderr, "%s may be a macro within a macro!\n", yytext);
|
// fprintf(stderr, "%s may be a macro within a macro!\n", yytext);
|
if ((!stb_current(yytext))&&(stb_isdefined(yytext))) {
|
if ((!stb_current(yytext))&&(stb_isdefined(yytext))) {
|
if (stb_hasargs(yytext)) {
|
if (stb_hasargs(yytext)) {
|
std::string str = stb_expand(yytext);
|
std::string str = stb_expand(yytext);
|
stb_macro(str.c_str());
|
stb_macro(str.c_str());
|
} else {
|
} else {
|
char *dup = strdup(yytext), *ptr;
|
char *dup = strdup(yytext), *ptr;
|
ptr = strchr(dup, '(');
|
ptr = strchr(dup, '(');
|
*ptr = '\0';
|
*ptr = '\0';
|
stb_macro(stb_getdefn(dup));
|
stb_macro(stb_getdefn(dup));
|
free(dup);
|
free(dup);
|
yyless(strchr(yytext,'(')-yytext);
|
yyless(strchr(yytext,'(')-yytext);
|
}
|
}
|
|
} else if (!stb_current(yytext)) {
|
|
stb_macro(yytext);
|
} else {
|
} else {
|
char *dup = strdup(yytext), *ptr;
|
char *dup = strdup(yytext), *ptr;
|
ptr = strchr(dup, '(');
|
ptr = strchr(dup, '(');
|
*ptr = '\0';
|
*ptr = '\0';
|
stb_macro(stb_getdefn(dup));
|
stb_macro(stb_getdefn(dup));
|
free(dup);
|
free(dup);
|
yyless(strchr(yytext,'(')-yytext);
|
yyless(strchr(yytext,'(')-yytext);
|
}
|
}
|
}
|
}
|
[^a-zA-Z_0-9 \t\n]+ {/* Parse to end of line, get value for our define */
|
[^a-zA-Z_0-9 \t\n]+ {/* Parse to end of line, get value for our define */
|
// fprintf(stderr, "A: Adding to macro %s\n", yytext);
|
// fprintf(stderr, "A: Adding to macro %s\n", yytext);
|
stb_macro(yytext);
|
stb_macro(yytext);
|
}
|
}
|
0[xX][0-9A-Fa-f]+ {/* Get any hexadecimal constants */
|
0[xX][0-9A-Fa-f]+ {/* Get any hexadecimal constants */
|
// fprintf(stderr, "B: Adding to macro %s\n", yytext);
|
// fprintf(stderr, "B: Adding to macro %s\n", yytext);
|
stb_macro(yytext);
|
stb_macro(yytext);
|
}
|
}
|
[0-9A-Fa-f]+[hH] {/* Get any hexadecimal constants */
|
[0-9A-Fa-f]+[hH] {/* Get any hexadecimal constants */
|
// fprintf(stderr, "C: Adding to macro %s\n", yytext);
|
// fprintf(stderr, "C: Adding to macro %s\n", yytext);
|
stb_macro(yytext);
|
stb_macro(yytext);
|
}
|
}
|
[0-7]+[oO] {/* Get any octal constants */
|
[0-7]+[oO] {/* Get any octal constants */
|
// fprintf(stderr, "D: Adding to macro %s\n", yytext);
|
// fprintf(stderr, "D: Adding to macro %s\n", yytext);
|
stb_macro(yytext);
|
stb_macro(yytext);
|
}
|
}
|
[0-9]+ {/* Get any decimal constants */
|
[0-9]+ {/* Get any decimal constants */
|
// fprintf(stderr, "E: Adding to macro %s\n", yytext);
|
// fprintf(stderr, "E: Adding to macro %s\n", yytext);
|
stb_macro(yytext);
|
stb_macro(yytext);
|
}
|
}
|
[ \t]*((;|"//").*)?$ {/* Parse to end of line, get value for our define */
|
[ \t]*((;|"//").*)?$ {/* Parse to end of line, get value for our define */
|
yy_pop_state();
|
yy_pop_state();
|
}
|
}
|
[ \t]*"\\"[ \t]*((;|"//").*)?\n {/* Continue onto next line */
|
[ \t]*"\\"[ \t]*((;|"//").*)?\n {/* Continue onto next line */
|
fprintf(yyout, "\n"); mark_line(); yylineno++;
|
fprintf(yyout, "\n"); mark_line(); yylineno++;
|
stb_macro("\n");
|
stb_macro("\n");
|
}
|
}
|
^"#define".*$ { yy_push_state(DONTDEF); }
|
^"#define".*$ { yy_push_state(DONTDEF); }
|
"\\"[ \t]*((;"//").*)?\n {/* Continue onto next line */
|
"\\"[ \t]*((;"//").*)?\n {/* Continue onto next line */
|
fprintf(yyout, "\n"); mark_line(); yylineno++; }
|
fprintf(yyout, "\n"); mark_line(); yylineno++; }
|
[a-zA-Z0-9_,.()]* { }
|
[a-zA-Z0-9_,.()]* { }
|
[ \t]* { }
|
[ \t]* { }
|
((;|"//").*)?$ { yy_pop_state(); }
|
((;|"//").*)?$ { yy_pop_state(); }
|
"\\"[ \t]*((;|"//").*)?\n {/* Continue onto next line */
|
"\\"[ \t]*((;|"//").*)?\n {/* Continue onto next line */
|
fprintf(yyout, "\n"); mark_line(); yylineno++; }
|
fprintf(yyout, "\n"); mark_line(); yylineno++; }
|
^[ \t]+.[dD][aA][tT][aA]? { fprintf(yyout, "\tWORD"); }
|
^[ \t]+.[dD][aA][tT][aA]? { fprintf(yyout, "\tWORD"); }
|
^"#defcont"[ \t]+ {
|
^"#defcont"[ \t]+ {
|
yy_push_state(DFV); }
|
yy_push_state(DFV); }
|
^"#ifdef"[ \t]* { ndef = 0;
|
^"#ifdef"[ \t]* { ndef = 0;
|
yy_push_state(IFDEFV); }
|
yy_push_state(IFDEFV); }
|
^"#ifndef"[ \t]* { ndef = 1;
|
^"#ifndef"[ \t]* { ndef = 1;
|
yy_push_state(IFDEFV); }
|
yy_push_state(IFDEFV); }
|
^"#ifdef"[ \t]* { ndef = 2;
|
^"#ifdef"[ \t]* { ndef = 2;
|
yy_push_state(IFDEFV); }
|
yy_push_state(IFDEFV); }
|
^"#ifndef"[ \t]* { ndef = 2;
|
^"#ifndef"[ \t]* { ndef = 2;
|
yy_push_state(IFDEFV); }
|
yy_push_state(IFDEFV); }
|
{IDDOT} {
|
{IDDOT} {
|
bool known = stb_isdefined(yytext);
|
bool known = stb_isdefined(yytext);
|
if (ndef == 2) {
|
if (ndef == 2) {
|
BEGIN NVRDEF_EOL;
|
BEGIN NVRDEF_EOL;
|
} else if ( ((known)&&(ndef==0)) || ((!known)&&(ndef!=0)) ) {
|
} else if ( ((known)&&(ndef==0)) || ((!known)&&(ndef!=0)) ) {
|
BEGIN INDEF_EOL;
|
BEGIN INDEF_EOL;
|
} else {
|
} else {
|
BEGIN INNOTDEF_EOL;
|
BEGIN INNOTDEF_EOL;
|
}
|
}
|
}
|
}
|
/* Not yet: ^"#if"[ \t]* { yy_push_state(IFDEFE); }
|
/* Not yet: ^"#if"[ \t]* { yy_push_state(IFDEFE); }
|
/* Not yet: ^"#if"[ \t]* { yy_push_state(IFDEFE); }
|
/* Not yet: ^"#if"[ \t]* { yy_push_state(IFDEFE); }
|
/* Not yet: { yy_push_state(IFDEFE); } */
|
/* Not yet: { yy_push_state(IFDEFE); } */
|
/* ^"#elsif"[ \t]* { yy_pop_state(); yy_push_state(IFDEFE); } */
|
/* ^"#elsif"[ \t]* { yy_pop_state(); yy_push_state(IFDEFE); } */
|
[ \t]*$ { BEGIN INDEF; }
|
[ \t]*$ { BEGIN INDEF; }
|
(;|"//").*$ { BEGIN INDEF; }
|
(;|"//").*$ { BEGIN INDEF; }
|
[ \t]*$ { BEGIN INNOTDEF; }
|
[ \t]*$ { BEGIN INNOTDEF; }
|
(;|"//").*$ { BEGIN INNOTDEF; }
|
(;|"//").*$ { BEGIN INNOTDEF; }
|
[ \t]*$ { BEGIN NVRDEF; }
|
[ \t]*$ { BEGIN NVRDEF; }
|
(;|"//").*$ { BEGIN NVRDEF; }
|
(;|"//").*$ { BEGIN NVRDEF; }
|
[^ \t\n].*$ { BEGIN INDEF; mark_line(); }
|
[^ \t\n].*$ { BEGIN INDEF; mark_line(); }
|
[^ \t\n].*$ { BEGIN INNOTDEF; mark_line(); }
|
[^ \t\n].*$ { BEGIN INNOTDEF; mark_line(); }
|
^"#else"[ \t]*((;|"//").*)?$ { BEGIN NODEF; }
|
^"#else"[ \t]*((;|"//").*)?$ { BEGIN NODEF; }
|
^"#else"[ \t]*((;|"//").*)?$ { BEGIN INDEF; }
|
^"#else"[ \t]*((;|"//").*)?$ { BEGIN INDEF; }
|
^[^#\n]([^\n]*)$ { /* Skip text */ }
|
^[^#\n]([^\n]*)$ { /* Skip text */ }
|
^"#elsif"[ \t]* { BEGIN NVRDEF; }
|
^"#elsif"[ \t]* { BEGIN NVRDEF; }
|
^"#endif"[ \t]*((;|"//").*)?$ {
|
^"#endif"[ \t]*((;|"//").*)?$ {
|
yy_pop_state(); }
|
yy_pop_state(); }
|
^"#endif"[ \t]*"/*" { BEGIN COMMENT; }
|
^"#endif"[ \t]*"/*" { BEGIN COMMENT; }
|
^"#endif" { yy_pop_state(); }
|
^"#endif" { yy_pop_state(); }
|
<*>^"#ifdef"[ \t]* { fprintf(stderr, "ERR: Line %d, Unknown ifdef!! (state = %d)\n", yylineno, YYSTATE);}
|
<*>^"#ifdef"[ \t]* { fprintf(stderr, "ERR: Line %d, Unknown ifdef!! (state = %d)\n", yylineno, YYSTATE);}
|
<*>^"#else"[ \t]* { fprintf(stderr, "ERR: Line %d, Unknown else!! (state = %d)\n", yylineno, YYSTATE);}
|
<*>^"#else"[ \t]* { fprintf(stderr, "ERR: Line %d, Unknown else!! (state = %d)\n", yylineno, YYSTATE);}
|
<*>^"#endif"[ \t]* { fprintf(stderr, "ERR: Line %d, Unknown endif!! (state = %d)\n", yylineno, YYSTATE);}
|
<*>^"#endif"[ \t]* { fprintf(stderr, "ERR: Line %d, Unknown endif!! (state = %d)\n", yylineno, YYSTATE);}
|
^"#struct"[ \t]* { yy_push_state(GETSTRUCTID); structno = 0; }
|
^"#struct"[ \t]* { yy_push_state(GETSTRUCTID); structno = 0; }
|
<*>^"#"{ID}[ \t]* {
|
<*>^"#"{ID}[ \t]* {
|
fprintf(stderr, "ERR: Line %d, unrecognized preprocessor instruction, \'%s\' (state = %d)\n",
|
fprintf(stderr, "ERR: Line %d, unrecognized preprocessor instruction, \'%s\' (state = %d)\n",
|
yylineno, yytext, YYSTATE);
|
yylineno, yytext, YYSTATE);
|
}
|
}
|
{ID}/[ \t\n;/] { BEGIN INSTRUCT;
|
{ID}/[ \t\n;/] { BEGIN INSTRUCT;
|
structid = strdup(yytext);
|
structid = strdup(yytext);
|
}
|
}
|
{ID}("."{ID})* {
|
{ID}("."{ID})* {
|
fprintf(yyout, "\t%s.%s\tequ\t%d", structid, yytext, structno++); }
|
fprintf(yyout, "\t%s.%s\tequ\t%d", structid, yytext, structno++); }
|
^"#endstruct".*$ { yy_pop_state(); }
|
^"#endstruct".*$ { yy_pop_state(); }
|
/* Not yet: ^"#struct"[ \t]* {} */
|
/* Not yet: ^"#struct"[ \t]* {} */
|
/* Not yet: ^"#endstruct"[ \t]* {} */
|
/* Not yet: ^"#endstruct"[ \t]* {} */
|
/* Not yet: ^"#seg"[ \t]* {} */
|
/* Not yet: ^"#seg"[ \t]* {} */
|
{ID}/[^(] {
|
{ID}/[^(] {
|
if (stb_isdefined(yytext))
|
if (stb_isdefined(yytext))
|
fprintf(yyout, "%s", stb_getdefn(yytext));
|
fprintf(yyout, "%s", stb_getdefn(yytext));
|
else
|
else
|
fprintf(yyout, "%s", yytext);
|
fprintf(yyout, "%s", yytext);
|
}
|
}
|
{ID}([ \t]*) {
|
{ID}([ \t]*) {
|
if (stb_isdefined(yytext))
|
if (stb_isdefined(yytext))
|
fprintf(yyout, "%s", stb_getdefn(yytext));
|
fprintf(yyout, "%s", stb_getdefn(yytext));
|
else
|
else
|
fprintf(yyout, "%s", yytext);
|
fprintf(yyout, "%s", yytext);
|
}
|
}
|
{ID}[ \t]*[(][ \t]*{ID}[ \t]*(,[ \t]*{ID}[ \t]*)*[)] {
|
{ID}[ \t]*[(][ \t]*{ID}[ \t]*(,[ \t]*{ID}[ \t]*)*[)] {
|
// fprintf(stderr, "%s may be a macro!\n", yytext);
|
// fprintf(stderr, "%s may be a macro!\n", yytext);
|
if (stb_isdefined(yytext)) {
|
if (stb_isdefined(yytext)) {
|
if (stb_hasargs(yytext)) {
|
if (stb_hasargs(yytext)) {
|
stb_expand(yyout, yytext);
|
stb_expand(yyout, yytext);
|
} else {
|
} else {
|
fprintf(yyout, "%s", stb_getdefn(yytext));
|
fprintf(yyout, "%s", stb_getdefn(yytext));
|
yyless(strchr(yytext,'(')-yytext);
|
yyless(strchr(yytext,'(')-yytext);
|
}
|
}
|
} else {
|
} else {
|
// fprintf(stderr, "But it is not defined\n");
|
// fprintf(stderr, "But it is not defined\n");
|
fprintf(yyout, "%s", yytext);
|
fprintf(yyout, "%s", yytext);
|
}
|
}
|
}
|
}
|
|
"\'"(("\\\'")|([^'\\])|("\\"[0abfnrtv])|("\\\\")|("\\\"")){1,4}"\'" { ECHO; }
|
<*>[ \t]*"//".*$ { /* Ignore (trailing) comment only lines */ }
|
<*>[ \t]*"//".*$ { /* Ignore (trailing) comment only lines */ }
|
<*>[ \t]*";".*$ { /* Ignore (trailing) comment only lines */ }
|
<*>[ \t]*";".*$ { /* Ignore (trailing) comment only lines */ }
|
<*>"#warning".*$ { fprintf(stderr, "WARNING: %s\n", &yytext[8]); }
|
<*>"#warning".*$ { fprintf(stderr, "WARNING: %s\n", &yytext[8]); }
|
<*>"#error".*$ { fprintf(stderr, "ERROR: %s\n", &yytext[8]); exit(-1); }
|
<*>"#error".*$ { fprintf(stderr, "ERROR: %s\n", &yytext[8]); exit(-1); }
|
<*>"/*" { yy_push_state(COMMENT); }
|
<*>"/*" { yy_push_state(COMMENT); }
|
<*>[ \t]+ { ECHO; }
|
<*>[ \t]+ { ECHO; }
|
<*>\n { ECHO; yylineno++; mark_line(); }
|
<*>\n { ECHO; yylineno++; mark_line(); }
|
/* <*>. { printf("Unmatched \'%c\'\n", yytext[0]); } */
|
/* <*>. { printf("Unmatched \'%c\'\n", yytext[0]); } */
|
<> { fprintf(stderr, "Unexpected EOF! Expecting #endif\n"); yyterminate(); }
|
<> { fprintf(stderr, "Unexpected EOF! Expecting #endif\n"); yyterminate(); }
|
<> { fprintf(stderr, "Unexpected EOF! Expecting #endif\n"); yyterminate(); }
|
<> { fprintf(stderr, "Unexpected EOF! Expecting #endif\n"); yyterminate(); }
|
<> { fprintf(stderr, "Unexpected EOF! Expecting */\n"); yyterminate(); }
|
<> { fprintf(stderr, "Unexpected EOF! Expecting */\n"); yyterminate(); }
|
<> { fprintf(stderr, "Unexpected EOF! Expecting #endstruct\n"); yyterminate(); }
|
<> { fprintf(stderr, "Unexpected EOF! Expecting #endstruct\n"); yyterminate(); }
|
<> { fprintf(stderr, "Unexpected EOF! Expecting #struct ID, then #endstruct\n"); yyterminate(); }
|
<> { fprintf(stderr, "Unexpected EOF! Expecting #struct ID, then #endstruct\n"); yyterminate(); }
|
<> { fprintf(stderr, "Unexpected EOF! Expecting end of line\n"); yyterminate(); }
|
<> { fprintf(stderr, "Unexpected EOF! Expecting end of line\n"); yyterminate(); }
|
< { fprintf(stderr, "Unexpected EOF Expecting end of line, then #endif\n"); yyterminate(); }
|
< { fprintf(stderr, "Unexpected EOF Expecting end of line, then #endif\n"); yyterminate(); }
|
<> { if (end_of_file()) yyterminate(); }
|
<> { if (end_of_file()) yyterminate(); }
|
|
|
%%
|
%%
|
|
|
class SYMTABLE_ACTION {
|
class SYMTABLE_ACTION {
|
public:
|
public:
|
// Types: 0 (end of actions), 1-X, argument number, <0 raw string
|
// Types: 0 (end of actions), 1-X, argument number, <0 raw string
|
int m_type;
|
int m_type;
|
std::string m_str; // if m_type < 0, have m_str
|
std::string m_str; // if m_type < 0, have m_str
|
};
|
};
|
|
|
class SYMTABLE_ENTRY {
|
class SYMTABLE_ENTRY {
|
private:
|
private:
|
bool m_reduced;
|
bool m_reduced;
|
std::string &trim(std::string &s) {
|
std::string &trim(std::string &s) {
|
std::string::iterator ptr = s.end();
|
std::string::iterator ptr = s.end();
|
|
|
while((ptr >= s.begin())&&(isspace(*ptr)))
|
while((ptr >= s.begin())&&(isspace(*ptr)))
|
*ptr-- = '\0';
|
*ptr-- = '\0';
|
|
|
return s;
|
return s;
|
}
|
}
|
|
|
public:
|
public:
|
std::string m_name, m_value, m_args;
|
std::string m_name, m_value, m_args;
|
std::vector m_actions;
|
std::vector m_actions;
|
SYMTABLE_ENTRY(const char *str) : m_name(str) {
|
SYMTABLE_ENTRY(const char *str) : m_name(str) {
|
trim(m_name);
|
trim(m_name);
|
m_reduced = false;
|
m_reduced = false;
|
}
|
}
|
SYMTABLE_ENTRY &operator+=(const char *str) {
|
SYMTABLE_ENTRY &operator+=(const char *str) {
|
const char *start = str;
|
const char *start = str;
|
|
|
while(isspace(*start))
|
while(isspace(*start))
|
start++;
|
start++;
|
if (m_value.length()!=0)
|
if (m_value.length()!=0)
|
m_value += " ";
|
m_value += " ";
|
|
|
m_value += str;
|
m_value += str;
|
|
|
/*
|
/*
|
printf("ENTRY::SYMBOL \'%s\' NOW = \'%s\'\n",
|
printf("ENTRY::SYMBOL \'%s\' NOW = \'%s\'\n",
|
m_name.c_str(), m_value.c_str());
|
m_name.c_str(), m_value.c_str());
|
*/
|
*/
|
return *this;
|
return *this;
|
}
|
}
|
SYMTABLE_ENTRY &setargs(const char *str) {
|
SYMTABLE_ENTRY &setargs(const char *str) {
|
m_args += str;
|
m_args += str;
|
return *this;
|
return *this;
|
}
|
}
|
|
|
const std::string &getdefn(void) {
|
const std::string &getdefn(void) {
|
return m_value;
|
return m_value;
|
}
|
}
|
|
|
bool hasargs(void) {
|
bool hasargs(void) {
|
return (m_args.size()>0);
|
return (m_args.size()>0);
|
}
|
}
|
|
|
void reduce(void) {
|
void reduce(void) {
|
if (m_reduced)
|
if (m_reduced)
|
return;
|
return;
|
|
|
// fprintf(stderr, "Reducing %s ( %s ) \n", m_name.c_str(), m_args.c_str());
|
// fprintf(stderr, "Reducing %s ( %s ) \n", m_name.c_str(), m_args.c_str());
|
std::vector alist;
|
std::vector alist;
|
int i=0, bg, en;
|
int i=0, bg, en;
|
do {
|
do {
|
if ((m_args[i] == ',')||(m_args[i] == '('))
|
if ((m_args[i] == ',')||(m_args[i] == '('))
|
i++;
|
i++;
|
while((m_args[i])&&(isspace(m_args[i])))
|
while((m_args[i])&&(isspace(m_args[i])))
|
i++;
|
i++;
|
bg = i;
|
bg = i;
|
while((m_args[i])&&(
|
while((m_args[i])&&(
|
(isalpha(m_args[i]))
|
(isalpha(m_args[i]))
|
||(m_args[i]==':')
|
||(m_args[i]==':')
|
||(m_args[i]=='_')
|
||(m_args[i]=='_')
|
||(isdigit(m_args[i]))))
|
||(isdigit(m_args[i]))))
|
i++;
|
i++;
|
en = i;
|
en = i;
|
while((m_args[i])&&(isspace(m_args[i])))
|
while((m_args[i])&&(isspace(m_args[i])))
|
i++;
|
i++;
|
|
|
alist.push_back(m_args.substr(bg,en-bg));
|
alist.push_back(m_args.substr(bg,en-bg));
|
// printf("Found argument %2ld of %s: \'%s\'\n",
|
// printf("Found argument %2ld of %s: \'%s\'\n",
|
// alist.size(),
|
// alist.size(),
|
// m_name.c_str(),
|
// m_name.c_str(),
|
// m_args.substr(bg,en-bg).c_str());
|
// m_args.substr(bg,en-bg).c_str());
|
} while((m_args[i])&&(m_args[i] == ','));
|
} while((m_args[i])&&(m_args[i] == ','));
|
|
|
assert(m_args[i] == ')');
|
assert(m_args[i] == ')');
|
|
|
// Now that we know our arguments, lets look for these
|
// Now that we know our arguments, lets look for these
|
// arguments in our macro definition
|
// arguments in our macro definition
|
std::string building;
|
std::string building;
|
i = 0;
|
i = 0;
|
while(m_value[i]) {
|
while(m_value[i]) {
|
int nxti = m_value.size(), nxtv;
|
int nxti = m_value.size(), nxtv;
|
for(int a=0; a
|
for(int a=0; a
|
const char *ptr;
|
const char *ptr;
|
ptr = strstr(m_value.c_str()+i, alist[a].c_str());
|
ptr = strstr(m_value.c_str()+i, alist[a].c_str());
|
while((ptr)&&(ptr-m_value.c_str() < nxti)) {
|
while((ptr)&&(ptr-m_value.c_str() < nxti)) {
|
int loc = ptr-m_value.c_str();
|
int loc = ptr-m_value.c_str();
|
const char *pre = ptr-1;
|
const char *pre = ptr-1;
|
const char *pst = ptr+alist[a].size();
|
const char *pst = ptr+alist[a].size();
|
if ((loc < nxti)&&(
|
if ((loc < nxti)&&(
|
(loc == i)
|
(loc == i)
|
||((!isalpha(*pre)
|
||((!isalpha(*pre)
|
&&(*pre != '_')
|
&&(*pre != '_')
|
&&(*pre != ':')
|
&&(*pre != ':')
|
&&(!isdigit(*pre)))))
|
&&(!isdigit(*pre)))))
|
&&((*pst=='\0')
|
&&((*pst=='\0')
|
||( (!isalpha(*pst))
|
||( (!isalpha(*pst))
|
&&(*pst != '_')
|
&&(*pst != '_')
|
&&(*pst != ':')
|
&&(*pst != ':')
|
&&(!isdigit(*pst)))))
|
&&(!isdigit(*pst)))))
|
{
|
{
|
nxti = loc;
|
nxti = loc;
|
nxtv = a;
|
nxtv = a;
|
break;
|
break;
|
} else {
|
} else {
|
ptr = strstr(m_value.c_str()+loc, alist[a].c_str());
|
ptr = strstr(m_value.c_str()+loc, alist[a].c_str());
|
loc = ptr-m_value.c_str();
|
loc = ptr-m_value.c_str();
|
}
|
}
|
}
|
}
|
}
|
}
|
|
|
if (nxti < m_value.size()) {
|
if (nxti < m_value.size()) {
|
// Found an argument!!
|
// Found an argument!!
|
SYMTABLE_ACTION act;
|
SYMTABLE_ACTION act;
|
if (nxti > i) {
|
if (nxti > i) {
|
act.m_type = -1;
|
act.m_type = -1;
|
act.m_str = m_value.substr(i,nxti-i);
|
act.m_str = m_value.substr(i,nxti-i);
|
// printf("ACTION: \'%s\'\n", act.m_str.c_str());
|
// printf("ACTION: \'%s\'\n", act.m_str.c_str());
|
m_actions.push_back(act);
|
m_actions.push_back(act);
|
}
|
}
|
act.m_type = nxtv;
|
act.m_type = nxtv;
|
act.m_str = "";
|
act.m_str = "";
|
m_actions.push_back(act);
|
m_actions.push_back(act);
|
// printf("ACTION[%2d]: \'%s\'\n", nxtv, alist[nxtv].c_str());
|
// printf("ACTION[%2d]: \'%s\'\n", nxtv, alist[nxtv].c_str());
|
|
|
i = nxti+alist[nxtv].size();
|
i = nxti+alist[nxtv].size();
|
} else break; // No more arguments
|
} else break; // No more arguments
|
} if (i
|
} if (i
|
SYMTABLE_ACTION act;
|
SYMTABLE_ACTION act;
|
act.m_type = -1;
|
act.m_type = -1;
|
act.m_str = m_value.substr(i);
|
act.m_str = m_value.substr(i);
|
// printf("ACTION: \'%s\'\n", act.m_str.c_str());
|
// printf("ACTION: \'%s\'\n", act.m_str.c_str());
|
m_actions.push_back(act);
|
m_actions.push_back(act);
|
}
|
}
|
m_reduced = true;
|
m_reduced = true;
|
}
|
}
|
|
|
std::string expand(std::string args) {
|
std::string expand(std::string args) {
|
if (!m_reduced)
|
if (!m_reduced)
|
reduce();
|
reduce();
|
std::vector alist;
|
std::vector alist;
|
std::string result;
|
std::string result;
|
|
|
// printf("Expanding %s\n", args.c_str());
|
// printf("Expanding %s\n", args.c_str());
|
int i=0, bg, en, nest=-1;
|
int i=0, bg, en, nest=-1;
|
do {
|
do {
|
if ((args[i] == '(')||(args[i] == ',')) {
|
if ((args[i] == '(')||(args[i] == ',')) {
|
if (args[i] =='(')
|
if (args[i] =='(')
|
nest++;
|
nest++;
|
i++;
|
i++;
|
}
|
}
|
while((args[i])&&(isspace(args[i])))
|
while((args[i])&&(isspace(args[i])))
|
i++;
|
i++;
|
bg = i;
|
bg = i;
|
while((args[i])&&(args[i] != ',')&&((args[i] != ')')||(nest != 0))) {
|
while((args[i])&&(args[i] != ',')&&((args[i] != ')')||(nest != 0))) {
|
if (args[i] == '(')
|
if (args[i] == '(')
|
nest++;
|
nest++;
|
else if (args[i] == ')')
|
else if (args[i] == ')')
|
nest--;
|
nest--;
|
i++;
|
i++;
|
} en = i-1;
|
} en = i-1;
|
while((en>0)&&(isspace(args[en])))
|
while((en>0)&&(isspace(args[en])))
|
en--;
|
en--;
|
alist.push_back(args.substr(bg,en+1-bg));
|
alist.push_back(args.substr(bg,en+1-bg));
|
// printf("Argument %2ld of %s maps to \'%s\'\n",
|
// printf("Argument %2ld of %s maps to \'%s\'\n",
|
// alist.size(),
|
// alist.size(),
|
// m_name.c_str(),
|
// m_name.c_str(),
|
// args.substr(bg,en+1-bg).c_str());
|
// args.substr(bg,en+1-bg).c_str());
|
} while((args[i])&&(args[i] == ','));
|
} while((args[i])&&(args[i] == ','));
|
|
|
// printf("At end, args[i] = \'%s\'\n", &args[i]);
|
// printf("At end, args[i] = \'%s\'\n", &args[i]);
|
assert(args[i] == ')');
|
assert(args[i] == ')');
|
|
|
// printf("Filling in %ld actions\n", m_actions.size());
|
// printf("Filling in %ld actions\n", m_actions.size());
|
for(i=0; i
|
for(i=0; i
|
if((m_actions[i].m_type >= 0)&&(m_actions[i].m_type < alist.size()))
|
if((m_actions[i].m_type >= 0)&&(m_actions[i].m_type < alist.size()))
|
result += alist[m_actions[i].m_type].c_str();
|
result += alist[m_actions[i].m_type].c_str();
|
else if (m_actions[i].m_type < 0)
|
else if (m_actions[i].m_type < 0)
|
result += m_actions[i].m_str.c_str();
|
result += m_actions[i].m_str.c_str();
|
// else {
|
// else {
|
// fprintf(fout, "m_type = %d, size = %ld\n", m_actions[i].m_type, alist.size());
|
// fprintf(fout, "m_type = %d, size = %ld\n", m_actions[i].m_type, alist.size());
|
// }
|
// }
|
}
|
}
|
|
|
return result;
|
return result;
|
}
|
}
|
};
|
};
|
|
|
class SYMBOL_TABLE {
|
class SYMBOL_TABLE {
|
private:
|
private:
|
typedef SYMTABLE_ENTRY *TBLV;
|
typedef SYMTABLE_ENTRY *TBLV;
|
typedef std::list TBLT;
|
typedef std::list TBLT;
|
|
|
TBLT m_tbl;
|
TBLT m_tbl;
|
TBLT::iterator lookup(const char *str) {
|
TBLT::iterator lookup(const char *str) {
|
TBLT::iterator i = m_tbl.begin();
|
TBLT::iterator i = m_tbl.begin();
|
for(; (i!= m_tbl.end())&&(strcmp(str, (*i)->m_name.c_str())>0); i++)
|
for(; (i!= m_tbl.end())&&(strcmp(str, (*i)->m_name.c_str())>0); i++)
|
;
|
;
|
if ((i != m_tbl.end())&&(strcmp(str, (*i)->m_name.c_str())==0))
|
if ((i != m_tbl.end())&&(strcmp(str, (*i)->m_name.c_str())==0))
|
return i;
|
return i;
|
return m_tbl.end();
|
return m_tbl.end();
|
}
|
}
|
|
|
public:
|
public:
|
SYMBOL_TABLE(void) {}
|
SYMBOL_TABLE(void) {}
|
|
|
void define(const char *str) {
|
void define(const char *str) {
|
SYMTABLE_ENTRY *v = new SYMTABLE_ENTRY(str);
|
SYMTABLE_ENTRY *v = new SYMTABLE_ENTRY(str);
|
TBLT::iterator i = m_tbl.begin();
|
TBLT::iterator i = m_tbl.begin();
|
for(; (i!= m_tbl.end())&&(strcmp(str, (*i)->m_name.c_str())>0); i++)
|
for(; (i!= m_tbl.end())&&(strcmp(str, (*i)->m_name.c_str())>0); i++)
|
;
|
;
|
m_tbl.insert(i, v);
|
m_tbl.insert(i, v);
|
|
|
// fprintf(stderr, "SYMS::Defining SYMBOL: \'%s\'\n", str);
|
// fprintf(stderr, "SYMS::Defining SYMBOL: \'%s\'\n", str);
|
}
|
}
|
|
|
bool defined(const char *str) {
|
bool defined(const char *str) {
|
TBLT::iterator i = lookup(str);
|
TBLT::iterator i = lookup(str);
|
if (i==m_tbl.end())
|
if (i==m_tbl.end())
|
return false;
|
return false;
|
else
|
else
|
return true;
|
return true;
|
}
|
}
|
|
|
|
|
void undefine(const char *str) {
|
void undefine(const char *str) {
|
TBLT::iterator i = lookup(str);
|
TBLT::iterator i = lookup(str);
|
if (i == m_tbl.end())
|
if (i == m_tbl.end())
|
return;
|
return;
|
TBLV v = (*i);
|
TBLV v = (*i);
|
m_tbl.erase(i);
|
m_tbl.erase(i);
|
delete v;
|
delete v;
|
}
|
}
|
|
|
void addmacro(const char *name, const char *str) {
|
void addmacro(const char *name, const char *str) {
|
TBLT::iterator i = lookup(name);
|
TBLT::iterator i = lookup(name);
|
if (i == m_tbl.end()) {
|
if (i == m_tbl.end()) {
|
fprintf(stderr, "ADDMACRO::INTERNAL ERR, \'%s\' NOT DEFINED!\n", name);
|
fprintf(stderr, "ADDMACRO::INTERNAL ERR, \'%s\' NOT DEFINED!\n", name);
|
} *(*i) += str;
|
} *(*i) += str;
|
}
|
}
|
|
|
void addargs(const char *name, const char *str) {
|
void addargs(const char *name, const char *str) {
|
TBLT::iterator i = lookup(name);
|
TBLT::iterator i = lookup(name);
|
if (i == m_tbl.end()) {
|
if (i == m_tbl.end()) {
|
fprintf(stderr, "INTERNAL ERR, %s NOT DEFINED!\n", name);
|
fprintf(stderr, "INTERNAL ERR, %s NOT DEFINED!\n", name);
|
} (*i)->setargs(str);
|
} (*i)->setargs(str);
|
}
|
}
|
bool hasargs(const char *name) {
|
bool hasargs(const char *name) {
|
TBLT::iterator i = lookup(name);
|
TBLT::iterator i = lookup(name);
|
if (i == m_tbl.end()) {
|
if (i == m_tbl.end()) {
|
return false;
|
return false;
|
} return (*i)->hasargs();
|
} return (*i)->hasargs();
|
}
|
}
|
const char *getdefn(const char *name) {
|
const char *getdefn(const char *name) {
|
TBLT::iterator i = lookup(name);
|
TBLT::iterator i = lookup(name);
|
if (i == m_tbl.end()) {
|
if (i == m_tbl.end()) {
|
fprintf(stderr, "GETDEFN::INTERNAL ERR, \'%s\' NOT DEFINED!\n", name);
|
fprintf(stderr, "GETDEFN::INTERNAL ERR, \'%s\' NOT DEFINED!\n", name);
|
return NULL;
|
return NULL;
|
} (*i)->getdefn().c_str();
|
} (*i)->getdefn().c_str();
|
}
|
}
|
|
|
std::string expand(const char *name, const char *ptr) {
|
std::string expand(const char *name, const char *ptr) {
|
TBLT::iterator i = lookup(name);
|
TBLT::iterator i = lookup(name);
|
if (i==m_tbl.end())
|
if (i==m_tbl.end())
|
return std::string("");
|
return std::string("");
|
return (*i)->expand(std::string(ptr));
|
return (*i)->expand(std::string(ptr));
|
}
|
}
|
|
|
};
|
};
|
|
|
SYMTABLE_ENTRY *last = NULL;
|
SYMTABLE_ENTRY *last = NULL;
|
SYMBOL_TABLE syms;
|
SYMBOL_TABLE syms;
|
std::string last_define;
|
std::string last_define;
|
|
|
char *stb_trim(const char *str) {
|
char *stb_trim(const char *str) {
|
// fprintf(stderr, "Checking whether %s needs to be expanded\n",str);
|
// fprintf(stderr, "Checking whether %s needs to be expanded\n",str);
|
char *dup = strdup(str), *chr;
|
char *dup = strdup(str), *chr;
|
chr = strchr(dup, '(');
|
chr = strchr(dup, '(');
|
if (chr != NULL)
|
if (chr != NULL)
|
*chr = '\0';
|
*chr = '\0';
|
// fprintf(stderr, "\tLooking it up by the name \'%s\'\n", dup);
|
// fprintf(stderr, "\tLooking it up by the name \'%s\'\n", dup);
|
|
|
// Now, let's trim our string
|
// Now, let's trim our string
|
char *end = dup+strlen(dup)-1;
|
char *end = dup+strlen(dup)-1;
|
while((*dup)&&(end>dup)&&(isspace(*end)))
|
while((*dup)&&(end>dup)&&(isspace(*end)))
|
*end-- = '\0';
|
*end-- = '\0';
|
return dup;
|
return dup;
|
}
|
}
|
|
|
void stb_define(const char *str) {
|
void stb_define(const char *str) {
|
/*
|
/*
|
if (last_define.size()>0) {
|
if (last_define.size()>0) {
|
fprintf(stderr, "LAST-DEFINE(%s): %s\n", last_define.c_str(),
|
fprintf(stderr, "LAST-DEFINE(%s): %s\n", last_define.c_str(),
|
stb_getdefn(last_define.c_str()));
|
stb_getdefn(last_define.c_str()));
|
} */
|
} */
|
char *alt = stb_trim(str);
|
char *alt = stb_trim(str);
|
if (syms.defined(alt)) {
|
if (syms.defined(alt)) {
|
fprintf(stderr, "WARNING! Symbol \'%s\' is already defined!\n", str);
|
fprintf(stderr, "WARNING! Symbol \'%s\' is already defined!\n", str);
|
syms.undefine(str);
|
syms.undefine(str);
|
}
|
}
|
|
|
syms.define(alt);
|
syms.define(alt);
|
last_define = alt;
|
last_define = alt;
|
free(alt);
|
free(alt);
|
}
|
}
|
|
|
void stb_args(const char *args) {
|
void stb_args(const char *args) {
|
syms.addargs(last_define.c_str(), args);
|
syms.addargs(last_define.c_str(), args);
|
}
|
}
|
|
|
void stb_macro(const char *value) {
|
void stb_macro(const char *value) {
|
syms.addmacro(last_define.c_str(), value);
|
syms.addmacro(last_define.c_str(), value);
|
}
|
}
|
|
|
void stb_addmacro(const char *value) {
|
void stb_addmacro(const char *value) {
|
syms.addmacro(last_define.c_str(),value);
|
syms.addmacro(last_define.c_str(),value);
|
}
|
}
|
|
|
bool stb_isdefined(const char *str) {
|
bool stb_isdefined(const char *str) {
|
char *dup = stb_trim(str);
|
char *dup = stb_trim(str);
|
bool r = (syms.defined(dup));
|
bool r = (syms.defined(dup));
|
free(dup);
|
free(dup);
|
return r;
|
return r;
|
}
|
}
|
|
|
const char *stb_getdefn(const char *str) {
|
const char *stb_getdefn(const char *str) {
|
char *dup = stb_trim(str);
|
char *dup = stb_trim(str);
|
const char *r;
|
const char *r;
|
r = syms.getdefn(dup);
|
r = syms.getdefn(dup);
|
free(dup);
|
free(dup);
|
return r;
|
return r;
|
}
|
}
|
|
|
bool stb_current(const char *str) {
|
bool stb_current(const char *str) {
|
return (strcmp(str, last_define.c_str())==0);
|
return (strcmp(str, last_define.c_str())==0);
|
}
|
}
|
|
|
bool stb_hasargs(const char *str) {
|
bool stb_hasargs(const char *str) {
|
char *dup = stb_trim(str);
|
char *dup = stb_trim(str);
|
bool r = (syms.hasargs(dup));
|
bool r = (syms.hasargs(dup));
|
// fprintf(stderr, "\t%s has %sarguments\n", dup, (r)?"":"no ");
|
// fprintf(stderr, "\t%s has %sarguments\n", dup, (r)?"":"no ");
|
free(dup);
|
free(dup);
|
return r;
|
return r;
|
}
|
}
|
|
|
std::string stb_expand(const char *macro) {
|
std::string stb_expand(const char *macro) {
|
const char *ptr;
|
const char *ptr;
|
std::string str;
|
std::string str;
|
ptr = strchr(macro, '(');
|
ptr = strchr(macro, '(');
|
assert(ptr);
|
assert(ptr);
|
ptr--;
|
ptr--;
|
while((ptr>macro)&&(isspace(*ptr)))
|
while((ptr>macro)&&(isspace(*ptr)))
|
ptr--;
|
ptr--;
|
char *nam = strndup(macro, ptr+1-macro);
|
char *nam = strndup(macro, ptr+1-macro);
|
ptr = strchr(ptr, '(');
|
ptr = strchr(ptr, '(');
|
// fprintf(stderr, "Requesting an expansion of %s -- %s\n", nam, ptr);
|
// fprintf(stderr, "Requesting an expansion of %s -- %s\n", nam, ptr);
|
str = syms.expand(nam, ptr);
|
str = syms.expand(nam, ptr);
|
free(nam);
|
free(nam);
|
|
|
return str;
|
return str;
|
}
|
}
|
|
|
void stb_expand(FILE *fout, const char *macro) {
|
void stb_expand(FILE *fout, const char *macro) {
|
std::string str = stb_expand(macro);
|
std::string str = stb_expand(macro);
|
fprintf(fout, "%s", str.c_str());
|
fprintf(fout, "%s", str.c_str());
|
}
|
}
|
|
|
class BUFSTACK {
|
class BUFSTACK {
|
public:
|
public:
|
FILE *m_fp;
|
FILE *m_fp;
|
char *m_fname;
|
char *m_fname;
|
int m_lineno;
|
int m_lineno;
|
BUFSTACK *m_prev;
|
BUFSTACK *m_prev;
|
YY_BUFFER_STATE m_bs;
|
YY_BUFFER_STATE m_bs;
|
|
|
static BUFSTACK *curbs;
|
static BUFSTACK *curbs;
|
static const char *curfilename;
|
static const char *curfilename;
|
|
|
BUFSTACK(void) {
|
BUFSTACK(void) {
|
m_fp = stdin;
|
m_fp = stdin;
|
|
|
if (curbs)
|
if (curbs)
|
curbs->m_lineno = yylineno;
|
curbs->m_lineno = yylineno;
|
m_prev = curbs;
|
m_prev = curbs;
|
// m_bs = yy_create_buffer(fp, YY_BUF_SIZE);
|
// m_bs = yy_create_buffer(fp, YY_BUF_SIZE);
|
m_fname = strdup("(stdin)");
|
m_fname = strdup("(stdin)");
|
// yy_switch_to_buffer(m_bs);
|
// yy_switch_to_buffer(m_bs);
|
m_bs = NULL;
|
m_bs = NULL;
|
curbs = this;
|
curbs = this;
|
m_lineno = 1;
|
m_lineno = 1;
|
curfilename = m_fname;
|
curfilename = m_fname;
|
|
|
yyrestart(m_fp);
|
yyrestart(m_fp);
|
yylineno = 1;
|
yylineno = 1;
|
}
|
}
|
|
|
BUFSTACK(const char *fname) {
|
BUFSTACK(const char *fname) {
|
m_fp = fopen(fname, "r");
|
m_fp = fopen(fname, "r");
|
if (!m_fp) {
|
if (!m_fp) {
|
char *pathptr = getenv("ZIPINC");
|
char *pathptr = getenv("ZIPINC");
|
if (!pathptr) {
|
if (!pathptr) {
|
fprintf(stderr, "Cannot open %s\n", fname);
|
fprintf(stderr, "Cannot open %s\n", fname);
|
perror("O/S Err:");
|
perror("O/S Err:");
|
exit(-1);
|
exit(-1);
|
} else {
|
} else {
|
char *dptr, *colonp;
|
char *dptr, *colonp;
|
char *pathcpy = new char[strlen(pathptr)+8192];
|
char *pathcpy = new char[strlen(pathptr)+8192];
|
strcpy(pathcpy, pathptr);
|
strcpy(pathcpy, pathptr);
|
|
|
// fprintf(stderr, "ZIPINC := %s\n", pathptr);
|
// fprintf(stderr, "ZIPINC := %s\n", pathptr);
|
dptr = pathptr;
|
dptr = pathptr;
|
while((!m_fp)&&(NULL != (colonp = strchr(dptr, ':')))) {
|
while((!m_fp)&&(NULL != (colonp = strchr(dptr, ':')))) {
|
strncpy(pathcpy, dptr, colonp-pathptr);
|
strncpy(pathcpy, dptr, colonp-pathptr);
|
strcat(pathcpy, "/");
|
strcat(pathcpy, "/");
|
strcat(pathcpy, fname);
|
strcat(pathcpy, fname);
|
// fprintf(stderr, "Looking for include file, %s\n", pathcpy);
|
// fprintf(stderr, "Looking for include file, %s\n", pathcpy);
|
if (access(fname, R_OK)==0)
|
if (access(fname, R_OK)==0)
|
m_fp = fopen(pathcpy, "r");
|
m_fp = fopen(pathcpy, "r");
|
dptr = colonp+1;
|
dptr = colonp+1;
|
} if ((!m_fp)&&(*dptr)) {
|
} if ((!m_fp)&&(*dptr)) {
|
strcpy(pathcpy, dptr);
|
strcpy(pathcpy, dptr);
|
strcat(pathcpy, "/");
|
strcat(pathcpy, "/");
|
strcat(pathcpy, fname);
|
strcat(pathcpy, fname);
|
// fprintf(stderr, "Looking for include file, %s\n", pathcpy);
|
// fprintf(stderr, "Looking for include file, %s\n", pathcpy);
|
m_fp = fopen(pathcpy, "r");
|
m_fp = fopen(pathcpy, "r");
|
} if (!m_fp) {
|
} if (!m_fp) {
|
fprintf(stderr, "Cannot open %s\n", fname);
|
fprintf(stderr, "Cannot open %s\n", fname);
|
perror("O/S Err:");
|
perror("O/S Err:");
|
exit(-1);
|
exit(-1);
|
}
|
}
|
|
|
delete[] pathcpy;
|
delete[] pathcpy;
|
}
|
}
|
}
|
}
|
|
|
if (curbs)
|
if (curbs)
|
curbs->m_lineno = yylineno;
|
curbs->m_lineno = yylineno;
|
m_prev = curbs;
|
m_prev = curbs;
|
m_bs = yy_create_buffer(m_fp, YY_BUF_SIZE);
|
m_bs = yy_create_buffer(m_fp, YY_BUF_SIZE);
|
m_fname = strdup(fname);
|
m_fname = strdup(fname);
|
yy_switch_to_buffer(m_bs);
|
yy_switch_to_buffer(m_bs);
|
curbs = this;
|
curbs = this;
|
m_lineno = 1;
|
m_lineno = 1;
|
curfilename = m_fname;
|
curfilename = m_fname;
|
|
|
yyrestart(m_fp);
|
yyrestart(m_fp);
|
yylineno = 1;
|
yylineno = 1;
|
}
|
}
|
|
|
~BUFSTACK(void) {
|
~BUFSTACK(void) {
|
// fprintf(stderr, "DELETING(%s)\n", m_fname);
|
// fprintf(stderr, "DELETING(%s)\n", m_fname);
|
fclose(m_fp);
|
fclose(m_fp);
|
free(m_fname);
|
free(m_fname);
|
if (m_bs)
|
if (m_bs)
|
yy_delete_buffer(m_bs);
|
yy_delete_buffer(m_bs);
|
curbs = m_prev;
|
curbs = m_prev;
|
|
|
if (curbs) {
|
if (curbs) {
|
yy_switch_to_buffer(curbs->m_bs);
|
yy_switch_to_buffer(curbs->m_bs);
|
yylineno = curbs->m_lineno;
|
yylineno = curbs->m_lineno;
|
curfilename = curbs->m_fname;
|
curfilename = curbs->m_fname;
|
}
|
}
|
}
|
}
|
|
|
void mark(void) {
|
void mark(void) {
|
FILE *fp = yyout;
|
FILE *fp = yyout;
|
if (!fp) fp = stdout;
|
if (!fp) fp = stdout;
|
fprintf(fp, "#line %d \"%s\"\n", yylineno, m_fname);
|
fprintf(fp, "#line %d \"%s\"\n", yylineno, m_fname);
|
}
|
}
|
|
|
void markline(void) {
|
void markline(void) {
|
FILE *fp = yyout;
|
FILE *fp = yyout;
|
if (!fp) fp = stdout;
|
if (!fp) fp = stdout;
|
fprintf(fp, "#line %d\n", yylineno);
|
fprintf(fp, "#line %d\n", yylineno);
|
}
|
}
|
|
|
static void pop(void) {
|
static void pop(void) {
|
// fprintf(stderr, "POP! (%s)\n", curbs->m_fname);
|
// fprintf(stderr, "POP! (%s)\n", curbs->m_fname);
|
if (curbs)
|
if (curbs)
|
delete curbs;
|
delete curbs;
|
}
|
}
|
};
|
};
|
|
|
BUFSTACK *BUFSTACK::curbs = NULL;
|
BUFSTACK *BUFSTACK::curbs = NULL;
|
const char *BUFSTACK::curfilename = NULL;
|
const char *BUFSTACK::curfilename = NULL;
|
|
|
int last_marked_line = -1;
|
int last_marked_line = -1;
|
const char *last_marked_file = NULL;
|
const char *last_marked_file = NULL;
|
void mark_line(void) {
|
void mark_line(void) {
|
if ((yylineno != last_marked_line+1)||(BUFSTACK::curfilename != last_marked_file))
|
if ((yylineno != last_marked_line+1)||(BUFSTACK::curfilename != last_marked_file))
|
if (BUFSTACK::curfilename == last_marked_file)
|
if (BUFSTACK::curfilename == last_marked_file)
|
BUFSTACK::curbs->markline();
|
BUFSTACK::curbs->markline();
|
else BUFSTACK::curbs->mark();
|
else BUFSTACK::curbs->mark();
|
last_marked_line = yylineno;
|
last_marked_line = yylineno;
|
last_marked_file = BUFSTACK::curfilename;
|
last_marked_file = BUFSTACK::curfilename;
|
}
|
}
|
|
|
int end_of_file(void) {
|
int end_of_file(void) {
|
BUFSTACK::pop();
|
BUFSTACK::pop();
|
return (BUFSTACK::curbs == NULL);
|
return (BUFSTACK::curbs == NULL);
|
}
|
}
|
|
|
void pushf(const char *fname) {
|
void pushf(const char *fname) {
|
BUFSTACK *bs = new BUFSTACK(fname);
|
BUFSTACK *bs = new BUFSTACK(fname);
|
}
|
}
|
|
|
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
yylineno = 1;
|
yylineno = 1;
|
if (argc < 2) { // Stdin only
|
if (argc < 2) { // Stdin only
|
BUFSTACK::curbs = new BUFSTACK();
|
BUFSTACK::curbs = new BUFSTACK();
|
yylex();
|
yylex();
|
} else {
|
} else {
|
for(int argn=1; argn
|
for(int argn=1; argn
|
BUFSTACK *bs = new BUFSTACK(argv[argn]);
|
BUFSTACK *bs = new BUFSTACK(argv[argn]);
|
mark_line();
|
mark_line();
|
yylex();
|
yylex();
|
// delete bs;
|
// delete bs;
|
}
|
}
|
}
|
}
|
|
|
return 0;
|
return 0;
|
}
|
}
|
// .* { fprintf(stderr, "Ignoring everything, line %d, \'%s\'\n", yylineno, yytext); /* Ignore everything in these states*/ }
|
// .* { fprintf(stderr, "Ignoring everything, line %d, \'%s\'\n", yylineno, yytext); /* Ignore everything in these states*/ }
|
|
|
|
|