context_flag.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_CONTEXT_FLAG_HPP_INCLUDED
5 #define LEXY_DSL_CONTEXT_FLAG_HPP_INCLUDED
6 
7 #include <lexy/action/base.hpp>
8 #include <lexy/dsl/base.hpp>
9 
10 namespace lexyd
11 {
12 template <typename Id>
14 
15 template <typename Id, bool InitialValue>
17 {
18  template <typename NextParser>
19  struct p
20  {
21  template <typename Context, typename Reader, typename... Args>
22  LEXY_PARSER_FUNC static bool parse(Context& context, Reader& reader, Args&&... args)
23  {
24  _ctx_flag<Id> var(InitialValue);
25  var.link(context);
26  auto result = NextParser::parse(context, reader, LEXY_FWD(args)...);
27  var.unlink(context);
28  return result;
29  }
30  };
31 };
32 
33 template <typename Id, bool Value>
35 {
36  template <typename NextParser>
37  struct p
38  {
39  template <typename Context, typename Reader, typename... Args>
40  LEXY_PARSER_FUNC static bool parse(Context& context, Reader& reader, Args&&... args)
41  {
42  _ctx_flag<Id>::get(context.control_block) = Value;
43  return NextParser::parse(context, reader, LEXY_FWD(args)...);
44  }
45  };
46 };
47 
48 template <typename Id>
50 {
51  template <typename NextParser>
52  struct p
53  {
54  template <typename Context, typename Reader, typename... Args>
55  LEXY_PARSER_FUNC static bool parse(Context& context, Reader& reader, Args&&... args)
56  {
57  auto& flag = _ctx_flag<Id>::get(context.control_block);
58  flag = !flag;
59  return NextParser::parse(context, reader, LEXY_FWD(args)...);
60  }
61  };
62 };
63 
64 template <typename Id, bool Value>
66 {
67  template <typename Reader>
68  struct bp
69  {
70  template <typename ControlBlock>
71  constexpr bool try_parse(const ControlBlock* cb, const Reader&)
72  {
73  return _ctx_flag<Id>::get(cb) == Value;
74  }
75 
76  template <typename Context>
77  constexpr void cancel(Context&)
78  {}
79 
80  template <typename NextParser, typename Context, typename... Args>
81  LEXY_PARSER_FUNC bool finish(Context& context, Reader& reader, Args&&... args)
82  {
83  return NextParser::parse(context, reader, LEXY_FWD(args)...);
84  }
85  };
86 
87  template <typename NextParser>
88  using p = NextParser;
89 };
90 
91 template <typename Id>
93 {
94  template <typename NextParser>
95  struct p
96  {
97  template <typename Context, typename Reader, typename... Args>
98  LEXY_PARSER_FUNC static bool parse(Context& context, Reader& reader, Args&&... args)
99  {
100  return NextParser::parse(context, reader, LEXY_FWD(args)...,
101  _ctx_flag<Id>::get(context.control_block));
102  }
103  };
104 };
105 } // namespace lexyd
106 
107 namespace lexyd
108 {
109 template <typename Id>
111 {
112  template <bool InitialValue = false>
113  constexpr auto create() const
114  {
116  }
117 
118  constexpr auto set() const
119  {
120  return _ctx_fset<Id, true>{};
121  }
122  constexpr auto reset() const
123  {
124  return _ctx_fset<Id, false>{};
125  }
126 
127  constexpr auto toggle() const
128  {
129  return _ctx_ftoggle<Id>{};
130  }
131 
132  constexpr auto is_set() const
133  {
134  return _ctx_fis<Id, true>{};
135  }
136  constexpr auto is_reset() const
137  {
138  return _ctx_fis<Id, false>{};
139  }
140 
141  constexpr auto value() const
142  {
143  return _ctx_fvalue<Id>{};
144  }
145 };
146 
148 template <typename Id>
149 constexpr auto context_flag = _ctx_flag_dsl<Id>{};
150 } // namespace lexyd
151 
152 #endif // LEXY_DSL_CONTEXT_FLAG_HPP_INCLUDED
153 
lexyd::branch_base
Definition: grammar.hpp:20
lexyd::_ctx_fcreate
Definition: context_flag.hpp:16
lexyd::_ctx_fis::bp::finish
LEXY_PARSER_FUNC bool finish(Context &context, Reader &reader, Args &&... args)
Definition: context_flag.hpp:81
lexyd::context_flag
constexpr auto context_flag
Declares a flag.
Definition: context_flag.hpp:149
lexyd::_ctx_fvalue::p
Definition: context_flag.hpp:95
lexy::_detail::parse_context_var::get
static constexpr T & get(const ControlBlock *cb)
Definition: action/base.hpp:54
lexyd::_ctx_ftoggle::p::parse
static LEXY_PARSER_FUNC bool parse(Context &context, Reader &reader, Args &&... args)
Definition: context_flag.hpp:55
lexyd::_ctx_fis::bp::try_parse
constexpr bool try_parse(const ControlBlock *cb, const Reader &)
Definition: context_flag.hpp:71
lexyd::_ctx_flag_dsl::set
constexpr auto set() const
Definition: context_flag.hpp:118
lexyd::_ctx_ftoggle
Definition: context_flag.hpp:49
LEXY_FWD
#define LEXY_FWD(...)
Definition: config.hpp:30
lexyd::_ctx_fcreate::p::parse
static LEXY_PARSER_FUNC bool parse(Context &context, Reader &reader, Args &&... args)
Definition: context_flag.hpp:22
lexyd::_ctx_fset::p::parse
static LEXY_PARSER_FUNC bool parse(Context &context, Reader &reader, Args &&... args)
Definition: context_flag.hpp:40
lexyd::_ctx_fcreate::p
Definition: context_flag.hpp:19
lexyd::_ctx_fis::p
NextParser p
Definition: context_flag.hpp:88
lexyd::_ctx_flag_dsl::toggle
constexpr auto toggle() const
Definition: context_flag.hpp:127
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::_ctx_fset::p
Definition: context_flag.hpp:37
lexyd::_ctx_fset
Definition: context_flag.hpp:34
lexy::_detail::parse_context_var_base::link
constexpr void link(Context &context)
Definition: action/base.hpp:27
lexyd::_ctx_ftoggle::p
Definition: context_flag.hpp:52
lexyd::rule_base
Definition: grammar.hpp:17
lexyd::_ctx_flag_dsl::reset
constexpr auto reset() const
Definition: context_flag.hpp:122
lexyd::_ctx_fvalue
Definition: context_flag.hpp:92
lexy::_detail::parse_context_var
Definition: action/base.hpp:43
lexyd::_ctx_flag_dsl::is_set
constexpr auto is_set() const
Definition: context_flag.hpp:132
LEXY_PARSER_FUNC
#define LEXY_PARSER_FUNC
Definition: dsl/base.hpp:108
base.hpp
lexyd::_ctx_fis::bp::cancel
constexpr void cancel(Context &)
Definition: context_flag.hpp:77
lexyd::_ctx_flag_dsl
Definition: context_flag.hpp:110
lexyd::_ctx_flag_dsl::is_reset
constexpr auto is_reset() const
Definition: context_flag.hpp:136
lexyd::_ctx_fvalue::p::parse
static LEXY_PARSER_FUNC bool parse(Context &context, Reader &reader, Args &&... args)
Definition: context_flag.hpp:98
lexyd::_ctx_flag_dsl::create
constexpr auto create() const
Definition: context_flag.hpp:113
base.hpp
lexy::_detail::parse_context_var_base::unlink
constexpr void unlink(Context &context)
Definition: action/base.hpp:35
lexyd::_ctx_fis
Definition: context_flag.hpp:65
lexyd
Definition: trace.hpp:22
lexyd::_ctx_flag_dsl::value
constexpr auto value() const
Definition: context_flag.hpp:141
lexyd::flag
constexpr auto flag(Rule)
Definition: flags.hpp:127
lexyd::_ctx_fis::bp
Definition: context_flag.hpp:68


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