]> granicus.if.org Git - flex/commitdiff
More m4 macro conversions.
authorJohn Millaway <john43@users.sourceforge.net>
Wed, 12 Mar 2003 09:24:45 +0000 (09:24 +0000)
committerJohn Millaway <john43@users.sourceforge.net>
Wed, 12 Mar 2003 09:24:45 +0000 (09:24 +0000)
Added debugging option --preproc-level=NUM.

buf.c
filter.c
flex.skl
flexdef.h
main.c
misc.c
options.c
options.h
scan.l
tests/test-reject/scanner.l

diff --git a/buf.c b/buf.c
index 85d206d296f35abd30bdaf515533623b9a98b1ab..427d711fedb2305abba26a2afce804f8494a4fac 100644 (file)
--- a/buf.c
+++ b/buf.c
@@ -117,9 +117,9 @@ struct Buf *buf_m4_define (struct Buf *buf, const char* def, const char* val)
 
     val = val?val:"";
     str = (char*)flex_alloc(strlen(fmt) + strlen(def) + strlen(val) + 2);
-    
+
     sprintf(str, fmt, def, val);
-    buf_append(buf, str, 1);
+    buf_append(buf, &str, 1);
 }
 
 /* create buf with 0 elements, each of size elem_size. */
index 7238ec929551a6f8bd5be6eba4f43cb670c1b2cd..787b4988ec6a4bf16a20cb03e3b4cce9ec3453bb 100644 (file)
--- a/filter.c
+++ b/filter.c
@@ -136,4 +136,25 @@ bool filter_apply_chain (struct filter * chain)
        return true;
 }
 
+/** Truncate the chain to max_len number of filters.
+ * @param chain the current chain.
+ * @param max_len the maximum length of the chain.
+ * @return the resulting length of the chain.
+ */
+int filter_truncate (struct filter *chain, int max_len)
+{
+       int     len = 1;
+
+       if (!chain)
+               return 0;
+
+       while (chain->next && len < max_len) {
+               chain = chain->next;
+               ++len;
+       }
+
+       chain->next = NULL;
+       return len;
+}
+
 /* vim:set expandtab cindent tabstop=4 softtabstop=4 shiftwidth=4 textwidth=0: */
index 2757fe29e3cb8b77c38f94a2dc8274a5ae5075ad..b9744a0d058f3b32ae844aad1e4e7da26b9254ef 100644 (file)
--- a/flex.skl
+++ b/flex.skl
@@ -21,6 +21,7 @@ m4preproc_changecom
 
 %# Macros for runtime processing stage.
 m4_changecom
+m4_changequote
 m4_changequote([[, ]])
 
 %# 
@@ -158,12 +159,14 @@ m4_define( [[YY_PROTO_ONLY_ARG]],  [[yyscan_t yyscanner]])
 
 %# For use in function definitions to append the additional argument.
 m4_ifdef( [[M4_YY_TRADITIONAL_FUNC_DEFS]],
+[[
     m4_define( [[YY_DEF_LAST_ARG]], [[, yyscanner]])
     m4_define( [[YY_DEF_ONLY_ARG]], [[yyscanner]])
-,
+]],
+[[
     m4_define( [[YY_DEF_LAST_ARG]],  [[, yyscan_t yyscanner]])
     m4_define( [[YY_DEF_ONLY_ARG]],  [[yyscan_t yyscanner]])
-)
+]])
 m4_define( [[YY_DECL_LAST_ARG]],  [[yyscan_t yyscanner;]])
 
 %# For use in function calls to pass the additional argument.
@@ -192,10 +195,12 @@ m4_define( [[YY_PROTO_ONLY_ARG]],  [[void]])
 m4_define( [[YY_DEF_LAST_ARG]])
 
 m4_ifdef( [[M4_YY_TRADITIONAL_FUNC_DEFS]],
+[[
     m4_define( [[YY_DEF_ONLY_ARG]])
-,
+]],
+[[
     m4_define( [[YY_DEF_ONLY_ARG]],  [[void]])
-)
+]])
 m4_define([[YY_DECL_LAST_ARG]])
 m4_define([[YY_CALL_LAST_ARG]])
 m4_define([[YY_CALL_ONLY_ARG]])
@@ -218,18 +223,20 @@ m4_define([[YY_CALL_ONLY_ARG]])
  *        yyscan_t yyscanner;
  */
 m4_ifdef( [[YY_TRADITIONAL_FUNC_DEFS]],
+[[
 %# Generate traditional function defs
     #define YYFARGS0(v) (YY_DEF_ONLY_ARG) YY_DECL_LAST_ARG
     #define YYFARGS1(t1,n1) (n1 YY_DEF_LAST_ARG) t1 n1; YY_DECL_LAST_ARG
     #define YYFARGS2(t1,n1,t2,n2) (n1,n2 YY_DEF_LAST_ARG) t1 n1; t2 n2; YY_DECL_LAST_ARG
     #define YYFARGS3(t1,n1,t2,n2,t3,n3) (n1,n2,n3 YY_DEF_LAST_ARG) t1 n1; t2 n2; t3 n3; YY_DECL_LAST_ARG
-,
+]],
+[[
 %# Generate C99 function defs.
     m4_define( [[YYFARGS0]], [[(YY_DEF_ONLY_ARG)]])
     m4_define( [[YYFARGS1]], [[($1 $2 YY_DEF_LAST_ARG)]])
     m4_define( [[YYFARGS2]], [[($1 $2, $3 $4 YY_DEF_LAST_ARG)]])
     m4_define( [[YYFARGS3]], [[($1 $2, $3 $4, $5 $6 YY_DEF_LAST_ARG)]])
-)
+]])
 
 /* Enter a start condition.  This macro really ought to take a parameter,
  * but we do it the disgusting crufty way forced on us by the ()-less
@@ -759,7 +766,9 @@ static int input YY_PARAMS(( YY_PROTO_ONLY_ARG ));
 
 
 %if-c-only
-#if YY_STACK_USED
+%# TODO: This is messy.
+m4_ifdef( [[M4_YY_STACK_USED]],
+[[
 %if-not-reentrant
 %not-for-header
 static int yy_start_stack_ptr = 0;
@@ -781,11 +790,12 @@ m4_ifdef( [[M4_YY_NO_TOP_STATE]],,
 static int yy_top_state YY_PARAMS(( YY_PROTO_ONLY_ARG ));
 ]])
 
-#else
-#define YY_NO_PUSH_STATE 1
-#define YY_NO_POP_STATE 1
-#define YY_NO_TOP_STATE 1
-#endif
+]],
+[[
+m4_define( [[M4_YY_NO_PUSH_STATE]])
+m4_define( [[M4_YY_NO_POP_STATE]])
+m4_define( [[M4_YY_NO_TOP_STATE]])
+]])
 %endif
 
 /* Amount of stuff to slurp up with each read. */
@@ -1187,7 +1197,8 @@ yyFlexLexer::yyFlexLexer( std::istream* arg_yyin, std::ostream* arg_yyout )
 m4_ifdef( [[M4_YY_USES_REJECT]],
 [[
        yy_state_buf = new yy_state_type[YY_BUF_SIZE + 2];
-,
+]],
+[[
        yy_state_buf = 0;
 ]])
        }
@@ -1317,8 +1328,8 @@ m4_ifdef( [[M4_YY_USES_REJECT]],
 [[
                        YY_FATAL_ERROR(
 "input buffer overflow, can't enlarge buffer because scanner uses REJECT" );
-,
-
+]],
+[[
                        /* just a shorter name for the current buffer */
                        YY_BUFFER_STATE b = YY_CURRENT_BUFFER;
 
@@ -2307,11 +2318,15 @@ int yylex_destroy  YYFARGS0(void)
        yyfree(YY_G(yy_buffer_stack) YY_CALL_LAST_ARG);
        YY_G(yy_buffer_stack) = NULL;
 
-#if defined(YY_STACK_USED) || defined(YY_REENTRANT)
+%# This is the m4 way to say "if (stack_used || is_reentrant){ destroy_stack }"
+m4_ifdef( [[M4_YY_STACK_USED]], [[m4_define([[M4_YY_DESTROY_START_STACK]])]])
+m4_ifdef( [[M4_YY_REENTRANT]],  [[m4_define([[M4_YY_DESTROY_START_STACK]])]])
+m4_ifdef( [[M4_YY_DESTROY_START_STACK]],
+[[
     /* Destroy the start condition stack. */
         yyfree( YY_G(yy_start_stack) YY_CALL_LAST_ARG );
         YY_G(yy_start_stack) = NULL;
-#endif
+]])
 
 m4_ifdef( [[M4_YY_USES_REJECT]],
 [[
index 2a047aa50431126ab8fafcd9c12bec432c4f2a17..d283c4a15fa742ac35c92283f39f0a78c12ba8cf 100644 (file)
--- a/flexdef.h
+++ b/flexdef.h
@@ -917,6 +917,7 @@ PROTO ((const char *, const char *, const char *, const char *));
 extern void out_str_dec PROTO ((const char *, const char *, int));
 extern void outc PROTO ((int));
 extern void outn PROTO ((const char *));
+extern void out_m4_define (const char* def, const char* val);
 
 /* Return a printable version of the given character, which might be
  * 8-bit.
@@ -1154,12 +1155,13 @@ extern struct filter * output_chain;
  *            not including argv[0].
  * @return newest filter in chain
  */
-struct filter *filter_create PROTO((struct filter * chain, const char *cmd, ...));
+extern struct filter *filter_create PROTO((struct filter * chain, const char *cmd, ...));
 
 /* Fork and exec entire filter chain.
  *  @param chain The head of the chain.
  * @return true on success.
  */
-bool filter_apply_chain PROTO((struct filter * chain));
+extern bool filter_apply_chain PROTO((struct filter * chain));
+extern int filter_truncate (struct filter * chain, int max_len);
 
 #endif /* not defined FLEXDEF_H */
diff --git a/main.c b/main.c
index 7cf006ad5bd2b837bd60140142fd6f7fc2c92793..3b8a19355b6631521b37195e10fbeaf792ed28e1 100644 (file)
--- a/main.c
+++ b/main.c
@@ -136,6 +136,9 @@ static char outfile_path[MAXLINE];
 static int outfile_created = 0;
 static char *skelname = NULL;
 
+/* For debugging. The max number of filters to apply to skeleton. */
+static int preproc_level = 1000;
+
 int flex_main PROTO ((int argc, char *argv[]));
 int main PROTO ((int argc, char *argv[]));
 void fix_line_dirs PROTO ((char *, char *, char *, int));
@@ -339,9 +342,14 @@ void check_options ()
     /* Setup the filter chain. */
     output_chain = filter_create(NULL,"m4","-P",0);
     /* filter_create(output_chain,"cat",0); */
-    filter_apply_chain(output_chain);
+
+    /* For debugging, only run the requested number of filters. */
+    if (preproc_level > 0) {
+        filter_truncate(output_chain, preproc_level);
+        filter_apply_chain(output_chain);
+    }
     yyout = stdout;
-    
+
 
        /* always generate the tablesverify flag. */
        buf_m4_define (&m4defs_buf, "M4_YY_TABLES_VERIFY", tablesverify ? "1" : "0");
@@ -483,6 +491,7 @@ void check_options ()
 
     /* Dump the m4 definitions. */
     buf_print_strings(&m4defs_buf, stdout);
+    m4defs_buf.nelts = 0; /* memory leak here. */
     
        /* Dump the user defined preproc directives. */
        if (userdef_buf.elts)
@@ -718,7 +727,6 @@ void flexend (exit_status)
                 "YY_RULE_SETUP",
                 "YY_SC_TO_UI",
                 "YY_SKIP_YYWRAP",
-                "YY_STACK_USED",
                 "YY_START",
                 "YY_START_STACK_INCR",
                 "YY_STATE_EOF",
@@ -1086,7 +1094,13 @@ void flexinit (argc, argv)
        buf_init (&userdef_buf, sizeof (char)); /* one long string */
        buf_init (&defs_buf, sizeof (char *));  /* list of strings */
        buf_init (&yydmap_buf, sizeof (char));  /* one long string */
-       buf_init (&m4defs_buf, sizeof (char *)); /* list of strings */
+
+    {
+        const char * m4defs_init_str[] = {"m4_changequote\n",
+                                          "m4_changequote([[, ]])\n"};
+        buf_init (&m4defs_buf, sizeof (char *));
+        buf_append (&m4defs_buf, &m4defs_init_str, 2);
+    }
 
 
        /* Enable C++ if program name ends with '+'. */
@@ -1212,6 +1226,10 @@ void flexinit (argc, argv)
                        posix_compat = true;
                        break;
 
+        case OPT_PREPROC_LEVEL:
+            preproc_level = strtol(arg,NULL,0);
+            break;
+
                case OPT_MAIN:
                        buf_strdefine (&userdef_buf, "YY_MAIN", "1");
                        do_yywrap = false;
@@ -1384,7 +1402,8 @@ void flexinit (argc, argv)
                        break;
 
                case OPT_STACK:
-                       buf_strdefine (&userdef_buf, "YY_STACK_USED", "1");
+                       //buf_strdefine (&userdef_buf, "YY_STACK_USED", "1");
+            buf_m4_define( &m4defs_buf, "M4_YY_STACK_USED",0);
                        break;
 
                case OPT_STDINIT:
@@ -1664,7 +1683,7 @@ void readin ()
        }
 
        if (reject){
-        buf_m4_define( &m4defs_buf, "M4_YY_USES_REJECT", NULL);
+        out_m4_define( "M4_YY_USES_REJECT", NULL);
                //outn ("\n#define YY_USES_REJECT");
     }
 
diff --git a/misc.c b/misc.c
index 0801bf8d976f769929ce06cf0c4fcf1bf652ee60..e3ad77702b8cee5cbc6956e58b503df6e0531094 100644 (file)
--- a/misc.c
+++ b/misc.c
@@ -801,6 +801,17 @@ void outn (str)
        ++out_linenum;
 }
 
+/** Print "m4_define( [[def]], [[val]])m4_dnl\n".
+ * @param def The m4 symbol to define.
+ * @param val The definition; may be NULL.
+ * @return buf
+ */
+void out_m4_define (const char* def, const char* val)
+{
+    const char * fmt = "m4_define( [[%s]], [[%s]])m4_dnl\n";
+    fprintf(stdout, fmt, def, val?val:"");
+}
+
 
 /* readable_form - return the the human-readable form of a character
  *
index 99cacf92e13c0016d72d763e07cda16178f86b60..2579747bc5064d31728fc5499cbb20486d5c09fc 100644 (file)
--- a/options.c
+++ b/options.c
@@ -127,6 +127,8 @@ optspec_t flexopts[] = {
        ,
        {"--posix-compat", OPT_POSIX_COMPAT, 0}
        ,                       /* Maximal compatibility with POSIX lex. */
+        {"--preproc=NUM", OPT_PREPROC_LEVEL, 0}
+        ,
        {"-L", OPT_NO_LINE, 0}
        ,                       /* Suppress #line directives in scanner. */
        {"--noline", OPT_NO_LINE, 0}
index 3cdbfe35aa3c82be85c1bd148169affee20fdb0b..e45c132acb9bd02b5b0586045aa6d52884921766 100644 (file)
--- a/options.h
+++ b/options.h
@@ -105,6 +105,7 @@ enum flexopt_flag_t {
        OPT_POINTER,
        OPT_PREFIX,
        OPT_PREPROCDEFINE,
+       OPT_PREPROC_LEVEL,
        OPT_READ,
        OPT_REENTRANT,
        OPT_REJECT,
diff --git a/scan.l b/scan.l
index 88b3240ce7e94377b9ee0853a7d5db07d3cc77e1..a0b770f86d25b1df3d749e96b8b127771d8b8b5c 100644 (file)
--- a/scan.l
+++ b/scan.l
@@ -301,7 +301,7 @@ LEXOPT              [aceknopr]
        read            use_read = option_sense;
     reentrant   reentrant = option_sense;
        reject          reject_really_used = option_sense;
-       stack           action_define( "YY_STACK_USED", option_sense );
+       stack           ACTION_M4_IFDEF( "M4_YY_STACK_USED", option_sense );
        stdinit         do_stdinit = option_sense;
        stdout          use_stdout = option_sense;
     unistd      ACTION_IFDEF("YY_NO_UNISTD_H", ! option_sense);
index dc439f163e25d964d2bbcaf0e0bf56ebd5934cd8..84fb1057246f5881dea263886c82952f0b334c62 100644 (file)
@@ -53,7 +53,7 @@ int main ( int argc, char** argv )
 
     if(yytables_fload(fp YY_CALL_LAST_ARG) < 0)
         yy_fatal_error("yytables_fload returned < 0" YY_CALL_LAST_ARG);
-    if(YY_TABLES_VERIFY)
+    if(M4_YY_TABLES_VERIFY)
         exit(0);
 #endif