]>
git.saurik.com Git - bison.git/blob - src/muscle_tab.c
   1 /* Muscle table manager for Bison, 
   2    Copyright (C) 2001, 2002 Free Software Foundation, Inc. 
   4    This file is part of Bison, the GNU Compiler Compiler. 
   6    Bison is free software; you can redistribute it and/or modify 
   7    it under the terms of the GNU General Public License as published by 
   8    the Free Software Foundation; either version 2, or (at your option) 
  11    Bison is distributed in the hope that it will be useful, 
  12    but WITHOUT ANY WARRANTY; without even the implied warranty of 
  13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  14    GNU General Public License for more details. 
  16    You should have received a copy of the GNU General Public License 
  17    along with Bison; see the file COPYING.  If not, write to 
  18    the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 
  19    Boston, MA 02111-1307, USA.  */ 
  24 #include "muscle_tab.h" 
  28 /* An obstack used to create some entries.  */ 
  29 struct obstack muscle_obstack
; 
  31 /* Initial capacity of muscles hash table.  */ 
  32 #define HT_INITIAL_CAPACITY 257 
  34 struct hash_table 
*muscle_table 
= NULL
; 
  37 hash_compare_muscles (void const *x
, void const *y
) 
  39   const muscle_entry_t 
*m1 
= x
; 
  40   const muscle_entry_t 
*m2 
= y
; 
  41   return strcmp (m1
->key
, m2
->key
) == 0; 
  45 hash_muscle (const void *x
, unsigned int tablesize
) 
  47   const muscle_entry_t 
*m 
= x
; 
  48   return hash_string (m
->key
, tablesize
); 
  51 /*-----------------------------------------------------------------. 
  52 | Create the MUSCLE_TABLE, and initialize it with default values.  | 
  53 | Also set up the MUSCLE_OBSTACK.                                  | 
  54 `-----------------------------------------------------------------*/ 
  59   /* Initialize the muscle obstack.  */ 
  60   obstack_init (&muscle_obstack
); 
  62   muscle_table 
= hash_initialize (HT_INITIAL_CAPACITY
, NULL
, hash_muscle
, 
  63                                   hash_compare_muscles
, free
); 
  65   /* Version and input file.  */ 
  66   MUSCLE_INSERT_STRING ("version", VERSION
); 
  67   MUSCLE_INSERT_STRING ("filename", infile
); 
  69   /* FIXME: there should probably be no default here, only in the 
  72   /* Default #line formatting.  */ 
  73   MUSCLE_INSERT_STRING ("linef", "#line %d %s\n"); 
  77 /*------------------------------------------------------------. 
  78 | Free all the memory consumed by the muscle machinery only.  | 
  79 `------------------------------------------------------------*/ 
  84   hash_free (muscle_table
); 
  85   obstack_free (&muscle_obstack
, NULL
); 
  90 /*------------------------------------------------------------. 
  91 | Insert (KEY, VALUE).  If KEY already existed, overwrite the | 
  93 `------------------------------------------------------------*/ 
  96 muscle_insert (const char *key
, char *value
) 
  99   muscle_entry_t 
*entry 
= NULL
; 
 102   entry 
= hash_lookup (muscle_table
, &probe
); 
 106       /* First insertion in the hash. */ 
 107       entry 
= XMALLOC (muscle_entry_t
, 1); 
 109       hash_insert (muscle_table
, entry
); 
 111   entry
->value 
= value
; 
 115 /*-------------------------------------------------------------------. 
 116 | Insert (KEY, VALUE).  If KEY already existed, overwrite the        | 
 117 | previous value.  Uses MUSCLE_OBSTACK.  De-allocates the previously | 
 118 | associated value.  VALUE and SEPARATOR are copied.                 | 
 119 `-------------------------------------------------------------------*/ 
 122 muscle_grow (const char *key
, const char *val
, const char *separator
) 
 124   muscle_entry_t probe
; 
 125   muscle_entry_t 
*entry 
= NULL
; 
 128   entry 
= hash_lookup (muscle_table
, &probe
); 
 132       /* First insertion in the hash. */ 
 133       entry 
= XMALLOC (muscle_entry_t
, 1); 
 135       hash_insert (muscle_table
, entry
); 
 136       entry
->value 
= xstrdup (val
); 
 140       /* Grow the current value. */ 
 142       obstack_sgrow (&muscle_obstack
, entry
->value
); 
 144       obstack_sgrow (&muscle_obstack
, separator
); 
 145       obstack_sgrow (&muscle_obstack
, val
); 
 146       obstack_1grow (&muscle_obstack
, 0); 
 147       new_val 
= obstack_finish (&muscle_obstack
); 
 148       entry
->value 
= xstrdup (new_val
); 
 149       obstack_free (&muscle_obstack
, new_val
); 
 154 /*-------------------------------------------------------------------. 
 155 | MUSCLE is an M4 list of pairs.  Create or extend it with the pair  | 
 156 | (A1, A2).  Note that because the muscle values are output *double* | 
 157 | quoted, one needs to strip the first level of quotes to reach the  | 
 159 `-------------------------------------------------------------------*/ 
 161 void muscle_pair_list_grow (const char *muscle
, 
 162                             const char *a1
, const char *a2
) 
 165   obstack_fgrow2 (&muscle_obstack
, "[[[%s]], [[%s]]]", a1
, a2
); 
 166   obstack_1grow (&muscle_obstack
, 0); 
 167   pair 
= obstack_finish (&muscle_obstack
); 
 168   muscle_grow (muscle
, pair
, ",\n"); 
 169   obstack_free (&muscle_obstack
, pair
); 
 172 /*-------------------------------. 
 173 | Find the value of muscle KEY.  | 
 174 `-------------------------------*/ 
 177 muscle_find (const char *key
) 
 179   muscle_entry_t probe
; 
 180   muscle_entry_t 
*result 
= NULL
; 
 183   result 
= hash_lookup (muscle_table
, &probe
); 
 184   return result 
? result
->value 
: NULL
; 
 188 /*------------------------------------------------. 
 189 | Output the definition of ENTRY as a m4_define.  | 
 190 `------------------------------------------------*/ 
 193 muscle_m4_output (muscle_entry_t 
*entry
, FILE *out
) 
 195   fprintf (out
, "m4_define([b4_%s],\n", entry
->key
); 
 196   fprintf (out
, "[[%s]])\n\n\n", entry
->value
); 
 201 /*----------------------------------------------------------------. 
 202 | Output the definition of all the current muscles into a list of | 
 204 `----------------------------------------------------------------*/ 
 207 muscles_m4_output (FILE *out
) 
 209   hash_do_for_each (muscle_table
, 
 210                     (Hash_processor
) muscle_m4_output
,