dsl/error.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_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 Rule>
15 {
16  static constexpr auto name = "<error>";
17  static constexpr auto max_recursion_depth = 0;
18  static constexpr auto rule = Rule{};
19 };
20 
21 template <typename Tag, typename Rule>
23 {
24  template <typename NextParser>
25  struct p
26  {
27  template <typename Context, typename Reader, typename... Args>
28  LEXY_PARSER_FUNC static bool parse(Context& context, Reader& reader, Args&&...)
29  {
30  auto begin = reader.position();
31  auto end = reader.position();
32  if constexpr (!std::is_same_v<Rule, void>)
33  {
34  auto backtrack = reader.current();
35 
36  // We match a dummy production that only consists of the rule.
40  context.control_block
41  ->parse_state,
42  reader);
43  end = reader.position();
44  reader.reset(LEXY_MOV(backtrack));
45  }
46 
47  auto err = lexy::error<Reader, Tag>(begin, end);
48  context.on(_ev::error{}, err);
49  return false;
50  }
51  };
52  template <typename Reader>
54 
56  template <typename R>
57  constexpr auto operator()(R) const
58  {
59  return _err<Tag, R>{};
60  }
61 };
62 
64 template <typename Tag>
65 constexpr auto error = _err<Tag, void>{};
66 } // namespace lexyd
67 
68 namespace lexyd
69 {
70 template <typename Branch, typename Error>
72 {
73  template <typename NextParser>
74  struct p
75  {
76  template <typename Context, typename Reader, typename... Args>
77  LEXY_PARSER_FUNC static bool parse(Context& context, Reader& reader, Args&&... args)
78  {
79  // Try and parse the branch.
81  if (branch.try_parse(context.control_block, reader))
82  return branch.template finish<NextParser>(context, reader, LEXY_FWD(args)...);
83  branch.cancel(context);
84 
85  // The branch wasn't taken, so we fail with the specific error by parsing Error.
86  auto result = lexy::parser_for<Error, lexy::pattern_parser<>>::parse(context, reader);
87  LEXY_ASSERT(!result, "error must not recover");
88 
89  return false;
90  }
91  };
92 
93  // As a branch we parse it exactly the same.
94  template <typename Reader>
96 };
97 
98 template <typename Branch>
99 struct _must_dsl
100 {
101  template <typename Tag>
102  struct _err : _must<Branch, lexyd::_err<Tag, void>>
103  {
104  template <typename Rule>
105  constexpr auto operator()(Rule rule) const
106  {
107  auto err = lexyd::error<Tag>(rule);
108  return _must<Branch, decltype(err)>{};
109  }
110  };
111 
112  template <typename Tag>
113  static constexpr _err<Tag> error = _err<Tag>{};
114 };
115 
118 template <typename Branch>
119 constexpr auto must(Branch)
120 {
121  LEXY_REQUIRE_BRANCH_RULE(Branch, "must()");
122  static_assert(!lexy::is_unconditional_branch_rule<Branch>);
123  return _must_dsl<Branch>{};
124 }
125 } // namespace lexyd
126 
127 #endif // LEXY_DSL_ERROR_HPP_INCLUDED
128 
LEXY_MOV
#define LEXY_MOV(...)
Definition: config.hpp:29
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:28
lexyd::_err::p
Definition: dsl/error.hpp:25
lexy::branch_parser_for
typename BranchRule::template bp< Reader > branch_parser_for
Definition: dsl/base.hpp:116
lexyd::error
constexpr auto error
Matches nothing, produces an error with the given tag.
Definition: dsl/error.hpp:65
LEXY_FWD
#define LEXY_FWD(...)
Definition: config.hpp:30
cx::end
constexpr auto end(const C &c) -> decltype(c.end())
Definition: wildcards.hpp:686
LEXY_REQUIRE_BRANCH_RULE
#define LEXY_REQUIRE_BRANCH_RULE(Rule, Name)
Definition: grammar.hpp:73
lexyd::_must
Definition: dsl/error.hpp:71
lexy::error
Generic failure.
Definition: error.hpp:14
lexyd::_must_dsl
Definition: dsl/error.hpp:99
lexyd::_must::p
Definition: dsl/error.hpp:74
lexyd::_must_dsl::_err::operator()
constexpr auto operator()(Rule rule) const
Definition: dsl/error.hpp:105
lexyd::_must< Branch, lexyd::_err< Tag, void > >::bp
lexy::branch_parser_for< Branch, Reader > bp
Definition: dsl/error.hpp:95
lexy::unconditional_branch_parser
A branch parser that takes a branch unconditionally and forwards to the regular parser.
Definition: dsl/base.hpp:127
lexy::parse_events::error
Definition: dsl/base.hpp:68
lexyd::unconditional_branch_base
Definition: grammar.hpp:23
lexyd::_err_production::rule
static constexpr auto rule
Definition: dsl/error.hpp:18
lexyd::_err_production
Definition: dsl/error.hpp:14
LEXY_PARSER_FUNC
#define LEXY_PARSER_FUNC
Definition: dsl/base.hpp:108
lexyd::_err_production::max_recursion_depth
static constexpr auto max_recursion_depth
Definition: dsl/error.hpp:17
lexyd::_err::operator()
constexpr auto operator()(R) const
Adds a rule whose match will be part of the error location.
Definition: dsl/error.hpp:57
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:113
base.hpp
lexyd::_must_dsl::_err
Definition: dsl/error.hpp:102
lexyd::_must::p::parse
static LEXY_PARSER_FUNC bool parse(Context &context, Reader &reader, Args &&... args)
Definition: dsl/error.hpp:77
lexyd::_err_production::name
static constexpr auto name
Definition: dsl/error.hpp:16
lexy::match_action
Definition: match.hpp:48
lexy::parser_for
typename Rule::template p< NextParser > parser_for
Definition: dsl/base.hpp:113
lexyd::_err
Definition: dsl/error.hpp:22
lexy::do_action
constexpr auto do_action(Handler &&handler, State *state, Reader &reader)
Definition: action/base.hpp:228
lexyd
Definition: trace.hpp:22
LEXY_ASSERT
#define LEXY_ASSERT(Expr, Msg)
Definition: assert.hpp:37
branch.hpp
lexy::_mh
Definition: match.hpp:11
lexyd::must
constexpr auto must(Branch)
Definition: dsl/error.hpp:119


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