separator.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_SEPARATOR_HPP_INCLUDED
5 #define LEXY_DSL_SEPARATOR_HPP_INCLUDED
6 
7 #include <lexy/dsl/base.hpp>
8 #include <lexy/dsl/if.hpp>
9 #include <lexy/error.hpp>
10 
11 namespace lexy
12 {
14 {
15  static LEXY_CONSTEVAL auto name()
16  {
17  return "unexpected trailing separator";
18  }
19 };
20 } // namespace lexy
21 
22 namespace lexyd
23 {
24 // Reports the trailing sep error.
25 template <typename Branch, typename Tag>
26 struct _nsep : rule_base
27 {
28  template <typename NextParser>
29  struct p
30  {
31  struct _pc
32  {
33  template <typename Context, typename Reader, typename... Args>
34  LEXY_PARSER_FUNC static bool parse(Context& context, Reader& reader,
35  typename Reader::iterator sep_begin, Args&&... args)
36  {
37  auto sep_end = reader.position();
38 
40  auto err = lexy::error<Reader, tag>(sep_begin, sep_end);
41  context.on(_ev::error{}, err);
42 
43  // Trivially recover.
44  return NextParser::parse(context, reader, LEXY_FWD(args)...);
45  }
46  };
47 
48  template <typename Context, typename Reader, typename... Args>
49  LEXY_PARSER_FUNC static bool parse(Context& context, Reader& reader, Args&&... args)
50  {
52  !parser.try_parse(context.control_block, reader))
53  {
54  // Didn't have the separator, everything is okay.
55  parser.cancel(context);
56  return NextParser::parse(context, reader, LEXY_FWD(args)...);
57  }
58  else
59  {
60  // Did have the separator, report error.
61  return parser.template finish<_pc>(context, reader, reader.position(),
62  LEXY_FWD(args)...);
63  }
64  }
65  };
66 };
67 
68 template <typename Branch, typename Tag>
69 struct _sep : _sep_base
70 {
71  using rule = Branch;
73 
74  template <typename Context, typename Reader>
75  static constexpr void report_trailing_error(Context& context, Reader&,
76  typename Reader::iterator sep_begin,
77  typename Reader::iterator sep_end)
78  {
80  auto err = lexy::error<Reader, tag>(sep_begin, sep_end);
81  context.on(_ev::error{}, err);
82  }
83 
84  //=== dsl ===//
85  template <typename NewTag>
86  static constexpr _sep<Branch, NewTag> trailing_error = {};
87 };
88 
90 template <typename Branch>
91 constexpr auto sep(Branch)
92 {
93  LEXY_REQUIRE_BRANCH_RULE(Branch, "sep");
94  return _sep<Branch, void>{};
95 }
96 
97 template <typename Branch>
98 struct _tsep : _sep_base
99 {
100  using rule = Branch;
101  using trailing_rule = decltype(lexyd::if_(Branch{}));
102 
103  template <typename Context, typename Reader>
104  static constexpr void report_trailing_error(Context&, Reader&, typename Reader::iterator,
105  typename Reader::iterator)
106  {}
107 };
108 
110 template <typename Branch>
111 constexpr auto trailing_sep(Branch)
112 {
113  LEXY_REQUIRE_BRANCH_RULE(Branch, "trailing_sep");
114  return _tsep<Branch>{};
115 }
116 
117 template <typename Branch>
118 struct _isep : _sep_base
119 {
120  using rule = Branch;
122 
123  template <typename Context, typename Reader>
124  static constexpr void report_trailing_error(Context&, Reader&, typename Reader::iterator,
125  typename Reader::iterator)
126  {}
127 };
128 
130 template <typename Branch>
131 constexpr auto ignore_trailing_sep(Branch)
132 {
133  static_assert(lexy::is_branch_rule<Branch>);
134  return _isep<Branch>{};
135 }
136 } // namespace lexyd
137 
138 #endif // LEXY_DSL_SEPARATOR_HPP_INCLUDED
139 
lexy::unexpected_trailing_separator::name
static LEXY_CONSTEVAL auto name()
Definition: separator.hpp:15
LEXY_CONSTEVAL
#define LEXY_CONSTEVAL
Definition: config.hpp:98
if.hpp
lexyd::_nsep::p
Definition: separator.hpp:29
lexy::unexpected_trailing_separator
Definition: separator.hpp:13
lexyd::_tsep::rule
Branch rule
Definition: separator.hpp:100
lexyd::sep
constexpr auto sep(Branch)
Defines a separator for a list.
Definition: separator.hpp:91
lexyd::_sep_base
Definition: grammar.hpp:34
lexyd::_else
Definition: branch.hpp:115
lexy::branch_parser_for
typename BranchRule::template bp< Reader > branch_parser_for
Definition: dsl/base.hpp:116
lexyd::_tsep
Definition: separator.hpp:98
LEXY_FWD
#define LEXY_FWD(...)
Definition: config.hpp:30
lexy::_detail::type_or
std::conditional_t< std::is_void_v< T >, Fallback, T > type_or
Definition: config.hpp:64
lexyd::_sep::rule
Branch rule
Definition: separator.hpp:71
lexyd::_nsep::p::_pc
Definition: separator.hpp:31
lexy
Definition: any_ref.hpp:12
lexyd::ignore_trailing_sep
constexpr auto ignore_trailing_sep(Branch)
Defines a separator for a list that ignores the existence of trailing separators.
Definition: separator.hpp:131
LEXY_REQUIRE_BRANCH_RULE
#define LEXY_REQUIRE_BRANCH_RULE(Rule, Name)
Definition: grammar.hpp:73
lexyd::_tsep::trailing_rule
decltype(lexyd::if_(Branch{})) trailing_rule
Definition: separator.hpp:101
lexy::error
Generic failure.
Definition: error.hpp:14
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::parse_events::error
Definition: dsl/base.hpp:68
lexyd::_sep::report_trailing_error
static constexpr void report_trailing_error(Context &context, Reader &, typename Reader::iterator sep_begin, typename Reader::iterator sep_end)
Definition: separator.hpp:75
lexyd::_isep::report_trailing_error
static constexpr void report_trailing_error(Context &, Reader &, typename Reader::iterator, typename Reader::iterator)
Definition: separator.hpp:124
lexyd::trailing_sep
constexpr auto trailing_sep(Branch)
Defines a separator for a list that can be trailing.
Definition: separator.hpp:111
lexyd::rule_base
Definition: grammar.hpp:17
LEXY_PARSER_FUNC
#define LEXY_PARSER_FUNC
Definition: dsl/base.hpp:108
lexyd::_sep
Definition: separator.hpp:69
base.hpp
lexyd::_isep::rule
Branch rule
Definition: separator.hpp:120
lexyd::_tsep::report_trailing_error
static constexpr void report_trailing_error(Context &, Reader &, typename Reader::iterator, typename Reader::iterator)
Definition: separator.hpp:104
lexyd::if_
constexpr auto if_(Branch)
If the branch condition matches, matches the branch then.
Definition: if.hpp:37
lexyd::_nsep::p::_pc::parse
static LEXY_PARSER_FUNC bool parse(Context &context, Reader &reader, typename Reader::iterator sep_begin, Args &&... args)
Definition: separator.hpp:34
lexyd::_nsep::p::parse
static LEXY_PARSER_FUNC bool parse(Context &context, Reader &reader, Args &&... args)
Definition: separator.hpp:49
lexyd::_isep
Definition: separator.hpp:118
lexyd
Definition: trace.hpp:22
lexyd::_nsep
Definition: separator.hpp:26
lexyd::_sep::trailing_error
static constexpr _sep< Branch, NewTag > trailing_error
Definition: separator.hpp:86
error.hpp


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