bison-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] More %define/%code encapsulation


From: Paolo Bonzini
Subject: Re: [PATCH] More %define/%code encapsulation
Date: Wed, 17 Jan 2007 16:22:32 +0100
User-agent: Thunderbird 1.5.0.9 (Macintosh/20061207)

Paolo Bonzini wrote:
How about b4_percent_define_if_true instead? Given that we have _ifdef, _if seems like it ought to be like m4_if, which is not about booleans.

Yes. OTOH we also have b4_flag_if. Maybe b4_percent_define_flag_if, but these long macro names are getting more and more annoying to me.

Here's an updated patch following the rationale in the message from a few hours ago and including documentation of %define. Pretty please...

Paolo
2006-11-16  Paolo Bonzini  <address@hidden>

        * data/bison.m4 (b4_percent_define_ifdef, b4_percent_define_flag_if,
        b4_percent_define_case, b4_percent_define_ifval, b4_percent_code_ifdef):
        New.
        (b4_percent_code_get): Map unqualified %code to b4_percent_code().
        * data/location.cc: Use b4_percent_define_flag_if.

        * doc/bison.texinfo (Directives): Document %define.

        * src/parse-gram.y (Unqualified %code): Change muscle name to
        b4_percent_code().
        (content.opt): Default to empty.

Index: data/bison.m4
===================================================================
RCS file: /sources/bison/bison/data/bison.m4,v
retrieving revision 1.12
diff -u -r1.12 bison.m4
--- data/bison.m4       16 Jan 2007 06:16:03 -0000      1.12
+++ data/bison.m4       17 Jan 2007 14:34:19 -0000
@@ -349,6 +349,47 @@
 [m4_define([b4_percent_define_skeleton_variables(]$1[)])dnl
 m4_ifdef([b4_percent_define(]$1[)], [m4_indir([b4_percent_define(]$1[)])])])
 
+# b4_percent_define_ifdef(VARIABLE, IF-TRUE, [IF-FALSE])
+# ------------------------------------------------------
+# If the %define variable VARIABLE is defined, expand IF-TRUE, else expand
+# IF-FALSE.  Also, record the skeleton's usage of VARIABLE by defining
+# b4_percent_define_skeleton_variables(VARIABLE).
+m4_define([b4_percent_define_ifdef],
+[m4_ifdef([b4_percent_define(]$1[)],
+         [m4_define([b4_percent_define_skeleton_variables(]$1[)])$2],
+         [$3])])
+
+# b4_percent_define_flag_if(VARIABLE, IF-TRUE, [IF-FALSE])
+# --------------------------------------------------------
+# If the %define variable VARIABLE is defined to anything but "0" or "false",
+# evaluate IF-TRUE. If it is defined to "0" or "false", evaluate IF-FALSE.  If
+# it is undefined, raise an error (this macro should be preceded by
+# b4_percent_define_default).  Also, record the skeleton's usage of VARIABLE by
+# defining b4_percent_define_skeleton_variables(VARIABLE).
+m4_define([b4_percent_define_flag_if],
+[b4_percent_define_ifdef([$1],
+                        [b4_percent_define_case([$1],
+                                                [0], [$3], [false], [$3],
+                                                [$2])],
+                        [m4_fatal([invalid %define variable passed to 
b4_percent_define_flag_if: ]$1)])])
+
+# b4_percent_define_case(VARIABLE, [VALUE1, THEN1]..., [ELSE])
+# ------------------------------------------------------------
+# If the %define variable VARIABLE is defined to VALUE1, evaluate THEN1, and
+# so on.  If none of the values match, expaand ELSE.  The default value is
+# empty.  Also, record the skeleton's usage of VARIABLE by defining
+# b4_percent_define_skeleton_variables(VARIABLE).
+m4_define([b4_percent_define_case],
+[m4_case(m4_quote(b4_percent_define_get([$1])), m4_shift($@))])
+
+# b4_percent_define_ifval(VARIABLE, IF-NOT-EMPTY, [IF-EMPTY])
+# -----------------------------------------------------------
+# If the %define variable VARIABLE is defined and not empty, expand
+# IF-NOT-EMPTY, else expand IF-EMPTY.  Also, record the skeleton's usage of
+# VARIABLE by defining b4_percent_define_skeleton_variables(VARIABLE).
+m4_define([b4_percent_define_ifval],
+[m4_ifval(m4_quote(b4_percent_define_get([$1])), [$2], [$3])])
+
 # b4_percent_define_default(VARIABLE, DEFAULT)
 # --------------------------------------------
 # If the %define variable VARIABLE is undefined, set its value to DEFAULT.
@@ -364,9 +405,9 @@
 # --------------------------------
 # If any %code blocks for QUALIFIER are defined, emit them beginning with a
 # comment and ending with synclines and a newline.  If QUALIFIER is not
-# specified (thus, b4_percent_code_get is invoked without parens), do this for
-# the unqualified %code blocks.  Also, record the skeleton's usage of QUALIFIER
-# (if specified) by defining b4_percent_code_skeleton_qualifiers(QUALIFIER).
+# specified or empty, do this for the unqualified %code blocks.  Also, record
+# the skeleton's usage of QUALIFIER (if specified) by defining
+# b4_percent_code_skeleton_qualifiers(QUALIFIER).
 #
 # For example, to emit any unqualified %code blocks followed by any %code
 # blocks for the qualifier foo:
@@ -374,9 +415,8 @@
 #   b4_percent_code_get
 #   b4_percent_code_get([[foo]])
 m4_define([b4_percent_code_get],
-[m4_pushdef([b4_macro_name], [[b4_percent_code]]m4_if([$#], [1], [[[(]$1[)]]],
-                                                      [[[_unqualified]]]))dnl
-m4_if([$#], [1], [m4_define([b4_percent_code_skeleton_qualifiers(]$1[)])])dnl
+[m4_pushdef([b4_macro_name], [[b4_percent_code(]$1[)]])dnl
+m4_ifval([$1], [m4_define([b4_percent_code_skeleton_qualifiers(]$1[)])])dnl
 m4_ifdef(b4_macro_name,
 [b4_comment([m4_if([$#], [0], [[Unqualified %code]],
                    [[%code "]$1["]])[ blocks.]])
@@ -384,6 +424,17 @@
 ])dnl
 m4_popdef([b4_macro_name])])
 
+# b4_percent_code_ifdef(QUALIFIER, IF-TRUE, [IF-FALSE])
+# -----------------------------------------------------
+# If any %code blocks for QUALIFIER (or unqualified %code blocks if
+# QUALIFIER is empty) are defined, expand IF-TRUE, else expand IF-FALSE.
+# Also, record the skeleton's usage of QUALIFIER (if specified) by defining
+# b4_percent_code_skeleton_qualifiers(QUALIFIER).
+m4_define([b4_percent_code_ifdef],
+[m4_ifdef([b4_percent_code(]$1[)],
+          [m4_ifval([$1], 
[m4_define([b4_percent_code_skeleton_qualifiers(]$1[)])])$2],
+         [$3])])
+
 
 ## --------------------------------------------------------- ##
 ## After processing the skeletons, check that all the user's ##
Index: data/location.cc
===================================================================
RCS file: /sources/bison/bison/data/location.cc,v
retrieving revision 1.16
diff -u -r1.16 location.cc
--- data/location.cc    16 Jan 2007 06:16:03 -0000      1.16
+++ data/location.cc    17 Jan 2007 14:34:20 -0000
@@ -113,7 +113,7 @@
   {
     return begin + -width;
   }
-]m4_if(b4_percent_define_get([[define_location_comparison]]), [1], [[
+]b4_percent_define_flag_if([[define_location_comparison]], [[
   /// Compare two position objects.
   inline bool
   operator== (const position& pos1, const position& pos2)
@@ -235,7 +235,7 @@
     res.columns (width);
     return res;
   }
-]m4_if(b4_percent_define_get([[define_location_comparison]]), [1], [[
+]b4_percent_define_flag_if([[define_location_comparison]], [[
   /// Compare two location objects.
   inline bool
   operator== (const location& loc1, const location& loc2)
Index: src/parse-gram.y
===================================================================
RCS file: /sources/bison/bison/src/parse-gram.y,v
retrieving revision 1.114
diff -u -r1.114 parse-gram.y
--- src/parse-gram.y    16 Jan 2007 06:16:04 -0000      1.114
+++ src/parse-gram.y    17 Jan 2007 14:34:21 -0000
@@ -324,7 +324,7 @@
     }
 | "%code" braceless
     {
-      muscle_code_grow ("percent_code_unqualified", $2, @2);
+      muscle_code_grow ("percent_code()", $2, @2);
       code_scanner_last_string_free ();
     }
 | "%code" ID braceless
Index: parse-gram.y
===================================================================
RCS file: /sources/bison/bison/src/parse-gram.y,v
retrieving revision 1.114
diff -u -r1.114 parse-gram.y
--- parse-gram.y        16 Jan 2007 06:16:04 -0000      1.114
+++ parse-gram.y        17 Jan 2007 15:18:39 -0000
@@ -324,7 +324,7 @@
     }
 | "%code" braceless
     {
-      muscle_code_grow ("percent_code_unqualified", $2, @2);
+      muscle_code_grow ("percent_code()", $2, @2);
       code_scanner_last_string_free ();
     }
 | "%code" ID braceless
@@ -528,12 +552,11 @@
   | STRING { $$ = uniqstr_new ($1); } /* deprecated and not M4-friendly */
   ;
 
-/* Some content or "1" by default. */
+/* Some content or empty by default. */
 content.opt:
   /* Nothing. */
     {
-      static char one[] = "1";
-      $$ = one;
+      $$ = "";
     }
 | STRING
 ;
Index: doc/bison.texinfo
===================================================================
RCS file: /sources/bison/bison/doc/bison.texinfo,v
retrieving revision 1.219
diff -u -r1.219 bison.texinfo
--- doc/bison.texinfo   9 Jan 2007 01:17:50 -0000       1.219
+++ doc/bison.texinfo   17 Jan 2007 14:34:21 -0000
@@ -4615,6 +4625,15 @@
 @xref{Table of Symbols, ,%code}.
 @end deffn
 
address@hidden {Directive} %define @var{define-variable}
address@hidden {Directive} %define @var{define-variable} @var{value}
+Define a variable to be used by the skeleton in order to adjust its
+behavior.  The @var{value} can be omitted for boolean variables; for
+boolean variables, the skeletons will treat a @var{value} of @samp{0}
+or @samp{false} as the boolean variable being false, and anything else
+as true.
address@hidden deffn
+
 @deffn {Directive} %defines @var{defines-file}
 Same as above, but save in the file @var{defines-file}.
 @end deffn

reply via email to

[Prev in Thread] Current Thread [Next in Thread]