biojppm / rapidyaml

Rapid YAML - a library to parse and emit YAML, and do it fast.
MIT License
583 stars 100 forks source link

How to get the correct col for a multiline string #402

Open mariusgreuel opened 9 months ago

mariusgreuel commented 9 months ago

Thanks for rapidyaml!

I have an embedded script in a YAML file, and I want to provide users with exact line/col information of possible syntax errors. I am having trouble getting the correct column for a multiline string.

For instance, for the print() statement in

script: |
  print()

I expect location.col to be 2.

This is how I do it, not sure if I use the correct API.

TEST(rapidyaml, fails)
{
    ryml::Parser parser{ ryml::ParserOptions().locations(true) };
    auto tree = parser.parse_in_arena("file", "script: |\n  print()");
    auto script = tree["script"];
    auto val = script.val();
    EXPECT_TRUE(val == "print()\n");
    auto location = parser.val_location(val.str);
    EXPECT_EQ(location.line, 1);
    EXPECT_EQ(location.col, 2);
}

however, the test fails with col == 0:

  location.col
    Which is: 0
  2

Any help would be greatly appreciated. TIA!