subgrammar.hpp
Go to the documentation of this file.
1 // Copyright (C) 2022 Jonathan Müller and lexy contributors
2 // SPDX-License-Identifier: BSL-1.0
3 
4 #ifndef LEXY_DSL_SUBGRAMMAR_HPP_INCLUDED
5 #define LEXY_DSL_SUBGRAMMAR_HPP_INCLUDED
6 
7 #include <lexy/action/base.hpp>
8 #include <lexy/dsl/base.hpp>
9 
10 namespace lexy
11 {
12 template <typename Production, typename Handler, typename State, typename Reader>
13 struct _subgrammar;
14 
15 template <typename Production, typename Action>
16 using _subgrammar_for = _subgrammar<Production, typename Action::handler, typename Action::state,
18 } // namespace lexy
19 
20 #define LEXY_DECLARE_SUBGRAMMAR(Production) \
21  namespace lexy \
22  { \
23  template <typename ParseState> \
24  constexpr auto production_has_value_callback<Production, ParseState> = true; \
25  \
26  template <typename Handler, typename State, typename Reader> \
27  struct _subgrammar<Production, Handler, State, Reader> \
28  { \
29  template <typename T> \
30  static bool parse(_detail::lazy_init<T>& value, \
31  _detail::parse_context_control_block<Handler, State>* control_block, \
32  Reader& reader); \
33  }; \
34  }
35 
36 #define LEXY_DEFINE_SUBGRAMMAR(Production) \
37  template <typename Handler, typename State, typename Reader> \
38  template <typename T> \
39  bool ::lexy::_subgrammar<Production, Handler, State, Reader>:: \
40  parse(::lexy::_detail::lazy_init<T>& value, \
41  ::lexy::_detail::parse_context_control_block<Handler, State>* control_block, \
42  Reader& reader) \
43  { \
44  lexy::_pc<Handler, State, Production> context(control_block); \
45  auto success = ::lexy::_do_action(context, reader); \
46  value = LEXY_MOV(context.value); \
47  return success; \
48  }
49 
50 #define LEXY_INSTANTIATE_SUBGRAMMAR(Production, ...) \
51  template bool ::lexy::_subgrammar_for<Production, __VA_ARGS__> \
52  ::parse<::lexy::_production_value_type<typename __VA_ARGS__::handler, \
53  typename __VA_ARGS__::state, Production>> \
54  (::lexy::_detail::lazy_init<::lexy::_production_value_type< \
55  typename __VA_ARGS__::handler, typename __VA_ARGS__::state, Production>>&, \
56  ::lexy::_detail::parse_context_control_block<typename __VA_ARGS__::handler, \
57  typename __VA_ARGS__::state>*, \
58  ::lexy::input_reader<typename __VA_ARGS__::input>&);
59 
60 namespace lexyd
61 {
62 template <typename Production, typename T>
63 struct _subg : rule_base
64 {
65  template <typename NextParser>
66  struct p
67  {
68  template <typename Context, typename Reader, typename... Args>
69  LEXY_PARSER_FUNC static bool parse(Context& context, Reader& reader, Args&&... args)
70  {
71  using control_block_type = LEXY_DECAY_DECLTYPE(*context.control_block);
72  using handler_type = typename control_block_type::handler_type;
73  using state_type = typename control_block_type::state_type;
74 
75  auto vars = context.control_block->vars;
76  context.control_block->vars = nullptr;
77 
78  constexpr auto production_uses_void_callback = std::is_same_v<
79  typename handler_type::template value_callback<Production, state_type>,
81  using value_type = std::conditional_t<production_uses_void_callback, void, T>;
83 
84  using subgrammar_traits
86  auto rule_result
87  = subgrammar_traits::template parse<value_type>(value, context.control_block,
88  reader);
89 
90  context.control_block->vars = vars;
91 
92  if (!rule_result)
93  return false;
94 
95  if constexpr (std::is_void_v<value_type>)
96  return NextParser::parse(context, reader, LEXY_FWD(args)...);
97  else
98  return NextParser::parse(context, reader, LEXY_FWD(args)..., *LEXY_MOV(value));
99  }
100  };
101 };
102 
105 template <typename Production, typename T>
106 constexpr auto subgrammar = _subg<Production, T>{};
107 } // namespace lexyd
108 
109 #endif // LEXY_DSL_SUBGRAMMAR_HPP_INCLUDED
LEXY_MOV
#define LEXY_MOV(...)
Definition: config.hpp:29
LEXY_FWD
#define LEXY_FWD(...)
Definition: config.hpp:30
lexy
Definition: any_ref.hpp:12
lexyd::subgrammar
constexpr auto subgrammar
Definition: subgrammar.hpp:106
lexyd::_subg::p::parse
static LEXY_PARSER_FUNC bool parse(Context &context, Reader &reader, Args &&... args)
Definition: subgrammar.hpp:69
lexy::parse
constexpr auto parse(const Input &input, const ErrorCallback &callback)
Parses the production into a value, invoking the callback on error.
Definition: parse.hpp:171
lexy::_detail::lazy_init< value_type >
lexyd::_subg
Definition: subgrammar.hpp:63
lexy::_subgrammar
Definition: subgrammar.hpp:13
LEXY_DECAY_DECLTYPE
#define LEXY_DECAY_DECLTYPE(...)
Definition: config.hpp:34
lexyd::rule_base
Definition: grammar.hpp:17
LEXY_PARSER_FUNC
#define LEXY_PARSER_FUNC
Definition: dsl/base.hpp:108
base.hpp
lexy::_detail::void_value_callback
Definition: action/base.hpp:253
base.hpp
lexyd
Definition: trace.hpp:22
lexy::input_reader
decltype(LEXY_DECLVAL(Input).reader()) input_reader
Definition: input/base.hpp:106
lexyd::_subg::p
Definition: subgrammar.hpp:66


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Dec 13 2024 03:19:17