Skip to content

Commit

Permalink
Invalid lines are now correctly skipped when parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Life4gal committed Sep 25, 2023
1 parent 24337cc commit 80102b3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ namespace
// begin with not '\r', '\n', '\r\n', whitespace or '='
dsl::peek(dsl::unicode::print - dsl::unicode::newline - dsl::unicode::blank - dsl::equal_sign),
// end until '='
dsl::peek(dsl::equal_sign)
// fixme: [invalid line] => [[empty_key], [this_line_content]]
dsl::peek(dsl::equal_sign / dsl::unicode::newline)
)(dsl::unicode::print.error<invalid_name>, string_literal::escape);
}
else
Expand Down Expand Up @@ -363,6 +364,9 @@ namespace
string_type value,
lexy::nullopt) -> void
{
// @see property_name::rule
if (key.empty()) { return; }

trim_right_whitespace(key);
trim_right_whitespace(value);
state.property(position, std::forward<string_type>(key), std::forward<string_type>(value), {});
Expand Down
38 changes: 37 additions & 1 deletion unit_test/src/test_extractor_generate_file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

#define GROUP3_NAME "inline comment"

#define GROUP4_NAME "invalid line"
#define GROUP5_NAME "key only"

inline auto do_generate_file() -> void
{
const std::filesystem::path file_path{TEST_INI_EXTRACTOR_FILE_PATH};
Expand Down Expand Up @@ -55,6 +58,18 @@ inline auto do_generate_file() -> void
file << " key4 = value4 ; kv4\n";
file << "\n";

file << '#' << GROUP4_NAME << '\n';
file << "[" GROUP4_NAME << "]\n";
file << " invalid line \n";
file << " =invalid line \n";
file << " = invalid line \n";

file << '#' << GROUP5_NAME << '\n';
file << "[" GROUP5_NAME << "]\n";
file << " key1= \n";
file << " key2 = \n";
file << "key3 = \n";

file.close();
}

Expand All @@ -65,13 +80,15 @@ auto do_check_result(const gal::ini::ExtractResult extract_result, const Context

"extract_ok"_test = [extract_result] { expect((extract_result == gal::ini::ExtractResult::SUCCESS) >> fatal); };

"group_size"_test = [&] { expect((data.size() == 3_i) >> fatal); };
"group_size"_test = [&] { expect((data.size() == 5_i) >> fatal); };

"group_name"_test = [&]
{
expect(data.contains(GROUP1_NAME) >> fatal);
expect(data.contains(GROUP2_NAME) >> fatal);
expect(data.contains(GROUP3_NAME) >> fatal);
expect(data.contains(GROUP4_NAME) >> fatal);
expect(data.contains(GROUP5_NAME) >> fatal);
};

"group1"_test = [&]
Expand Down Expand Up @@ -124,6 +141,25 @@ auto do_check_result(const gal::ini::ExtractResult extract_result, const Context
expect((group.at("key3") == "value3") >> fatal);
expect((group.at("key4") == "value4") >> fatal);
};

"group4"_test = [&]
{
const auto& group = data.at(GROUP4_NAME);

expect((group.empty() == "empty"_b) >> fatal);
expect((group.size() == 0_i) >> fatal);
};

"group5"_test = [&]
{
const auto& group = data.at(GROUP5_NAME);

expect((group.size() == 3_i) >> fatal);

expect(group.contains("key1") >> fatal);
expect(group.contains("key2") >> fatal);
expect(group.contains("key3") >> fatal);
};
}

template<typename ContextType>
Expand Down

0 comments on commit 80102b3

Please sign in to comment.