From 5467147d9470ee294ddab938098c3ef172222066 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Thu, 26 Mar 2020 22:20:27 +0100 Subject: Prepared new Node Tree (WIP) --- grammer.h | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) (limited to 'grammer.h') diff --git a/grammer.h b/grammer.h index d70f675..39e6884 100644 --- a/grammer.h +++ b/grammer.h @@ -11,23 +11,27 @@ class GrammerTest; namespace Gram { +struct NodePosition { + index_t node_id{}; // 0-based + index_t child_pos{}; // 0-based +}; + // TreeNodes are only intermediate. Terminal symbols don't get of TreeNodes // token_id: index into token list // node_id: index into tree node list struct TreeNode { - index_t parent_node_id{}; // root if parent_node_id == node_id + NodePosition pos; // position of this node in tree index_t node_id{}; std::string type; index_t variant; // bnf[type][variant] - std::vector child_ids; // < 0: terminal: token_id; >= 0: non-terminal: node_id; fill token by token + std::vector child_ids; // < 0: terminal: token_id; > 0: non-terminal: node_id; = 0: unset }; class Compiler { private: // The result - index_t root_node_id{}; std::vector nodes; // Input @@ -44,11 +48,18 @@ private: // Node specific std::string GetTypeOfNode(index_t node_id) const; - bool IsRootNode(index_t node_id) const; - bool rootIsStartSymbol() const; - bool treeIsComplete() const; - void Validate() const; - index_t AddNode(const std::string& child_type, index_t parent_index); + index_t AddNode(const std::string& type, index_t variant, NodePosition pos = {}); + void RemoveNode(); + // Adds Node and Removes it on scope exit (RAII) + class AddNodeGuard { + Compiler& m_compiler; + public: + AddNodeGuard(Compiler& compiler, const std::string& type, index_t variant, NodePosition pos); + ~AddNodeGuard(); + }; + void IncNodePosition(NodePosition& pos); + + NodePosition begin_pos; // top-down algorithm std::unordered_map m_min; // cache -- cgit v1.2.3