lookahead.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_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  typename Reader::iterator begin;
54  typename Reader::iterator end;
55 
56  constexpr bool try_parse(const void*, Reader reader)
57  {
58  begin = reader.position();
59 
60  auto result = [&] {
61  using matcher = lexy::_detail::lit_trie_matcher<
62  _look_trie<typename Reader::encoding, Needle, End>, 0>;
63 
64  while (true)
65  {
66  auto result = matcher::try_match(reader);
67  if (result == 0)
68  // We've found the needle.
69  return true;
70  else if (result == 1 || reader.peek() == Reader::encoding::eof())
71  // We've failed.
72  return false;
73  else
74  // Try again.
75  reader.bump();
76  }
77 
78  return false; // unreachable
79  }();
80 
81  end = reader.position();
82  return result;
83  }
84 
85  template <typename Context>
86  constexpr void cancel(Context& context)
87  {
88  context.on(_ev::backtracked{}, begin, end);
89  }
90 
91  template <typename NextParser, typename Context, typename... Args>
92  LEXY_PARSER_FUNC bool finish(Context& context, Reader& reader, Args&&... args)
93  {
94  context.on(_ev::backtracked{}, begin, end);
95  return NextParser::parse(context, reader, LEXY_FWD(args)...);
96  }
97  };
98 
99  template <typename NextParser>
100  struct p
101  {
102  template <typename Context, typename Reader, typename... Args>
103  LEXY_PARSER_FUNC static bool parse(Context& context, Reader& reader, Args&&... args)
104  {
105  bp<Reader> impl{};
106  if (!impl.try_parse(context.control_block, reader))
107  {
108  // Report that we've failed.
110  auto err = lexy::error<Reader, tag>(impl.begin, impl.end);
111  context.on(_ev::error{}, err);
112 
113  // But recover immediately, as we wouldn't have consumed anything either way.
114  }
115 
116  context.on(_ev::backtracked{}, impl.begin, impl.end);
117  return NextParser::parse(context, reader, LEXY_FWD(args)...);
118  }
119  };
120 
121  template <typename Error>
122  static constexpr _look<Needle, End, Error> error = {};
123 };
124 
127 template <typename Needle, typename End>
128 constexpr auto lookahead(Needle _needle, End _end)
129 {
130  auto needle = literal_set() / _needle;
131  auto end = literal_set() / _end;
132  return _look<decltype(needle), decltype(end), void>{};
133 }
134 } // namespace lexyd
135 
136 #endif // LEXY_DSL_LOOKAHEAD_HPP_INCLUDED
137 
lexyd::_look::bp::end
Reader::iterator end
Definition: lookahead.hpp:54
LEXY_CONSTEVAL
#define LEXY_CONSTEVAL
Definition: config.hpp:90
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:56
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:122
LEXY_FWD
#define LEXY_FWD(...)
Definition: config.hpp:22
lexy::_detail::type_or
std::conditional_t< std::is_void_v< T >, Fallback, T > type_or
Definition: config.hpp:56
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:128
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:92
lexy::parse_events::backtracked
Definition: dsl/base.hpp:50
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:103
lexy::parse_events::error
Definition: dsl/base.hpp:55
lexyd::_look::bp::cancel
constexpr void cancel(Context &context)
Definition: lookahead.hpp:86
lexyd::_look::p
Definition: lookahead.hpp:100
LEXY_PARSER_FUNC
#define LEXY_PARSER_FUNC
Definition: dsl/base.hpp:95
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:239
lexyd::literal_set
constexpr auto literal_set(Literals...)
Matches one of the specified literals.
Definition: literal.hpp:592
lexy::_detail::make_empty_trie
LEXY_CONSTEVAL auto make_empty_trie()
Definition: literal.hpp:198
lexyd
Definition: trace.hpp:22
lexyd::_lset
Definition: literal.hpp:516
lexyd::eof
constexpr auto eof
Matches EOF.
Definition: eof.hpp:72
error.hpp


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Mon Sep 16 2024 02:19:23