summaryrefslogtreecommitdiffhomepage
path: root/cpp.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'cpp.cpp')
-rw-r--r--cpp.cpp50
1 files changed, 31 insertions, 19 deletions
diff --git a/cpp.cpp b/cpp.cpp
index 6e8a28e..563ba4c 100644
--- a/cpp.cpp
+++ b/cpp.cpp
@@ -11,6 +11,7 @@
#include <gmock/gmock.h>
#include <functional>
+#include <optional>
#include <unordered_set>
#include <unordered_map>
#include <filesystem>
@@ -19,7 +20,16 @@ using namespace Gram;
namespace fs = std::filesystem;
-CPP::CPP(){}
+CPP::CPP(): map_translation_unit ({
+ {"/translation-unit/top-level-declaration-seq/top-level-declaration/declaration/function-definition",
+ [&](fs::path& path, index_t node_id)
+ {
+ //std::cout << "DEBUG: " << path << ", " << node_id << ", " << valueOfNode(node_id, m_nodes) << ", " << m_nodes[node_id].node_id << ", " << m_nodes[node_id].pos.node_id << std::endl;
+ }
+ },
+})
+{
+}
CPP::~CPP(){}
@@ -66,6 +76,8 @@ void CPP::concatenate_strings()
std::string CPP::valueOfNode(index_t node_index, const std::vector<TreeNode>& Tree)
{
std::string result;
+ std::optional<size_t> pos0;
+ index_t last_index;
std::vector<int32_t> todo(1, int32_t(node_index));
@@ -75,7 +87,10 @@ std::string CPP::valueOfNode(index_t node_index, const std::vector<TreeNode>& Tr
// visit node if token
if (ChildIdIsToken(current_index)) {
- result += m_code[TokenIdFromChildId(current_index)];
+ if (!pos0) {
+ pos0 = m_tokens[TokenIdFromChildId(current_index)].location.pos;
+ }
+ last_index = TokenIdFromChildId(current_index);
} else {
const TreeNode &node{Tree[current_index]};
@@ -87,7 +102,10 @@ std::string CPP::valueOfNode(index_t node_index, const std::vector<TreeNode>& Tr
}
}
- return result;
+ if (!pos0)
+ throw std::runtime_error("ICE: Node value not available");
+
+ return m_code.substr(*pos0, m_tokens[last_index].location.pos - *pos0) + m_tokens[last_index].value;
};
namespace {
@@ -203,16 +221,16 @@ std::vector<Token> CPP::tokens_from_pptokens(const std::vector<Token>& pp_tokens
if (pp_types.find(token.type) != pp_types.end()) {
if (token.type == "identifier") {
if (keywords.find(token.value) != keywords.end())
- result.emplace_back(Token{token.value, token.value});
+ result.emplace_back(Token{token.value, token.value, token.location});
else
- result.emplace_back(Token{"identifier"s, token.value});
+ result.emplace_back(Token{"identifier"s, token.value, token.location});
}
else if (token.type == "preprocessing-op-or-punc")
- result.emplace_back(Token{token.value, token.value});
+ result.emplace_back(Token{token.value, token.value, token.location});
else
- result.emplace_back(Token{"literal", token.value});
+ result.emplace_back(Token{"literal", token.value, token.location});
} else
- throw std::runtime_error("Unhandled preprocessing token: "s + token.value + " ("s + token.type + ")"s);
+ throw std::runtime_error("Unhandled preprocessing token: "s + token.toString());
}
return result;
}
@@ -227,14 +245,6 @@ std::vector<Gram::TreeNode> CPP::analysis(const std::vector<Token>& tokens)
return compiler.compile(tokens);
}
-namespace {
-
- CPP::map_type map_translation_unit {
- {"/translation-unit/top-level-declaration-seq/top-level-declaration/declaration/function-definition", [](){}},
- };
-
-} // anonymous namespace
-
void CPP::traverse(index_t node_id, map_type& map, fs::path parent_path)
{
fs::path current_path{parent_path / m_nodes[node_id].type};
@@ -242,7 +252,7 @@ void CPP::traverse(index_t node_id, map_type& map, fs::path parent_path)
// execute callbacks
auto it{map.find(current_path.generic_string())};
if (it != map.end()) {
- std::cout << "DEBUG: Found " << current_path << std::endl;
+ it->second(current_path, node_id);
}
// recurse tree
@@ -277,6 +287,8 @@ void CPP::link()
// phases of translation, according to standard
void CPP::compile(const std::string& code)
{
+ m_code = code;
+
source_charset_map(); // phase 1
backslash_escape(); // phase 2
@@ -289,8 +301,8 @@ void CPP::compile(const std::string& code)
concatenate_strings(); // phase 6
- auto tokens = tokens_from_pptokens(pp_tokens); // phase 7a
- m_nodes = analysis(tokens); // phase 7b
+ m_tokens = tokens_from_pptokens(pp_tokens); // phase 7a
+ m_nodes = analysis(m_tokens); // phase 7b
translate(); // phase 7c
instantiate(); // phase 8