dsl/error.hpp
Go to the documentation of this file.
1 // Copyright (C) 2020-2023 Jonathan Müller and lexy contributors
2 // SPDX-License-Identifier: BSL-1.0
3 
4 #ifndef LEXY_DSL_ERROR_HPP_INCLUDED
5 #define LEXY_DSL_ERROR_HPP_INCLUDED
6 
7 #include <lexy/dsl/base.hpp>
8 #include <lexy/dsl/branch.hpp>
9 #include <lexy/dsl/token.hpp>
10 
11 namespace lexyd
12 {
13 template <typename Tag, typename Rule>
15 {
16  template <typename NextParser>
17  struct p
18  {
19  template <typename Context, typename Reader, typename... Args>
20  LEXY_PARSER_FUNC static bool parse(Context& context, Reader& reader, Args&&...)
21  {
22  auto begin = reader.position();
23  auto end = reader.position();
24  if constexpr (!std::is_same_v<Rule, void>)
25  {
26  lexy::token_parser_for<decltype(lexyd::token(Rule{})), Reader> parser(reader);
27  parser.try_parse(reader);
28  end = parser.end;
29  }
30 
31  auto err = lexy::error<Reader, Tag>(begin, end);
32  context.on(_ev::error{}, err);
33  return false;
34  }
35  };
36  template <typename Reader>
38 
40  template <typename R>
41  constexpr auto operator()(R) const
42  {
43  return _err<Tag, R>{};
44  }
45 };
46 
48 template <typename Tag>
49 constexpr auto error = _err<Tag, void>{};
50 } // namespace lexyd
51 
52 namespace lexyd
53 {
54 template <typename Branch, typename Error>
56 {
57  template <typename NextParser>
58  struct p
59  {
60  template <typename Context, typename Reader, typename... Args>
61  LEXY_PARSER_FUNC static bool parse(Context& context, Reader& reader, Args&&... args)
62  {
63  // Try and parse the branch.
65  if (branch.try_parse(context.control_block, reader))
66  return branch.template finish<NextParser>(context, reader, LEXY_FWD(args)...);
67  branch.cancel(context);
68 
69  // The branch wasn't taken, so we fail with the specific error by parsing Error.
70  auto result = lexy::parser_for<Error, lexy::pattern_parser<>>::parse(context, reader);
71  LEXY_ASSERT(!result, "error must not recover");
72 
73  return false;
74  }
75  };
76 
77  // As a branch we parse it exactly the same.
78  template <typename Reader>
80 };
81 
82 template <typename Branch>
83 struct _must_dsl
84 {
85  template <typename Tag>
86  struct _err : _must<Branch, lexyd::_err<Tag, void>>
87  {
88  template <typename Rule>
89  constexpr auto operator()(Rule rule) const
90  {
91  auto err = lexyd::error<Tag>(rule);
92  return _must<Branch, decltype(err)>{};
93  }
94  };
95 
96  template <typename Tag>
97  static constexpr _err<Tag> error = _err<Tag>{};
98 };
99 
102 template <typename Branch>
103 constexpr auto must(Branch)
104 {
105  static_assert(lexy::is_branch_rule<Branch>);
106  static_assert(!lexy::is_unconditional_branch_rule<Branch>);
107  return _must_dsl<Branch>{};
108 }
109 } // namespace lexyd
110 
111 #endif // LEXY_DSL_ERROR_HPP_INCLUDED
112 
lexyd::token
constexpr auto token(Rule)
Turns the arbitrary rule into a token by matching it without producing any values.
Definition: dsl/token.hpp:214
lexyd::branch_base
Definition: grammar.hpp:20
token.hpp
lexyd::_err::p::parse
static LEXY_PARSER_FUNC bool parse(Context &context, Reader &reader, Args &&...)
Definition: dsl/error.hpp:20
lexyd::_err::p
Definition: dsl/error.hpp:17
lexy::branch_parser_for
typename BranchRule::template bp< Reader > branch_parser_for
Definition: dsl/base.hpp:103
lexyd::error
constexpr auto error
Matches nothing, produces an error with the given tag.
Definition: dsl/error.hpp:49
LEXY_FWD
#define LEXY_FWD(...)
Definition: config.hpp:22
cx::end
constexpr auto end(const C &c) -> decltype(c.end())
Definition: wildcards.hpp:686
lexyd::_must
Definition: dsl/error.hpp:55
lexy::error
Generic failure.
Definition: error.hpp:14
lexyd::_must_dsl
Definition: dsl/error.hpp:83
lexyd::_must::p
Definition: dsl/error.hpp:58
lexyd::_must_dsl::_err::operator()
constexpr auto operator()(Rule rule) const
Definition: dsl/error.hpp:89
lexyd::_must< Branch, lexyd::_err< Tag, void > >::bp
lexy::branch_parser_for< Branch, Reader > bp
Definition: dsl/error.hpp:79
lexy::unconditional_branch_parser
A branch parser that takes a branch unconditionally and forwards to the regular parser.
Definition: dsl/base.hpp:114
lexy::parse_events::error
Definition: dsl/base.hpp:55
lexyd::unconditional_branch_base
Definition: grammar.hpp:23
LEXY_PARSER_FUNC
#define LEXY_PARSER_FUNC
Definition: dsl/base.hpp:95
lexyd::_err::operator()
constexpr auto operator()(R) const
Adds a rule whose match will be part of the error location.
Definition: dsl/error.hpp:41
cx::begin
constexpr auto begin(const C &c) -> decltype(c.begin())
Definition: wildcards.hpp:661
lexyd::_must_dsl::error
static constexpr _err< Tag > error
Definition: dsl/error.hpp:97
base.hpp
lexyd::_must_dsl::_err
Definition: dsl/error.hpp:86
lexyd::_must::p::parse
static LEXY_PARSER_FUNC bool parse(Context &context, Reader &reader, Args &&... args)
Definition: dsl/error.hpp:61
lexy::parser_for
typename Rule::template p< NextParser > parser_for
Definition: dsl/base.hpp:100
lexyd::_err
Definition: dsl/error.hpp:14
lexy::token_parser_for
typename TokenRule::template tp< Reader > token_parser_for
Definition: dsl/base.hpp:229
lexyd
Definition: trace.hpp:22
LEXY_ASSERT
#define LEXY_ASSERT(Expr, Msg)
Definition: assert.hpp:37
branch.hpp
lexyd::must
constexpr auto must(Branch)
Definition: dsl/error.hpp:103


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Jun 28 2024 02:20:07