mirror of
https://github.com/boostorg/spirit.git
synced 2026-01-19 04:42:11 +00:00
`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.
145 lines
3.7 KiB
C++
145 lines
3.7 KiB
C++
/*=============================================================================
|
|
Copyright (c) 2001-2015 Joel de Guzman
|
|
Copyright (c) 2025 Nana Sakisaka
|
|
|
|
Distributed under 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)
|
|
=============================================================================*/
|
|
#define BOOST_SPIRIT_X3_DEBUG
|
|
|
|
#include "test.hpp"
|
|
|
|
#include <boost/spirit/home/x3.hpp>
|
|
#include <boost/fusion/include/std_pair.hpp>
|
|
#include <boost/fusion/include/vector.hpp>
|
|
|
|
#include <iterator>
|
|
#include <vector>
|
|
#include <string>
|
|
#include <cstring>
|
|
#include <iostream>
|
|
|
|
struct my_error_handler
|
|
{
|
|
template <std::forward_iterator It, std::sentinel_for<It> Se, typename Exception, typename Context>
|
|
boost::spirit::x3::error_handler_result
|
|
operator()(It const&, Se const& last, Exception const& x, Context const&) const
|
|
{
|
|
std::cout
|
|
<< "Error! Expecting: "
|
|
<< x.which()
|
|
<< ", got: \""
|
|
<< std::string(x.where(), last)
|
|
<< "\""
|
|
<< std::endl;
|
|
return boost::spirit::x3::error_handler_result::fail;
|
|
}
|
|
};
|
|
|
|
struct my_attribute
|
|
{
|
|
bool alive = true;
|
|
|
|
void access() const
|
|
{
|
|
BOOST_TEST(alive);
|
|
}
|
|
~my_attribute()
|
|
{
|
|
alive = false;
|
|
}
|
|
|
|
friend std::ostream& operator<<(std::ostream & os, my_attribute const & attr)
|
|
{
|
|
attr.access();
|
|
return os << "my_attribute";
|
|
}
|
|
};
|
|
|
|
int main()
|
|
{
|
|
using spirit_test::test_attr;
|
|
using spirit_test::test;
|
|
|
|
using namespace boost::spirit::x3::standard;
|
|
using boost::spirit::x3::rule;
|
|
using boost::spirit::x3::symbols;
|
|
using boost::spirit::x3::int_;
|
|
|
|
{ // basic tests
|
|
|
|
auto a = rule<class a_id>("a") = 'a';
|
|
auto b = rule<class b_id>("b") = 'b';
|
|
auto c = rule<class c_id>("c") = 'c';
|
|
|
|
{
|
|
auto start = *(a | b | c);
|
|
BOOST_TEST(test("abcabcacb", start));
|
|
}
|
|
|
|
{
|
|
rule<class start> start("start");
|
|
auto start_def =
|
|
start = (a | b) >> (start | b);
|
|
|
|
BOOST_TEST(test("aaaabababaaabbb", start_def));
|
|
BOOST_TEST(test("aaaabababaaabba", start_def, false));
|
|
}
|
|
}
|
|
|
|
{ // basic tests w/ skipper
|
|
|
|
auto a = rule<class a_id>("a") = 'a';
|
|
auto b = rule<class b_id>("b") = 'b';
|
|
auto c = rule<class c_id>("c") = 'c';
|
|
|
|
{
|
|
auto start = *(a | b | c);
|
|
BOOST_TEST(test(" a b c a b c a c b ", start, space));
|
|
}
|
|
|
|
{
|
|
rule<class start> start("start");
|
|
auto start_def =
|
|
start = (a | b) >> (start | b);
|
|
|
|
BOOST_TEST(test(" a a a a b a b a b a a a b b b ", start_def, space));
|
|
BOOST_TEST(test(" a a a a b a b a b a a a b b a ", start_def, space, false));
|
|
}
|
|
}
|
|
|
|
{ // std::container attributes
|
|
|
|
typedef boost::fusion::vector<int, char> fs;
|
|
rule<class start, std::vector<fs>> start("start");
|
|
auto start_def =
|
|
start = *(int_ >> alpha);
|
|
|
|
BOOST_TEST(test("1 a 2 b 3 c", start_def, space));
|
|
}
|
|
|
|
{ // error handling
|
|
|
|
auto r_def = '(' > int_ > ',' > int_ > ')';
|
|
auto r = r_def.on_error(my_error_handler());
|
|
|
|
BOOST_TEST(test("(123,456)", r));
|
|
BOOST_TEST(!test("(abc,def)", r));
|
|
BOOST_TEST(!test("(123,456]", r));
|
|
BOOST_TEST(!test("(123;456)", r));
|
|
BOOST_TEST(!test("[123,456]", r));
|
|
}
|
|
|
|
{
|
|
symbols<my_attribute> a{{{ "a", my_attribute{} }}};
|
|
|
|
auto b = rule<struct b_id, my_attribute>("b") = a;
|
|
|
|
my_attribute attr;
|
|
|
|
BOOST_TEST(test_attr("a", b, attr));
|
|
}
|
|
|
|
return boost::report_errors();
|
|
}
|