summaryrefslogtreecommitdiffhomepage
path: root/grammer.h
diff options
context:
space:
mode:
Diffstat (limited to 'grammer.h')
-rw-r--r--grammer.h27
1 files changed, 19 insertions, 8 deletions
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<int32_t> child_ids; // < 0: terminal: token_id; >= 0: non-terminal: node_id; fill token by token
+ std::vector<int32_t> 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<TreeNode> 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<std::string, size_t> m_min; // cache