From 2f4118526972f7f3d5147342bc65909fcc82b6c7 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sun, 16 Feb 2020 16:33:19 +0100 Subject: clang++-9, integrate cppbnf --- grammer.cpp | 59 +++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 33 insertions(+), 26 deletions(-) (limited to 'grammer.cpp') diff --git a/grammer.cpp b/grammer.cpp index 3c7671b..808c49e 100644 --- a/grammer.cpp +++ b/grammer.cpp @@ -4,7 +4,16 @@ using namespace Gram; -void Compiler::clear() { +static bool debug{false}; + +void Debug(std::string s) +{ + if (debug) + std::cout << s << std::endl; +} + +void Compiler::clear() +{ nodes.clear(); root_node_id = 0; @@ -71,52 +80,51 @@ namespace { void Compiler::DumpTree() { - std::cout << "= Dump =======================================" << std::endl; - std::cout << "nodes.size()=" << nodes.size() << std::endl; + Debug("= Dump ======================================="); + Debug("nodes.size()="s + std::to_string(nodes.size())); if (nodes.size() > 0) { if (0) { - std::cout << "--- Nodes ------------------------------------" << std::endl; - std::cout << "root_node_id=" << root_node_id << std::endl; + Debug("--- Nodes ------------------------------------"); + Debug("root_node_id="s + std::to_string(root_node_id)); for (const auto& node : nodes) { - std::cout << node.type << "(" << node.node_id << "):"; + std::string line{"("s + std::to_string(node.node_id) + "):"s}; for (const auto& child : node.child_ids) { - std::cout << " "; + line += " "s; if (ChildIdIsToken(child)) - std::cout << "t" << TokenIdFromChildId(child); + line += "t"s + std::to_string(TokenIdFromChildId(child)); else - std::cout << child; + line += std::to_string(child); } - std::cout << std::endl; + Debug(line); } } - std::cout << "--- Tree -------------------------------------" << std::endl; + Debug("--- Tree -------------------------------------"); std::deque> todo{std::pair{static_cast(root_node_id), 0}}; // id, indent while (!todo.empty()) { auto [current_index, indent] {todo.front()}; todo.pop_front(); - std::cout << std::string(indent, ' '); + std::string line(indent, ' '); if (ChildIdIsToken(current_index)) { index_t token_id {TokenIdFromChildId(current_index)}; - std::cout << "Token(" << token_id << "): " << tokens[token_id].type << "(" << tokens[token_id].value << ")"; + line += "Token("s + std::to_string(token_id) + "): "s + tokens[token_id].type + "("s + tokens[token_id].value + ")"s; } else { auto& node {nodes[current_index]}; - std::cout << "Node(" << current_index << "): " << node.type << "/" << node.variant; + line += "Node("s + std::to_string(current_index) + "): "s + node.type + "/" + std::to_string(node.variant); auto child_ids{node.child_ids}; for (int i = 0; i < child_ids.size(); i++) { todo.insert(todo.begin() + i, std::pair{child_ids[i], indent + 1}); } if (node.alternatives.size()) { - std::cout << ", " << node.alternatives.size() << " alternatives available"; + line += ", "s + std::to_string(node.alternatives.size()) + " alternatives available"s; } } - std::cout << std::endl; - + Debug(line); } } - std::cout << "==============================================" << std::endl; + Debug("=============================================="); } bool Compiler::AllTokensUsed() const @@ -240,7 +248,7 @@ bool Compiler::AddRootNode() return false; // now add! - std::cout << "AddRootNode(): Adding " << node_type << std::endl; + Debug("AddRootNode(): Adding "s + node_type); nodes[old_root_node_id].parent_node_id = new_root_node_id; root_node_id = new_root_node_id; nodes.emplace_back(TreeNode{root_node_id, root_node_id, node_type, node_variant, alternatives, child_ids}); @@ -258,7 +266,6 @@ void Compiler::removeTokensUpTo(index_t token_id) void Compiler::removeTokensUpTo(index_t token_id, index_t node_id) { - // std::cout << "DEBUG " << token_id << " " << tokens_used << std::endl; // token_id should be the new tokens_used if (token_id < tokens_used) { @@ -267,7 +274,7 @@ void Compiler::removeTokensUpTo(index_t token_id, index_t node_id) // remove relevant tokens from end while (token_id < tokens_used && child_ids.size() && ChildIdIsToken(child_ids.back()) && TokenIdFromChildId(child_ids.back()) >= token_id) { - std::cout << "Removing token " << TokenIdFromChildId(child_ids.back()) << std::endl; + Debug("Removing token "s + std::to_string(TokenIdFromChildId(child_ids.back()))); child_ids.pop_back(); if (tokens_used > 0) tokens_used--; @@ -301,7 +308,7 @@ void Compiler::RemoveLastNode() nodes[node.child_ids[0]].parent_node_id = node.child_ids[0]; root_node_id = node.child_ids[0]; } - std::cout << "Removing root node " << nodes.back().type << "(" << nodes.back().node_id << ")" << std::endl; + Debug("Removing root node "s + nodes.back().type + "("s + std::to_string(nodes.back().node_id) + ")"s); nodes.pop_back(); } else { DumpTree(); @@ -313,7 +320,7 @@ void Compiler::RemoveLastNode() if (parent.child_ids.empty() || parent.child_ids.back() != node_id) throw std::runtime_error("ICE: Backtrack: Bad child nodes"); parent.child_ids.pop_back(); - std::cout << "Removing " << nodes.back().type << "(" << nodes.back().node_id << ")" << std::endl; + Debug("Removing "s + nodes.back().type + "("s + std::to_string(nodes.back().node_id) + ")"s); nodes.pop_back(); } else if (ChildIdIsToken(node.child_ids.back())) { removeTokensUpTo(TokenIdFromChildId(node.child_ids.back())); @@ -435,7 +442,7 @@ index_t Compiler::AddNode(const std::string& child_type, index_t parent_index) nodes.emplace_back(TreeNode{parent_index, index, child_type, variant, alternatives, std::vector{}}); //root stays, tokens_used stays - std::cout << "AddNode(): " << parent.type << "->" << child_type << ": "<< index << std::endl; + Debug("AddNode(): "s + parent.type + "->"s + child_type + ": "s + std::to_string(index)); DumpTree(); return index; @@ -450,7 +457,7 @@ void Compiler::AddPath(const std::vector& path, index_t current_ind nodes.back().child_ids.emplace_back(ChildIdFromTokenId(tokens_used)); tokens_used++; - std::cout << "AddPath(): token " << tokens.back().type << std::endl; + Debug("AddPath(): token "s + tokens.back().type); DumpTree(); } @@ -470,7 +477,7 @@ bool Compiler::FillTree() if (next_child == tokens[tokens_used].type) { // add token directly node.child_ids.push_back(ChildIdFromTokenId(tokens_used)); tokens_used++; - std::cout << "tokens_used++: " << tokens_used << std::endl; + Debug("tokens_used++: "s + std::to_string(tokens_used)); DumpTree(); } else { // add inner nodes auto list = GetPath(next_child, tokens[tokens_used].type); -- cgit v1.2.3