reduce memory usage and cleanup code
This commit is contained in:
116
gen/s2/lexer.lpp
116
gen/s2/lexer.lpp
@ -28,7 +28,8 @@ void s2_pop_header(xsk::gsc::context* ctx);
|
||||
RGX_PATH ([_A-Za-z0-9]+\\)+[_A-Za-z0-9]+
|
||||
RGX_IDENTIFIER [_A-Za-z][_A-Za-z0-9]*
|
||||
RGX_STRING \"(?:\\.|[^\"])*?\"|\'(?:\\.|[^\'])*?\'
|
||||
RGX_COLOR #([0-9a-fA-F]{6}|[0-9a-fA-F]{3})
|
||||
RGX_COLOR_S #[0-9a-fA-F]{3}
|
||||
RGX_COLOR_L #[0-9a-fA-F]{6}
|
||||
RGX_FLT_DEC [0-9]+\.(?:[0-9]*)?f?|\.[0-9]+f?
|
||||
RGX_INT_OCT 0[1-7][0-7]*
|
||||
RGX_INT_BIN 0[bB][01]+
|
||||
@ -153,16 +154,17 @@ RGX_DEFAULT (.|\n)
|
||||
\| { return s2::parser::make_BITWISE_OR(ctx->loc); }
|
||||
\& { return s2::parser::make_BITWISE_AND(ctx->loc); }
|
||||
\^ { return s2::parser::make_BITWISE_EXOR(ctx->loc); }
|
||||
{RGX_PATH} { return s2::parser::make_PATH(xsk::utils::string::fordslash(yytext), ctx->loc); }
|
||||
{RGX_IDENTIFIER} { return s2::parser::make_IDENTIFIER((std::string(yytext, 3) == "_ID") ? std::string(yytext) : xsk::utils::string::to_lower(yytext), ctx->loc); }
|
||||
\&{RGX_STRING} { return s2::parser::make_ISTRING(std::string(yytext).substr(1), ctx->loc); }
|
||||
{RGX_PATH} { return s2::parser::make_PATH(xsk::gsc::s2::resolver::make_token(std::string_view(yytext, yyleng)), ctx->loc); }
|
||||
{RGX_IDENTIFIER} { return s2::parser::make_IDENTIFIER(xsk::gsc::s2::resolver::make_token(std::string_view(yytext, yyleng)), ctx->loc); }
|
||||
\&{RGX_STRING} { return s2::parser::make_ISTRING(std::string(++yytext, --yyleng), ctx->loc); }
|
||||
{RGX_STRING} { return s2::parser::make_STRING(std::string(yytext), ctx->loc); }
|
||||
{RGX_COLOR} { return s2::parser::make_COLOR(std::string(yytext).substr(1), ctx->loc); }
|
||||
{RGX_COLOR_S} { return s2::parser::make_COLOR(std::string(++yytext, --yyleng), ctx->loc); }
|
||||
{RGX_COLOR_L} { return s2::parser::make_COLOR(std::string(++yytext, --yyleng), ctx->loc); }
|
||||
{RGX_FLT_DEC} { return s2::parser::make_FLOAT(std::string(yytext), ctx->loc); }
|
||||
{RGX_INT_OCT} { return s2::parser::make_INT_OCT(xsk::utils::string::oct_to_dec(yytext), ctx->loc); }
|
||||
{RGX_INT_BIN} { return s2::parser::make_INT_BIN(xsk::utils::string::bin_to_dec(yytext), ctx->loc); }
|
||||
{RGX_INT_HEX} { return s2::parser::make_INT_HEX(xsk::utils::string::hex_to_dec(yytext), ctx->loc); }
|
||||
{RGX_INT_DEC} { return s2::parser::make_INT_DEC(std::string(yytext), ctx->loc); }
|
||||
{RGX_INT_OCT} { return s2::parser::make_INTEGER(xsk::utils::string::oct_to_dec(yytext), ctx->loc); }
|
||||
{RGX_INT_BIN} { return s2::parser::make_INTEGER(xsk::utils::string::bin_to_dec(yytext), ctx->loc); }
|
||||
{RGX_INT_HEX} { return s2::parser::make_INTEGER(xsk::utils::string::hex_to_dec(yytext), ctx->loc); }
|
||||
{RGX_INT_DEC} { return s2::parser::make_INTEGER(std::string(yytext), ctx->loc); }
|
||||
<<EOF>> { if(ctx->header_top > 0) s2_pop_header(ctx); else return s2::parser::make_S2EOF(ctx->loc); }
|
||||
<*>{RGX_DEFAULT} { throw s2::parser::syntax_error(ctx->loc, "bad token: \'" + std::string(yytext) + "\'"); }
|
||||
|
||||
@ -170,60 +172,60 @@ RGX_DEFAULT (.|\n)
|
||||
|
||||
void s2_push_header(xsk::gsc::context* ctx, const std::string& file)
|
||||
{
|
||||
try
|
||||
try
|
||||
{
|
||||
if (ctx->header_top >= 4)
|
||||
{
|
||||
throw xsk::gsc::error("maximum gsh depth exceeded '4'");
|
||||
}
|
||||
if (ctx->header_top >= 4)
|
||||
{
|
||||
throw xsk::gsc::error("maximum gsh depth exceeded '4'");
|
||||
}
|
||||
|
||||
ctx->header_top++;
|
||||
ctx->header_top++;
|
||||
|
||||
char* buf_data = 0;
|
||||
size_t buf_size = 0;
|
||||
char* buf_data = 0;
|
||||
size_t buf_size = 0;
|
||||
|
||||
for (auto& src : *ctx->sources)
|
||||
{
|
||||
if (src.name == file)
|
||||
{
|
||||
buf_data = reinterpret_cast<char*>(src.buf.data());
|
||||
buf_size = src.buf.size();
|
||||
for (auto& src : *ctx->sources)
|
||||
{
|
||||
if (src.name == file)
|
||||
{
|
||||
buf_data = reinterpret_cast<char*>(src.buf.data());
|
||||
buf_size = src.buf.size();
|
||||
|
||||
ctx->locs.push(ctx->loc);
|
||||
ctx->loc.initialize(&src.name);
|
||||
}
|
||||
}
|
||||
ctx->locs.push(ctx->loc);
|
||||
ctx->loc.initialize(&src.name);
|
||||
}
|
||||
}
|
||||
|
||||
if (buf_data == 0)
|
||||
{
|
||||
ctx->sources->push_back(xsk::gsc::source());
|
||||
auto& source = ctx->sources->back();
|
||||
source.name = file;
|
||||
source.buf = ctx->read_callback(file + ".gsh");
|
||||
source.buf.push_back(0);
|
||||
source.buf.push_back(0);
|
||||
if (buf_data == 0)
|
||||
{
|
||||
ctx->sources->push_back(xsk::gsc::source());
|
||||
auto& source = ctx->sources->back();
|
||||
source.name = file;
|
||||
source.buf = ctx->read_callback(file + ".gsh");
|
||||
source.buf.push_back(0);
|
||||
source.buf.push_back(0);
|
||||
|
||||
buf_data = reinterpret_cast<char*>(source.buf.data());
|
||||
buf_size = source.buf.size();
|
||||
buf_data = reinterpret_cast<char*>(source.buf.data());
|
||||
buf_size = source.buf.size();
|
||||
|
||||
ctx->locs.push(ctx->loc);
|
||||
ctx->loc.initialize(&source.name);
|
||||
}
|
||||
ctx->locs.push(ctx->loc);
|
||||
ctx->loc.initialize(&source.name);
|
||||
}
|
||||
|
||||
auto state = new yy_buffer_state();
|
||||
state->yy_buf_size = buf_size - 2;
|
||||
state->yy_buf_pos = state->yy_ch_buf = buf_data;
|
||||
state->yy_is_our_buffer = 0;
|
||||
state->yy_input_file = NULL;
|
||||
state->yy_n_chars = state->yy_buf_size;
|
||||
state->yy_is_interactive = 0;
|
||||
state->yy_at_bol = 1;
|
||||
state->yy_fill_buffer = 0;
|
||||
state->yy_buffer_status = 0;
|
||||
auto state = new yy_buffer_state();
|
||||
state->yy_buf_size = buf_size - 2;
|
||||
state->yy_buf_pos = state->yy_ch_buf = buf_data;
|
||||
state->yy_is_our_buffer = 0;
|
||||
state->yy_input_file = NULL;
|
||||
state->yy_n_chars = state->yy_buf_size;
|
||||
state->yy_is_interactive = 0;
|
||||
state->yy_at_bol = 1;
|
||||
state->yy_fill_buffer = 0;
|
||||
state->yy_buffer_status = 0;
|
||||
|
||||
yypush_buffer_state(state, ctx->scanner);
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
yypush_buffer_state(state, ctx->scanner);
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
throw xsk::gsc::error("parsing header file '" + file + "': " + e.what());
|
||||
}
|
||||
@ -231,8 +233,8 @@ void s2_push_header(xsk::gsc::context* ctx, const std::string& file)
|
||||
|
||||
void s2_pop_header(xsk::gsc::context* ctx)
|
||||
{
|
||||
ctx->header_top--;
|
||||
ctx->loc = ctx->locs.top();
|
||||
ctx->locs.pop();
|
||||
yypop_buffer_state(ctx->scanner);
|
||||
ctx->header_top--;
|
||||
ctx->loc = ctx->locs.top();
|
||||
ctx->locs.pop();
|
||||
yypop_buffer_state(ctx->scanner);
|
||||
}
|
||||
|
@ -137,10 +137,7 @@ xsk::gsc::s2::parser::symbol_type S2lex(yyscan_t yyscanner, xsk::gsc::context* c
|
||||
%token <std::string> ISTRING "localized string"
|
||||
%token <std::string> COLOR "color"
|
||||
%token <std::string> FLOAT "float"
|
||||
%token <std::string> INT_DEC "int"
|
||||
%token <std::string> INT_OCT "octal int"
|
||||
%token <std::string> INT_BIN "binary int"
|
||||
%token <std::string> INT_HEX "hexadecimal int"
|
||||
%token <std::string> INTEGER "integer"
|
||||
|
||||
%type <ast::program::ptr> program
|
||||
%type <ast::include::ptr> include
|
||||
@ -468,7 +465,7 @@ stmt_default
|
||||
;
|
||||
|
||||
stmt_break
|
||||
: BREAK SEMICOLON
|
||||
: BREAK SEMICOLON
|
||||
{ $$ = std::make_unique<ast::stmt_break>(@$); }
|
||||
;
|
||||
|
||||
@ -821,15 +818,9 @@ expr_float
|
||||
;
|
||||
|
||||
expr_integer
|
||||
: SUB INT_DEC %prec NEG
|
||||
: SUB INTEGER %prec NEG
|
||||
{ $$ = std::make_unique<ast::expr_integer>(@$, "-" + $2); };
|
||||
| INT_DEC
|
||||
{ $$ = std::make_unique<ast::expr_integer>(@$, $1); };
|
||||
| INT_OCT
|
||||
{ $$ = std::make_unique<ast::expr_integer>(@$, $1); };
|
||||
| INT_BIN
|
||||
{ $$ = std::make_unique<ast::expr_integer>(@$, $1); };
|
||||
| INT_HEX
|
||||
| INTEGER
|
||||
{ $$ = std::make_unique<ast::expr_integer>(@$, $1); };
|
||||
;
|
||||
|
||||
|
Reference in New Issue
Block a user