lookahead.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_LOOKAHEAD_HPP_INCLUDED
5 #define LEXY_DSL_LOOKAHEAD_HPP_INCLUDED
6 
7 #include <lexy/dsl/base.hpp>
8 #include <lexy/dsl/literal.hpp>
9 #include <lexy/error.hpp>
10 
11 namespace lexy
12 {
15 {
16  static LEXY_CONSTEVAL auto name()
17  {
18  return "lookahead failure";
19  }
20 };
21 } // namespace lexy
22 
23 namespace lexyd
24 {
25 template <typename Encoding, typename... Needle, typename... End>
27 {
28  auto result = lexy::_detail::make_empty_trie<Encoding, Needle..., End...>();
29  auto char_class = std::size_t(0);
30 
31  // We insert all needles with value 0.
32  ((result.node_value[Needle::lit_insert(result, 0, char_class)] = 0,
33  char_class += Needle::lit_char_classes.size),
34  ...);
35 
36  // And all ends with value 1.
37  ((result.node_value[End::lit_insert(result, 0, char_class)] = 1,
38  char_class += End::lit_char_classes.size),
39  ...);
40 
41  return result;
42 }
43 template <typename Encoding, typename Needle, typename End>
44 static constexpr auto _look_trie
45  = _build_look_trie<Encoding>(typename Needle::as_lset{}, typename End::as_lset{});
46 
47 template <typename Needle, typename End, typename Tag>
49 {
50  template <typename Reader>
51  struct bp
52  {
53  static_assert(lexy::is_char_encoding<typename Reader::encoding>);
54 
55  typename Reader::iterator begin;
56  typename Reader::iterator end;
57 
58  constexpr bool try_parse(const void*, Reader reader)
59  {
60  begin = reader.position();
61 
62  auto result = [&] {
63  using matcher = lexy::_detail::lit_trie_matcher<
64  _look_trie<typename Reader::encoding, Needle, End>, 0>;
65 
66  while (true)
67  {
68  auto result = matcher::try_match(reader);
69  if (result == 0)
70  // We've found the needle.
71  return true;
72  else if (result == 1 || reader.peek() == Reader::encoding::eof())
73  // We've failed.
74  return false;
75  else
76  // Try again.
77  reader.bump();
78  }
79 
80  return false; // unreachable
81  }();
82 
83  end = reader.position();
84  return result;
85  }
86 
87  template <typename Context>
88  constexpr void cancel(Context& context)
89  {
90  context.on(_ev::backtracked{}, begin, end);
91  }
92 
93  template <typename NextParser, typename Context, typename... Args>
94  LEXY_PARSER_FUNC bool finish(Context& context, Reader& reader, Args&&... args)
95  {
96  context.on(_ev::backtracked{}, begin, end);
97  return NextParser::parse(context, reader, LEXY_FWD(args)...);
98  }
99  };
100 
101  template <typename NextParser>
102  struct p
103  {
104  template <typename Context, typename Reader, typename... Args>
105  LEXY_PARSER_FUNC static bool parse(Context& context, Reader& reader, Args&&... args)
106  {
107  static_assert(lexy::is_char_encoding<typename Reader::encoding>);
108  bp<Reader> impl{};
109  if (!impl.try_parse(context.control_block, reader))
110  {
111  // Report that we've failed.
113  auto err = lexy::error<Reader, tag>(impl.begin, impl.end);
114  context.on(_ev::error{}, err);
115 
116  // But recover immediately, as we wouldn't have consumed anything either way.
117  }
118 
119  context.on(_ev::backtracked{}, impl.begin, impl.end);
120  return NextParser::parse(context, reader, LEXY_FWD(args)...);
121  }
122  };
123 
124  template <typename Error>
125  static constexpr _look<Needle, End, Error> error = {};
126 };
127 
130 template <typename Needle, typename End>
131 constexpr auto lookahead(Needle _needle, End _end)
132 {
133  auto needle = literal_set() / _needle;
134  auto end = literal_set() / _end;
135  return _look<decltype(needle), decltype(end), void>{};
136 }
137 } // namespace lexyd
138 
139 #endif // LEXY_DSL_LOOKAHEAD_HPP_INCLUDED
140 
lexyd::_look::bp::end
Reader::iterator end
Definition: lookahead.hpp:56
LEXY_CONSTEVAL
#define LEXY_CONSTEVAL
Definition: config.hpp:98
lexyd::branch_base
Definition: grammar.hpp:20
literal.hpp
lexyd::_look::bp::try_parse
constexpr bool try_parse(const void *, Reader reader)
Definition: lookahead.hpp:58
lexyd::_look
Definition: lookahead.hpp:48
lexyd::_look::bp
Definition: lookahead.hpp:51
lexyd::_look::error
static constexpr _look< Needle, End, Error > error
Definition: lookahead.hpp:125
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
lexy
Definition: any_ref.hpp:12
cx::end
constexpr auto end(const C &c) -> decltype(c.end())
Definition: wildcards.hpp:686
lexyd::lookahead
constexpr auto lookahead(Needle _needle, End _end)
Definition: lookahead.hpp:131
lexyd::_look_trie
static constexpr auto _look_trie
Definition: lookahead.hpp:45
lexy::error
Generic failure.
Definition: error.hpp:14
lexyd::_look::bp::finish
LEXY_PARSER_FUNC bool finish(Context &context, Reader &reader, Args &&... args)
Definition: lookahead.hpp:94
lexy::parse_events::backtracked
Definition: dsl/base.hpp:63
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
lexyd::_look::p::parse
static LEXY_PARSER_FUNC bool parse(Context &context, Reader &reader, Args &&... args)
Definition: lookahead.hpp:105
lexy::parse_events::error
Definition: dsl/base.hpp:68
lexyd::_look::bp::cancel
constexpr void cancel(Context &context)
Definition: lookahead.hpp:88
lexyd::_look::p
Definition: lookahead.hpp:102
LEXY_PARSER_FUNC
#define LEXY_PARSER_FUNC
Definition: dsl/base.hpp:108
lexy::lookahead_failure
We've failed to match a lookahead.
Definition: lookahead.hpp:14
lexyd::_build_look_trie
LEXY_CONSTEVAL auto _build_look_trie(_lset< Needle... >, _lset< End... >)
Definition: lookahead.hpp:26
base.hpp
lexy::lookahead_failure::name
static LEXY_CONSTEVAL auto name()
Definition: lookahead.hpp:16
lexyd::_look::bp::begin
Reader::iterator begin
Definition: lookahead.hpp:53
lexy::_detail::lit_trie_matcher
Definition: literal.hpp:240
lexyd::literal_set
constexpr auto literal_set(Literals...)
Matches one of the specified literals.
Definition: literal.hpp:594
lexy::_detail::make_empty_trie
LEXY_CONSTEVAL auto make_empty_trie()
Definition: literal.hpp:199
lexyd
Definition: trace.hpp:22
lexyd::_lset
Definition: literal.hpp:518
lexyd::eof
constexpr auto eof
Matches EOF.
Definition: eof.hpp:72
error.hpp


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