2
0
mirror of https://github.com/boostorg/spirit.git synced 2026-01-19 04:42:11 +00:00
Files
spirit/test/x3/rule_separate_tu_grammar.hpp
Nana Sakisaka 47945d1e81 Modernize rule, guard, on_error and on_success
`rule` now resolves the parse function using concepts, providing
significantly faster compile time & better errors.

`core/error_handler_types.hpp`: New header to separate enum
definition.

`annotate_on_success`: Modernized.

`on_error` and `on_success` now only accepts const iterators.
This is a breaking change, but we should apply this immediately
due to the reasons described below.

Historically, Spirit has passed mutable lvalue references of the
*internal* iterators to the `on_success`/`on_error` handlers. This
behavior was semantically a mistake, because:
  (1) `on_success`/`on_error` mechanism was designed to be
      grammar-agnostic, and
  (2) it does not make sense to modify the grammar-specific
      iterator on the grammar-agnostic callback.

Furthermore, any modification to X3's internal iterator variables
may invoke undefined behavior, since we had never provided any
kind of guarantee on how those variables are processed in X3's
implementation details.

In other words, I consider the old behavior as a serious BUG
that involves undefined behavior which may even lead to
security issues.

`BOOST_SPIRIT_DECLARE`: Deprecated regarding compile-time slowness
of `BOOST_PP_SEQ_FOR_EACH`.

`BOOST_SPIRIT_DEFINE`: Ditto.

`BOOST_SPIRIT_INSTANTIATE`: Deprecated because the name was not
correctly prefixed with `X3_`.

`BOOST_SPIRIT_X3_DECLARE`: New macro with correctly prefixed name.

`BOOST_SPIRIT_X3_DEFINE`: Ditto.

`BOOST_SPIRIT_X3_INSTANTIATE`: Ditto.
2025-09-07 07:29:17 +09:00

51 lines
1.5 KiB
C++

/*=============================================================================
Copyright (c) 2019 Nikita Kniazev
Copyright (c) 2025 Nana Sakisaka
Use, modification and distribution is subject to the Boost Software
License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#include <boost/spirit/home/x3.hpp>
// Check that `BOOST_SPIRIT_X3_INSTANTIATE` instantiates `parse_rule` with proper
// types when a rule has no attribute.
namespace unused_attr {
namespace x3 = boost::spirit::x3;
// skipper must has no attribute, checks `parse` and `skip_over`
using skipper_type = x3::rule<class skipper_r>;
const skipper_type skipper;
BOOST_SPIRIT_X3_DECLARE(skipper_type)
// the `unused_type const` must have the same effect as no attribute
using skipper2_type = x3::rule<class skipper2_r, x3::unused_type const>;
const skipper2_type skipper2;
BOOST_SPIRIT_X3_DECLARE(skipper2_type)
// grammar must has no attribute, checks `parse` and `phrase_parse`
using grammar_type = x3::rule<class grammar_r>;
const grammar_type grammar;
BOOST_SPIRIT_X3_DECLARE(grammar_type)
}
// Check instantiation when rule has an attribute.
namespace used_attr {
namespace x3 = boost::spirit::x3;
using skipper_type = x3::rule<class skipper_r>;
const skipper_type skipper;
BOOST_SPIRIT_X3_DECLARE(skipper_type)
using grammar_type = x3::rule<class grammar_r, int, true>;
const grammar_type grammar;
BOOST_SPIRIT_X3_DECLARE(grammar_type)
}