summaryrefslogtreecommitdiffhomepage
path: root/grammer.cpp
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2020-02-16 16:33:19 +0100
committerRoland Reichwein <mail@reichwein.it>2020-02-16 16:33:19 +0100
commit2f4118526972f7f3d5147342bc65909fcc82b6c7 (patch)
tree3514b840da299d7203ce1bbcbf81d36be1108260 /grammer.cpp
parent0aaa6440a23e1dedf4a907fa46f979ea9d248e99 (diff)
clang++-9, integrate cppbnf
Diffstat (limited to 'grammer.cpp')
-rw-r--r--grammer.cpp59
1 files changed, 33 insertions, 26 deletions
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<std::pair<int32_t, size_t>> todo{std::pair<int32_t, size_t>{static_cast<int32_t>(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<int32_t, size_t>{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<int32_t>{}});
//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<std::string>& 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);