Finishing the WlaDx label's parser

This commit is contained in:
Zoe Roux
2021-02-17 17:10:35 +01:00
parent f33593c51f
commit a53568bf5d
6 changed files with 94 additions and 21 deletions
+64 -8
View File
@@ -3,23 +3,79 @@
//
#include <regex>
#include "Utility/Utility.hpp"
#include "WlaDx.hpp"
namespace ComSquare::Debugger
{
std::vector<Label> WlaDx::parse(std::fstream symbolFile)
std::vector<Label> WlaDx::parse(std::ifstream &symbolFile)
{
std::vector<Label> labels;
std::vector<Label> ret;
std::map<std::string, Label> labels;
std::string line;
std::smatch match;
std::regex re(R"(\[(\S+)\])");
while (std::getline(symbolFile, line)) {
line = line.substr(0, line.rfind(';'));
std::smatch match;
std::regex re("\\[\\S+\\]");
while (symbolFile) {
if (line.empty()) {
std::getline(symbolFile, line);
line = WlaDx::_cleanLine(line);
}
if (!std::regex_match(line, match, re))
continue;
if (!std::regex_search(line, match, re)
if (match[1] == "labels")
line = WlaDx::_parseLabels(labels, symbolFile);
else if (match[1] == "definitions")
line = WlaDx::_parseDefinitions(labels, symbolFile);
}
return labels;
std::transform(labels.begin(), labels.end(), std::back_inserter(ret), [](auto &i){return i.second;});
return ret;
}
std::string WlaDx::_parseLabels(std::map<std::string, Label> &labels, std::ifstream &f)
{
std::string line;
std::smatch match;
std::regex re(R"(([0-9a-fA-F]{2}):([0-9a-fA-F]{4}) (.*))");
while (std::getline(f, line)) {
line = WlaDx::_cleanLine(line);
if (!std::regex_match(line, match, re))
return line;
labels[match[3]].name = match[3];
labels[match[3]].address = std::stoi(match[1], nullptr, 16) << 16
| std::stoi(match[2], nullptr, 16);
}
return "";
}
std::string WlaDx::_parseDefinitions(std::map<std::string, Label> &labels, std::ifstream &f)
{
std::string line;
std::smatch match;
std::regex re(R"(([0-9a-fA-F]{8}) (.*))");
std::string name;
while (std::getline(f, line)) {
line = WlaDx::_cleanLine(line);
if (!std::regex_match(line, match, re))
return line;
// TODO support other definitions.
if (!match[2].str().starts_with("_sizeof_"))
continue;
name = match[2].str().substr(std::string("_sizeof_").size());
labels[name].size = std::stoi(match[1], nullptr, 16);
}
return "";
}
std::string WlaDx::_cleanLine(std::string line)
{
line = line.substr(0, line.rfind(';'));
line.erase(0, line.find_first_not_of(Utility::WHITESPACES));
line.erase(line.find_last_not_of(Utility::WHITESPACES) + 1);
return line;
}
}
+18 -1
View File
@@ -12,6 +12,23 @@ namespace ComSquare::Debugger
//! @brief Class to parse WLA-DX symbol files.
class WlaDx {
public:
static std::vector<Label> parse(std::fstream symbolFile);
//! @brief Parse the whole file
//! @param symbolFile The file to parse (opened but not seeked)
//! @return The list of symbol found.
static std::vector<Label> parse(std::ifstream &symbolFile);
//! @brief Parse labels name & addresses (the [labels] section)
//! @param labels An list of labels. If one is already defined, the address will be set.
//! @param symbolFile The file to parse, opened and seeked to the line just after the [labels] line.
//! @return The line after the label section (the first line that doesn't match the section's regex).
static std::string _parseLabels(std::map<std::string, Label> &labels, std::ifstream &symbolFile);
//! @brief Parse definitions (and size of labels) (the [definitions] section)
//! @param labels An list of labels. If one is already defined, the size will be set.
//! @param symbolFile The file to parse, opened and seeked to the line just after the [definitions] line.
//! @return The line after the definitions section (the first line that doesn't match the section's regex).
static std::string _parseDefinitions(std::map<std::string, Label> &labels, std::ifstream &symbolFile);
//! @brief Remove comments, indentation & trailing spaces from a line.
//! @param line The line to clean
//! @return The line cleaned up.
static std::string _cleanLine(std::string line);
};
}