2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2017-03-16 21:35:37 +01:00
|
|
|
|
2019-04-21 00:41:11 +02:00
|
|
|
#ifndef HAVE_IRCD_SPIRIT_H
|
2017-09-21 04:31:54 +02:00
|
|
|
#define HAVE_IRCD_SPIRIT_H
|
|
|
|
|
|
|
|
/// This file is not part of the IRCd standard include list (stdinc.h) because
|
|
|
|
/// it involves extremely expensive boost headers for creating formal spirit
|
|
|
|
/// grammars. Include this in a definition file which defines such grammars.
|
2019-05-06 19:38:37 +02:00
|
|
|
///
|
|
|
|
/// Note that directly sharing elements of a grammar between two compilation
|
|
|
|
/// units can be achieved with forward declarations in `ircd/grammar.h`.
|
2017-09-21 04:31:54 +02:00
|
|
|
|
2019-04-21 00:41:11 +02:00
|
|
|
// ircd.h is included here so that it can be compiled into this header. Then
|
|
|
|
// this becomes the single leading precompiled header.
|
|
|
|
#include <ircd/ircd.h>
|
|
|
|
|
2020-02-27 20:54:47 +01:00
|
|
|
// Disables asserts in spirit headers even when we're NDEBUG due to
|
|
|
|
// some false asserts around boolean character tests in spirit.
|
|
|
|
#define BOOST_DISABLE_ASSERTS
|
|
|
|
|
2020-07-25 07:09:19 +02:00
|
|
|
// This prevents spirit grammar rules from generating a very large and/or deep
|
|
|
|
// call-graph where rules compose together using wrapped indirect calls through
|
|
|
|
// boost::function -- this is higly inefficient as the grammar's logic ends up
|
|
|
|
// being a fraction of the generated code and the rest is invocation related
|
|
|
|
// overhead. By force-flattening here we can allow each entry-point into
|
|
|
|
// spirit to compose rules at once and eliminate the wrapping complex.
|
|
|
|
#pragma clang attribute push ([[gnu::always_inline]], apply_to = function)
|
|
|
|
#pragma clang attribute push ([[gnu::flatten]], apply_to = function)
|
|
|
|
|
2020-02-26 03:54:01 +01:00
|
|
|
#include <boost/config.hpp>
|
|
|
|
#include <boost/function.hpp>
|
|
|
|
|
2020-07-25 07:09:19 +02:00
|
|
|
#pragma GCC visibility push (internal)
|
2020-05-20 01:23:20 +02:00
|
|
|
#include <boost/fusion/sequence.hpp>
|
|
|
|
#include <boost/fusion/iterator.hpp>
|
|
|
|
#include <boost/fusion/adapted.hpp>
|
|
|
|
#include <boost/fusion/functional.hpp>
|
|
|
|
#include <boost/fusion/algorithm.hpp>
|
|
|
|
#include <boost/fusion/include/std_pair.hpp>
|
2017-03-16 21:35:37 +01:00
|
|
|
#include <boost/spirit/include/qi.hpp>
|
|
|
|
#include <boost/spirit/include/karma.hpp>
|
2018-01-18 12:39:19 +01:00
|
|
|
#include <boost/spirit/include/phoenix.hpp>
|
2020-05-13 09:31:56 +02:00
|
|
|
#include <boost/spirit/repository/include/qi_seek.hpp>
|
|
|
|
#include <boost/spirit/repository/include/qi_subrule.hpp>
|
2019-04-17 02:12:17 +02:00
|
|
|
#pragma GCC visibility pop
|
2018-03-17 18:44:40 +01:00
|
|
|
|
2020-07-25 07:09:19 +02:00
|
|
|
#pragma clang attribute pop
|
|
|
|
#pragma clang attribute pop
|
|
|
|
|
2020-02-26 03:54:01 +01:00
|
|
|
namespace ircd {
|
|
|
|
namespace spirit
|
|
|
|
__attribute__((visibility("default")))
|
2019-04-17 02:12:17 +02:00
|
|
|
{
|
|
|
|
template<class parent_error> struct expectation_failure;
|
|
|
|
|
2020-05-26 17:21:56 +02:00
|
|
|
IRCD_EXCEPTION(ircd::error, error);
|
|
|
|
IRCD_EXCEPTION(error, generator_error);
|
|
|
|
IRCD_EXCEPTION(generator_error, buffer_overrun);
|
2020-02-26 03:54:01 +01:00
|
|
|
}}
|
2019-04-17 02:12:17 +02:00
|
|
|
|
|
|
|
namespace ircd {
|
|
|
|
namespace spirit
|
2020-07-20 06:03:27 +02:00
|
|
|
__attribute__((visibility("internal")))
|
2018-03-17 18:44:40 +01:00
|
|
|
{
|
2020-06-24 17:51:09 +02:00
|
|
|
namespace phx = boost::phoenix;
|
|
|
|
namespace fusion = boost::fusion;
|
2018-03-17 18:44:40 +01:00
|
|
|
namespace spirit = boost::spirit;
|
|
|
|
namespace ascii = spirit::ascii;
|
|
|
|
namespace karma = spirit::karma;
|
|
|
|
namespace qi = spirit::qi;
|
|
|
|
|
2020-05-13 09:31:56 +02:00
|
|
|
using _val_type = phx::actor<spirit::attribute<0>>;
|
|
|
|
using _r0_type = phx::actor<spirit::attribute<0>>;
|
|
|
|
using _r1_type = phx::actor<spirit::attribute<1>>;
|
|
|
|
using _r2_type = phx::actor<spirit::attribute<2>>;
|
|
|
|
using _r3_type = phx::actor<spirit::attribute<3>>;
|
|
|
|
|
|
|
|
using spirit::unused_type;
|
2020-05-20 08:59:34 +02:00
|
|
|
using spirit::auto_;
|
2020-05-13 09:31:56 +02:00
|
|
|
using spirit::_pass;
|
2020-05-20 08:59:34 +02:00
|
|
|
using spirit::_val;
|
2020-05-13 09:31:56 +02:00
|
|
|
|
2018-07-02 03:52:22 +02:00
|
|
|
using qi::locals;
|
|
|
|
using qi::_a;
|
2020-05-21 06:09:15 +02:00
|
|
|
using qi::_a_type;
|
2018-07-02 03:52:22 +02:00
|
|
|
using qi::_r1_type;
|
|
|
|
using qi::raw;
|
|
|
|
using qi::omit;
|
|
|
|
using qi::matches;
|
|
|
|
using qi::hold;
|
|
|
|
using qi::eoi;
|
|
|
|
using qi::eps;
|
2020-04-18 03:57:17 +02:00
|
|
|
using qi::expect;
|
2018-07-02 03:52:22 +02:00
|
|
|
using qi::attr;
|
|
|
|
using qi::attr_cast;
|
|
|
|
using qi::repeat;
|
2018-03-17 18:44:40 +01:00
|
|
|
using qi::lit;
|
|
|
|
using qi::char_;
|
2018-07-02 03:42:56 +02:00
|
|
|
using qi::byte_;
|
2018-03-17 18:44:40 +01:00
|
|
|
using qi::string;
|
|
|
|
using qi::short_;
|
2018-03-19 02:07:22 +01:00
|
|
|
using qi::ushort_;
|
2018-07-02 03:42:56 +02:00
|
|
|
using qi::word;
|
|
|
|
using qi::big_word;
|
|
|
|
using qi::little_word;
|
2018-03-17 18:44:40 +01:00
|
|
|
using qi::int_;
|
2018-03-19 02:07:22 +01:00
|
|
|
using qi::uint_;
|
2018-07-02 03:42:56 +02:00
|
|
|
using qi::dword;
|
|
|
|
using qi::big_dword;
|
|
|
|
using qi::little_dword;
|
2018-03-17 18:44:40 +01:00
|
|
|
using qi::long_;
|
2018-03-19 02:07:22 +01:00
|
|
|
using qi::ulong_;
|
2018-07-02 03:42:56 +02:00
|
|
|
using qi::qword;
|
|
|
|
using qi::big_qword;
|
|
|
|
using qi::little_qword;
|
|
|
|
using qi::float_;
|
|
|
|
using qi::bin_float;
|
|
|
|
using qi::big_bin_float;
|
|
|
|
using qi::little_bin_float;
|
2018-03-17 18:44:40 +01:00
|
|
|
using qi::double_;
|
2018-07-02 03:42:56 +02:00
|
|
|
using qi::bin_double;
|
|
|
|
using qi::big_bin_double;
|
|
|
|
using qi::little_bin_double;
|
2020-05-13 09:31:56 +02:00
|
|
|
using spirit::repository::qi::seek;
|
|
|
|
|
2018-03-17 18:44:40 +01:00
|
|
|
using karma::lit;
|
|
|
|
using karma::char_;
|
|
|
|
using karma::long_;
|
|
|
|
using karma::double_;
|
|
|
|
using karma::bool_;
|
|
|
|
using karma::eps;
|
|
|
|
using karma::attr_cast;
|
|
|
|
using karma::maxwidth;
|
|
|
|
using karma::buffer;
|
2020-05-20 08:59:34 +02:00
|
|
|
using karma::skip;
|
2020-05-21 04:21:35 +02:00
|
|
|
|
2020-05-26 12:03:18 +02:00
|
|
|
using prop_mask = mpl_::int_
|
|
|
|
<
|
|
|
|
karma::generator_properties::no_properties
|
|
|
|
| karma::generator_properties::buffering
|
|
|
|
| karma::generator_properties::counting
|
|
|
|
| karma::generator_properties::tracking
|
|
|
|
| karma::generator_properties::disabling
|
|
|
|
>;
|
2020-05-26 17:21:56 +02:00
|
|
|
|
2020-05-26 12:03:18 +02:00
|
|
|
using sink_type = karma::detail::output_iterator<char *, prop_mask, unused_type>;
|
|
|
|
|
2020-05-21 04:21:35 +02:00
|
|
|
template<size_t idx,
|
|
|
|
class semantic_context>
|
|
|
|
auto &
|
|
|
|
attr_at(semantic_context&&);
|
|
|
|
|
|
|
|
template<size_t idx,
|
|
|
|
class semantic_context>
|
|
|
|
auto &
|
|
|
|
local_at(semantic_context&&);
|
2019-04-17 02:12:17 +02:00
|
|
|
}}
|
2018-07-02 03:52:22 +02:00
|
|
|
|
|
|
|
namespace ircd::spirit::local
|
|
|
|
{
|
2020-05-21 04:21:35 +02:00
|
|
|
using qi::_0;
|
2018-07-02 03:52:22 +02:00
|
|
|
using qi::_1;
|
|
|
|
using qi::_2;
|
|
|
|
using qi::_3;
|
|
|
|
}
|
2018-10-03 02:16:48 +02:00
|
|
|
|
2020-06-10 14:29:07 +02:00
|
|
|
namespace ircd {
|
|
|
|
namespace spirit
|
|
|
|
__attribute__((visibility("default")))
|
|
|
|
{
|
|
|
|
// parse.cc
|
|
|
|
extern thread_local char rule_buffer[64];
|
2020-08-28 10:20:22 +02:00
|
|
|
extern thread_local char generator_buffer[8][64_KiB];
|
2020-06-10 14:29:07 +02:00
|
|
|
extern thread_local struct generator_state *generator_state;
|
|
|
|
}}
|
|
|
|
|
|
|
|
namespace ircd {
|
|
|
|
namespace spirit
|
|
|
|
__attribute__((visibility("internal")))
|
|
|
|
{
|
2020-07-19 08:55:12 +02:00
|
|
|
struct substring_view;
|
|
|
|
struct custom_parser;
|
|
|
|
BOOST_SPIRIT_TERMINAL(custom);
|
|
|
|
|
2020-06-10 14:29:07 +02:00
|
|
|
template<class gen,
|
|
|
|
class... attr>
|
|
|
|
bool parse(const char *&start, const char *const &stop, gen&&, attr&&...);
|
|
|
|
|
|
|
|
template<class parent_error,
|
|
|
|
size_t error_show_max = 48,
|
|
|
|
class gen,
|
|
|
|
class... attr>
|
|
|
|
bool parse(const char *&start, const char *const &stop, gen&&, attr&&...);
|
|
|
|
|
|
|
|
template<bool truncation = false,
|
|
|
|
class gen,
|
|
|
|
class... attr>
|
|
|
|
bool generate(mutable_buffer &out, gen&&, attr&&...);
|
|
|
|
}}
|
|
|
|
|
|
|
|
namespace ircd
|
|
|
|
{
|
|
|
|
using spirit::generate;
|
|
|
|
using spirit::parse;
|
|
|
|
}
|
|
|
|
|
2020-07-19 08:55:12 +02:00
|
|
|
namespace boost {
|
2020-02-26 03:54:01 +01:00
|
|
|
namespace spirit
|
2020-07-19 08:55:12 +02:00
|
|
|
__attribute__((visibility("internal")))
|
2020-02-26 03:54:01 +01:00
|
|
|
{
|
2020-07-19 08:55:12 +02:00
|
|
|
namespace qi
|
|
|
|
{
|
|
|
|
template<class modifiers>
|
|
|
|
struct make_primitive<ircd::spirit::tag::custom, modifiers>;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct use_terminal<qi::domain, ircd::spirit::tag::custom>
|
|
|
|
:mpl::true_
|
|
|
|
{};
|
2020-02-26 03:54:01 +01:00
|
|
|
}}
|
|
|
|
|
2020-07-19 08:55:12 +02:00
|
|
|
struct [[gnu::visibility("internal")]]
|
|
|
|
ircd::spirit::custom_parser
|
|
|
|
:qi::primitive_parser<custom_parser>
|
|
|
|
{
|
|
|
|
template<class context,
|
|
|
|
class iterator>
|
|
|
|
struct attribute
|
|
|
|
{
|
|
|
|
using type = iterator;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class context>
|
|
|
|
boost::spirit::info what(context &) const
|
|
|
|
{
|
|
|
|
return boost::spirit::info("custom");
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class iterator,
|
|
|
|
class context,
|
|
|
|
class skipper,
|
|
|
|
class attr>
|
|
|
|
bool parse(iterator &, const iterator &, context &, const skipper &, attr &) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class modifiers>
|
|
|
|
struct [[gnu::visibility("internal")]]
|
|
|
|
boost::spirit::qi::make_primitive<ircd::spirit::tag::custom, modifiers>
|
|
|
|
{
|
|
|
|
using result_type = ircd::spirit::custom_parser;
|
|
|
|
|
|
|
|
result_type operator()(unused_type, unused_type) const
|
|
|
|
{
|
|
|
|
return result_type{};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-20 08:59:34 +02:00
|
|
|
struct ircd::spirit::substring_view
|
|
|
|
:ircd::string_view
|
|
|
|
{
|
|
|
|
using _iterator = boost::spirit::karma::detail::indirect_iterator<const char *>;
|
|
|
|
using _iterator_range = boost::iterator_range<_iterator>;
|
|
|
|
|
|
|
|
using ircd::string_view::string_view;
|
|
|
|
explicit substring_view(const _iterator_range &range)
|
|
|
|
:ircd::string_view
|
|
|
|
{
|
|
|
|
std::addressof(*range.begin()), std::addressof(*range.end())
|
|
|
|
}
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2018-10-03 02:16:48 +02:00
|
|
|
template<class parent_error>
|
2020-02-26 03:54:01 +01:00
|
|
|
struct __attribute__((visibility("default")))
|
|
|
|
ircd::spirit::expectation_failure
|
2018-10-03 02:16:48 +02:00
|
|
|
:parent_error
|
|
|
|
{
|
|
|
|
template<class it = const char *>
|
|
|
|
expectation_failure(const qi::expectation_failure<it> &e,
|
|
|
|
const ssize_t &show_max = 64);
|
|
|
|
|
|
|
|
template<class it = const char *>
|
|
|
|
expectation_failure(const qi::expectation_failure<it> &e,
|
|
|
|
const it &start,
|
|
|
|
const ssize_t &show_max = 64);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class parent>
|
|
|
|
template<class it>
|
|
|
|
ircd::spirit::expectation_failure<parent>::expectation_failure(const qi::expectation_failure<it> &e,
|
|
|
|
const ssize_t &show_max)
|
|
|
|
:parent
|
|
|
|
{
|
|
|
|
"Expected %s. You input %zd invalid characters :%s",
|
2020-05-27 23:33:39 +02:00
|
|
|
ircd::string(rule_buffer, e.what_),
|
2018-10-03 02:16:48 +02:00
|
|
|
std::distance(e.first, e.last),
|
|
|
|
string_view{e.first, e.first + std::min(std::distance(e.first, e.last), show_max)}
|
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
|
|
|
template<class parent>
|
|
|
|
template<class it>
|
|
|
|
ircd::spirit::expectation_failure<parent>::expectation_failure(const qi::expectation_failure<it> &e,
|
|
|
|
const it &start,
|
|
|
|
const ssize_t &show_max)
|
|
|
|
:parent
|
|
|
|
{
|
2020-06-18 07:39:03 +02:00
|
|
|
"Expected %s. You input %zd invalid characters somewhere between position %zd and %zd :%s",
|
2020-05-27 23:33:39 +02:00
|
|
|
ircd::string(rule_buffer, e.what_),
|
2018-10-03 02:16:48 +02:00
|
|
|
std::distance(e.first, e.last),
|
|
|
|
std::distance(start, e.first),
|
|
|
|
std::distance(start, e.last),
|
|
|
|
string_view{e.first, e.first + std::min(std::distance(e.first, e.last), show_max)}
|
|
|
|
}
|
|
|
|
{}
|
2019-04-21 00:41:11 +02:00
|
|
|
|
2020-06-10 14:29:07 +02:00
|
|
|
struct [[gnu::visibility("hidden")]]
|
|
|
|
ircd::spirit::generator_state
|
2020-05-29 01:42:38 +02:00
|
|
|
{
|
2020-08-28 10:20:22 +02:00
|
|
|
/// The number of instances stacked behind the current state
|
2020-08-28 15:21:44 +02:00
|
|
|
static uint depth() noexcept;
|
2020-08-28 10:20:22 +02:00
|
|
|
|
2020-08-27 14:37:57 +02:00
|
|
|
/// User's buffer.
|
2020-05-29 01:42:38 +02:00
|
|
|
mutable_buffer &out;
|
|
|
|
|
2020-08-27 14:37:57 +02:00
|
|
|
/// Previous in buffer stack
|
|
|
|
struct generator_state *prev {nullptr};
|
2020-05-29 01:42:38 +02:00
|
|
|
|
2020-08-27 14:37:57 +02:00
|
|
|
/// The number of characters we're storing in buffer
|
2020-08-28 15:21:44 +02:00
|
|
|
uint consumed {0};
|
2020-05-29 01:42:38 +02:00
|
|
|
|
2020-08-27 14:37:57 +02:00
|
|
|
/// The number of characters attempted, which may be larger than
|
|
|
|
/// the buffer capacity indicating an overflow amount.
|
2020-08-28 15:21:44 +02:00
|
|
|
uint generated {0};
|
2020-05-29 01:42:38 +02:00
|
|
|
};
|
|
|
|
|
2020-08-28 15:21:44 +02:00
|
|
|
inline uint
|
2020-08-28 10:20:22 +02:00
|
|
|
ircd::spirit::generator_state::depth()
|
|
|
|
noexcept
|
|
|
|
{
|
2020-08-28 15:21:44 +02:00
|
|
|
uint ret(0);
|
2020-08-28 10:20:22 +02:00
|
|
|
for(auto p(ircd::spirit::generator_state); p; p = p->prev)
|
|
|
|
++ret;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-06-05 18:23:38 +02:00
|
|
|
template<bool truncation,
|
|
|
|
class gen,
|
2020-05-26 17:21:56 +02:00
|
|
|
class... attr>
|
2020-07-20 06:03:27 +02:00
|
|
|
[[using gnu: always_inline, gnu_inline]]
|
2020-06-10 14:29:07 +02:00
|
|
|
extern inline bool
|
|
|
|
ircd::spirit::generate(mutable_buffer &out,
|
|
|
|
gen&& g,
|
|
|
|
attr&&... a)
|
2020-05-26 17:21:56 +02:00
|
|
|
|
2020-05-26 12:03:18 +02:00
|
|
|
{
|
2020-06-05 18:23:38 +02:00
|
|
|
const auto max(size(out));
|
|
|
|
const auto start(data(out));
|
2020-06-10 14:29:07 +02:00
|
|
|
struct generator_state state
|
2020-05-26 12:03:18 +02:00
|
|
|
{
|
2020-05-29 01:42:38 +02:00
|
|
|
out
|
2020-05-26 12:03:18 +02:00
|
|
|
};
|
|
|
|
|
2020-05-29 01:42:38 +02:00
|
|
|
const scope_restore _state
|
2020-05-26 12:03:18 +02:00
|
|
|
{
|
2020-06-10 14:29:07 +02:00
|
|
|
generator_state, &state
|
2020-05-26 12:03:18 +02:00
|
|
|
};
|
|
|
|
|
2020-06-05 18:23:38 +02:00
|
|
|
sink_type sink
|
|
|
|
{
|
|
|
|
begin(out)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto ret
|
2020-05-26 12:03:18 +02:00
|
|
|
{
|
2020-05-26 17:21:56 +02:00
|
|
|
karma::generate(sink, std::forward<gen>(g), std::forward<attr>(a)...)
|
2020-05-26 12:03:18 +02:00
|
|
|
};
|
|
|
|
|
2020-08-27 14:37:57 +02:00
|
|
|
assert(state.generated >= state.consumed);
|
|
|
|
const auto overflow
|
|
|
|
{
|
|
|
|
state.generated - state.consumed
|
|
|
|
};
|
|
|
|
|
2020-06-05 18:23:38 +02:00
|
|
|
if constexpr(truncation)
|
|
|
|
{
|
2020-08-27 14:37:57 +02:00
|
|
|
begin(out) = begin(out) > end(out)? end(out): begin(out);
|
2020-06-05 18:23:38 +02:00
|
|
|
assert(begin(out) <= end(out));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-08-27 14:37:57 +02:00
|
|
|
if(unlikely(overflow || begin(out) > end(out)))
|
2020-05-26 12:03:18 +02:00
|
|
|
{
|
2020-08-27 14:37:57 +02:00
|
|
|
begin(out) = start;
|
2020-06-23 09:53:56 +02:00
|
|
|
assert(begin(out) <= end(out));
|
2020-08-27 14:37:57 +02:00
|
|
|
|
|
|
|
char pbuf[2][48];
|
2020-06-10 14:29:07 +02:00
|
|
|
throw buffer_overrun
|
2020-05-26 17:21:56 +02:00
|
|
|
{
|
2020-06-05 18:23:38 +02:00
|
|
|
"Insufficient buffer of %s; required at least %s",
|
2020-08-27 14:37:57 +02:00
|
|
|
pretty(pbuf[0], iec(state.consumed)),
|
|
|
|
pretty(pbuf[1], iec(state.generated)),
|
2020-05-26 17:21:56 +02:00
|
|
|
};
|
|
|
|
}
|
2020-05-26 12:03:18 +02:00
|
|
|
|
2020-06-05 18:10:58 +02:00
|
|
|
assert(begin(out) >= end(out) - max);
|
|
|
|
assert(begin(out) <= end(out));
|
2020-05-26 12:03:18 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-10-14 22:38:34 +02:00
|
|
|
template<class parent_error,
|
2020-06-10 14:29:07 +02:00
|
|
|
size_t error_show_max,
|
|
|
|
class gen,
|
|
|
|
class... attr>
|
2020-07-20 06:03:27 +02:00
|
|
|
[[using gnu: always_inline, gnu_inline, artificial]]
|
2020-06-10 14:29:07 +02:00
|
|
|
extern inline bool
|
|
|
|
ircd::spirit::parse(const char *&start,
|
|
|
|
const char *const &stop,
|
|
|
|
gen&& g,
|
|
|
|
attr&&... a)
|
2019-10-14 22:38:34 +02:00
|
|
|
try
|
|
|
|
{
|
2020-06-10 14:29:07 +02:00
|
|
|
return qi::parse(start, stop, std::forward<gen>(g), std::forward<attr>(a)...);
|
2019-10-14 22:38:34 +02:00
|
|
|
}
|
2020-06-10 14:29:07 +02:00
|
|
|
catch(const qi::expectation_failure<const char *> &e)
|
|
|
|
{
|
|
|
|
throw expectation_failure<parent_error>
|
|
|
|
{
|
|
|
|
e, start, error_show_max
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class gen,
|
|
|
|
class... attr>
|
2020-07-20 06:03:27 +02:00
|
|
|
[[using gnu: always_inline, gnu_inline, artificial]]
|
2020-06-10 14:29:07 +02:00
|
|
|
extern inline bool
|
|
|
|
ircd::spirit::parse(const char *&start,
|
|
|
|
const char *const &stop,
|
|
|
|
gen&& g,
|
|
|
|
attr&&... a)
|
2019-10-14 22:38:34 +02:00
|
|
|
{
|
2020-06-10 14:29:07 +02:00
|
|
|
return qi::parse(start, stop, std::forward<gen>(g), std::forward<attr>(a)...);
|
2019-10-14 22:38:34 +02:00
|
|
|
}
|
|
|
|
|
2020-05-21 04:21:35 +02:00
|
|
|
template<size_t idx,
|
|
|
|
class semantic_context>
|
2020-05-27 23:34:22 +02:00
|
|
|
inline auto &
|
2020-05-21 04:21:35 +02:00
|
|
|
ircd::spirit::local_at(semantic_context&& c)
|
|
|
|
{
|
|
|
|
return boost::fusion::at_c<idx>(c.locals);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t idx,
|
|
|
|
class semantic_context>
|
2020-05-27 23:34:22 +02:00
|
|
|
inline auto &
|
2020-05-21 04:21:35 +02:00
|
|
|
ircd::spirit::attr_at(semantic_context&& c)
|
|
|
|
{
|
|
|
|
return boost::fusion::at_c<idx>(c.attributes);
|
|
|
|
}
|
|
|
|
|
2020-08-27 14:37:57 +02:00
|
|
|
//
|
|
|
|
// boost::spirit::karma
|
|
|
|
//
|
|
|
|
|
2020-05-29 02:40:09 +02:00
|
|
|
namespace boost::spirit::karma::detail
|
|
|
|
{
|
2020-08-27 14:37:57 +02:00
|
|
|
template<> struct enable_buffering<ircd::spirit::sink_type>;
|
2020-05-29 02:40:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2020-08-28 15:21:44 +02:00
|
|
|
struct [[gnu::visibility("internal")]]
|
|
|
|
boost::spirit::karma::detail::enable_buffering<ircd::spirit::sink_type>
|
2020-05-29 02:40:09 +02:00
|
|
|
{
|
2020-08-28 15:21:44 +02:00
|
|
|
uint width
|
2020-08-27 14:37:57 +02:00
|
|
|
{
|
|
|
|
0
|
|
|
|
};
|
2020-05-29 02:40:09 +02:00
|
|
|
|
2020-08-28 15:21:44 +02:00
|
|
|
uint depth
|
2020-08-27 14:37:57 +02:00
|
|
|
{
|
2020-08-28 10:20:22 +02:00
|
|
|
ircd::spirit::generator_state::depth()
|
|
|
|
};
|
|
|
|
|
|
|
|
ircd::mutable_buffer buffer
|
|
|
|
{
|
|
|
|
depth?
|
|
|
|
ircd::spirit::generator_buffer[depth - 1]:
|
|
|
|
data(ircd::spirit::generator_state->out),
|
|
|
|
|
|
|
|
depth?
|
2020-08-28 15:21:44 +02:00
|
|
|
std::min(width, uint(sizeof(ircd::spirit::generator_buffer[depth - 1]))):
|
|
|
|
size(ircd::spirit::generator_state->out),
|
2020-08-27 14:37:57 +02:00
|
|
|
};
|
2020-05-29 02:40:09 +02:00
|
|
|
|
2020-08-27 14:37:57 +02:00
|
|
|
struct ircd::spirit::generator_state state
|
2020-05-29 02:40:09 +02:00
|
|
|
{
|
2020-08-27 14:37:57 +02:00
|
|
|
buffer, ircd::spirit::generator_state
|
|
|
|
};
|
|
|
|
|
|
|
|
ircd::scope_restore<struct ircd::spirit::generator_state *> stack_state
|
|
|
|
{
|
|
|
|
ircd::spirit::generator_state, std::addressof(state)
|
|
|
|
};
|
|
|
|
|
|
|
|
std::size_t buffer_size() const
|
|
|
|
{
|
|
|
|
return state.generated;
|
2020-05-29 02:40:09 +02:00
|
|
|
}
|
|
|
|
|
2020-08-27 14:37:57 +02:00
|
|
|
void disable()
|
|
|
|
{
|
|
|
|
const auto width
|
|
|
|
{
|
2020-08-28 15:21:44 +02:00
|
|
|
this->width != -1U? this->width: state.consumed
|
2020-08-27 14:37:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
assert(width >= state.consumed);
|
2020-08-28 15:21:44 +02:00
|
|
|
const uint off
|
2020-08-27 14:37:57 +02:00
|
|
|
{
|
|
|
|
width - state.consumed
|
|
|
|
};
|
|
|
|
|
|
|
|
const ircd::const_buffer src
|
|
|
|
{
|
|
|
|
state.out, state.consumed
|
|
|
|
};
|
|
|
|
|
|
|
|
const ircd::mutable_buffer dst
|
|
|
|
{
|
|
|
|
state.out + off
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto moved
|
|
|
|
{
|
|
|
|
ircd::move(dst, src)
|
|
|
|
};
|
2020-05-26 17:21:56 +02:00
|
|
|
|
2020-08-27 14:37:57 +02:00
|
|
|
assert(moved == state.consumed);
|
|
|
|
state.consumed = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool buffer_copy(std::size_t maxwidth = std::size_t(-1))
|
2020-05-26 17:21:56 +02:00
|
|
|
{
|
2020-08-27 14:37:57 +02:00
|
|
|
assert(state.prev);
|
2020-08-28 15:21:44 +02:00
|
|
|
#if __has_builtin(__builtin_assume)
|
|
|
|
__builtin_assume(state.prev != nullptr);
|
|
|
|
#endif
|
|
|
|
|
2020-08-27 14:37:57 +02:00
|
|
|
auto &prev
|
|
|
|
{
|
|
|
|
*state.prev
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool prev_base
|
|
|
|
{
|
|
|
|
prev.prev == nullptr
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto width
|
|
|
|
{
|
2020-08-28 15:21:44 +02:00
|
|
|
this->width != -1U? this->width: state.consumed
|
2020-08-27 14:37:57 +02:00
|
|
|
};
|
2020-05-26 17:21:56 +02:00
|
|
|
|
2020-08-27 14:37:57 +02:00
|
|
|
const ircd::const_buffer src
|
|
|
|
{
|
|
|
|
state.out, std::max(state.consumed, width)
|
|
|
|
};
|
|
|
|
|
|
|
|
const ircd::mutable_buffer dst
|
|
|
|
{
|
2020-08-28 10:20:22 +02:00
|
|
|
data(prev.out) - (prev_base? state.consumed: -prev.consumed),
|
|
|
|
prev_base? state.consumed: (size(prev.out) - prev.consumed)
|
2020-08-27 14:37:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const auto copied
|
|
|
|
{
|
|
|
|
ircd::copy(dst, src)
|
|
|
|
};
|
|
|
|
|
|
|
|
prev.generated += state.generated;
|
|
|
|
prev.consumed += copied;
|
|
|
|
return true; // sink.good();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename OutputIterator>
|
|
|
|
bool buffer_copy_to(OutputIterator& sink, std::size_t maxwidth = std::size_t(-1)) const
|
2020-05-26 17:21:56 +02:00
|
|
|
{
|
2020-08-27 14:37:57 +02:00
|
|
|
ircd::always_assert(false);
|
|
|
|
return true;
|
|
|
|
}
|
2020-05-26 17:21:56 +02:00
|
|
|
|
2020-08-27 14:37:57 +02:00
|
|
|
template <typename RestIterator>
|
|
|
|
bool buffer_copy_rest(RestIterator& sink, std::size_t start_at = 0) const
|
|
|
|
{
|
|
|
|
ircd::always_assert(false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
enable_buffering(ircd::spirit::sink_type &sink,
|
|
|
|
std::size_t width = std::size_t(-1))
|
|
|
|
:width
|
|
|
|
{
|
2020-08-28 15:21:44 +02:00
|
|
|
uint(width)
|
2020-08-27 14:37:57 +02:00
|
|
|
}
|
|
|
|
{
|
|
|
|
assert(size(buffer) != 0);
|
2020-08-28 15:21:44 +02:00
|
|
|
assert(this->depth <= 8);
|
2020-08-27 14:37:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
~enable_buffering() noexcept
|
|
|
|
{
|
|
|
|
assert(ircd::spirit::generator_state == &state);
|
|
|
|
//disable();
|
|
|
|
}
|
|
|
|
};
|
2020-05-26 17:21:56 +02:00
|
|
|
|
|
|
|
template<>
|
2020-05-26 12:03:18 +02:00
|
|
|
inline bool
|
2020-08-27 14:37:57 +02:00
|
|
|
boost::spirit::karma::detail::buffering_policy::output(const char &value)
|
2020-05-26 12:03:18 +02:00
|
|
|
{
|
2020-05-29 01:42:38 +02:00
|
|
|
assert(ircd::spirit::generator_state);
|
2020-08-28 15:21:44 +02:00
|
|
|
#if __has_builtin(__builtin_assume)
|
|
|
|
__builtin_assume(ircd::spirit::generator_state != nullptr);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
auto &state
|
2020-05-29 01:42:38 +02:00
|
|
|
{
|
2020-08-28 15:21:44 +02:00
|
|
|
*ircd::spirit::generator_state
|
2020-05-29 01:42:38 +02:00
|
|
|
};
|
|
|
|
|
2020-08-27 14:37:57 +02:00
|
|
|
const bool buffering
|
2020-05-29 01:42:38 +02:00
|
|
|
{
|
2020-08-27 14:37:57 +02:00
|
|
|
this->buffer != nullptr
|
2020-05-29 01:42:38 +02:00
|
|
|
};
|
|
|
|
|
2020-08-27 14:37:57 +02:00
|
|
|
const bool base
|
|
|
|
{
|
2020-08-28 15:21:44 +02:00
|
|
|
state.prev == nullptr
|
|
|
|
};
|
|
|
|
|
|
|
|
const uint off
|
|
|
|
{
|
|
|
|
ircd::boolmask<uint>(!base | buffering) & state.consumed
|
2020-08-27 14:37:57 +02:00
|
|
|
};
|
2020-05-29 01:42:38 +02:00
|
|
|
|
2020-08-27 14:37:57 +02:00
|
|
|
const auto dst
|
2020-05-29 01:42:38 +02:00
|
|
|
{
|
2020-08-28 15:21:44 +02:00
|
|
|
state.out + off
|
2020-05-29 01:42:38 +02:00
|
|
|
};
|
|
|
|
|
2020-08-27 14:37:57 +02:00
|
|
|
const auto copied
|
2020-05-29 01:42:38 +02:00
|
|
|
{
|
2020-08-27 14:37:57 +02:00
|
|
|
ircd::copy(dst, value)
|
2020-05-29 01:42:38 +02:00
|
|
|
};
|
|
|
|
|
2020-08-28 15:21:44 +02:00
|
|
|
state.consumed += copied;
|
|
|
|
state.generated += sizeof(char);
|
2020-08-27 14:37:57 +02:00
|
|
|
return !buffering;
|
|
|
|
}
|
2020-05-29 01:42:38 +02:00
|
|
|
|
2020-08-27 14:37:57 +02:00
|
|
|
template<>
|
|
|
|
inline void
|
|
|
|
boost::spirit::karma::detail::position_policy::output(const char &value)
|
|
|
|
{
|
2020-05-26 12:03:18 +02:00
|
|
|
}
|
|
|
|
|
2020-08-27 14:37:57 +02:00
|
|
|
#if 0
|
2020-05-26 12:03:18 +02:00
|
|
|
template<>
|
2020-08-27 14:37:57 +02:00
|
|
|
template<>
|
|
|
|
inline void
|
|
|
|
boost::spirit::karma::detail::counting_policy<ircd::spirit::sink_type>::output(const char &value)
|
2020-05-26 12:03:18 +02:00
|
|
|
{
|
|
|
|
}
|
2020-08-27 14:37:57 +02:00
|
|
|
#endif
|
2020-05-26 12:03:18 +02:00
|
|
|
|
|
|
|
template<>
|
2020-05-26 17:21:56 +02:00
|
|
|
inline bool
|
2020-05-29 01:42:38 +02:00
|
|
|
ircd::spirit::sink_type::good()
|
2020-05-26 17:21:56 +02:00
|
|
|
const
|
2020-05-26 12:03:18 +02:00
|
|
|
{
|
2020-05-29 01:42:38 +02:00
|
|
|
return true;
|
2020-05-26 12:03:18 +02:00
|
|
|
}
|
|
|
|
|
2019-04-21 00:41:11 +02:00
|
|
|
#endif // HAVE_IRCD_SPIRIT_H
|