From: Joel E. Denny Date: Sun, 20 May 2007 05:01:53 +0000 (+0000) Subject: Extend the front-end API for %define variables to more completely X-Git-Tag: v2.3b~127 X-Git-Url: https://git.saurik.com/bison.git/commitdiff_plain/f124d4233fcdb7c5e5cb966cbe24f060e737190f Extend the front-end API for %define variables to more completely mirror the back-end. This will be useful in the future. * data/bison.m4 (b4_percent_define_get, b4_percent_define_ifdef): Update comments to mention the new front-end counterparts of these macros. * src/muscle_tab.c (MUSCLE_COMMON_DECODE): New macro with common code for muscle_string_decode and muscle_location_decode. (muscle_string_decode): New static function. (muscle_location_decode): Use MUSCLE_COMMON_DECODE. (muscle_percent_define_get, muscle_percent_define_ifdef): New functions. (muscle_percent_define_flag_if): Use muscle_percent_define_ifdef and muscle_percent_define_get to mimic the b4_percent_define_flag_if implementation more closely. (muscle_percent_define_invalid_value): New function. * src/muscle_tab.h (muscle_percent_define_get, muscle_percent_define_ifdef, muscle_percent_define_invalid_value): Prototype. --- diff --git a/ChangeLog b/ChangeLog index dc8d7835..e151d0bc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,24 @@ +2007-05-20 Joel E. Denny + + Extend the front-end API for %define variables to more completely + mirror the back-end. This will be useful in the future. + * data/bison.m4 (b4_percent_define_get, b4_percent_define_ifdef): + Update comments to mention the new front-end counterparts of these + macros. + * src/muscle_tab.c (MUSCLE_COMMON_DECODE): New macro with common code + for muscle_string_decode and muscle_location_decode. + (muscle_string_decode): New static function. + (muscle_location_decode): Use MUSCLE_COMMON_DECODE. + (muscle_percent_define_get, muscle_percent_define_ifdef): New + functions. + (muscle_percent_define_flag_if): Use muscle_percent_define_ifdef and + muscle_percent_define_get to mimic the b4_percent_define_flag_if + implementation more closely. + (muscle_percent_define_invalid_value): New function. + * src/muscle_tab.h (muscle_percent_define_get, + muscle_percent_define_ifdef, muscle_percent_define_invalid_value): + Prototype. + 2007-05-07 Joel E. Denny * NEWS (2.3a+): Mention yesterday's state-removal change. diff --git a/data/bison.m4 b/data/bison.m4 index 0aa1efdc..644b66d7 100644 --- a/data/bison.m4 +++ b/data/bison.m4 @@ -338,7 +338,8 @@ m4_popdef([b4_end])dnl # b4_percent_define_get(VARIABLE) # ------------------------------- -# If the %define variable VARIABLE is defined, emit its value. Also, record +# Mimic muscle_percent_define_get in ../src/muscle_tab.h exactly. That is, if +# the %define variable VARIABLE is defined, emit its value. Also, record # Bison's usage of VARIABLE by defining # b4_percent_define_bison_variables(VARIABLE). # @@ -351,7 +352,8 @@ 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 +# Mimic muscle_percent_define_ifdef in ../src/muscle_tab.h exactly. That is, +# if the %define variable VARIABLE is defined, expand IF-TRUE, else expand # IF-FALSE. Also, record Bison's usage of VARIABLE by defining # b4_percent_define_bison_variables(VARIABLE). # diff --git a/src/muscle_tab.c b/src/muscle_tab.c index f096dc0b..9f671673 100644 --- a/src/muscle_tab.c +++ b/src/muscle_tab.c @@ -275,6 +275,52 @@ muscle_location_grow (char const *key, location loc) muscle_grow (key, "]]", ""); } +#define MUSCLE_COMMON_DECODE(Value) \ + case '$': \ + aver (*++(Value) == ']'); \ + aver (*++(Value) == '['); \ + obstack_sgrow (&muscle_obstack, "$"); \ + break; \ + case '@': \ + switch (*++(Value)) \ + { \ + case '@': obstack_sgrow (&muscle_obstack, "@" ); break; \ + case '{': obstack_sgrow (&muscle_obstack, "[" ); break; \ + case '}': obstack_sgrow (&muscle_obstack, "]" ); break; \ + default: aver (false); break; \ + } \ + break; \ + default: \ + obstack_1grow (&muscle_obstack, *(Value)); \ + break; + +/* Reverse of MUSCLE_OBSTACK_SGROW. */ +static char * +muscle_string_decode (char const *key) +{ + char const *value; + char *value_decoded; + char *result; + + value = muscle_find_const (key); + if (!value) + return NULL; + do { + switch (*value) + { + MUSCLE_COMMON_DECODE (value) + case '[': + case ']': + aver (false); + break; + } + } while (*value++); + value_decoded = obstack_finish (&muscle_obstack); + result = xstrdup (value_decoded); + obstack_free (&muscle_obstack, value_decoded); + return result; +} + /* Reverse of muscle_location_grow. */ static location muscle_location_decode (char const *key) @@ -287,20 +333,7 @@ muscle_location_decode (char const *key) while (*++value) switch (*value) { - case '$': - aver (*++value == ']'); - aver (*++value == '['); - obstack_sgrow (&muscle_obstack, "$"); - break; - case '@': - switch (*++value) - { - case '@': obstack_sgrow (&muscle_obstack, "@" ); break; - case '{': obstack_sgrow (&muscle_obstack, "[" ); break; - case '}': obstack_sgrow (&muscle_obstack, "]" ); break; - default: aver (false); break; - } - break; + MUSCLE_COMMON_DECODE (value) case '[': aver (false); break; @@ -330,9 +363,6 @@ muscle_location_decode (char const *key) } } break; - default: - obstack_1grow (&muscle_obstack, *value); - break; } aver (false); return loc; @@ -385,13 +415,51 @@ muscle_percent_define_insert (char const *variable, location variable_loc, variable_loc); } +char * +muscle_percent_define_get (char const *variable) +{ + char const *name; + char const *usage_name; + char *value; + + MUSCLE_USER_NAME_CONVERT (name, "percent_define(", variable, ")"); + MUSCLE_USER_NAME_CONVERT (usage_name, "percent_define_bison_variables(", + variable, ")"); + + muscle_insert (usage_name, ""); + value = muscle_string_decode (name); + if (!value) + value = xstrdup (""); + return value; +} + +bool +muscle_percent_define_ifdef (char const *variable) +{ + char const *name; + char const *usage_name; + char const *value; + + MUSCLE_USER_NAME_CONVERT (name, "percent_define(", variable, ")"); + MUSCLE_USER_NAME_CONVERT (usage_name, "percent_define_bison_variables(", + variable, ")"); + + value = muscle_find_const (name); + if (value) + { + muscle_insert (usage_name, ""); + return true; + } + + return false; +} + bool muscle_percent_define_flag_if (char const *variable) { char const *name; char const *loc_name; char const *usage_name; - char const *value; bool result = false; MUSCLE_USER_NAME_CONVERT (name, "percent_define(", variable, ")"); @@ -399,9 +467,9 @@ muscle_percent_define_flag_if (char const *variable) MUSCLE_USER_NAME_CONVERT (usage_name, "percent_define_bison_variables(", variable, ")"); - value = muscle_find_const (name); - if (value) + if (muscle_percent_define_ifdef (variable)) { + char *value = muscle_percent_define_get (variable); if (value[0] == '\0' || 0 == strcmp (value, "true")) result = true; else if (0 == strcmp (value, "false")) @@ -410,13 +478,12 @@ muscle_percent_define_flag_if (char const *variable) complain_at(muscle_location_decode (loc_name), _("invalid value for %%define boolean variable `%s'"), variable); + free (value); } else fatal(_("undefined %%define variable `%s' passed to muscle_percent_define_flag_if"), variable); - muscle_insert (usage_name, ""); - return result; } @@ -439,6 +506,16 @@ muscle_percent_define_default (char const *variable, char const *value) } } +void +muscle_percent_define_invalid_value (char const *variable) +{ + char const *loc_name; + MUSCLE_USER_NAME_CONVERT (loc_name, "percent_define_loc(", variable, ")"); + complain_at(muscle_location_decode (loc_name), + _("invalid value for %%define variable `%s'"), variable); +} + + void muscle_percent_code_grow (char const *qualifier, location qualifier_loc, char const *code, location code_loc) diff --git a/src/muscle_tab.h b/src/muscle_tab.h index aafaaae5..96efaa3b 100644 --- a/src/muscle_tab.h +++ b/src/muscle_tab.h @@ -131,6 +131,19 @@ void muscle_user_name_list_grow (char const *key, char const *user_name, void muscle_percent_define_insert (char const *variable, location variable_loc, char const *value); +/* Mimic b4_percent_define_get in ../data/bison.m4 exactly. That is, if the + %define variable VARIABLE is defined, return its value. Otherwise, return + the empty string. Also, record Bison's usage of VARIABLE by defining + b4_percent_define_bison_variables(VARIABLE). The caller is responsible for + freeing the memory of the returned string. */ +char *muscle_percent_define_get (char const *variable); + +/* Mimic b4_percent_define_ifdef in ../data/bison.m4 exactly. That is, if the + %define variable VARIABLE is defined, return true. Otherwise, return false. + Also, record Bison's usage of VARIABLE by defining + b4_percent_define_bison_variables(VARIABLE). */ +bool muscle_percent_define_ifdef (char const *variable); + /* Mimic b4_percent_define_flag_if in ../data/bison.m4 exactly. That is, if the %define variable VARIABLE is defined to "" or "true", return true. If it is defined to "false", return false. Complain if it is undefined (a @@ -145,6 +158,10 @@ bool muscle_percent_define_flag_if (char const *variable); suspect that the value has yet influenced the output. */ void muscle_percent_define_default (char const *variable, char const *value); +/* Complain that the value set for the %define variable VARIABLE is + invalid. */ +void muscle_percent_define_invalid_value (char const *variable); + /* Grow the muscle for the %code qualifier QUALIFIER appearing at QUALIFIER_LOC in the grammar file with code CODE appearing at CODE_LOC. Record this as a grammar occurrence of VARIABLE by invoking muscle_user_name_list_grow. */