rapidyaml icon indicating copy to clipboard operation
rapidyaml copied to clipboard

multiline assignment will make the line number is not correctly

Open CairoLee opened this issue 2 years ago • 1 comments

Description

When yaml include | mark for multiline assignment, the line number is not correctly

Example Code

ryml::Parser parser;

ryml::Tree tree = parser.parse_in_arena("source.yml", R"(Body:
  - Id: 1
    Name: Apple
    Script: |
      Line One
      Line Two
  - Id: 2
    Name: Cat
    Script: |
      Line One
      Line Two
  - Id: 3
    Name: Dog
    Script: |
      Line One
      Line Two)");

ryml::Location loc = parser.location(tree.rootref());
loc = parser.location(tree["Body"][2]["Name"]);

sprintf("loc.line = %d\n", loc.line + 1);

Expected results

loc.line = 13

Current results

loc.line = 15

CairoLee avatar May 20 '22 16:05 CairoLee

Bump~ the project rathena already using rapidyaml to parse the yaml file. in the project, a lot of data using | to write the item script, incorrectly line numbers can make debugging data errors very difficult

CairoLee avatar Jun 05 '22 09:06 CairoLee

@CairoLee I finally had some time to look at the problem. I'm sorry to say the situation is not good.

This is happening because all multiline scalars (ie all of plain, single-quoted, double-quoted, block-literal or block-folded) have their newline characters filtered during the parsing phase. As currently written, the location data structure is only filled after parsing, which means that by the time this happens, the original newline characters are no longer there, so those lines fail to count for the location. So the reported line is shifted down in some cases and up in other cases, on every multiline scalar. Which means that the shift will compound as the file progresses.

Fixing this will be hard. It will consist of ensuring that after being filtered, each scalar should have the same number of newlines in its original source region. So if newlines were removed, they must be added after the end of the filtered scalar, but before the next YAML token. For all the multiline scalar types.

Expect more time before this gets fixed.

biojppm avatar Sep 08 '22 23:09 biojppm

@CairoLee This is now fixed in master. Beware that the fix changes are breaking; you will need to change your code to instantiate the parser with options setting it to track locations. For an example, see the notes added to the changelog or the quickstart file in #307

biojppm avatar Sep 27 '22 10:09 biojppm

@biojppm Thank you for following up on this issue, I noticed what needs to be adjusted.

I will use the latest code and test it later and give you feedback, thanks again.

CairoLee avatar Sep 27 '22 12:09 CairoLee