loop.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_LOOP_HPP_INCLUDED
5 #define LEXY_DSL_LOOP_HPP_INCLUDED
6 
7 #include <lexy/dsl/base.hpp>
8 #include <lexy/dsl/branch.hpp>
9 
10 namespace lexyd
11 {
13 {
14  template <typename NextParser>
15  struct p
16  {
17  template <typename Context, typename Reader, typename LoopControl, typename... Args>
18  LEXY_PARSER_FUNC static bool parse(Context&, Reader&, LoopControl& cntrl, Args&&...)
19  {
20  cntrl.loop_break = true;
21  return true;
22  }
23  };
24 
25  template <typename Reader>
27 };
28 
30 constexpr auto break_ = _break{};
31 } // namespace lexyd
32 
33 namespace lexyd
34 {
35 template <typename Rule>
36 struct _loop : rule_base
37 {
38  template <typename NextParser>
39  struct p
40  {
41  template <typename Context, typename Reader, typename... Args>
42  LEXY_PARSER_FUNC static bool parse(Context& context, Reader& reader, Args&&... args)
43  {
44  struct loop_control_t
45  {
46  bool loop_break = false;
47  } control;
48 
49  while (!control.loop_break)
50  {
52  if (!parser::parse(context, reader, control))
53  return false;
54  }
55 
56  return NextParser::parse(context, reader, LEXY_FWD(args)...);
57  }
58  };
59 };
60 
62 template <typename Rule>
63 constexpr auto loop(Rule)
64 {
65  return _loop<Rule>{};
66 }
67 } // namespace lexyd
68 
69 namespace lexyd
70 {
71 template <typename Branch>
72 struct _whl : rule_base
73 {
74  template <typename NextParser>
75  struct p
76  {
77  template <typename Context, typename Reader, typename... Args>
78  LEXY_PARSER_FUNC static bool parse(Context& context, Reader& reader, Args&&... args)
79  {
81  while (branch.try_parse(context.control_block, reader))
82  {
83  if (!branch.template finish<lexy::pattern_parser<>>(context, reader))
84  return false;
85  }
86  branch.cancel(context);
87 
88  return NextParser::parse(context, reader, LEXY_FWD(args)...);
89  }
90  };
91 };
92 
94 template <typename Rule>
95 constexpr auto while_(Rule)
96 {
97  LEXY_REQUIRE_BRANCH_RULE(Rule, "while()");
98  return _whl<Rule>{};
99 }
100 } // namespace lexyd
101 
102 namespace lexyd
103 {
105 template <typename Rule>
106 constexpr auto while_one(Rule rule)
107 {
108  LEXY_REQUIRE_BRANCH_RULE(Rule, "while_one()");
109  return rule >> while_(rule);
110 }
111 } // namespace lexyd
112 
113 namespace lexyd
114 {
116 template <typename Then, typename Condition>
117 constexpr auto do_while(Then then, Condition condition)
118 {
119  return _maybe_branch(then, while_(condition >> then));
120 }
121 } // namespace lexyd
122 
123 #endif // LEXY_DSL_LOOP_HPP_INCLUDED
124 
lexyd::_break::p::parse
static LEXY_PARSER_FUNC bool parse(Context &, Reader &, LoopControl &cntrl, Args &&...)
Definition: loop.hpp:18
lexyd::while_
constexpr auto while_(Rule)
Matches the branch rule as often as possible.
Definition: loop.hpp:95
lexyd::_maybe_branch
constexpr auto _maybe_branch(Condition condition, Then then)
Definition: branch.hpp:104
lexy::branch_parser_for
typename BranchRule::template bp< Reader > branch_parser_for
Definition: dsl/base.hpp:116
LEXY_FWD
#define LEXY_FWD(...)
Definition: config.hpp:30
lexyd::_whl
Definition: loop.hpp:72
lexyd::_loop::p
Definition: loop.hpp:39
LEXY_REQUIRE_BRANCH_RULE
#define LEXY_REQUIRE_BRANCH_RULE(Rule, Name)
Definition: grammar.hpp:73
lexyd::_break::p
Definition: loop.hpp:15
lexyd::loop
constexpr auto loop(Rule)
Repeatedly matches the rule until a break rule matches.
Definition: loop.hpp:63
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
lexyd::_whl::p
Definition: loop.hpp:75
lexyd::do_while
constexpr auto do_while(Then then, Condition condition)
Matches then once, then while_(condition >> then).
Definition: loop.hpp:117
lexyd::unconditional_branch_base
Definition: grammar.hpp:23
lexyd::rule_base
Definition: grammar.hpp:17
lexyd::_loop::p::parse
static LEXY_PARSER_FUNC bool parse(Context &context, Reader &reader, Args &&... args)
Definition: loop.hpp:42
LEXY_PARSER_FUNC
#define LEXY_PARSER_FUNC
Definition: dsl/base.hpp:108
lexyd::_loop
Definition: loop.hpp:36
base.hpp
lexy::pattern_parser
A parser that does not support any arguments.
Definition: dsl/base.hpp:172
lexyd::break_
constexpr auto break_
Exits a loop().
Definition: loop.hpp:30
lexyd::ascii::control
constexpr auto control
Definition: ascii.hpp:42
lexyd::while_one
constexpr auto while_one(Rule rule)
Matches the rule at least once, then as often as possible.
Definition: loop.hpp:106
lexyd::_whl::p::parse
static LEXY_PARSER_FUNC bool parse(Context &context, Reader &reader, Args &&... args)
Definition: loop.hpp:78
lexy::parser_for
typename Rule::template p< NextParser > parser_for
Definition: dsl/base.hpp:113
lexyd
Definition: trace.hpp:22
branch.hpp
lexyd::_break
Definition: loop.hpp:12


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