brackets.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_BRACKETS_HPP_INCLUDED
5 #define LEXY_DSL_BRACKETS_HPP_INCLUDED
6 
7 #include <lexy/dsl/base.hpp>
8 #include <lexy/dsl/literal.hpp>
10 
11 namespace lexyd
12 {
13 template <typename Open, typename Close, typename RecoveryLimit = void>
14 struct _brackets
15 {
17  template <typename... Literals>
18  constexpr auto limit(Literals... literals) const
19  {
20  static_assert(sizeof...(Literals) > 0);
21 
22  auto l = (recovery_rule().get_limit() / ... / literals);
23  return _brackets<Open, Close, decltype(l)>{};
24  }
25 
26  //=== rules ===//
28  template <typename R>
29  constexpr auto operator()(R r) const
30  {
31  return open() >> as_terminator()(r);
32  }
33 
35  template <typename R>
36  constexpr auto try_(R r) const
37  {
38  return open() >> as_terminator().try_(r);
39  }
40 
43  template <typename R>
44  constexpr auto opt(R r) const
45  {
46  return open() >> as_terminator().opt(r);
47  }
48 
51  template <typename R>
52  constexpr auto list(R r) const
53  {
54  return open() >> as_terminator().list(r);
55  }
56  template <typename R, typename S>
57  constexpr auto list(R r, S sep) const
58  {
59  return open() >> as_terminator().list(r, sep);
60  }
61 
64  template <typename R>
65  constexpr auto opt_list(R r) const
66  {
67  return open() >> as_terminator().opt_list(r);
68  }
69  template <typename R, typename S>
70  constexpr auto opt_list(R r, S sep) const
71  {
72  return open() >> as_terminator().opt_list(r, sep);
73  }
74 
75  //=== access ===//
77  constexpr auto open() const
78  {
79  return Open{};
80  }
82  constexpr auto close() const
83  {
84  return Close{};
85  }
86 
88  constexpr auto as_terminator() const
89  {
91  }
92 
93  constexpr auto recovery_rule() const
94  {
95  return as_terminator().recovery_rule();
96  }
97 };
98 
100 template <typename Open, typename Close>
101 constexpr auto brackets(Open, Close)
102 {
103  LEXY_REQUIRE_BRANCH_RULE(Open, "brackets()");
104  LEXY_REQUIRE_BRANCH_RULE(Close, "brackets()");
105  return _brackets<Open, Close>{};
106 }
107 
108 constexpr auto round_bracketed = brackets(lit_c<'('>, lit_c<')'>);
109 constexpr auto square_bracketed = brackets(lit_c<'['>, lit_c<']'>);
110 constexpr auto curly_bracketed = brackets(lit_c<'{'>, lit_c<'}'>);
111 constexpr auto angle_bracketed = brackets(lit_c<'<'>, lit_c<'>'>);
112 
113 constexpr auto parenthesized = round_bracketed;
114 } // namespace lexyd
115 
116 #endif // LEXY_DSL_BRACKETS_HPP_INCLUDED
117 
lexyd::_brackets::open
constexpr auto open() const
Matches the open bracket.
Definition: brackets.hpp:77
lexyd::_brackets::recovery_rule
constexpr auto recovery_rule() const
Definition: brackets.hpp:93
literal.hpp
lexyd::sep
constexpr auto sep(Branch)
Defines a separator for a list.
Definition: separator.hpp:91
literals
Definition: json.hpp:24461
lexyd::_brackets::close
constexpr auto close() const
Matches the closing bracket.
Definition: brackets.hpp:82
lexyd::_term
Definition: terminator.hpp:24
lexyd::_brackets::opt
constexpr auto opt(R r) const
Definition: brackets.hpp:44
lexyd::_brackets::try_
constexpr auto try_(R r) const
Matches the rule surrounded by brackets, recovering on error.
Definition: brackets.hpp:36
lexyd::round_bracketed
constexpr auto round_bracketed
Definition: brackets.hpp:108
lexyd::angle_bracketed
constexpr auto angle_bracketed
Definition: brackets.hpp:111
LEXY_REQUIRE_BRANCH_RULE
#define LEXY_REQUIRE_BRANCH_RULE(Rule, Name)
Definition: grammar.hpp:73
lexyd::curly_bracketed
constexpr auto curly_bracketed
Definition: brackets.hpp:110
lexyd::_brackets::opt_list
constexpr auto opt_list(R r, S sep) const
Definition: brackets.hpp:70
lexyd::_brackets::as_terminator
constexpr auto as_terminator() const
Returns an equivalent terminator.
Definition: brackets.hpp:88
base.hpp
lexyd::_brackets::limit
constexpr auto limit(Literals... literals) const
Adds the literal tokens to the recovery limit.
Definition: brackets.hpp:18
terminator.hpp
lexyd::lit_c
constexpr auto lit_c
Definition: literal.hpp:381
lexyd::brackets
constexpr auto brackets(Open, Close)
Defines open and close brackets.
Definition: brackets.hpp:101
lexyd::_brackets::list
constexpr auto list(R r, S sep) const
Definition: brackets.hpp:57
lexyd::_brackets
Definition: brackets.hpp:14
lexyd::_brackets::opt_list
constexpr auto opt_list(R r) const
Definition: brackets.hpp:65
lexyd
Definition: trace.hpp:22
lexyd::_brackets::list
constexpr auto list(R r) const
Definition: brackets.hpp:52
lexyd::_brackets::operator()
constexpr auto operator()(R r) const
Matches the rule surrounded by brackets.
Definition: brackets.hpp:29
lexyd::square_bracketed
constexpr auto square_bracketed
Definition: brackets.hpp:109


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