follow.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_FOLLOW_HPP_INCLUDED
5 #define LEXY_DSL_FOLLOW_HPP_INCLUDED
6 
7 #include <lexy/dsl/base.hpp>
9 #include <lexy/dsl/literal.hpp>
10 
11 namespace lexy
12 {
14 {
15  static LEXY_CONSTEVAL auto name()
16  {
17  return "follow restriction";
18  }
19 };
20 } // namespace lexy
21 
22 namespace lexyd
23 {
24 template <typename Literal, typename CharClass>
25 struct _nf : token_base<_nf<Literal, CharClass>>, _lit_base
26 {
27  static constexpr auto lit_max_char_count = Literal::lit_max_char_count;
28 
30 
31  using lit_case_folding = typename Literal::lit_case_folding;
32 
33  template <typename Encoding>
34  static constexpr auto lit_first_char() -> typename Encoding::char_type
35  {
36  return Literal::template lit_first_char<Encoding>();
37  }
38 
39  template <typename Trie>
40  static LEXY_CONSTEVAL std::size_t lit_insert(Trie& trie, std::size_t pos,
41  std::size_t char_class)
42  {
43  auto end = Literal::lit_insert(trie, pos, char_class);
44  trie.node_char_class[end] = char_class;
45  return end;
46  }
47 
48  template <typename Reader>
49  struct tp
50  {
52  typename Reader::marker end;
54 
55  constexpr explicit tp(const Reader& reader)
56  : impl(reader), end(reader.current()), literal_success(false)
57  {}
58 
59  constexpr bool try_parse(Reader reader)
60  {
61  literal_success = false;
62 
63  // Need to match the literal.
64  if (!impl.try_parse(reader))
65  return false;
66  end = impl.end;
67  literal_success = true;
68 
69  // To match, we must not match the char class now.
70  reader.reset(end);
71  if constexpr (std::is_void_v<lit_case_folding>)
72  {
73  return !lexy::try_match_token(CharClass{}, reader);
74  }
75  else
76  {
77  typename lit_case_folding::template reader<Reader> case_folded{reader};
78  return !lexy::try_match_token(CharClass{}, case_folded);
79  }
80  }
81 
82  template <typename Context>
83  constexpr void report_error(Context& context, Reader reader)
84  {
85  if (!literal_success)
86  {
87  impl.report_error(context, reader);
88  }
89  else
90  {
91  auto err
92  = lexy::error<Reader, lexy::follow_restriction>(end.position(), end.position());
93  context.on(_ev::error{}, err);
94  }
95  }
96  };
97 };
98 
100 template <typename Literal, typename CharClass>
101 constexpr auto not_followed_by(Literal, CharClass cc)
102 {
103  static_assert(lexy::is_literal_rule<Literal> && Literal::lit_char_classes.size == 0);
104  return _nf<Literal, decltype(_make_char_class(cc))>{};
105 }
106 
108 template <typename Literal, typename CharClass>
109 constexpr auto followed_by(Literal lit, CharClass cc)
110 {
111  return not_followed_by(lit, -cc);
112 }
113 } // namespace lexyd
114 
115 namespace lexy
116 {
117 template <typename Literal, typename CharClass>
118 constexpr auto token_kind_of<lexy::dsl::_nf<Literal, CharClass>> = lexy::literal_token_kind;
119 } // namespace lexy
120 
121 #endif // LEXY_DSL_FOLLOW_HPP_INCLUDED
122 
cx::size
constexpr auto size(const C &c) -> decltype(c.size())
Definition: wildcards.hpp:636
LEXY_CONSTEVAL
#define LEXY_CONSTEVAL
Definition: config.hpp:98
literal.hpp
magic_enum::char_type
string_view::value_type char_type
Definition: magic_enum.hpp:145
lexy::_detail::char_class_list
Definition: literal.hpp:154
lexyd::_nf::tp::report_error
constexpr void report_error(Context &context, Reader reader)
Definition: follow.hpp:83
lexyd::_nf::lit_case_folding
typename Literal::lit_case_folding lit_case_folding
Definition: follow.hpp:31
lexyd::_nf::lit_first_char
static constexpr auto lit_first_char() -> typename Encoding::char_type
Definition: follow.hpp:34
lexy
Definition: any_ref.hpp:12
lexy::literal_token_kind
@ literal_token_kind
Definition: grammar.hpp:90
cx::end
constexpr auto end(const C &c) -> decltype(c.end())
Definition: wildcards.hpp:686
char_class.hpp
lexyd::_nf::lit_char_classes
static constexpr auto lit_char_classes
Definition: follow.hpp:29
lexy::follow_restriction::name
static LEXY_CONSTEVAL auto name()
Definition: follow.hpp:15
lexy::error
Generic failure.
Definition: error.hpp:14
lexy::parse_events::error
Definition: dsl/base.hpp:68
lexy::try_match_token
constexpr LEXY_FORCE_INLINE auto try_match_token(TokenRule, Reader &reader)
Definition: dsl/base.hpp:245
lexyd::not_followed_by
constexpr auto not_followed_by(Literal, CharClass cc)
Match a literal but only if not followed by the given char class.
Definition: follow.hpp:101
lexyd::token_base
Definition: dsl/token.hpp:42
lexyd::_nf::tp::impl
lexy::token_parser_for< Literal, Reader > impl
Definition: follow.hpp:51
lexy::follow_restriction
Definition: follow.hpp:13
lexyd::_nf::tp::tp
constexpr tp(const Reader &reader)
Definition: follow.hpp:55
lexyd::_nf::lit_max_char_count
static constexpr auto lit_max_char_count
Definition: follow.hpp:27
base.hpp
lexyd::_nf::tp::literal_success
bool literal_success
Definition: follow.hpp:53
lexyd::_nf::tp
Definition: follow.hpp:49
lexyd::_nf
Definition: follow.hpp:25
lexyd::followed_by
constexpr auto followed_by(Literal lit, CharClass cc)
Match a literal but only if followed by the given char class.
Definition: follow.hpp:109
lexyd::_nf::tp::end
Reader::marker end
Definition: follow.hpp:52
lexyd::_nf::tp::try_parse
constexpr bool try_parse(Reader reader)
Definition: follow.hpp:59
lexyd::_make_char_class
constexpr auto _make_char_class(C c)
Definition: char_class.hpp:360
lexyd::_nf::lit_insert
static LEXY_CONSTEVAL std::size_t lit_insert(Trie &trie, std::size_t pos, std::size_t char_class)
Definition: follow.hpp:40
lexy::token_parser_for
typename TokenRule::template tp< Reader > token_parser_for
Definition: dsl/base.hpp:242
lexyd::_lit_base
Definition: grammar.hpp:32
lexyd
Definition: trace.hpp:22


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