mirror of
https://github.com/fish-shell/fish-shell.git
synced 2025-02-16 19:32:46 +08:00
Introduce tnode
This commit is contained in:
parent
cf116e4880
commit
3d4dd4abef
|
@ -82,6 +82,9 @@ template <class T0, class... Ts>
|
|||
struct seq {
|
||||
static constexpr production_t<1 + sizeof...(Ts)> production = {
|
||||
{element<T0>(), element<Ts>()..., token_type_invalid}};
|
||||
|
||||
using type_tuple = std::tuple<T0, Ts...>;
|
||||
|
||||
static const production_element_t *resolve(const parse_token_t &, const parse_token_t &,
|
||||
parse_node_tag_t *) {
|
||||
return production_for<seq>();
|
||||
|
@ -103,7 +106,7 @@ struct alternative {
|
|||
};
|
||||
|
||||
// Following are the grammar productions.
|
||||
#define BODY(T)
|
||||
#define BODY(T) static constexpr parse_token_type_t symbol = symbol_##T;
|
||||
#define DEF(T) struct T : public
|
||||
|
||||
#define DEF_ALT(T) struct T : public alternative
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
|
||||
#include "common.h"
|
||||
#include "parse_constants.h"
|
||||
#include "parse_grammar.h"
|
||||
#include "tokenizer.h"
|
||||
|
||||
class parse_node_tree_t;
|
||||
|
@ -227,6 +228,44 @@ class parse_node_tree_t : public std::vector<parse_node_t> {
|
|||
bool job_should_be_backgrounded(const parse_node_t &job) const;
|
||||
};
|
||||
|
||||
/// A helper for type-safe manipulation of parse nodes.
|
||||
/// This is a lightweight value-type class.
|
||||
template <typename Type>
|
||||
class tnode_t {
|
||||
/// The tree containing our node.
|
||||
const parse_node_tree_t *tree = nullptr;
|
||||
|
||||
/// The node in the tree
|
||||
const parse_node_t *nodeptr = nullptr;
|
||||
|
||||
// Helper to get a child type at a given index.
|
||||
template <class Element, uint32_t Index>
|
||||
using child_at = typename std::tuple_element<Index, typename Element::type_tuple>::type;
|
||||
|
||||
public:
|
||||
tnode_t() = default;
|
||||
|
||||
tnode_t(const parse_node_tree_t *t, const parse_node_t *n) : tree(t), nodeptr(n) {
|
||||
assert(t && "tree cannot be null in this constructor");
|
||||
assert((!n || n->type == Type::symbol) && "node has wrong type");
|
||||
}
|
||||
|
||||
/// Return the underlying (type-erased) node.
|
||||
const parse_node_t *node() const { return nodeptr; }
|
||||
|
||||
/// Check whether we're populated.
|
||||
explicit operator bool() const { return nodeptr != nullptr; }
|
||||
|
||||
/// Type-safe access to a child at the given index.
|
||||
template <node_offset_t Index>
|
||||
tnode_t<child_at<Type, Index>> child() const {
|
||||
using child_type = child_at<Type, Index>;
|
||||
const parse_node_t *child = nullptr;
|
||||
if (nodeptr) child = tree->get_child(*nodeptr, Index, child_type::symbol);
|
||||
return tnode_t<child_type>{tree, child};
|
||||
}
|
||||
};
|
||||
|
||||
/// The big entry point. Parse a string, attempting to produce a tree for the given goal type.
|
||||
bool parse_tree_from_string(const wcstring &str, parse_tree_flags_t flags,
|
||||
parse_node_tree_t *output, parse_error_list_t *errors,
|
||||
|
|
Loading…
Reference in New Issue
Block a user