branch.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_BRANCH_HPP_INCLUDED
5 #define LEXY_DSL_BRANCH_HPP_INCLUDED
6 
7 #include <lexy/dsl/base.hpp>
8 #include <lexy/dsl/sequence.hpp>
9 
10 namespace lexyd
11 {
12 template <typename Condition, typename... R>
13 struct _br : _copy_base<Condition>
14 {
15  static_assert(sizeof...(R) >= 0);
16 
17  template <typename NextParser>
18  using _pc = lexy::parser_for<_seq_impl<R...>, NextParser>;
19 
20  // We parse Condition and then seq<R...>.
21  // Condition's try_parse() checks the branch condition, which is what we want.
22  template <typename Reader>
24 
25  template <typename NextParser>
26  using p = lexy::parser_for<_seq_impl<Condition, R...>, NextParser>;
27 };
28 
29 //=== operator>> ===//
31 template <typename Condition, typename Then>
32 constexpr auto operator>>(Condition, Then)
33 {
34  LEXY_REQUIRE_BRANCH_RULE(Condition, "Left-hand-side of >>");
35  return _br<Condition, Then>{};
36 }
37 template <typename Condition, typename... R>
38 constexpr auto operator>>(Condition, _seq<R...>)
39 {
40  LEXY_REQUIRE_BRANCH_RULE(Condition, "Left-hand-side of >>");
41  return _br<Condition, R...>{};
42 }
43 template <typename Condition, typename C, typename... R>
44 constexpr auto operator>>(Condition, _br<C, R...>)
45 {
46  LEXY_REQUIRE_BRANCH_RULE(Condition, "Left-hand-side of >>");
47  return _br<Condition, C, R...>{};
48 }
49 
50 // Prevent nested branches in `_br`'s condition.
51 template <typename C, typename... R, typename Then>
52 constexpr auto operator>>(_br<C, R...>, Then)
53 {
54  return C{} >> _seq<R..., Then>{};
55 }
56 template <typename C, typename... R, typename... S>
58 {
59  return C{} >> _seq<R..., S...>{};
60 }
61 
62 // Disambiguation.
63 template <typename C1, typename... R, typename C2, typename... S>
65 {
66  return _br<C1, R..., C2, S...>{};
67 }
68 
69 //=== operator+ ===//
70 // If we add something on the left to a branch, we loose the branchy-ness.
71 template <typename Rule, typename Condition, typename... R>
72 constexpr auto operator+(Rule rule, _br<Condition, R...>)
73 {
74  return rule + _seq<Condition, R...>{};
75 }
76 // Disambiguation.
77 template <typename... R, typename Condition, typename... S>
79 {
80  return _seq<R...>{} + _seq<Condition, S...>{};
81 }
82 
83 // If we add something on the right to a branch, we extend the then.
84 template <typename Condition, typename... R, typename Rule>
85 constexpr auto operator+(_br<Condition, R...>, Rule)
86 {
87  return _br<Condition, R..., Rule>{};
88 }
89 // Disambiguation.
90 template <typename Condition, typename... R, typename... S>
92 {
93  return _br<Condition, R..., S...>{};
94 }
95 
96 // If we add two branches, we use the condition of the first one and treat the second as sequence.
97 template <typename C1, typename... R, typename C2, typename... S>
99 {
100  return _br<C1, R..., C2, S...>{};
101 }
102 
103 template <typename Condition, typename Then>
104 constexpr auto _maybe_branch(Condition condition, Then then)
105 {
106  if constexpr (lexy::is_branch_rule<Condition>)
107  return condition >> then;
108  else
109  return condition + then;
110 }
111 } // namespace lexyd
112 
113 namespace lexyd
114 {
116 {
117  template <typename NextParser>
118  using p = NextParser;
119 
120  template <typename Reader>
122 };
123 struct _else_dsl
124 {
125  template <typename R>
126  friend constexpr auto operator>>(_else_dsl, R rule)
127  {
128  return _else{} >> rule;
129  }
130  template <typename... R>
131  friend constexpr auto operator>>(_else_dsl, _seq<R...> rule)
132  {
133  return _else{} >> rule;
134  }
135  template <typename C, typename... R>
136  friend constexpr auto operator>>(_else_dsl, _br<C, R...> rule)
137  {
138  return _else{} >> rule;
139  }
140 };
141 
143 inline constexpr auto else_ = _else_dsl{};
144 } // namespace lexyd
145 
146 #endif // LEXY_DSL_BRANCH_HPP_INCLUDED
147 
lexyd::_seq
Definition: sequence.hpp:94
lexyd::_seq_impl
Definition: operator.hpp:17
lexyd::_else_dsl::operator>>
constexpr friend auto operator>>(_else_dsl, _seq< R... > rule)
Definition: branch.hpp:131
lexyd::_maybe_branch
constexpr auto _maybe_branch(Condition condition, Then then)
Definition: branch.hpp:104
lexyd::_else
Definition: branch.hpp:115
lexyd::_else_dsl
Definition: branch.hpp:123
lexyd::_else::p
NextParser p
Definition: branch.hpp:118
sequence.hpp
LEXY_REQUIRE_BRANCH_RULE
#define LEXY_REQUIRE_BRANCH_RULE(Rule, Name)
Definition: grammar.hpp:73
lexy::unconditional_branch_parser
A branch parser that takes a branch unconditionally and forwards to the regular parser.
Definition: dsl/base.hpp:127
lexyd::_br
Definition: branch.hpp:13
lexyd::unconditional_branch_base
Definition: grammar.hpp:23
lexyd::operator>>
constexpr auto operator>>(Condition, Then)
Parses Then only after Condition has matched.
Definition: branch.hpp:32
lexyd::else_
constexpr auto else_
Takes the branch unconditionally.
Definition: branch.hpp:143
base.hpp
lexyd::_else_dsl::operator>>
constexpr friend auto operator>>(_else_dsl, R rule)
Definition: branch.hpp:126
lexy::continuation_branch_parser
A branch parser that parses a branch rule but with a special continuation.
Definition: dsl/base.hpp:147
lexy::parser_for
typename Rule::template p< NextParser > parser_for
Definition: dsl/base.hpp:113
lexyd::_else_dsl::operator>>
constexpr friend auto operator>>(_else_dsl, _br< C, R... > rule)
Definition: branch.hpp:136
lexyd::operator+
constexpr auto operator+(Rule rule, _br< Condition, R... >)
Definition: branch.hpp:72
lexyd
Definition: trace.hpp:22
lexyd::_br::_pc
lexy::parser_for< _seq_impl< R... >, NextParser > _pc
Definition: branch.hpp:18
lexyd::_br::p
lexy::parser_for< _seq_impl< Condition, R... >, NextParser > p
Definition: branch.hpp:26
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 Dec 13 2024 03:19:16