mirror of
https://github.com/mariiaan/minipp.git
synced 2026-05-14 10:11:17 +02:00
Improve error logging
This commit is contained in:
@@ -332,7 +332,8 @@ namespace minipp
|
|||||||
#define PP_COUT(msg)
|
#define PP_COUT(msg)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define PP_COUT_SYNTAX_ERROR(line, msg) PP_COUT("Error in line " << line << ": " << msg)
|
#define PP_COUT_SYNTAX_ERROR_LINE(line, msg) PP_COUT(line << ": " << msg)
|
||||||
|
#define PP_COUT_SYNTAX_ERROR(msg) PP_COUT("Syntax error: " << msg)
|
||||||
|
|
||||||
std::unique_ptr<minipp::MiniPPFile::Value> minipp::MiniPPFile::Value::ParseValue(std::string value, EResult* result)
|
std::unique_ptr<minipp::MiniPPFile::Value> minipp::MiniPPFile::Value::ParseValue(std::string value, EResult* result)
|
||||||
{
|
{
|
||||||
@@ -404,7 +405,7 @@ minipp::EResult minipp::MiniPPFile::Values::StringValue::Parse(const std::string
|
|||||||
{
|
{
|
||||||
if (i + 1 >= str.size())
|
if (i + 1 >= str.size())
|
||||||
{
|
{
|
||||||
PP_COUT("Syntax error: '\\' at end of string");
|
PP_COUT_SYNTAX_ERROR("'\\' at end of string");
|
||||||
return EResult::BadEscapeSequence;
|
return EResult::BadEscapeSequence;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -426,7 +427,7 @@ minipp::EResult minipp::MiniPPFile::Values::StringValue::Parse(const std::string
|
|||||||
m_value.push_back('\\');
|
m_value.push_back('\\');
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
PP_COUT("Syntax error: Unknown escape sequence '\\" << str[i + 1] << "'");
|
PP_COUT_SYNTAX_ERROR("Unknown escape sequence '\\" << str[i + 1] << "'");
|
||||||
return EResult::UnknownEscapeSequence;
|
return EResult::UnknownEscapeSequence;
|
||||||
}
|
}
|
||||||
++i;
|
++i;
|
||||||
@@ -482,7 +483,7 @@ minipp::EResult minipp::MiniPPFile::Values::IntValue::Parse(const std::string& s
|
|||||||
Tools::RemoveAll(sanitizedValue, '_');
|
Tools::RemoveAll(sanitizedValue, '_');
|
||||||
if (sanitizedValue.empty())
|
if (sanitizedValue.empty())
|
||||||
{
|
{
|
||||||
PP_COUT("Empty string for integer value.");
|
PP_COUT_SYNTAX_ERROR("Empty string for integer value.");
|
||||||
return EResult::FormatError;
|
return EResult::FormatError;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -504,7 +505,7 @@ minipp::EResult minipp::MiniPPFile::Values::IntValue::Parse(const std::string& s
|
|||||||
{
|
{
|
||||||
if (!Tools::IsIntegerDecimal(sanitizedValue))
|
if (!Tools::IsIntegerDecimal(sanitizedValue))
|
||||||
{
|
{
|
||||||
PP_COUT("Invalid decimal integer value: " << sanitizedValue);
|
PP_COUT_SYNTAX_ERROR("Invalid decimal integer value: " << sanitizedValue);
|
||||||
return EResult::IntegerValueInvalid;
|
return EResult::IntegerValueInvalid;
|
||||||
}
|
}
|
||||||
m_value = std::stoll(sanitizedValue);
|
m_value = std::stoll(sanitizedValue);
|
||||||
@@ -513,12 +514,12 @@ minipp::EResult minipp::MiniPPFile::Values::IntValue::Parse(const std::string& s
|
|||||||
}
|
}
|
||||||
catch (const std::invalid_argument&)
|
catch (const std::invalid_argument&)
|
||||||
{
|
{
|
||||||
PP_COUT("Invalid integer value: " << sanitizedValue);
|
PP_COUT_SYNTAX_ERROR("Invalid integer value: " << sanitizedValue);
|
||||||
return EResult::IntegerValueInvalid;
|
return EResult::IntegerValueInvalid;
|
||||||
}
|
}
|
||||||
catch (const std::out_of_range&)
|
catch (const std::out_of_range&)
|
||||||
{
|
{
|
||||||
PP_COUT("Integer value out of range: " << sanitizedValue);
|
PP_COUT_SYNTAX_ERROR("Integer value out of range: " << sanitizedValue);
|
||||||
return EResult::IntegerValueOutOfRange;
|
return EResult::IntegerValueOutOfRange;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -558,7 +559,7 @@ minipp::EResult minipp::MiniPPFile::Values::IntValue::ToString(std::string& dest
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
PP_COUT("Invalid integer style.");
|
PP_COUT_SYNTAX_ERROR("Invalid integer style.");
|
||||||
return EResult::IntegerStyleInvalid;
|
return EResult::IntegerStyleInvalid;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -573,7 +574,7 @@ minipp::EResult minipp::MiniPPFile::Values::BooleanValue::Parse(const std::strin
|
|||||||
m_value = false;
|
m_value = false;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
PP_COUT("Invalid boolean value: " << str << " (may only contain lowercase true and false)");
|
PP_COUT_SYNTAX_ERROR("Invalid boolean value: " << str << " (may only contain lowercase true and false)");
|
||||||
return EResult::BooleanValueInvalid;
|
return EResult::BooleanValueInvalid;
|
||||||
}
|
}
|
||||||
return EResult::Success;
|
return EResult::Success;
|
||||||
@@ -593,7 +594,7 @@ minipp::EResult minipp::MiniPPFile::Values::FloatValue::Parse(const std::string&
|
|||||||
}
|
}
|
||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
PP_COUT("Invalid float value: " << str);
|
PP_COUT_SYNTAX_ERROR("Invalid float value: " << str);
|
||||||
return EResult::FloatValueInvalid;
|
return EResult::FloatValueInvalid;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -616,7 +617,7 @@ minipp::EResult minipp::MiniPPFile::Values::ArrayValue::Parse(const std::string&
|
|||||||
{
|
{
|
||||||
if (str.front() != '[' || str.back() != ']')
|
if (str.front() != '[' || str.back() != ']')
|
||||||
{
|
{
|
||||||
PP_COUT("Array value must be enclosed in [] brackets.");
|
PP_COUT_SYNTAX_ERROR("Array value must be enclosed in [] brackets.");
|
||||||
return EResult::FormatError;
|
return EResult::FormatError;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -636,7 +637,7 @@ minipp::EResult minipp::MiniPPFile::Values::ArrayValue::Parse(const std::string&
|
|||||||
{
|
{
|
||||||
if (i + 1 >= str.size())
|
if (i + 1 >= str.size())
|
||||||
{
|
{
|
||||||
PP_COUT("Syntax error: Bad escape sequence: '\\' at end of string");
|
PP_COUT_SYNTAX_ERROR("Bad escape sequence: '\\' at end of string");
|
||||||
return EResult::BadEscapeSequence;
|
return EResult::BadEscapeSequence;
|
||||||
}
|
}
|
||||||
currentElement += c;
|
currentElement += c;
|
||||||
@@ -670,7 +671,7 @@ minipp::EResult minipp::MiniPPFile::Values::ArrayValue::Parse(const std::string&
|
|||||||
--bracketCounter;
|
--bracketCounter;
|
||||||
if (bracketCounter < 0)
|
if (bracketCounter < 0)
|
||||||
{
|
{
|
||||||
PP_COUT("Array brackets are not balanced. (One ] too much or encountered too early)");
|
PP_COUT_SYNTAX_ERROR("Array brackets are not balanced. (One ] too much or encountered too early)");
|
||||||
return EResult::ArrayBracketsInbalanced;
|
return EResult::ArrayBracketsInbalanced;
|
||||||
}
|
}
|
||||||
else if (bracketCounter >= 1)
|
else if (bracketCounter >= 1)
|
||||||
@@ -688,7 +689,7 @@ minipp::EResult minipp::MiniPPFile::Values::ArrayValue::Parse(const std::string&
|
|||||||
|
|
||||||
if (bracketCounter != 0) // will always be a positive value because negative values are caught earlier
|
if (bracketCounter != 0) // will always be a positive value because negative values are caught earlier
|
||||||
{
|
{
|
||||||
PP_COUT("Array brackets are not balanced. (Missing " << bracketCounter << " closing brackets)");
|
PP_COUT_SYNTAX_ERROR("Array brackets are not balanced. (Missing " << bracketCounter << " closing brackets)");
|
||||||
return EResult::ArrayBracketsInbalanced;
|
return EResult::ArrayBracketsInbalanced;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -774,7 +775,7 @@ minipp::EResult minipp::MiniPPFile::Section::GetSubSection(const std::string& ke
|
|||||||
auto it = m_subSections.find(thisKey);
|
auto it = m_subSections.find(thisKey);
|
||||||
if (it == m_subSections.end())
|
if (it == m_subSections.end())
|
||||||
{
|
{
|
||||||
PP_COUT("Sub-Section not found: " << thisKey);
|
PP_COUT_SYNTAX_ERROR("Sub-Section not found: " << thisKey);
|
||||||
return EResult::SectionNotPresent;
|
return EResult::SectionNotPresent;
|
||||||
}
|
}
|
||||||
if (rest.empty())
|
if (rest.empty())
|
||||||
@@ -808,7 +809,7 @@ minipp::EResult minipp::MiniPPFile::WriteSection(const Section* section, std::of
|
|||||||
{
|
{
|
||||||
if (!Tools::IsNameValid(pair.first))
|
if (!Tools::IsNameValid(pair.first))
|
||||||
{
|
{
|
||||||
PP_COUT("Invalid name for key: " << pair.first);
|
PP_COUT_SYNTAX_ERROR("Invalid name for key: " << pair.first);
|
||||||
return EResult::InvalidName;
|
return EResult::InvalidName;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -831,7 +832,7 @@ minipp::EResult minipp::MiniPPFile::WriteSection(const Section* section, std::of
|
|||||||
{
|
{
|
||||||
if (!Tools::IsNameValid(pair.first))
|
if (!Tools::IsNameValid(pair.first))
|
||||||
{
|
{
|
||||||
PP_COUT("Invalid name for section: " << pair.first);
|
PP_COUT_SYNTAX_ERROR("Invalid name for section: " << pair.first);
|
||||||
return EResult::InvalidName;
|
return EResult::InvalidName;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -849,6 +850,7 @@ minipp::EResult minipp::MiniPPFile::WriteSection(const Section* section, std::of
|
|||||||
|
|
||||||
minipp::EResult minipp::MiniPPFile::Parse(const std::string& path, bool additional) noexcept
|
minipp::EResult minipp::MiniPPFile::Parse(const std::string& path, bool additional) noexcept
|
||||||
{
|
{
|
||||||
|
#define PP_COUT_HERE() PP_COUT_SYNTAX_ERROR_LINE(lineCounter, currentLine << " <- HERE");
|
||||||
if (!additional)
|
if (!additional)
|
||||||
{
|
{
|
||||||
m_rootSection.m_comments.clear();
|
m_rootSection.m_comments.clear();
|
||||||
@@ -886,14 +888,16 @@ minipp::EResult minipp::MiniPPFile::Parse(const std::string& path, bool addition
|
|||||||
{
|
{
|
||||||
if (lastChar != ']')
|
if (lastChar != ']')
|
||||||
{
|
{
|
||||||
PP_COUT_SYNTAX_ERROR(lineCounter, "Expected ']' at the end of the line.");
|
PP_COUT_SYNTAX_ERROR("Expected ']' at the end of the line.");
|
||||||
|
PP_COUT_HERE();
|
||||||
return EResult::SectionExpectedClosingBracket;
|
return EResult::SectionExpectedClosingBracket;
|
||||||
}
|
}
|
||||||
std::string sectionPathStr = currentLine.substr(1, currentLine.size() - 2);
|
std::string sectionPathStr = currentLine.substr(1, currentLine.size() - 2);
|
||||||
Tools::StringTrim(sectionPathStr);
|
Tools::StringTrim(sectionPathStr);
|
||||||
if (sectionPathStr.empty())
|
if (sectionPathStr.empty())
|
||||||
{
|
{
|
||||||
PP_COUT_SYNTAX_ERROR(lineCounter, "Expected section path. Found empty section begin notation.");
|
PP_COUT_SYNTAX_ERROR("Expected section path. Found empty section begin notation.");
|
||||||
|
PP_COUT_HERE();
|
||||||
return EResult::EmptySectionName;
|
return EResult::EmptySectionName;
|
||||||
}
|
}
|
||||||
// Create section tree
|
// Create section tree
|
||||||
@@ -906,14 +910,16 @@ minipp::EResult minipp::MiniPPFile::Parse(const std::string& path, bool addition
|
|||||||
const std::string& sectionName = sectionPath[i];
|
const std::string& sectionName = sectionPath[i];
|
||||||
if (!Tools::IsNameValid(sectionName))
|
if (!Tools::IsNameValid(sectionName))
|
||||||
{
|
{
|
||||||
PP_COUT_SYNTAX_ERROR(lineCounter, "Invalid section name. (\"" << sectionName << "\") May only contain [a - z][A - Z][0 - 9] and _.");
|
PP_COUT_SYNTAX_ERROR("Invalid section name. (\"" << sectionName << "\") May only contain [a - z][A - Z][0 - 9] and _.");
|
||||||
|
PP_COUT_HERE();
|
||||||
return EResult::InvalidName;
|
return EResult::InvalidName;
|
||||||
}
|
}
|
||||||
|
|
||||||
result = ubSection->SetSubSection(sectionName, std::make_unique<Section>(), false);
|
result = ubSection->SetSubSection(sectionName, std::make_unique<Section>(), false);
|
||||||
if (result == EResult::SectionAlreadyPresent && i == sectionPath.size() - 1)
|
if (result == EResult::SectionAlreadyPresent && i == sectionPath.size() - 1)
|
||||||
{
|
{
|
||||||
PP_COUT_SYNTAX_ERROR(lineCounter, "All (sub-) sections may only be defined once.");
|
PP_COUT_SYNTAX_ERROR("All (sub-) sections may only be defined once.");
|
||||||
|
PP_COUT_HERE();
|
||||||
return EResult::SectionAlreadyPresent;
|
return EResult::SectionAlreadyPresent;
|
||||||
}
|
}
|
||||||
ubSection->GetSubSection(sectionName, &ubSection);
|
ubSection->GetSubSection(sectionName, &ubSection);
|
||||||
@@ -925,14 +931,16 @@ minipp::EResult minipp::MiniPPFile::Parse(const std::string& path, bool addition
|
|||||||
}
|
}
|
||||||
if (currentSection == nullptr)
|
if (currentSection == nullptr)
|
||||||
{
|
{
|
||||||
PP_COUT_SYNTAX_ERROR(lineCounter, "Expected section begin before key-value pair.");
|
PP_COUT_SYNTAX_ERROR("Expected section begin before key-value pair.");
|
||||||
|
PP_COUT_HERE();
|
||||||
return EResult::KeyValuePairNotInSection;
|
return EResult::KeyValuePairNotInSection;
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t keyValueDelimiterIndex = Tools::FirstIndexOf(currentLine, '=');
|
int64_t keyValueDelimiterIndex = Tools::FirstIndexOf(currentLine, '=');
|
||||||
if (keyValueDelimiterIndex == -1)
|
if (keyValueDelimiterIndex == -1)
|
||||||
{
|
{
|
||||||
PP_COUT_SYNTAX_ERROR(lineCounter, "Expected '=' in line.");
|
PP_COUT_SYNTAX_ERROR("Expected '=' in line.");
|
||||||
|
PP_COUT_HERE();
|
||||||
return EResult::ExpectedKeyValuePair;
|
return EResult::ExpectedKeyValuePair;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -942,25 +950,29 @@ minipp::EResult minipp::MiniPPFile::Parse(const std::string& path, bool addition
|
|||||||
|
|
||||||
if (keyValuePair.first.empty())
|
if (keyValuePair.first.empty())
|
||||||
{
|
{
|
||||||
PP_COUT_SYNTAX_ERROR(lineCounter, "Expected key in line.");
|
PP_COUT_SYNTAX_ERROR("Expected key in line.");
|
||||||
|
PP_COUT_HERE();
|
||||||
return EResult::KeyEmpty;
|
return EResult::KeyEmpty;
|
||||||
}
|
}
|
||||||
if (!Tools::IsNameValid(keyValuePair.first))
|
if (!Tools::IsNameValid(keyValuePair.first))
|
||||||
{
|
{
|
||||||
PP_COUT_SYNTAX_ERROR(lineCounter, "Invalid key name. (\"" << keyValuePair.first << "\") May only contain [a - z][A - Z][0 - 9] and _.");
|
PP_COUT_SYNTAX_ERROR("Invalid key name. (\"" << keyValuePair.first << "\") May only contain [a - z][A - Z][0 - 9] and _.");
|
||||||
|
PP_COUT_HERE();
|
||||||
return EResult::InvalidName;
|
return EResult::InvalidName;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (keyValuePair.second.empty())
|
if (keyValuePair.second.empty())
|
||||||
{
|
{
|
||||||
PP_COUT_SYNTAX_ERROR(lineCounter, "Empty keys are not allowed");
|
PP_COUT_SYNTAX_ERROR( "Empty keys are not allowed");
|
||||||
|
PP_COUT_HERE();
|
||||||
return EResult::ValueEmpty;
|
return EResult::ValueEmpty;
|
||||||
}
|
}
|
||||||
auto parsedValue = Value::ParseValue(keyValuePair.second);
|
EResult parseResult;
|
||||||
|
auto parsedValue = Value::ParseValue(keyValuePair.second, &parseResult);
|
||||||
if (parsedValue == nullptr)
|
if (parsedValue == nullptr)
|
||||||
{
|
{
|
||||||
PP_COUT_SYNTAX_ERROR(lineCounter, "Invalid value");
|
PP_COUT_HERE();
|
||||||
return EResult::InvalidName;
|
return parseResult;
|
||||||
}
|
}
|
||||||
parsedValue->m_comments = commentBuffer;
|
parsedValue->m_comments = commentBuffer;
|
||||||
commentBuffer.clear();
|
commentBuffer.clear();
|
||||||
@@ -968,7 +980,8 @@ minipp::EResult minipp::MiniPPFile::Parse(const std::string& path, bool addition
|
|||||||
auto valueSetResult = currentSection->SetValue(keyValuePair.first, std::move(parsedValue), false);
|
auto valueSetResult = currentSection->SetValue(keyValuePair.first, std::move(parsedValue), false);
|
||||||
if (valueSetResult != EResult::Success)
|
if (valueSetResult != EResult::Success)
|
||||||
{
|
{
|
||||||
PP_COUT_SYNTAX_ERROR(lineCounter, "Key already present: " << keyValuePair.first);
|
PP_COUT_SYNTAX_ERROR("Key already present: " << keyValuePair.first);
|
||||||
|
PP_COUT_HERE();
|
||||||
return valueSetResult;
|
return valueSetResult;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ int main()
|
|||||||
EResult result;
|
EResult result;
|
||||||
|
|
||||||
MiniPPFile file;
|
MiniPPFile file;
|
||||||
result = file.Parse("simple.mini");
|
result = file.Parse("test.mini");
|
||||||
|
|
||||||
auto& root = file.GetRoot();
|
auto& root = file.GetRoot();
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[game]
|
[game]
|
||||||
name = "Test Game\nNext Line"
|
name = "Test Game\nNext Line"
|
||||||
version = "1.0.0"
|
ver sion = "1.0.0"
|
||||||
year = 2025
|
year = 2025
|
||||||
completionPercentage = 50.0f
|
completionPercentage = 50.0f
|
||||||
# Should only be true if completionPercentage is 100
|
# Should only be true if completionPercentage is 100
|
||||||
|
|||||||
Reference in New Issue
Block a user