dsl/base.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_BASE_HPP_INCLUDED
5 #define LEXY_DSL_BASE_HPP_INCLUDED
6 
9 #include <lexy/grammar.hpp>
10 #include <lexy/input/base.hpp>
11 
12 //=== parse_events ===//
13 namespace lexy::parse_events
14 {
18 {};
22 {};
26 {};
27 
31 {};
35 {};
39 {};
40 
45 {};
49 {};
53 {};
54 
57 struct token
58 {};
59 
64 {};
65 
68 struct error
69 {};
70 
75 {};
80 {};
85 {};
86 } // namespace lexy::parse_events
87 
88 namespace lexyd
89 {
90 namespace _ev = lexy::parse_events;
91 
92 // Does not copy token tags.
93 template <typename Rule>
95 {
96  if constexpr (lexy::is_unconditional_branch_rule<Rule>)
98  else if constexpr (lexy::is_branch_rule<Rule>)
99  return branch_base{};
100  else
101  return rule_base{};
102 }
103 template <typename Rule>
104 using _copy_base = decltype(_copy_base_impl<Rule>());
105 } // namespace lexyd
106 
107 //=== parser ===//
108 #define LEXY_PARSER_FUNC LEXY_FORCE_INLINE constexpr
109 
110 namespace lexy
111 {
112 template <typename Rule, typename NextParser>
113 using parser_for = typename Rule::template p<NextParser>;
114 
115 template <typename BranchRule, typename Reader>
116 using branch_parser_for = typename BranchRule::template bp<Reader>;
117 
118 template <typename Production, typename Reader>
119 struct _pb : lexy::branch_parser_for<lexy::production_rule<Production>, Reader>
120 {};
121 // We create a new type here to shorten its name.
122 template <typename Production, typename Reader>
124 
126 template <typename Rule, typename Reader>
128 {
129  constexpr std::true_type try_parse(const void*, const Reader&)
130  {
131  return {};
132  }
133 
134  template <typename Context>
135  constexpr void cancel(Context&)
136  {}
137 
138  template <typename NextParser, typename Context, typename... Args>
139  LEXY_PARSER_FUNC bool finish(Context& context, Reader& reader, Args&&... args)
140  {
141  return parser_for<Rule, NextParser>::parse(context, reader, LEXY_FWD(args)...);
142  }
143 };
144 
146 template <typename BranchRule, typename Reader, template <typename> typename Continuation>
148 {
150 
151  template <typename ControlBlock>
152  constexpr auto try_parse(const ControlBlock* cb, const Reader& reader)
153  {
154  return impl.try_parse(cb, reader);
155  }
156 
157  template <typename Context>
158  constexpr void cancel(Context& context)
159  {
160  impl.cancel(context);
161  }
162 
163  template <typename NextParser, typename Context, typename... Args>
164  LEXY_PARSER_FUNC bool finish(Context& context, Reader& reader, Args&&... args)
165  {
166  return impl.template finish<Continuation<NextParser>>(context, reader, LEXY_FWD(args)...);
167  }
168 };
169 
171 template <typename... PrevArgs>
173 {
174  template <typename Context, typename Reader, typename... Args>
175  LEXY_PARSER_FUNC static std::true_type parse(Context&, Reader&, const PrevArgs&..., Args&&...)
176  {
177  // A rule is used inside a loop or similar situation, where it must not produce values, but
178  // it did.
179  static_assert(sizeof...(Args) == 0, "pattern rule must not produce any values");
180  return {};
181  }
182 };
183 
186 {
187  template <typename Context, typename Reader, typename Sink, typename... Args>
188  LEXY_PARSER_FUNC static std::true_type parse(Context&, Reader&, Sink& sink, Args&&... args)
189  {
190  if constexpr (sizeof...(Args) > 0)
191  sink(LEXY_FWD(args)...);
192 
193  return {};
194  }
195 };
196 
198 template <typename NextParser>
200 {
201  template <typename Context, typename Reader, typename Sink, typename... Args>
202  LEXY_PARSER_FUNC static auto parse(Context& context, Reader& reader, Sink& sink, Args&&... args)
203  {
204  if constexpr (std::is_same_v<typename Sink::return_type, void>)
205  {
206  LEXY_MOV(sink).finish();
207  return NextParser::parse(context, reader, LEXY_FWD(args)...);
208  }
209  else
210  {
211  return NextParser::parse(context, reader, LEXY_FWD(args)..., LEXY_MOV(sink).finish());
212  }
213  }
214 };
215 } // namespace lexy
216 
217 //=== whitespace ===//
218 namespace lexy::_detail
219 {
220 template <typename NextParser>
222 } // namespace lexy::_detail
223 
224 namespace lexy
225 {
226 template <typename Context, typename NextParser,
227  typename = lexy::production_whitespace<typename Context::production,
228  typename Context::whitespace_production>>
230 {};
231 // If we know the whitespace rule is void, go to NextParser immediately.
232 // This is both an optimization and also doesn't require inclusion of whitespace.hpp.
233 template <typename Context, typename NextParser>
234 struct whitespace_parser<Context, NextParser, void> : NextParser
235 {};
236 } // namespace lexy
237 
238 //=== token parser ===//
239 namespace lexy
240 {
241 template <typename TokenRule, typename Reader>
242 using token_parser_for = typename TokenRule::template tp<Reader>;
243 
244 template <typename TokenRule, typename Reader>
245 LEXY_FORCE_INLINE constexpr auto try_match_token(TokenRule, Reader& reader)
246 {
248 
249  using try_parse_result = decltype(parser.try_parse(reader));
250  if constexpr (std::is_same_v<try_parse_result, std::true_type>)
251  {
252  parser.try_parse(reader);
253  reader.reset(parser.end);
254  return std::true_type{};
255  }
256  else if constexpr (std::is_same_v<try_parse_result, std::false_type>)
257  {
258  // try_parse() is pure and we don't want to advance the reader, so no need to call it.
259  return std::false_type{};
260  }
261  else
262  {
263  if (!parser.try_parse(reader))
264  return false;
265 
266  reader.reset(parser.end);
267  return true;
268  }
269 }
270 } // namespace lexy
271 
272 #endif // LEXY_DSL_BASE_HPP_INCLUDED
273 
LEXY_MOV
#define LEXY_MOV(...)
Definition: config.hpp:29
lexy::continuation_branch_parser::cancel
constexpr void cancel(Context &context)
Definition: dsl/base.hpp:158
lexy::parse_events::production_finish
Definition: dsl/base.hpp:34
lexyd::branch_base
Definition: grammar.hpp:20
lexy::parse_events::recovery_start
Definition: dsl/base.hpp:74
lexy::sink_parser::parse
static LEXY_PARSER_FUNC std::true_type parse(Context &, Reader &, Sink &sink, Args &&... args)
Definition: dsl/base.hpp:188
config.hpp
lexy::parse_events
Definition: trace.hpp:14
lexy::branch_parser_for
typename BranchRule::template bp< Reader > branch_parser_for
Definition: dsl/base.hpp:116
lexy::parse_events::grammar_finish
Definition: dsl/base.hpp:21
LEXY_FWD
#define LEXY_FWD(...)
Definition: config.hpp:30
lexy
Definition: any_ref.hpp:12
detail::void
j template void())
Definition: json.hpp:4893
grammar.hpp
lexy::parse_events::backtracked
Definition: dsl/base.hpp:63
lexyd::_copy_base_impl
auto _copy_base_impl()
Definition: dsl/base.hpp:94
lexy::unconditional_branch_parser::cancel
constexpr void cancel(Context &)
Definition: dsl/base.hpp:135
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::unconditional_branch_parser
A branch parser that takes a branch unconditionally and forwards to the regular parser.
Definition: dsl/base.hpp:127
lexy::parse_events::error
Definition: dsl/base.hpp:68
lexy::try_match_token
constexpr LEXY_FORCE_INLINE auto try_match_token(TokenRule, Reader &reader)
Definition: dsl/base.hpp:245
lexy::sink_finish_parser
A parser that finishes a sink and continues with the next one.
Definition: dsl/base.hpp:199
lexyd::unconditional_branch_base
Definition: grammar.hpp:23
lexy::_detail::automatic_ws_parser
Definition: dsl/base.hpp:221
lexy::sink_parser
A parser that forwards all arguments to a sink, which is the first argument.
Definition: dsl/base.hpp:185
lexy::pattern_parser::parse
static LEXY_PARSER_FUNC std::true_type parse(Context &, Reader &, const PrevArgs &..., Args &&...)
Definition: dsl/base.hpp:175
lexyd::rule_base
Definition: grammar.hpp:17
lexy::parse_events::grammar_start
Definition: dsl/base.hpp:17
lexy::parse_events::production_start
Definition: dsl/base.hpp:30
lexy::unconditional_branch_parser::try_parse
constexpr std::true_type try_parse(const void *, const Reader &)
Definition: dsl/base.hpp:129
lexy::parse_events::recovery_finish
Definition: dsl/base.hpp:79
lexy::production_whitespace
decltype(_production_whitespace< Production, WhitespaceProduction >()) production_whitespace
Definition: grammar.hpp:243
LEXY_PARSER_FUNC
#define LEXY_PARSER_FUNC
Definition: dsl/base.hpp:108
lexy::whitespace_parser
Definition: dsl/base.hpp:229
lexy::parse_events::operation_chain_op
Definition: dsl/base.hpp:48
lexy::_detail
Definition: any_ref.hpp:12
lexy::continuation_branch_parser::finish
LEXY_PARSER_FUNC bool finish(Context &context, Reader &reader, Args &&... args)
Definition: dsl/base.hpp:164
lexy::parse_events::production_cancel
Definition: dsl/base.hpp:38
lexy::pattern_parser
A parser that does not support any arguments.
Definition: dsl/base.hpp:172
lexy::parse_events::token
Definition: dsl/base.hpp:57
LEXY_FORCE_INLINE
#define LEXY_FORCE_INLINE
Definition: config.hpp:171
lexy::continuation_branch_parser
A branch parser that parses a branch rule but with a special continuation.
Definition: dsl/base.hpp:147
lexy::_pb
Definition: dsl/base.hpp:119
lexy::sink_finish_parser::parse
static LEXY_PARSER_FUNC auto parse(Context &context, Reader &reader, Sink &sink, Args &&... args)
Definition: dsl/base.hpp:202
lexy::parser_for
typename Rule::template p< NextParser > parser_for
Definition: dsl/base.hpp:113
base.hpp
lexy::unconditional_branch_parser::finish
LEXY_PARSER_FUNC bool finish(Context &context, Reader &reader, Args &&... args)
Definition: dsl/base.hpp:139
lexy::parse_events::recovery_cancel
Definition: dsl/base.hpp:84
lexy::parse_events::grammar_cancel
Definition: dsl/base.hpp:25
lexy::token_parser_for
typename TokenRule::template tp< Reader > token_parser_for
Definition: dsl/base.hpp:242
lexy::parse_events::operation_chain_finish
Definition: dsl/base.hpp:52
lexyd
Definition: trace.hpp:22
lexy::continuation_branch_parser::try_parse
constexpr auto try_parse(const ControlBlock *cb, const Reader &reader)
Definition: dsl/base.hpp:152
lexy::continuation_branch_parser::impl
branch_parser_for< BranchRule, Reader > impl
Definition: dsl/base.hpp:149
lazy_init.hpp
lexy::parse_events::operation_chain_start
Definition: dsl/base.hpp:44
lexyd::_copy_base
decltype(_copy_base_impl< Rule >()) _copy_base
Definition: dsl/base.hpp:104


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Nov 1 2024 02:20:50