Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/fix multifile parsing #16

Merged
merged 2 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions include/borr/language.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ namespace borr {
static constexpr string_view TRANSLATION_REGEX = R"(^[A-z_][A-z0-9_]+(\[\])?[\s]+?=[\s]+?"([^"]+)?"$)"; //!< The regex used to find translations in a file

public: // +++ Static +++
static void fromFile(const fs::directory_entry&, language&); //!< Load a language from disk
static void fromString(const string&, language&); //!< Load a pre-loaded language file from memory
static language fromFile(const fs::directory_entry&); //!< Load a language from disk
static language fromString(const string&); //!< Load a pre-loaded language file from memory

public: // +++ Constructor / Destructor +++
// language(const language&) = default; //!< Default copy ctor
language(language&&) = default; //!< Default move ctor
~ language() = default; //!< Default dtor

public: // +++ Getters +++
Expand All @@ -176,7 +176,7 @@ namespace borr {
static void removeVarExpansionCallback(const string& varName); //!< Removes the variable expander for a given variable name

public: // +++ Constructor ++
language(); //!< Protected default ctor
language(); //!< Protected default ctor

protected: // +++ Actual Parsing +++
virtual bool isEmptyOrComment(const string&) const; //!< Determines whether or not the current line is empty or a comment so it can be ignored.
Expand Down Expand Up @@ -205,12 +205,13 @@ namespace borr {
static varcbacklist_t _defaultExpandersList; //!< The list of default expanders provided by the lib

private:
dict_t m_translationDict; //!< The translation dictionary containing sections and translations
dict_t m_translationDict{}; //!< The translation dictionary containing sections and translations

langversion m_langVer; //!< The language file's version
langversion m_langVer{}; //!< The language file's version

string m_langId; //!< The language's ID (region_COUNTRY)
string m_langDescription; //!< The language's description
string m_currentSection{}; //!< The current section the parser is at
string m_langId{}; //!< The language's ID (region_COUNTRY)
string m_langDescription{}; //!< The language's description

private: // +++ Static +++
static varcbacklist_t _callbackList; //!< The list of callbacks for variable expansion
Expand Down
31 changes: 16 additions & 15 deletions src/language.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ namespace borr {
* @brief Parses a borrfile and ensures its contents are parsed into the given object reference.
*
* @param file The file to parse.
* @param outLang A reference to the language instance into which the file should be parsed.
*/
void language::fromFile(const fs::directory_entry& file, language& outLang) {
language language::fromFile(const fs::directory_entry& file) {
if (!file.exists() || !file.is_regular_file()) {
throw fs::filesystem_error("Invalid file path given!", file.path(), error_code(EINVAL, std::generic_category()));
}
Expand All @@ -72,18 +71,18 @@ namespace borr {

fContents << inStream.rdbuf();

fromString(fContents.str(), outLang);
return fromString(fContents.str());
}

/**
* @brief Parses a string object into a language instance.
*
* @param fContents The string (file contents) to parse.
* @param outLang A reference to the language object into which the string should be parsed.
*
* @throws runtime_error If an error occurred. TODO: Custom exceptions.
*/
void language::fromString(const string& fContents, language& outLang) {
language language::fromString(const string& fContents) {
language outLang{};
vector<string> tokens;
if (!extensions::splitString(fContents, "\n", tokens)) {
throw std::runtime_error("Failed to split input string! Are newlines missing?");
Expand All @@ -94,15 +93,14 @@ namespace borr {
for (const auto& line : tokens) {
outLang.parseLine(line);
}

return outLang;
}

/**
* @brief Default constructor.
*/
language::language():
m_langDescription({}), m_langId({}),
m_langVer(), m_translationDict({})
{ }
language::language() { }

/**
* @brief Allows a user to add custom variable expansion callbacks.
Expand Down Expand Up @@ -319,6 +317,7 @@ namespace borr {
m_langDescription = {};
m_langId = {};
m_langVer = {};
m_currentSection = {};
m_translationDict.clear();
}

Expand All @@ -333,32 +332,34 @@ namespace borr {
// now search for inline comments
const auto commentlessLine = removeInlineComments(line);

static string currentSection{};
if (isSection(commentlessLine, currentSection)) { return; } // nothing more to do here
if (isSection(commentlessLine, m_currentSection)) { return; } // nothing more to do here

string field;
string translation;
if (!isTranslation(commentlessLine, field, translation)) { return; } // nothing more to do here

if (currentSection.empty()) {
if (m_currentSection.empty()) {
if (field == LANG_DESC_FIELD) {
printf("Found LANG_DESC_FIELD\n");
m_langDescription = translation;
return;
} else if (field == LANG_ID_FIELD) {
printf("Found LANG_ID_FIELD\n");
m_langId = translation;
return;
} else if (field == LANG_VER_FIELD) {
printf("Found LANG_VER_FIELD\n");
langversion::fromString(translation, m_langVer);
return;
}
return;
}

if (!getSection(currentSection).has_value()) {
m_translationDict.emplace(currentSection, sect_t{});
if (!getSection(m_currentSection).has_value()) {
m_translationDict.emplace(m_currentSection, sect_t{});
}

auto& section = m_translationDict[currentSection];
auto& section = m_translationDict[m_currentSection];

const auto trimmedField = extensions::trim(field, "[]");

Expand Down
Loading