From: John Millaway Date: Thu, 19 Sep 2002 19:46:14 +0000 (+0000) Subject: Added %push and %pop operations to skel processing. X-Git-Tag: flex-2-5-23~68 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4d54469bbb468205ca6e0441701f762d831a5ba8;p=flex Added %push and %pop operations to skel processing. --- diff --git a/flex.skl b/flex.skl index 64d559f..4435b34 100644 --- a/flex.skl +++ b/flex.skl @@ -6,6 +6,9 @@ %# listed and processed in misc.c. %# %# %# - A comment. The current line is ommited from the generated scanner. +%# %push - Push the current state of the skel processing +%# A push is typically followed by one of the commands below. +%# %pop - Pop the state. %# %c++-only - The following lines are printed for C++ scanners ONLY. %# %c-only - The following lines are NOT printed for C++ scanners. %# %c-or-c++ - The following lines are printed in BOTH C and C++ scanners. @@ -15,7 +18,6 @@ %# %not-for-header - Begin code that should NOT appear in a ".h" file. %# %ok-for-header - %c and %e are used for building a header file. %# %tables-serialization-code-begin -%# %tables-serialization-code-end %# %# All control-lines EXCEPT comment lines ("%#") will be inserted into %# the generated scanner as a C-style comment. This is to aid those who @@ -34,9 +36,10 @@ #include #include #include +%push %tables-serialization-code-begin #include -%tables-serialization-code-end +%pop /* end standard C headers. */ %c++-only /* begin standard C++ headers. */ @@ -755,6 +758,7 @@ static int yy_top_state YY_PARAMS(( YY_PROTO_ONLY_ARG )); %c-or-c++ #endif +%push %tables-serialization-code-begin structures and prototypes m4_include(`tables_shared.h') @@ -788,7 +792,8 @@ struct yytbl_reader { uint32_t bread; /**< bytes read since beginning of current tableset */ }; -%tables-serialization-code-end structures and prototypes +/* end tables serialization structures and prototypes */ +%pop %ok-for-header /* Default declaration of generated scanner - a define so the user can @@ -2107,6 +2112,7 @@ void yyfree YYFARGS1( void *,ptr) } #endif +%push %tables-serialization-code-begin definitions m4_include(`tables_shared.c') @@ -2494,7 +2500,8 @@ int yytables_destroy YYFARGS0(void) return 0; } -%tables-serialization-code-end definitions +/* end table serialization code definitions */ +%pop #if YY_MAIN int main YY_PARAMS((void)); diff --git a/main.c b/main.c index f6112a0..a168920 100644 --- a/main.c +++ b/main.c @@ -106,7 +106,7 @@ jmp_buf flex_main_jmp_buf; bool *rule_has_nl, *ccl_has_nl; int nlch = '\n'; -bool tablesext, tablestoggle, tablesverify, gentables; +bool tablesext, tablesverify, gentables; char *tablesfilename=0,*tablesname=0; struct yytbl_writer tableswr; @@ -1044,7 +1044,7 @@ void flexinit (argc, argv) prefix = "yy"; yyclass = 0; use_read = use_stdout = false; - tablesext = tablestoggle = tablesverify = false; + tablesext = tablesverify = false; gentables = true; tablesfilename = tablesname = NULL; diff --git a/misc.c b/misc.c index 20a284c..5364404 100644 --- a/misc.c +++ b/misc.c @@ -43,6 +43,49 @@ #define CMD_C_OR_CPP "%c-or-c++" #define CMD_NOT_FOR_HEADER "%not-for-header" #define CMD_OK_FOR_HEADER "%ok-for-header" +#define CMD_PUSH "%push" +#define CMD_POP "%pop" + +/* we allow the skeleton to push and pop. */ +struct sko_state { + bool dc; /**< do_copy */ + bool tt; /**< tables_toggle */ +}; +static struct sko_state *sko_stack=0; +static int sko_len=0,sko_sz=0; +static void sko_push(bool dc, bool tt) +{ + if(!sko_stack){ + sko_sz = 1; + sko_stack = (struct sko_state*)flex_alloc(sizeof(struct sko_state)*sko_sz); + sko_len = 0; + } + if(sko_len >= sko_sz){ + sko_sz *= 2; + sko_stack = (struct sko_state*)flex_realloc(sko_stack,sizeof(struct sko_state)*sko_sz); + } + + /* initialize to zero and push */ + sko_stack[sko_len].dc = dc; + sko_stack[sko_len].tt = tt; + sko_len++; +} +static void sko_peek(bool *dc, bool* tt) +{ + if(sko_len <= 0) + flex_die("peek attempt when sko stack is empty"); + if(dc) + *dc = sko_stack[sko_len-1].dc; + if(tt) + *tt = sko_stack[sko_len-1].tt; +} +static void sko_pop(bool* dc, bool *tt) +{ + sko_peek(dc,tt); + sko_len--; + if(sko_len < 0) + flex_die("popped too many times in skeleton."); +} /* Append "#define defname value\n" to the running buffer. */ void action_define (defname, value) @@ -812,7 +855,15 @@ void skelout () { char buf_storage[MAXLINE]; char *buf = buf_storage; - int do_copy = 1; + bool do_copy = true; + bool tablestoggle=false; + + /* "reset" the state by clearing the buffer and pushing a '1' */ + if(sko_len > 0) + sko_peek(&do_copy,&tablestoggle); + sko_len = 0; + sko_push(do_copy=true,tablestoggle); + /* Loop pulling lines either from the skelfile, if we're using * one, or from the skel[] array. @@ -845,6 +896,16 @@ void skelout () /* %% is a break point for skelout() */ return; } + else if (cmd_match (CMD_PUSH)){ + sko_push(do_copy,tablestoggle); + out_str("/*(state = (%s,",do_copy?"true":"false"); + out_str( "%s)*/\n",tablestoggle?"true":"false"); + } + else if (cmd_match (CMD_POP)){ + sko_pop(&do_copy,&tablestoggle); + out_str("/*(state = (%s,",do_copy?"true":"false"); + out_str( "%s)*/\n",tablestoggle?"true":"false"); + } else if (cmd_match (CMD_TABLES_SER_BEGIN)) { tablestoggle = true; } @@ -869,7 +930,7 @@ void skelout () } else if (cmd_match (CMD_C_OR_CPP)) { /* %* for C and C++ */ - do_copy = 1; + do_copy = true; } else if (cmd_match (CMD_NOT_FOR_HEADER)) { /* %c begin linkage-only (non-header) code. */ diff --git a/tables.h b/tables.h index 87326ed..79f4194 100644 --- a/tables.h +++ b/tables.h @@ -53,14 +53,13 @@ struct yytbl_writer { /* These are used by main.c, gen.c, etc. * tablesext - if true, create external tables - * tablestoggle - if true, output external tables code while processing skel * tablesfilename - filename for external tables * tablesname - name that goes in serialized data, e.g., "yytables" * tableswr - writer for external tables * tablesverify - true if tables-verify option specified * gentables - true if we should spit out the normal C tables */ -extern bool tablesext, tablestoggle, tablesverify,gentables; +extern bool tablesext, tablesverify,gentables; extern char *tablesfilename, *tablesname; extern struct yytbl_writer tableswr;