c++: provide control over the stack.hh file name

It was not a good idea to generate the file stack.hh.  It never was.
But now we have to deal with backward compatibility: if we stop
generating it, the build system of some build system will probably
break.

So offer the user a means to (i) decide what the name of the output
file should be, and (ii) not generate this file at all (its content
will be inline where the parser is defined).

* data/lalr1.cc (b4_percent_define_check_file_complain)
(b4_percent_define_check_file): New.
* data/stack.hh: Generate the file only if api.stack.file is not
empty.
In that case, use it as file name.
* data/lalr1.cc: Adjust to include the right file, or to include
the definition of stack.
* tests/calc.at, tests/output.at: Exercise api.stack.file.
This commit is contained in:
Akim Demaille
2018-09-23 17:59:46 +02:00
parent 1a0b3bf83b
commit b122a71c83
6 changed files with 99 additions and 10 deletions

View File

@@ -784,6 +784,39 @@ m4_define([b4_percent_define_ifdef],
[$3])])
# b4_percent_define_check_file_complain(VARIABLE)
# -----------------------------------------------
# Warn about %define variable VARIABLE having an incorrect
# value.
m4_define([b4_percent_define_check_file_complain],
[b4_complain_at(b4_percent_define_get_loc([$1]),
[[%%define variable '%s' requires 'none' or '"..."' values]],
[$1])])
# b4_percent_define_check_file(MACRO, VARIABLE, DEFAULT)
# ------------------------------------------------------
# If the %define variable VARIABLE:
# - is undefined, then if DEFAULT is non-empty, define MACRO to DEFAULT
# - is a string, define MACRO to its value
# - is the keyword 'none', do nothing
# - otherwise, warn about the incorrect value.
m4_define([b4_percent_define_check_file],
[b4_percent_define_ifdef([$2],
[m4_case(b4_percent_define_get_kind([$2]),
[string],
[m4_define([$1], b4_percent_define_get([$2]))],
[keyword],
[m4_if(b4_percent_define_get([$2]), [none], [],
[b4_percent_define_check_file_complain([$2])])],
[b4_percent_define_check_file_complain([$2])])
],
[m4_ifval([$3],
[m4_define([$1], [$3])])])
])
## --------- ##
## Options. ##
## --------- ##

View File

@@ -20,6 +20,7 @@ m4_include(b4_pkgdatadir/[c++.m4])
# api.value.type=variant is valid.
m4_define([b4_value_type_setup_variant])
# b4_integral_parser_table_declare(TABLE-NAME, CONTENT, COMMENT)
# --------------------------------------------------------------
# Declare "parser::yy<TABLE-NAME>_" whose contents is CONTENT.
@@ -162,8 +163,9 @@ m4_define([b4_shared_declarations],
# include <vector>
]b4_cxx_portability[
]m4_ifdef([b4_stack_file],
[[# include "]b4_stack_file["]])[
]b4_defines_if([[
# include "stack.hh"
]b4_bison_locations_if([[# include "location.hh"]])])[
]b4_variant_if([b4_variant_includes])[
@@ -174,9 +176,9 @@ m4_define([b4_shared_declarations],
]b4_namespace_open[
]m4_ifdef([b4_stack_file], [], [b4_stack_define])[
]b4_defines_if([],
[b4_stack_define
b4_bison_locations_if([b4_position_define
[b4_bison_locations_if([b4_position_define
b4_location_define])])[
]b4_variant_if([b4_variant_define])[

View File

@@ -18,6 +18,14 @@
m4_pushdef([b4_copyright_years],
[2002-2015, 2018])
# b4_stack_file
# -------------
# Name of the file containing the stack class, if we want this file.
b4_percent_define_check_file([b4_stack_file],
[[api.stack.file]],
b4_defines_if([[stack.hh]]))
# b4_stack_define
# ---------------
m4_define([b4_stack_define],
@@ -129,16 +137,17 @@ m4_define([b4_stack_define],
};
]])
b4_defines_if(
[b4_output_begin([b4_dir_prefix[]stack.hh])
b4_copyright([Stack handling for Bison parsers in C++])[
m4_ifdef([b4_stack_file],
[b4_output_begin([b4_dir_prefix[]b4_stack_file])[
]b4_copyright([Stack handling for Bison parsers in C++])[
/**
** \file ]b4_dir_prefix[stack.hh
** \file ]b4_dir_prefix[]b4_stack_file[
** Define the ]b4_namespace_ref[::stack class.
*/
]b4_cpp_guard_open([b4_dir_prefix[]stack.hh])[
]b4_cpp_guard_open([b4_dir_prefix[]b4_stack_file])[
# include <vector>
@@ -148,7 +157,8 @@ b4_copyright([Stack handling for Bison parsers in C++])[
]b4_stack_define[
]b4_namespace_close[
]b4_cpp_guard_close([b4_dir_prefix[]stack.hh])[
]b4_output_end])
]b4_cpp_guard_close([b4_dir_prefix[]b4_stack_file])[
]b4_output_end[
]])
m4_popdef([b4_copyright_years])

View File

@@ -694,6 +694,9 @@ AT_CHECK_CALC_LALR1_CC([%defines %locations %define parse.error verbose %debug %
AT_CHECK_CALC_LALR1_CC([%define parse.error verbose %debug %define api.prefix {calc} %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}])
AT_CHECK_CALC_LALR1_CC([%defines %locations %define parse.error verbose %debug %define api.prefix {calc} %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}])
AT_CHECK_CALC_LALR1_CC([%locations %define api.stack.file "my-stack.hh"])
AT_CHECK_CALC_LALR1_CC([%locations %defines %define api.stack.file none])
AT_CHECK_CALC_LALR1_CC([%defines %locations %define api.stack.file "my-stack.hh"])
# --------------------------- #

View File

@@ -1649,6 +1649,28 @@ input.y:1.9-21: accepted value: 'both'
AT_CLEANUP
## ------------------------ ##
## %define file variables. ##
## ------------------------ ##
AT_SETUP([["%define" file variables]])
AT_DATA([[input.y]],
[[%skeleton "lalr1.cc"
%define api.stack.file bogus
%%
start: %empty;
]])
AT_BISON_CHECK([[-fcaret input.y]], [[1]], [[]],
[[input.y:2.9-22: error: %define variable 'api.stack.file' requires 'none' or '"..."' values
%define api.stack.file bogus
^^^^^^^^^^^^^^
]])
AT_CLEANUP
## -------------------------------- ##
## %define backward compatibility. ##
## -------------------------------- ##

View File

@@ -145,6 +145,9 @@ AT_CHECK([grep 'include .subdir/' $1.cc], 1, [])
AT_CHECK([grep 'include .subdir/' $1.hh], 1, [])
])
AT_CHECK_OUTPUT([foo.yy], [%skeleton "lalr1.cc"], [],
[foo.tab.cc])
AT_CHECK_OUTPUT([foo.yy], [%skeleton "lalr1.cc" %verbose], [],
[foo.output foo.tab.cc])
@@ -176,6 +179,22 @@ AT_CHECK_OUTPUT([gram_dir/foo.yy],
[],
[output_dir/foo.output output_dir/foo.tab.cc output_dir/foo.tab.hh output_dir/location.hh output_dir/position.hh output_dir/stack.hh])
# api.stack.file.
AT_CHECK_OUTPUT([foo.yy],
[%skeleton "lalr1.cc" %define api.stack.file "foo.sta.hh"],
[],
[foo.sta.hh foo.tab.cc])
AT_CHECK_OUTPUT([foo.yy],
[%skeleton "lalr1.cc" %defines %define api.stack.file none],
[],
[foo.tab.cc foo.tab.hh])
AT_CHECK_OUTPUT([gram_dir/foo.yy],
[%skeleton "lalr1.cc" %verbose %defines %define api.stack.file "foo.sta.hh" %file-prefix "output_dir/foo"],
[],
[output_dir/foo.output output_dir/foo.sta.hh output_dir/foo.tab.cc output_dir/foo.tab.hh])
# AT_CHECK_CONFLICTING_OUTPUT(INPUT-FILE, DIRECTIVES, FLAGS, STDERR,
# [EXIT-STATUS])