summaryrefslogtreecommitdiffhomepage
path: root/grammer.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'grammer.cpp')
-rw-r--r--grammer.cpp19
1 files changed, 10 insertions, 9 deletions
diff --git a/grammer.cpp b/grammer.cpp
index 8243fa8..be01adc 100644
--- a/grammer.cpp
+++ b/grammer.cpp
@@ -170,8 +170,8 @@ void Compiler::AddFirstNode()
root_node_id = 0;
const std::string& child_type = tokens[0].type;
- auto it = ReverseBNF.find(child_type);
- if (it == ReverseBNF.end())
+ auto it = reversedFirst.find(child_type);
+ if (it == reversedFirst.end())
throw std::runtime_error("Illegal first token: "s + child_type + " ("s + tokens[0].value + ")"s);
std::set<std::string>& alternatives_set {it->second};
@@ -212,8 +212,8 @@ bool Compiler::AddRootNode()
AddFirstNode();
} else {
const std::string& child_type = nodes[root_node_id].type; // starting at old root
- auto it = ReverseBNF.find(child_type);
- if (it == ReverseBNF.end()) // this one doesn't have a parent, maybe a start symbol to discard?
+ auto it = reversedFirst.find(child_type);
+ if (it == reversedFirst.end()) // this one doesn't have a parent, maybe a start symbol to discard?
return false;
index_t old_root_node_id {root_node_id};
@@ -260,6 +260,7 @@ void Compiler::removeTokensUpTo(index_t token_id)
removeTokensUpTo(token_id, root_node_id);
}
+// operate on node_id
void Compiler::removeTokensUpTo(index_t token_id, index_t node_id)
{
// token_id should be the new tokens_used
@@ -279,7 +280,7 @@ void Compiler::removeTokensUpTo(index_t token_id, index_t node_id)
}
// recurse from back, to remove tokens from end
- for (auto i = child_ids.size() - 1; token_id < tokens_used && i >= 0; i--) {
+ for (int i = child_ids.size() - 1; token_id < tokens_used && i >= 0; i--) {
if (!ChildIdIsToken(child_ids[i])) {
removeTokensUpTo(token_id, child_ids[i]);
}
@@ -376,9 +377,9 @@ std::map<std::string, std::string> Compiler::traverse(const std::string& lower,
auto it {visited.find(current_node)};
if (it == visited.end()) { // not visited, yet: visit now
- auto parents_it {ReverseBNF.find(current_node)};
+ auto parents_it {reversedFirst.find(current_node)};
- if (parents_it != ReverseBNF.end()) {
+ if (parents_it != reversedFirst.end()) {
auto& parents {parents_it->second};
visited[current_node] = current_child;
@@ -441,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
- Debug("AddNode(): "s + parent.type + "->"s + child_type + ": "s + std::to_string(index));
+ Debug("AddNode(): "s + nodes[parent_index].type + "->"s + child_type + ": "s + std::to_string(index));
DumpTree();
return index;
@@ -490,7 +491,7 @@ bool Compiler::FillTree()
return true;
}
-Compiler::Compiler(BNF& bnf, const std::string& Top): bnf(bnf), Top(Top), ReverseBNF{Reverse(bnf)}
+Compiler::Compiler(BNF& bnf, const std::string& Top): bnf(bnf), Top(Top), ReverseBNF{Reverse(bnf)}, reversedFirst{reverseFirst(bnf)}
{
}