@ -2,7 +2,6 @@
%option warn
%option warn
%option yylineno
%option yylineno
%option noyywrap
%option noyywrap
%option yylineno
%option header-file="lexical.h"
%option header-file="lexical.h"
%option outfile="lexical.cpp"
%option outfile="lexical.cpp"
%option prefix="conf"
%option prefix="conf"
@ -10,66 +9,87 @@
%option bison-bridge
%option bison-bridge
%option bison-locations
%option bison-locations
%option nounput
%option nounput
%option stack
%x PSTRING
%x PSTRING
%x PARSE
%x PARSE
%x INCLUDE
%{
%{
#if __cplusplus > 199711L
#if __cplusplus > 199711L
#define register // Deprecated in C++11.
#define register // Deprecated in C++11.
#endif // #if __cplusplus > 199711L
#endif // #if __cplusplus > 199711L
#include <stdlib.h>
#include <stdlib.h>
#include <string>
#include "../debug.h"
#include "../debug.h"
#include "../object.h"
#include "../object.h"
#include "parser.h"
#include "parser.h"
#include "grammatical.h"
#include "grammatical.h"
// Get rid of warning on unneed ed function
// Get rid of warning on unus ed function
#define YY_NO_INPUT
#define YY_NO_INPUT
static std::string str;
// definitions for bison-bridge
#define YYSTYPE CONFSTYPE
#define YYLTYPE CONFLTYPE
std::string str;
#define yyterminate(ret) { while(YY_CURRENT_BUFFER) yypop_buffer_state(yyscanner); str.erase(); yyextra->retcode=-(ret); return (ret); }
#define yyerrormessage(message,ret) {COUT(ERROR)<<std::endl<<" Lexical error in file "<<yyextra->state.curdir<<yyextra->state.filename<<" at line "<<yyextra->state.curline<<": "<<(message)<<std::endl; yyterminate(ret);}
#define yynextline {yyextra->state.curline++; yyextra->state.curpos=0; yyextra->state.curoffset++;}
#define yynextsym {yyextra->state.curpos+=yyleng; yyextra->state.curoffset+=yyleng;}
%}
%}
%%
%%
<PARSE,INITIAL,PSTRING>\n yyextra->curline++; yyextra->curpos=0; yyextra->curoffset++; REJECT;
<PARSE,INITIAL>@include[ \t]*\" yy_push_state(INCLUDE,yyscanner); str.erase(); yynextsym;
<PARSE,INITIAL,PSTRING>. yyextra->curpos++; yyextra->curoffset++; REJECT;
<INCLUDE>\" {
include\(\".+\"\); {/*
if(yyextra->state.inclevel>=yyextra->maxinclevel) yyerrormessage("maximal include level reached",-1);
if(yyextra->inclevel>=yyextra->maxinclevel) { COUT(ERROR)<<"Max include level reached in file "<<yyextra->filename<<" at line "<<yylineno<<std::endl; return 1; }
FILE* fd;
yyscan_t scanner;
struct lexical_extra extra;
// yyextra->curdir is directory with currently scanned file
std::string fname(yytext+9,yyleng-12);
// !!!NONPORTABLE!!!
FILE* fd;
if('/'==str[0]) fd=fopen(str.c_str(),"r"); // absolute path
fd=fopen(fname.c_str(),"r");
else
if(fd==0) { COUT(ERROR)<<"Can't open file "<<fname<<std::endl; return -1; }
{
COUT(DEBUG)<<"Include "<<fname<<std::endl;
// first, try to search file in working directory
extra.filename=fname.c_str();
fd=fopen(str.c_str(),"r");
extra.inclevel=yyextra->inclevel+1;
// if fail, try to search in directory with currently scanned file
extra.maxinclevel=yyextra->maxinclevel;
if(fd==0) fd=fopen((yyextra->state.curdir+str).c_str(),"r");
yylex_init_extra(&extra,&scanner);
}
yyset_in(fd,scanner);
yylex(scanner);
if(fd==0) yyerrormessage("can't open file "+str,-1);
yylex_destroy(scanner);
yynextsym;
fclose(fd);
yyextra->fds.push(fd);
yyextra->states.push(yyextra->state);
COUT(DEBUG)<<std::endl<<"Include "<<str<<std::endl;
yyextra->state.inclevel++;
yyextra->ParsePath(str);
yyextra->state.curline=1;
yyextra->state.curpos=yyextra->state.curoffset=0;
*/}
yypush_buffer_state(yy_create_buffer(fd,YY_BUF_SIZE,yyscanner),yyscanner);
[a-zA-Z][a-zA-Z0-9_]* COUT(MOREDEBUG)<<"NAME("<<yytext<<")"; BEGIN(PARSE); yylval_param->str=new std::string(yytext); return NAME;
// Get rid of warning on unused function
<PARSE>[+\-*/^] COUT(MOREDEBUG)<<" OPERATION("<<yytext<<")"; return yytext[0];
if(__builtin_expect(yy_top_state(yyscanner)==PSTRING,0)) yyerrormessage("misterious error",-1);
<PARSE>[0-9]+ COUT(MOREDEBUG)<<" INTEGER("<<yytext<<")"; yylval_param->i=atoll(yytext); return INTEGER;
yy_pop_state(yyscanner);
<PARSE>[0-9]+(\.[0-9]*)?([eE][+-][0-9]+)? COUT(MOREDEBUG)<<" REAL("<<yytext<<")"; yylval_param->r=atof(yytext); return REAL;
}
<PARSE>[TF] COUT(MOREDEBUG)<<" BOOL("<<yytext<<")"; yylval_param->b=(yytext[0]=='T')?true:false; return BOOL;
[a-zA-Z][a-zA-Z0-9_]* COUT(MOREDEBUG)<<"NAME("<<yytext<<")"; BEGIN(PARSE); yylval_param->str=new std::string(yytext); yynextsym; return NAME;
<PARSE>\( COUT(MOREDEBUG)<<" OBRACE()"; return OBRACE;
<PARSE>[+\-*/^] COUT(MOREDEBUG)<<" OPERATION("<<yytext<<")"; return yytext[0]; yynextsym;
<PARSE>\) COUT(MOREDEBUG)<<" CBRACE()"; return CBRACE;
<PARSE>[0-9]+ COUT(MOREDEBUG)<<" INTEGER("<<yytext<<")"; yylval_param->i=atoll(yytext); yynextsym; return INTEGER;
<PARSE>\; COUT(MOREDEBUG)<<" ENDL()"<<std::endl; BEGIN(0); return ENDL;
<PARSE>[0-9]+(\.[0-9]*)?([eE][+-][0-9]+)? COUT(MOREDEBUG)<<" REAL("<<yytext<<")"; yylval_param->r=atof(yytext); yynextsym; return REAL;
<PARSE>= COUT(MOREDEBUG)<<" ASSIGN()"; return ASSIGN;
<PARSE>[TF] COUT(MOREDEBUG)<<" BOOL("<<yytext<<")"; yylval_param->b=(yytext[0]=='T')?true:false; yynextsym; return BOOL;
<PARSE>, COUT(MOREDEBUG)<<" DELIM()"; return DELIM;
<PARSE>\( COUT(MOREDEBUG)<<" OBRACE()"; yynextsym; return OBRACE;
<PARSE>[a-zA-Z][a-zA-Z0-9_]* COUT(MOREDEBUG)<<" IDENTIFIER("<<yytext<<")"; yylval_param->str=new std::string(yytext); return IDENTIFIER;
<PARSE>\) COUT(MOREDEBUG)<<" CBRACE()"; yynextsym; return CBRACE;
<PARSE>\. COUT(MOREDEBUG)<<" DOT()"; return yytext[0];
<PARSE>\; COUT(MOREDEBUG)<<" ENDL()"<<std::endl; BEGIN(0); yynextsym; return ENDL;
<PARSE,INITIAL>[ \n\t]
<PARSE>= COUT(MOREDEBUG)<<" ASSIGN()"; yynextsym; return ASSIGN;
<PARSE,INITIAL>\#.*
<PARSE>, COUT(MOREDEBUG)<<" DELIM()"; yynextsym; return DELIM;
<PARSE>\" BEGIN(PSTRING); str.erase();
<PARSE>[a-zA-Z][a-zA-Z0-9_]* COUT(MOREDEBUG)<<" IDENTIFIER("<<yytext<<")"; yylval_param->str=new std::string(yytext); yynextsym; return IDENTIFIER;
<PARSE,INITIAL>. COUT(ERROR)<<"Unknown symbol "<<yytext<<" in file "<<yyextra->filename<<" at line "<<yylineno<<std::endl; yyterminate(); return -1;
<PARSE>\. COUT(MOREDEBUG)<<" DOT()"; yynextsym; return yytext[0];
<PSTRING>\\\\ str+='\\';
<PARSE,INITIAL>[ \t]+ yynextsym;
<PSTRING>\\\" str+='\"';
<PARSE,INITIAL>\n yynextline;
<PSTRING>\" BEGIN(PARSE); COUT(MOREDEBUG)<<" STRING("<<str<<")"; yylval_param->str=&str; return STRING;
<PARSE,INITIAL>\#.* yynextsym;
<PSTRING>. str+=yytext[0];
<PARSE>\" BEGIN(PSTRING); str.erase(); yynextsym;
<PSTRING><<EOF>> COUT(ERROR)<<"Unclosed quote!"<<std::endl; str.erase(); yyterminate(); return -1;
<PARSE,INITIAL>. yyerrormessage(std::string("unknown symbol ")+yytext+" at position "+std::to_string(yyextra->state.curpos),-1);
<<EOF>> str.erase(); yyterminate(); return 0;
<PSTRING,INCLUDE>\\\\ str+='\\'; yynextsym;
<PSTRING,INCLUDE>\\\" str+='\"'; yynextsym;
<PSTRING>\n str+=yytext[0]; yynextline;
<PSTRING>\" BEGIN(PARSE); COUT(MOREDEBUG)<<" STRING("<<str<<")"; yylval_param->str=new std::string(str); yynextsym; return STRING;
<PSTRING,INCLUDE>. str+=yytext[0]; yynextsym;
<PSTRING,INCLUDE><<EOF>> yyerrormessage("unclosed quote",-1);
<<EOF>> yypop_buffer_state(yyscanner); if(YY_CURRENT_BUFFER) {yyextra->state=yyextra->states.top(); yyextra->states.pop(); fclose(yyextra->fds.top()); yyextra->fds.pop();} else yyterminate(0);
%%
%%