member.hpp
Go to the documentation of this file.
1 // Copyright (C) 2020-2024 Jonathan Müller and lexy contributors
2 // SPDX-License-Identifier: BSL-1.0
3 
4 #ifndef LEXY_DSL_MEMBER_HPP_INCLUDED
5 #define LEXY_DSL_MEMBER_HPP_INCLUDED
6 
8 #include <lexy/dsl/base.hpp>
9 #include <lexy/dsl/branch.hpp>
10 
11 namespace lexy
12 {
13 template <auto Ptr>
15 {
16  template <typename Object, typename Value>
17  constexpr void operator()(Object& object, Value&& value) const
18  {
19  object.*Ptr = LEXY_FWD(value);
20  }
21 };
22 
23 template <typename Fn>
24 struct member
25 {};
26 
27 template <auto Ptr>
29 } // namespace lexy
30 
31 namespace lexyd
32 {
33 template <typename Fn, typename Rule>
34 struct _mem : _copy_base<Rule>
35 {
36  template <typename Reader>
37  struct bp
38  {
40 
41  template <typename ControlBlock>
42  constexpr auto try_parse(const ControlBlock* cb, const Reader& reader)
43  {
44  return rule.try_parse(cb, reader);
45  }
46 
47  template <typename Context>
48  constexpr void cancel(Context& context)
49  {
50  rule.cancel(context);
51  }
52 
53  template <typename NextParser, typename Context, typename... Args>
54  LEXY_PARSER_FUNC auto finish(Context& context, Reader& reader, Args&&... args)
55  {
56  // Add member tag here.
57  return rule.template finish<NextParser>(context, reader, LEXY_FWD(args)...,
59  }
60  };
61 
62  template <typename NextParser>
63  struct p
64  {
65  template <typename Context, typename Reader, typename... Args>
66  LEXY_PARSER_FUNC static bool parse(Context& context, Reader& reader, Args&&... args)
67  {
68  // Forward to the rule, but add member tag.
70  return parser::parse(context, reader, LEXY_FWD(args)..., lexy::member<Fn>{});
71  }
72  };
73 };
74 
75 template <typename Fn>
76 struct _mem_dsl
77 {
78  constexpr _mem_dsl(Fn = {}) {}
79 
80  template <typename Rule>
81  constexpr auto operator=(Rule) const // NOLINT: it _is_ an unconventional assignment operator
82  {
83  using lambda = std::conditional_t<std::is_default_constructible_v<Fn>, Fn,
85  return _mem<lambda, Rule>{};
86  }
87 };
88 
91 template <auto MemPtr>
93 
94 #define LEXY_MEM(Name) \
95  ::lexyd::_mem_dsl([](auto& obj, auto&& value) { obj.Name = LEXY_FWD(value); })
96 } // namespace lexyd
97 
98 #endif // LEXY_DSL_MEMBER_HPP_INCLUDED
99 
lexyd::_mem_dsl::_mem_dsl
constexpr _mem_dsl(Fn={})
Definition: member.hpp:78
lexyd::_mem::bp
Definition: member.hpp:37
lexy::_mem_ptr_fn
Definition: member.hpp:14
lexyd::_mem::bp::try_parse
constexpr auto try_parse(const ControlBlock *cb, const Reader &reader)
Definition: member.hpp:42
lexy::_mem_ptr_fn::operator()
constexpr void operator()(Object &object, Value &&value) const
Definition: member.hpp:17
lexy::branch_parser_for
typename BranchRule::template bp< Reader > branch_parser_for
Definition: dsl/base.hpp:116
lexy::_detail::stateless_lambda
Definition: stateless_lambda.hpp:12
LEXY_FWD
#define LEXY_FWD(...)
Definition: config.hpp:30
lexyd::_mem_dsl::operator=
constexpr auto operator=(Rule) const
Definition: member.hpp:81
lexyd::_mem::bp::rule
lexy::branch_parser_for< Rule, Reader > rule
Definition: member.hpp:39
lexy
Definition: any_ref.hpp:12
lexyd::_mem
Definition: member.hpp:34
stateless_lambda.hpp
lexyd::_mem::p
Definition: member.hpp:63
lexy::member
Definition: member.hpp:24
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
lexyd::member
constexpr auto member
Definition: member.hpp:92
lexyd::_mem::p::parse
static LEXY_PARSER_FUNC bool parse(Context &context, Reader &reader, Args &&... args)
Definition: member.hpp:66
LEXY_PARSER_FUNC
#define LEXY_PARSER_FUNC
Definition: dsl/base.hpp:108
base.hpp
lexyd::_mem_dsl
Definition: member.hpp:76
lexy::parser_for
typename Rule::template p< NextParser > parser_for
Definition: dsl/base.hpp:113
lexyd::_mem::bp::finish
LEXY_PARSER_FUNC auto finish(Context &context, Reader &reader, Args &&... args)
Definition: member.hpp:54
lexyd
Definition: trace.hpp:22
branch.hpp
lexyd::_copy_base
decltype(_copy_base_impl< Rule >()) _copy_base
Definition: dsl/base.hpp:104
lexyd::_mem::bp::cancel
constexpr void cancel(Context &context)
Definition: member.hpp:48


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