bison-patches
[Top][All Lists]
Advanced

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

[PATCH] Extract language-independent stuff out of c.m4


From: Paolo Bonzini
Subject: [PATCH] Extract language-independent stuff out of c.m4
Date: Tue, 03 Oct 2006 11:59:12 +0200
User-agent: Thunderbird 1.5.0.7 (Macintosh/20060909)

A cleanup which is necessary for the Java front-end, but worthwhile on its own.

Most of the stuff in c.m4 is absolutely language independent. This patch extracts it in a separate bison.m4 file. In the meanwhile, I moved one tiny language dependent detail out of the C source code into M4: the default prefix for Java will be YY, not yy. :-)

Posted for review while my copyright assignment reaches the States, regtested successfully.

Paolo
2006-10-03  Paolo Bonzini

        * data/c.m4 (b4_copyright, b4_epilogue, b4_location_initial_column,
        b4_location_initial_line, p4_parse_param, b4_ints_in, b4_flag_if,
        b4_define_flag_if and uses, b4_basename, b4_syncline, b4_user_code,
        b4_define_user_cde and uses): Remove.
        (b4_comment, b4_prefix, b4_sync_start): New.
        * data/bison.m4: New file, with most of the content removed from c.m4.
        * src/muscle_tab.h: Use "do {...} while(0)" throughout.
        * src/output.c (output_skeleton): Pass bison.m4.
        (prepare): Pass glr_flag and nondeterministic_flag.  Pass prefix
        only if specified.

Index: data/c.m4
===================================================================
RCS file: /sources/bison/bison/data/c.m4,v
retrieving revision 1.66
diff -u -p -r1.66 c.m4
--- data/c.m4   15 Sep 2006 15:56:26 -0000      1.66
+++ data/c.m4   3 Oct 2006 09:41:16 -0000
@@ -18,48 +18,15 @@ m4_divert(-1)                           
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 # 02110-1301  USA
 
-
 ## ---------------- ##
 ## Identification.  ##
 ## ---------------- ##
 
-# b4_copyright(TITLE, YEARS)
-# --------------------------
-m4_define([b4_copyright],
-[/* A Bison parser, made by GNU Bison b4_version.  */
-
-/* $1
-
-m4_text_wrap([Copyright (C) $2 Free Software Foundation, Inc.], [   ])
-
-   This program is free software; you can redistribute it and/or modify
-   it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2, or (at your option)
-   any later version.
-
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU General Public License for more details.
-
-   You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor,
-   Boston, MA 02110-1301, USA.  */
-
-/* As a special exception, you may create a larger work that contains
-   part or all of the Bison parser skeleton and distribute that work
-   under terms of your choice, so long as that work isn't itself a
-   parser generator using the skeleton or a modified version thereof
-   as a parser skeleton.  Alternatively, if you modify or redistribute
-   the parser skeleton itself, you may (at your option) remove this
-   special exception, which will cause the skeleton and the resulting
-   Bison output files to be licensed under the GNU General Public
-   License without this special exception.
-
-   This special exception was added by the Free Software Foundation in
-   version 2.2 of Bison.  */])
-
+# b4_comment(TEXT)
+# ----------------
+m4_define([b4_comment], [/* m4_bpatsubst([$1], [
+], [
+   ])  */])
 
 # b4_identification
 # -----------------
@@ -88,15 +55,11 @@ m4_define([b4_identification],
 ## Default values.  ##
 ## ---------------- ##
 
-m4_define_default([b4_epilogue], [])
-
 # If the %union is not named, its name is YYSTYPE.
 m4_define_default([b4_union_name], [YYSTYPE])
 
-# The initial column and line.
-m4_define_default([b4_location_initial_column], [1])
-m4_define_default([b4_location_initial_line],   [1])
-
+# If the %name-prefix is not given, it is yy.
+m4_define_default([b4_prefix], [yy])
 
 ## ------------------------ ##
 ## Pure/impure interfaces.  ##
@@ -112,7 +75,6 @@ m4_define([b4_user_args],
 # --------------
 # If defined, b4_parse_param arrives double quoted, but below we prefer
 # it to be single quoted.
-m4_define_default([b4_parse_param])
 m4_define([b4_parse_param],
 b4_parse_param))
 
@@ -143,13 +105,6 @@ m4_define([b4_parse_param_use],
 ## Data Types.  ##
 ## ------------ ##
 
-# b4_ints_in(INT1, INT2, LOW, HIGH)
-# ---------------------------------
-# Return 1 iff both INT1 and INT2 are in [LOW, HIGH], 0 otherwise.
-m4_define([b4_ints_in],
-[m4_eval([$3 <= $1 && $1 <= $4 && $3 <= $2 && $2 <= $4])])
-
-
 # b4_int_type(MIN, MAX)
 # ---------------------
 # Return the smallest int type able to handle numbers ranging from
@@ -173,7 +128,6 @@ m4_define([b4_int_type],
 m4_define([b4_int_type_for],
 [b4_int_type($1_min, $1_max)])
 
-
 ## ---------##
 ## Values.  ##
 ## ---------##
@@ -185,59 +139,6 @@ m4_define([b4_int_type_for],
 m4_define([b4_null], [0])
 
 
-## ------------------ ##
-## Decoding options.  ##
-## ------------------ ##
-
-# b4_flag_if(FLAG, IF-TRUE, IF-FALSE)
-# -----------------------------------
-# Run IF-TRUE if b4_FLAG_flag is 1, IF-FALSE if FLAG is 0, otherwise fail.
-m4_define([b4_flag_if],
-[m4_case(b4_$1_flag,
-         [0], [$3],
-        [1], [$2],
-        [m4_fatal([invalid $1 value: ]$1)])])
-
-
-# b4_define_flag_if(FLAG)
-# -----------------------
-# Define "b4_FLAG_if(IF-TRUE, IF-FALSE)" that depends on the
-# value of the Boolean FLAG.
-m4_define([b4_define_flag_if],
-[_b4_define_flag_if($[1], $[2], [$1])])
-
-# _b4_define_flag_if($1, $2, FLAG)
-# --------------------------------
-# This macro works around the impossibility to define macros
-# inside macros, because issuing `[$1]' is not possible in M4 :(.
-# This sucks hard, GNU M4 should really provide M5 like $$1.
-m4_define([_b4_define_flag_if],
-[m4_if([$1$2], $[1]$[2], [],
-       [m4_fatal([$0: Invalid arguments: address@hidden)])dnl
-m4_define([b4_$3_if],
-          [b4_flag_if([$3], [$1], [$2])])])
-
-
-# b4_FLAG_if(IF-TRUE, IF-FALSE)
-# -----------------------------
-# Expand IF-TRUE, if FLAG is true, IF-FALSE otherwise.
-b4_define_flag_if([defines])        # Whether headers are requested.
-b4_define_flag_if([error_verbose])  # Whether error are verbose.
-b4_define_flag_if([locations])      # Whether locations are tracked.
-b4_define_flag_if([pure])           # Whether the interface is pure.
-b4_define_flag_if([yacc])           # Whether POSIX Yacc is emulated.
-
-
-# b4_push_if(IF-TRUE, IF-FALSE)
-# -----------------------------
-# Expand IF-TRUE, if %push-parser, IF-FALSE otherwise.
-m4_define([b4_push_if],
-[m4_if(b4_push_flag, [1],
-       [$1],
-       [$2])])
-
-
-
 ## ------------------------- ##
 ## Assigning token numbers.  ##
 ## ------------------------- ##
@@ -429,50 +330,9 @@ m4_define([b4_c_arg],
 ## Synclines.  ##
 ## ----------- ##
 
-# b4_basename(NAME)
-# -----------------
-# Similar to POSIX basename; the differences don't matter here.
-# Beware that NAME is not evaluated.
-m4_define([b4_basename],
-[m4_bpatsubst([$1], [^.*/\([^/]+\)/*$], [\1])])
-
-
-# b4_syncline(LINE, FILE)
+# b4_sync_start(LINE, FILE)
 # -----------------------
-m4_define([b4_syncline],
-[b4_flag_if([synclines],
-[/* Line __line__ of b4_basename(m4_quote(__file__)).  */
-[#]line $1 $2])])
-
-
-# b4_user_code(USER-CODE)
-# -----------------------
-# Emit code from the user, ending it with synclines.
-m4_define([b4_user_code],
-[$1
-b4_syncline(address@hidden@], address@hidden@])])
-
-
-# b4_define_user_code(MACRO)
-# --------------------------
-# From b4_MACRO, build b4_user_MACRO that includes the synclines.
-m4_define([b4_define_user_code],
-[m4_define([b4_user_$1],
-[b4_user_code([b4_$1])])])
-
-
-# b4_user_actions
-# b4_user_initial_action
-# b4_user_post_prologue
-# b4_user_start_header
-# b4_user_stype
-# ----------------------
-# Macros that issue user code, ending with synclines.
-b4_define_user_code([actions])
-b4_define_user_code([initial_action])
-b4_define_user_code([post_prologue])
-b4_define_user_code([start_header])
-b4_define_user_code([stype])
+m4_define([b4_sync_start], [[#]line $1 $2])
 
 
 ## -------------- ##
Index: src/muscle_tab.h
===================================================================
RCS file: /sources/bison/bison/src/muscle_tab.h,v
retrieving revision 1.16
diff -u -p -r1.16 muscle_tab.h
--- src/muscle_tab.h    13 Jul 2006 20:05:34 -0000      1.16
+++ src/muscle_tab.h    3 Oct 2006 09:41:26 -0000
@@ -33,34 +33,34 @@ void muscle_free (void);
 extern struct obstack muscle_obstack;
 
 #define MUSCLE_INSERT_BOOL(Key, Value)                         \
-{                                                              \
+do {                                                           \
   int v = Value;                                               \
   MUSCLE_INSERT_INT (Key, v);                                  \
-}
+} while(0)
 
 #define MUSCLE_INSERT_INT(Key, Value)                          \
-{                                                              \
+do {                                                           \
   obstack_fgrow1 (&muscle_obstack, "%d", Value);               \
   obstack_1grow (&muscle_obstack, 0);                          \
   muscle_insert (Key, obstack_finish (&muscle_obstack));       \
-}
+} while(0)
 
 #define MUSCLE_INSERT_LONG_INT(Key, Value)                     \
-{                                                              \
+do {                                                           \
   obstack_fgrow1 (&muscle_obstack, "%ld", Value);              \
   obstack_1grow (&muscle_obstack, 0);                          \
   muscle_insert (Key, obstack_finish (&muscle_obstack));       \
-}
+} while(0)
 
 #define MUSCLE_INSERT_STRING(Key, Value)                       \
-{                                                              \
+do {                                                           \
   obstack_sgrow (&muscle_obstack, Value);                      \
   obstack_1grow (&muscle_obstack, 0);                          \
   muscle_insert (Key, obstack_finish (&muscle_obstack));       \
-}
+} while(0)
 
 #define MUSCLE_OBSTACK_SGROW(Obstack, Value)                   \
-{                                                              \
+do {                                                           \
   char const *p;                                               \
   for (p = Value; *p; p++)                                     \
     switch (*p)                                                        \
@@ -71,16 +71,16 @@ extern struct obstack muscle_obstack;
       case ']':        obstack_sgrow (Obstack, "@}" ); break;          \
       default: obstack_1grow (Obstack, *p); break;             \
       }                                                                \
-}
+} while(0)
 
 #define MUSCLE_INSERT_C_STRING(Key, Value)                     \
-{                                                              \
+do {                                                           \
   MUSCLE_OBSTACK_SGROW (&muscle_obstack,                       \
                        quotearg_style (c_quoting_style,        \
                                        Value));                \
   obstack_1grow (&muscle_obstack, 0);                          \
   muscle_insert (Key, obstack_finish (&muscle_obstack));       \
-}
+} while(0)
 
 /* Append VALUE to the current value of KEY.  If KEY did not already
    exist, create it.  Use MUSCLE_OBSTACK.  De-allocate the previously
Index: src/output.c
===================================================================
RCS file: /sources/bison/bison/src/output.c,v
retrieving revision 1.256
diff -u -p -r1.256 output.c
--- src/output.c        15 Sep 2006 18:59:40 -0000      1.256
+++ src/output.c        3 Oct 2006 09:41:26 -0000
@@ -499,7 +499,7 @@ output_skeleton (void)
   FILE *in;
   FILE *out;
   int filter_fd[2];
-  char const *argv[6];
+  char const *argv[7];
   pid_t pid;
 
   /* Compute the names of the package data dir and skeleton file.
@@ -507,7 +507,9 @@ output_skeleton (void)
      installation.  A faulty installation can cause deadlock, so a
      cheap sanity check is worthwhile.  */
   char const m4sugar[] = "m4sugar/m4sugar.m4";
+  char const m4bison[] = "bison.m4";
   char *full_m4sugar;
+  char *full_m4bison;
   char *full_skeleton;
   char const *p;
   char const *m4 = (p = getenv ("M4")) ? p : M4;
@@ -523,24 +525,28 @@ output_skeleton (void)
   full_skeleton[pkgdatadirlen] = '/';
   strcpy (full_skeleton + pkgdatadirlen + 1, m4sugar);
   full_m4sugar = xstrdup (full_skeleton);
+  strcpy (full_skeleton + pkgdatadirlen + 1, m4bison);
+  full_m4bison = xstrdup (full_skeleton);
   strcpy (full_skeleton + pkgdatadirlen + 1, skeleton);
   xfclose (xfopen (full_m4sugar, "r"));
 
   /* Create an m4 subprocess connected to us via two pipes.  */
 
   if (trace_flag & trace_tools)
-    fprintf (stderr, "running: %s %s - %s\n",
-            m4, full_m4sugar, full_skeleton);
+    fprintf (stderr, "running: %s %s - %s %s\n",
+            m4, full_m4sugar, full_m4bison, full_skeleton);
 
   argv[0] = m4;
   argv[1] = full_m4sugar;
   argv[2] = "-";
-  argv[3] = full_skeleton;
-  argv[4] = trace_flag & trace_m4 ? "-dV" : NULL;
-  argv[5] = NULL;
+  argv[3] = full_m4bison;
+  argv[4] = full_skeleton;
+  argv[5] = trace_flag & trace_m4 ? "-dV" : NULL;
+  argv[6] = NULL;
 
   init_subpipe ();
   pid = create_subpipe (argv, filter_fd);
+  free (full_m4bison);
   free (full_m4sugar);
   free (full_skeleton);
 
@@ -584,7 +590,9 @@ prepare (void)
   MUSCLE_INSERT_BOOL ("debug_flag", debug_flag);
   MUSCLE_INSERT_BOOL ("defines_flag", defines_flag);
   MUSCLE_INSERT_BOOL ("error_verbose_flag", error_verbose);
+  MUSCLE_INSERT_BOOL ("glr_flag", glr_parser);
   MUSCLE_INSERT_BOOL ("locations_flag", locations_flag);
+  MUSCLE_INSERT_BOOL ("nondeterministic_flag", nondeterministic_parser);
   MUSCLE_INSERT_BOOL ("pure_flag", pure_parser);
   MUSCLE_INSERT_BOOL ("push_flag", push_parser);
   MUSCLE_INSERT_BOOL ("synclines_flag", !no_lines_flag);
@@ -592,7 +600,9 @@ prepare (void)
   MUSCLE_INSERT_BOOL ("yacc_flag", yacc_flag);
 
   /* File names.  */
-  MUSCLE_INSERT_STRING ("prefix", spec_name_prefix ? spec_name_prefix : "yy");
+  if (spec_name_prefix)
+    MUSCLE_INSERT_STRING ("prefix", spec_name_prefix);
+
 #define DEFINE(Name) MUSCLE_INSERT_STRING (#Name, Name ? Name : "")
   DEFINE (dir_prefix);
   DEFINE (parser_file_name);



m4_divert(-1)                                               -*- Autoconf -*-

# Language-independent M4 Macros for Bison.
# Copyright (C) 2002, 2004, 2005, 2006 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301  USA


## ---------------- ##
## Identification.  ##
## ---------------- ##

# b4_copyright(TITLE, YEARS)
# --------------------------
m4_define([b4_copyright],
[b4_comment([A Bison parser, made by GNU Bison b4_version.])

b4_comment([$1

m4_text_wrap([Copyright (C) $2 Free Software Foundation, Inc.], [   ])

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.])

b4_comment([As a special exception, you may create a larger work that contains
part or all of the Bison parser skeleton and distribute that work
under terms of your choice, so long as that work isn't itself a
parser generator using the skeleton or a modified version thereof
as a parser skeleton.  Alternatively, if you modify or redistribute
the parser skeleton itself, you may (at your option) remove this
special exception, which will cause the skeleton and the resulting
Bison output files to be licensed under the GNU General Public
License without this special exception.

This special exception was added by the Free Software Foundation in
version 2.2 of Bison.])])


## ---------------- ##
## Default values.  ##
## ---------------- ##

# m4_define_default([b4_lex_param], [])   dnl breaks other skeletons
m4_define_default([b4_epilogue], [])
m4_define_default([b4_parse_param], [])

# The initial column and line.
m4_define_default([b4_location_initial_column], [1])
m4_define_default([b4_location_initial_line],   [1])


## ------------ ##
## Data Types.  ##
## ------------ ##

# b4_ints_in(INT1, INT2, LOW, HIGH)
# ---------------------------------
# Return 1 iff both INT1 and INT2 are in [LOW, HIGH], 0 otherwise.
m4_define([b4_ints_in],
[m4_eval([$3 <= $1 && $1 <= $4 && $3 <= $2 && $2 <= $4])])



## ------------------ ##
## Decoding options.  ##
## ------------------ ##

# b4_flag_if(FLAG, IF-TRUE, IF-FALSE)
# -----------------------------------
# Run IF-TRUE if b4_FLAG_flag is 1, IF-FALSE if FLAG is 0, otherwise fail.
m4_define([b4_flag_if],
[m4_case(b4_$1_flag,
         [0], [$3],
         [1], [$2],
         [m4_fatal([invalid $1 value: ]$1)])])


# b4_define_flag_if(FLAG)
# -----------------------
# Define "b4_FLAG_if(IF-TRUE, IF-FALSE)" that depends on the
# value of the Boolean FLAG.
m4_define([b4_define_flag_if],
[_b4_define_flag_if($[1], $[2], [$1])])

# _b4_define_flag_if($1, $2, FLAG)
# --------------------------------
# This macro works around the impossibility to define macros
# inside macros, because issuing `[$1]' is not possible in M4 :(.
# This sucks hard, GNU M4 should really provide M5 like $$1.
m4_define([_b4_define_flag_if],
[m4_if([$1$2], $[1]$[2], [],
       [m4_fatal([$0: Invalid arguments: address@hidden)])dnl
m4_define([b4_$3_if],
          [b4_flag_if([$3], [$1], [$2])])])


# b4_FLAG_if(IF-TRUE, IF-FALSE)
# -----------------------------
# Expand IF-TRUE, if FLAG is true, IF-FALSE otherwise.
b4_define_flag_if([defines])            # Whether headers are requested.
b4_define_flag_if([error_verbose])      # Whether error are verbose.
b4_define_flag_if([glr])                # Whether a GLR parser is requested.
b4_define_flag_if([locations])          # Whether locations are tracked.
b4_define_flag_if([nondeterministic])   # Whether conflicts should be handled.
b4_define_flag_if([pure])               # Whether the interface is pure.
b4_define_flag_if([push])               # Whether push parsing is supported.
b4_define_flag_if([yacc])               # Whether POSIX Yacc is emulated.


## ------------------------- ##
## Assigning token numbers.  ##
## ------------------------- ##


## ----------- ##
## Synclines.  ##
## ----------- ##

# b4_basename(NAME)
# -----------------
# Similar to POSIX basename; the differences don't matter here.
# Beware that NAME is not evaluated.
m4_define([b4_basename],
[m4_bpatsubst([$1], [^.*/\([^/]+\)/*$], [\1])])


# b4_syncline(LINE, FILE)
# -----------------------
m4_define([b4_syncline],
[b4_flag_if([synclines], [
b4_sync_end([__line__], [b4_basename(m4_quote(__file__))])
b4_sync_start([$1], [$2])])])

m4_define([b4_sync_end], [b4_comment([Line $1 of $2])])
m4_define([b4_sync_start], [b4_comment([Line $1 of $2])])

# b4_user_code(USER-CODE)
# -----------------------
# Emit code from the user, ending it with synclines.
m4_define([b4_user_code],
[$1
b4_syncline(address@hidden@], address@hidden@])])


# b4_define_user_code(MACRO)
# --------------------------
# From b4_MACRO, build b4_user_MACRO that includes the synclines.
m4_define([b4_define_user_code],
[m4_define([b4_user_$1],
[b4_user_code([b4_$1])])])


# b4_user_actions
# b4_user_initial_action
# b4_user_post_prologue
# b4_user_start_header
# b4_user_stype
# ----------------------
# Macros that issue user code, ending with synclines.
b4_define_user_code([actions])
b4_define_user_code([initial_action])
b4_define_user_code([post_prologue])
b4_define_user_code([start_header])
b4_define_user_code([stype])



reply via email to

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