parse_tree_input.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_INPUT_PARSE_TREE_INPUT_HPP_INCLUDED
5 #define LEXY_INPUT_PARSE_TREE_INPUT_HPP_INCLUDED
6 
7 #include <lexy/error.hpp>
8 #include <lexy/grammar.hpp>
9 #include <lexy/input/base.hpp>
10 #include <lexy/lexeme.hpp>
11 
12 #if !LEXY_EXPERIMENTAL
13 # error "lexy::parse_tree_input is experimental"
14 #endif
15 
16 namespace lexy
17 {
18 template <typename Node>
20 
21 struct _parse_tree_eof // not real EOF, just no more siblings
22 {
23  template <typename Node>
24  friend constexpr bool operator==(const Node& node, _parse_tree_eof) noexcept
25  {
27  }
28  template <typename Node>
29  friend constexpr bool operator==(_parse_tree_eof, const Node& node) noexcept
30  {
32  }
33 
34  template <typename Node>
35  friend constexpr bool operator!=(const Node& node, _parse_tree_eof) noexcept
36  {
38  }
39  template <typename Node>
40  friend constexpr bool operator!=(_parse_tree_eof, const Node& node) noexcept
41  {
43  }
44 };
45 
46 template <typename Node>
48 {
50 
51 public:
52  using char_encoding = typename _traits::char_encoding;
54  using value_type = Node;
55 
56  static LEXY_CONSTEVAL auto eof()
57  {
58  return _parse_tree_eof{};
59  }
60 
61  template <typename NodeKind>
62  static bool match(const Node& node, const NodeKind& node_kind)
63  {
64  return _traits::has_kind(node, node_kind);
65  }
66 };
67 template <typename Node>
68 constexpr auto is_node_encoding<parse_tree_encoding<Node>> = true;
69 
70 template <typename Node>
71 class _ptr // parse tree reader
72 {
74 
75 public:
77  using iterator = typename _traits::iterator;
78 
79  struct marker
80  {
81  Node _parent = _traits::null();
82  Node _cur = _traits::null();
83 
84  constexpr iterator position() const noexcept
85  {
86  return _cur == _parse_tree_eof{} ? _traits::position_end(_parent)
87  : _traits::position_begin(_cur);
88  }
89  };
90 
91  constexpr explicit _ptr(const Node& root) noexcept
92  : _parent(root), _cur(_traits::first_child(root))
93  {}
94 
95  constexpr _ptr child_reader() const& noexcept
96  {
97  return _ptr(_cur);
98  }
99  constexpr auto lexeme_reader() const& noexcept
100  {
101  auto lexeme = _traits::lexeme(_cur);
102  return _range_reader<typename encoding::char_encoding>(lexeme.begin(), lexeme.end());
103  }
104 
105  constexpr const Node& peek() const noexcept
106  {
107  return _cur;
108  }
109 
110  constexpr void bump() noexcept
111  {
113  _cur = _traits::sibling(_cur);
114  }
115 
116  constexpr marker current() const noexcept
117  {
118  return {_parent, _cur};
119  }
120  constexpr void reset(marker m) noexcept
121  {
122  _cur = m._cur;
123  }
124 
125  constexpr iterator position() const noexcept
126  {
127  return current().position();
128  }
129 
130 private:
131  Node _parent;
132  Node _cur;
133 };
134 
135 template <typename Node>
137 {
138 public:
140  using value_type = Node;
141 
142  //=== constructors ===//
143  constexpr parse_tree_input() noexcept : _root(nullptr) {}
144 
145  constexpr explicit parse_tree_input(Node root) noexcept : _root(LEXY_MOV(root)) {}
146 
147  template <typename ParseTree, typename = std::enable_if_t<std::is_same_v<
148  Node, LEXY_DECAY_DECLTYPE(LEXY_DECLVAL(ParseTree).root())>>>
149  constexpr explicit parse_tree_input(const ParseTree& tree) noexcept : _root(tree.root())
150  {}
151 
152  //=== access ===//
153  constexpr const Node& root() const noexcept
154  {
155  return _root;
156  }
157 
158  //=== reader ===//
159  constexpr auto reader() const& noexcept
160  {
161  return _ptr<Node>(_root);
162  }
163 
164 private:
165  Node _root;
166 };
167 
168 template <typename ParseTree>
169 parse_tree_input(const ParseTree&)
170  -> parse_tree_input<LEXY_DECAY_DECLTYPE(LEXY_DECLVAL(ParseTree).root())>;
171 
172 //=== convenience typedefs ===//
173 template <typename Node>
175 
176 template <typename Tag, typename Node>
178 
179 template <typename Node>
181 } // namespace lexy
182 
183 #endif // LEXY_INPUT_PARSE_TREE_INPUT_HPP_INCLUDED
184 
LEXY_MOV
#define LEXY_MOV(...)
Definition: config.hpp:29
lexy::_ptr::child_reader
constexpr _ptr child_reader() const &noexcept
Definition: parse_tree_input.hpp:95
lexy::_parse_tree_eof::operator!=
constexpr friend bool operator!=(_parse_tree_eof, const Node &node) noexcept
Definition: parse_tree_input.hpp:40
LEXY_CONSTEVAL
#define LEXY_CONSTEVAL
Definition: config.hpp:98
lexy::parse_tree_encoding::eof
static LEXY_CONSTEVAL auto eof()
Definition: parse_tree_input.hpp:56
magic_enum::char_type
string_view::value_type char_type
Definition: magic_enum.hpp:145
lexy::parse_tree_input::reader
constexpr auto reader() const &noexcept
Definition: parse_tree_input.hpp:159
lexy::_ptr::peek
constexpr const Node & peek() const noexcept
Definition: parse_tree_input.hpp:105
lexy::_ptr::position
constexpr iterator position() const noexcept
Definition: parse_tree_input.hpp:125
lexy::parse_tree_encoding::value_type
Node value_type
Definition: parse_tree_input.hpp:54
lexy::lexeme::begin
constexpr iterator begin() const noexcept
Definition: lexeme.hpp:45
lexy::parse_tree_input_traits
Definition: parse_tree_input.hpp:19
lexy::error_context
Contains information about the context of an error, production is type-erased.
Definition: error.hpp:233
lexy::parse_tree_encoding
Definition: parse_tree_input.hpp:47
lexy
Definition: any_ref.hpp:12
LEXY_PRECONDITION
#define LEXY_PRECONDITION(Expr)
Definition: assert.hpp:36
grammar.hpp
lexy::_ptr::current
constexpr marker current() const noexcept
Definition: parse_tree_input.hpp:116
lexy::parse_tree_input
parse_tree_input(const ParseTree &) -> parse_tree_input< LEXY_DECAY_DECLTYPE(LEXY_DECLVAL(ParseTree).root())>
lexy::_ptr::bump
constexpr void bump() noexcept
Definition: parse_tree_input.hpp:110
lexy::error
Generic failure.
Definition: error.hpp:14
lexy::_parse_tree_eof::operator==
constexpr friend bool operator==(const Node &node, _parse_tree_eof) noexcept
Definition: parse_tree_input.hpp:24
lexy::_ptr::reset
constexpr void reset(marker m) noexcept
Definition: parse_tree_input.hpp:120
lexeme.hpp
lexy::parse_tree_input::parse_tree_input
constexpr parse_tree_input(const ParseTree &tree) noexcept
Definition: parse_tree_input.hpp:149
lexy::_ptr::marker
Definition: parse_tree_input.hpp:79
LEXY_DECAY_DECLTYPE
#define LEXY_DECAY_DECLTYPE(...)
Definition: config.hpp:34
lexy::_parse_tree_eof::operator==
constexpr friend bool operator==(_parse_tree_eof, const Node &node) noexcept
Definition: parse_tree_input.hpp:29
lexy::lexeme::end
constexpr iterator end() const noexcept
Definition: lexeme.hpp:49
lexy::_ptr::marker::position
constexpr iterator position() const noexcept
Definition: parse_tree_input.hpp:84
lexy::parse_tree_encoding::char_type
typename char_encoding::char_type char_type
Definition: parse_tree_input.hpp:53
lexy::parse_tree_input::root
constexpr const Node & root() const noexcept
Definition: parse_tree_input.hpp:153
lexy::parse_tree_input::value_type
Node value_type
Definition: parse_tree_input.hpp:140
lexy::parse_tree_input::_root
Node _root
Definition: parse_tree_input.hpp:165
lexy::_parse_tree_eof
Definition: parse_tree_input.hpp:21
lexy::parse_tree_input::parse_tree_input
constexpr parse_tree_input(Node root) noexcept
Definition: parse_tree_input.hpp:145
lexy::_ptr::iterator
typename _traits::iterator iterator
Definition: parse_tree_input.hpp:77
lexy::lexeme
Definition: lexeme.hpp:16
lexy::parse_tree_encoding::match
static bool match(const Node &node, const NodeKind &node_kind)
Definition: parse_tree_input.hpp:62
lexy::lexeme
lexeme(const Reader &, typename Reader::iterator) -> lexeme< typename Reader::canonical_reader >
lexy::parse_tree_encoding::char_encoding
typename _traits::char_encoding char_encoding
Definition: parse_tree_input.hpp:52
lexy::_ptr::marker::_parent
Node _parent
Definition: parse_tree_input.hpp:81
lexy::_ptr::lexeme_reader
constexpr auto lexeme_reader() const &noexcept
Definition: parse_tree_input.hpp:99
base.hpp
lexy::_ptr::_ptr
constexpr _ptr(const Node &root) noexcept
Definition: parse_tree_input.hpp:91
lexy::_parse_tree_eof::operator!=
constexpr friend bool operator!=(const Node &node, _parse_tree_eof) noexcept
Definition: parse_tree_input.hpp:35
lexy::parse_tree_input::parse_tree_input
constexpr parse_tree_input() noexcept
Definition: parse_tree_input.hpp:143
LEXY_DECLVAL
#define LEXY_DECLVAL(...)
Definition: config.hpp:32
lexy::parse_tree_input
Definition: parse_tree_input.hpp:136
lexy::_ptr::_cur
Node _cur
Definition: parse_tree_input.hpp:132
lexy::_ptr
Definition: parse_tree_input.hpp:71
lexy::_ptr::marker::_cur
Node _cur
Definition: parse_tree_input.hpp:82
detail::enable_if_t
typename std::enable_if< B, T >::type enable_if_t
Definition: json.hpp:3095
error.hpp
lexy::_ptr::_parent
Node _parent
Definition: parse_tree_input.hpp:131


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