Skip to content

Commit

Permalink
[fix] re #361: fix parse of scalars with : and starting on the next line
Browse files Browse the repository at this point in the history
  • Loading branch information
biojppm committed Jun 6, 2023
1 parent 0e864cb commit 91f6890
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 7 deletions.
10 changes: 10 additions & 0 deletions changelog/current.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@

### Fixes

- Fix [#361](https://github.com/biojppm/rapidyaml/pull/361) - parse error on map scalars containing `:` and starting on the next line:
```yaml
---
# failed to parse:
description:
foo:bar
---
# but this was ok:
description: foo:bar
```
- [PR#368](https://github.com/biojppm/rapidyaml/pull/368) - fix pedantic compiler warnings.
- Fix [#373](https://github.com/biojppm/rapidyaml/issues/373) - false parse error with empty quoted keys in block-style map ([PR#374](https://github.com/biojppm/rapidyaml/pull/374)).
- Fix [#356](https://github.com/biojppm/rapidyaml/issues/356) - fix overzealous check in `emit_as()`. An id may be larger than the tree's size, eg when nodes were removed.
Expand Down
24 changes: 19 additions & 5 deletions src/c4/yml/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2784,20 +2784,34 @@ bool Parser::_scan_scalar_unk(csubstr *C4_RESTRICT scalar, bool *C4_RESTRICT quo
}
size_t pos = s.find(" #");
if(pos != npos)
{
_c4dbgpf("RUNK: found ' #' at {}", pos);
s = s.left_of(pos);
}
pos = s.find(": ");
if(pos != npos)
{
_c4dbgpf("RUNK: found ': ' at {}", pos);
s = s.left_of(pos);
}
else if(s.ends_with(':'))
{
_c4dbgp("RUNK: ends with ':'");
s = s.left_of(s.len-1);
}
_RYML_WITH_TAB_TOKENS(
else if((pos = s.find(":\t")) != npos) // TABS
{
_c4dbgp("RUNK: ends with ':\\t'");
s = s.left_of(pos);
)
})
else
{
_c4dbgp("RUNK: trimming left of ,");
s = s.left_of(s.first_of(','));
}
s = s.trim(" \t");
_c4dbgpf("RUNK: scalar='{}'", s);
_c4dbgpf("RUNK: scalar=[{}]~~~{}~~~", s.len, s);

if(s.empty())
return false;
Expand All @@ -2808,11 +2822,11 @@ bool Parser::_scan_scalar_unk(csubstr *C4_RESTRICT scalar, bool *C4_RESTRICT quo

if(_at_line_end() && s != '~')
{
_c4dbgpf("at line end. curr='{}'", s);
_c4dbgpf("at line end. curr=[{}]~~~{}~~", s.len, s);
s = _extend_scanned_scalar(s);
}

_c4dbgpf("scalar was '{}'", s);
_c4dbgpf("scalar was [{}]~~~{}~~~", s.len, s);

*scalar = s;
*quoted = false;
Expand Down Expand Up @@ -4046,7 +4060,7 @@ bool Parser::_handle_indentation()
_RYML_CB_ASSERT(m_stack.m_callbacks, ind > m_state->indref);
if(has_all(RMAP|RVAL))
{
if(_is_scalar_next__rmap_val(remt) && remt.first_of(":?") == npos)
if(_is_scalar_next__rmap_val(remt) && (!remt.first_of_any(": ", "? ")) && (!remt.ends_with(":")))
{
_c4dbgpf("actually it seems a value: '{}'", remt);
}
Expand Down
56 changes: 54 additions & 2 deletions test/test_simple_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,59 @@
namespace c4 {
namespace yml {

TEST(simple_map, issue361)
{
{
const Tree t = parse_in_arena(R"(desc: foo bar)");
ASSERT_EQ(t["desc"].val(), "foo bar");
}
//
{
const Tree t = parse_in_arena(R"(desc:
foo bar)");
ASSERT_EQ(t["desc"].val(), "foo bar");
}
//
{
const Tree t = parse_in_arena(R"(desc: foo:bar)");
ASSERT_EQ(t["desc"].val(), "foo:bar");
}
//
{
const Tree t = parse_in_arena(R"(desc:
foo:bar)");
ASSERT_EQ(t["desc"].val(), "foo:bar");
}
//
{
const Tree t = parse_in_arena(R"(desc: Timecode in "HH:MM:SS:FF" format where the last ':' can be replaced by ',' or ';'.)");
ASSERT_EQ(t["desc"].val(), "Timecode in \"HH:MM:SS:FF\" format where the last ':' can be replaced by ',' or ';'.");
}
//
{
const Tree t = parse_in_arena(R"(desc:
Timecode in "HH:MM:SS:FF" format where the last ':' can be replaced by ',' or ';'.)");
ASSERT_EQ(t["desc"].val(), "Timecode in \"HH:MM:SS:FF\" format where the last ':' can be replaced by ',' or ';'.");
}
}

TEST(simple_map, issue377)
{
csubstr yaml = R"(
const game::perception::PerceiveData:
PerceiveData: {default: nullptr}
)";
test_check_emit_check(yaml, [](Tree const &t){
ASSERT_TRUE(t.rootref().has_child("const game::perception::PerceiveData"));
const ConstNodeRef cpd = t["const game::perception::PerceiveData"];
ASSERT_TRUE(cpd.is_map());
ASSERT_TRUE(cpd.has_child("PerceiveData"));
ASSERT_EQ(cpd["PerceiveData"].num_children(), 1u);
ASSERT_TRUE(cpd["PerceiveData"].has_child("default"));
ASSERT_EQ(cpd["PerceiveData"]["default"].val(), "nullptr");
});
}

TEST(simple_map, issue373)
{
{
Expand Down Expand Up @@ -833,8 +886,7 @@ h3 ,i3: val3 ,0003
})",
L{ // this is crazy...
N(KEYVAL, "a0", /*"~"*/{}),
N("b0", "val0"),
N(KEYVAL, "0000 c0", /*"~"*/{}),
N("b0", "val0"), N(KEYVAL, "0000 c0", /*"~"*/{}),
N("d0", "val0"), N(KEYVAL, "0000 e0", /*"~"*/{}),
N("f0", "val0"), N(KEYVAL, "0000 h0", /*"~"*/{}),
N("i0", "val0"), N(KEYVAL, "0000 a1", /*"~"*/{}),
Expand Down

0 comments on commit 91f6890

Please sign in to comment.