2007-01-23 Paolo Bonzini <bonzini@gnu.org>

Simplify implementation of unqualified %code, implement macros for
	uniform treatment of boolean %define flags.  Document %define.
	* data/bison.m4 (b4_percent_define_ifdef, b4_percent_define_flag_if,
	b4_percent_code_ifdef): New.
	(b4_percent_code_get): Map unqualified %code to b4_percent_code().
	* data/c++.m4: Define default value for global_tokens_and_yystype.
	* data/glr.cc: Likewise.
	* 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.
This commit is contained in:
Paolo Bonzini
2007-01-23 12:24:27 +00:00
parent 5f340b4884
commit 592d0b1eb5
10 changed files with 203 additions and 144 deletions

View File

@@ -349,6 +349,30 @@ m4_define([b4_percent_define_get],
[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",
# expand IF-TRUE. If it is defined to "0" or "false", expand 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],
[m4_case(b4_percent_define_get([$1]),
[0], [$3], [false], [$3],
[$2])],
[m4_fatal([invalid %define variable passed to b4_percent_define_flag_if: ]$1)])])
# b4_percent_define_default(VARIABLE, DEFAULT)
# --------------------------------------------
# If the %define variable VARIABLE is undefined, set its value to DEFAULT.
@@ -364,9 +388,9 @@ m4_define([b4_percent_define_default],
# --------------------------------
# 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 +398,8 @@ m4_define([b4_percent_define_default],
# 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 +407,17 @@ b4_user_code([m4_indir(b4_macro_name)])
])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 ##

View File

@@ -31,6 +31,7 @@ b4_percent_define_default([[parser_class_name]], [[parser]])
b4_percent_define_default([[location_type]], [[location]])
b4_percent_define_default([[filename_type]], [[std::string]])
b4_percent_define_default([[namespace]], m4_defn([b4_prefix]))
b4_percent_define_default([[global_tokens_and_yystype]], [[0]])
b4_percent_define_default([[define_location_comparison]],
[m4_if(b4_percent_define_get([[filename_type]]),
[std::string], [[1]], [[0]])])

View File

@@ -366,7 +366,7 @@ b4_user_stype
};
]dnl Redirections for glr.c.
m4_ifval(b4_percent_define_get([[global_tokens_and_yystype]]),
b4_percent_define_flag_if([[global_tokens_and_yystype]],
[b4_token_defines(b4_tokens)])
[
#ifndef YYSTYPE

View File

@@ -290,7 +290,7 @@ b4_error_verbose_if([, int tok])[);
};
}
]m4_ifval(b4_percent_define_get([[global_tokens_and_yystype]]),
]b4_percent_define_flag_if([[global_tokens_and_yystype]],
[b4_token_defines(b4_tokens)
#ifndef YYSTYPE

View File

@@ -114,7 +114,7 @@ namespace ]b4_percent_define_get([[namespace]])[
{
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)
@@ -236,7 +236,7 @@ namespace ]b4_percent_define_get([[namespace]])[
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)