Header lexy/input/parse_tree_input.hpp
Inputs that read a lexy::parse_tree
.
Encoding lexy::parse_tree_encoding
lexy/input/parse_tree_input.hpp
namespace lexy
{
template <typename Node>
struct parse_tree_encoding {};
}
The encoding of a lexy::parse_tree_input
.
It is a node encoding of the specified Node
type, which must be a lexy::parse_tree::node
.
Input lexy::parse_tree_input
lexy/input/parse_tree_input.hpp
namespace lexy
{
template <typename Node>
class parse_tree_input
{
public:
using encoding = parse_tree_encoding<Node>;
using value_type = Node;
//=== constructors ===//
constexpr parse_tree_input();
constexpr explicit parse_tree_input(const Node& node) noexcept;
template <typename ParseTree>
constexpr explicit parse_tree_input(const ParseTree& tree) noexcept;
//=== access ===//
constexpr const Node& root() const noexcept;
};
template <typename ParseTree>
parse_tree_input(const ParseTree&) -> parse_tree_input<typename ParseTree::node>;
}
The class parse_tree_input
uses a lexy::parse_tree::node
as an input.
It is a lightweight view and does not own the node.
It is used in combination with the lexy::dsl::tnode
and lexy::dsl::pnode
rules.
Example 1. Re-parse a parse tree
// A simple grammar for a single key-value pair.
namespace grammar
{
struct key
{
static constexpr auto rule = dsl::identifier(dsl::ascii::alnum);
};
struct integer
{
static constexpr auto rule = dsl::digits<>;
};
struct key_value_pair
{
static constexpr auto whitespace = dsl::ascii::space;
static constexpr auto rule = dsl::p<key> + dsl::lit_c<'='> + dsl::p<integer>;
};
} // namespace grammar
namespace tree_grammar
{
struct integer
{
// Match the digits token and extract their integer value.
static constexpr auto rule = dsl::tnode<lexy::digits_token_kind>(dsl::integer<int>);
static constexpr auto value = lexy::as_integer<int>;
};
struct key_value_pair
{
// Skip over literal and whitespace tokens automatically.
static constexpr auto whitespace
= dsl::tnode<lexy::literal_token_kind> | dsl::tnode<lexy::whitespace_token_kind>;
// Skip the key production but parse the children of the integer production.
static constexpr auto rule = [] {
auto key = dsl::pnode<grammar::key>;
auto value = dsl::pnode<grammar::integer>(dsl::p<integer>);
return key + value;
}();
static constexpr auto value = lexy::forward<int>;
};
} // namespace tree_grammar
int main()
{
auto input = lexy_ext::compiler_explorer_input();
// Parse the string into a tree.
lexy::parse_tree_for<lexy::buffer<lexy::utf8_encoding>> tree;
if (!lexy::parse_as_tree<grammar::key_value_pair>(tree, input, lexy_ext::report_error))
return 1;
// Parse the tree to extract the value (we ignore error reporting here since it's a bug for the
// parse tree grammar to fail).
auto result
= lexy::parse<tree_grammar::key_value_pair>(lexy::parse_tree_input(tree), lexy::noop);
if (!result)
return 2;
std::printf("Value: %d\n", result.value());
}
Constructors
lexy/input/parse_tree_input.hpp
constexpr parse_tree_input(); (1)
constexpr explicit parse_tree_input(const Node& node) noexcept; (2)
template <typename ParseTree>
constexpr explicit parse_tree_input(const ParseTree& tree) noexcept; (3)
Creates an empty input.
Creates an input that contains all descendants of
node
.Creates an input that contains all descendants of
tree.root()
.
Note | The input never contains node itself. |
Access
lexy/input/parse_tree_input.hpp
constexpr const Node& root() const noexcept;
Returns the root node of the input, passed to the constructor.
Typedefs
lexy/input/parse_tree_input.hpp
namespace lexy
{
template <typename Node>
using parse_tree_lexeme = lexeme_for<parse_tree_input<Node>>;
template <typename Tag, typename Node>
using parse_tree_error = error_for<parse_tree_input<Node>, Tag>;
template <typename Node>
using parse_tree_error_context = error_context_for<parse_tree_input<Node>>;
}
Convenience typedefs for parse tree inputs.