terminator.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_TERMINATOR_HPP_INCLUDED
5 #define LEXY_DSL_TERMINATOR_HPP_INCLUDED
6 
7 #include <lexy/dsl/base.hpp>
8 #include <lexy/dsl/branch.hpp>
9 #include <lexy/dsl/error.hpp>
10 #include <lexy/dsl/recover.hpp>
11 #include <lexy/dsl/sequence.hpp>
12 #include <lexy/dsl/whitespace.hpp>
13 
14 namespace lexyd
15 {
16 template <typename Terminator, typename Rule>
17 struct _optt;
18 template <typename Terminator, typename R, typename Sep, typename Recover>
19 struct _lstt;
20 template <typename Terminator, typename R, typename Sep, typename Recover>
21 struct _olstt;
22 
23 template <typename Terminator, typename RecoveryLimit = void>
24 struct _term
25 {
27  template <typename... Literals>
28  constexpr auto limit(Literals... literals) const
29  {
30  static_assert(sizeof...(Literals) > 0);
31 
32  auto l = (recovery_rule().get_limit() / ... / literals);
33  return _term<Terminator, decltype(l)>{};
34  }
35 
36  //=== rules ===//
38  template <typename Rule>
39  constexpr auto operator()(Rule rule) const
40  {
41  return _maybe_branch(rule, terminator());
42  }
43 
45  template <typename Rule>
46  constexpr auto try_(Rule) const
47  {
48  return _tryt<Terminator, Rule, decltype(recovery_rule())>{};
49  }
50 
53  template <typename Rule>
54  constexpr auto opt(Rule) const
55  {
56  return _optt<Terminator, _tryt<Terminator, Rule, decltype(recovery_rule())>>{};
57  }
58 
61  template <typename Rule>
62  constexpr auto list(Rule) const
63  {
64  return _lstt<Terminator, Rule, void, decltype(recovery_rule())>{};
65  }
66  template <typename Rule, typename Sep>
67  constexpr auto list(Rule, Sep) const
68  {
69  static_assert(lexy::is_separator<Sep>);
70  return _lstt<Terminator, Rule, Sep, decltype(recovery_rule())>{};
71  }
72 
75  template <typename Rule>
76  constexpr auto opt_list(Rule) const
77  {
78  return _optt<Terminator, _lstt<Terminator, Rule, void, decltype(recovery_rule())>>{};
79  }
80  template <typename Rule, typename Sep>
81  constexpr auto opt_list(Rule, Sep) const
82  {
83  static_assert(lexy::is_separator<Sep>);
84  return _optt<Terminator, _lstt<Terminator, Rule, Sep, decltype(recovery_rule())>>{};
85  }
86 
87  //=== access ===//
89  constexpr auto terminator() const
90  {
91  return Terminator{};
92  }
93 
95  constexpr auto recovery_rule() const
96  {
97  if constexpr (std::is_void_v<RecoveryLimit>)
98  return recover(terminator());
99  else
100  return recover(terminator()).limit(RecoveryLimit{});
101  }
102 };
103 
105 template <typename Branch>
106 constexpr auto terminator(Branch)
107 {
108  LEXY_REQUIRE_BRANCH_RULE(Branch, "terminator");
109  return _term<Branch>{};
110 }
111 } // namespace lexyd
112 
113 #endif // LEXY_DSL_TERMINATOR_HPP_INCLUDED
114 
lexyd::_term::opt
constexpr auto opt(Rule) const
Definition: terminator.hpp:54
error.hpp
lexyd::terminator
constexpr auto terminator(Branch)
Creates a terminator using the given branch.
Definition: terminator.hpp:106
lexyd::_maybe_branch
constexpr auto _maybe_branch(Condition condition, Then then)
Definition: branch.hpp:104
lexyd::_tryt
Definition: recover.hpp:218
literals
Definition: json.hpp:24461
lexyd::recover
constexpr auto recover(Branches...)
Discards input until one of the branches matches to recover from an error.
Definition: recover.hpp:207
lexyd::_term::terminator
constexpr auto terminator() const
Matches the terminator alone.
Definition: terminator.hpp:89
lexyd::_term
Definition: terminator.hpp:24
sequence.hpp
lexyd::_term::list
constexpr auto list(Rule) const
Definition: terminator.hpp:62
detail::void
j template void())
Definition: json.hpp:4893
lexyd::_term::opt_list
constexpr auto opt_list(Rule) const
Definition: terminator.hpp:76
LEXY_REQUIRE_BRANCH_RULE
#define LEXY_REQUIRE_BRANCH_RULE(Rule, Name)
Definition: grammar.hpp:73
lexyd::_olstt
Definition: terminator.hpp:21
lexyd::_term::try_
constexpr auto try_(Rule) const
Matches rule followed by the terminator, recovering on error.
Definition: terminator.hpp:46
lexyd::_term::operator()
constexpr auto operator()(Rule rule) const
Matches rule followed by the terminator.
Definition: terminator.hpp:39
recover.hpp
lexyd::_term::recovery_rule
constexpr auto recovery_rule() const
Matches the recovery rule alone.
Definition: terminator.hpp:95
whitespace.hpp
lexyd::_lstt
Definition: list.hpp:166
base.hpp
lexyd::_term::limit
constexpr auto limit(Literals... literals) const
Adds the literal tokens to the recovery limit.
Definition: terminator.hpp:28
lexyd::_term::list
constexpr auto list(Rule, Sep) const
Definition: terminator.hpp:67
lexyd::_optt
Definition: option.hpp:95
lexyd
Definition: trace.hpp:22
lexyd::_term::opt_list
constexpr auto opt_list(Rule, Sep) const
Definition: terminator.hpp:81
branch.hpp


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