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

#292 Better handling for flow indicators in permitted scalar contexts #293

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
33 changes: 27 additions & 6 deletions include/fkYAML/detail/input/lexical_analyzer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -823,17 +823,38 @@ class lexical_analyzer
// Allow a space in an unquoted string only if the space is surrounded by non-space characters.
// See https://yaml.org/spec/1.2.2/#733-plain-style for more details.
current = m_input_handler.get_next();

// These characters are permitted when not inside a flow collection, and not inside an implicit key.
// TODO: Support detection of implicit key context for this check.
if (m_flow_context_depth > 0)
{
switch (current)
{
case '{':
case '}':
case '[':
case ']':
case ',':
return lexical_token_t::STRING_VALUE;
}
}

// " :" is permitted in a plain style string token, but not when followed by a space.
if (current == ':')
{
char_int_type next = m_input_handler.get_next();
m_input_handler.unget();
if (next == ' ')
{
return lexical_token_t::STRING_VALUE;
}
}

switch (current)
{
case ' ':
case '\r':
case '\n':
case '{':
case '}':
case '[':
case ']':
case ',':
case ':':
case '#':
case '\\':
return lexical_token_t::STRING_VALUE;
Expand Down
33 changes: 27 additions & 6 deletions single_include/fkYAML/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2982,17 +2982,38 @@ class lexical_analyzer
// Allow a space in an unquoted string only if the space is surrounded by non-space characters.
// See https://yaml.org/spec/1.2.2/#733-plain-style for more details.
current = m_input_handler.get_next();

// These characters are permitted when not inside a flow collection, and not inside an implicit key.
// TODO: Support detection of implicit key context for this check.
if (m_flow_context_depth > 0)
{
switch (current)
{
case '{':
case '}':
case '[':
case ']':
case ',':
return lexical_token_t::STRING_VALUE;
}
}

// " :" is permitted in a plain style string token, but not when followed by a space.
if (current == ':')
{
char_int_type next = m_input_handler.get_next();
m_input_handler.unget();
if (next == ' ')
{
return lexical_token_t::STRING_VALUE;
}
}

switch (current)
{
case ' ':
case '\r':
case '\n':
case '{':
case '}':
case '[':
case ']':
case ',':
case ':':
case '#':
case '\\':
return lexical_token_t::STRING_VALUE;
Expand Down
41 changes: 41 additions & 0 deletions test/unit_test/test_deserializer_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,47 @@ TEST_CASE("DeserializerClassTest_DeserializeBlockMappingTest", "[DeserializerCla
REQUIRE(root["Baz[123]"].get_value<double>() == 3.14);
}

SECTION("Flow indicators inside unquoted plain scalar values")
{
REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter("Foo: Bar, abc{abc")));
REQUIRE(root.is_mapping());
REQUIRE(root.size() == 1);
REQUIRE(root.contains("Foo"));
REQUIRE(root["Foo"].is_string());
REQUIRE(root["Foo"].get_value_ref<std::string&>() == "Bar, abc{abc");

REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter("Foo: Bar, abc}abc")));
REQUIRE(root.is_mapping());
REQUIRE(root.size() == 1);
REQUIRE(root.contains("Foo"));
REQUIRE(root["Foo"].is_string());
REQUIRE(root["Foo"].get_value_ref<std::string&>() == "Bar, abc}abc");

REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter("Foo: Bar, abc[abc")));
REQUIRE(root.is_mapping());
REQUIRE(root.size() == 1);
REQUIRE(root.contains("Foo"));
REQUIRE(root["Foo"].is_string());
REQUIRE(root["Foo"].get_value_ref<std::string&>() == "Bar, abc[abc");

REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter("Foo: Bar, abc]abc")));
REQUIRE(root.is_mapping());
REQUIRE(root.size() == 1);
REQUIRE(root.contains("Foo"));
REQUIRE(root["Foo"].is_string());
REQUIRE(root["Foo"].get_value_ref<std::string&>() == "Bar, abc]abc");

REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter("Foo: Bar, {[123] :3.14}")));
REQUIRE(root.is_mapping());
REQUIRE(root.size() == 1);
REQUIRE(root.contains("Foo"));
REQUIRE(root["Foo"].is_string());
REQUIRE(root["Foo"].get_value_ref<std::string&>() == "Bar, {[123] :3.14}");
REQUIRE_THROWS_AS(
root = deserializer.deserialize(fkyaml::detail::input_adapter("Foo: Bar, {[123] : 3.14}")),
fkyaml::parse_error);
}

SECTION("a comment right after a block mapping key.")
{
REQUIRE_NOTHROW(
Expand Down