I am using SnakeYAML to parse documents whose string scalars have additional internal structure specific to my application. When processing those strings, I may need to show errors to the user. I'd like to describe error locations in detail, with start line/column and possibly end line/column. However, those source coordinates need to make sense in the original YAML document. For example, suppose "x" is not allowed in items[0] here:
items:
- aaabbcxddd
Relative to the string value itself, the disallowed "x" appears on line 0, column 6. (I'll use 0-based coordinates throughout this message.) However, as embedded in the complete YAML document, the disallowed "x" appears on line 1, column 10. When reporting the problem to the YAML document's author, I want to use these coordinates: problem on line 1, column 10.
Naïvely, this feels easy enough: (1) determine the starting coordinates of the string scalar, then add those to any coordinates that are relative to the scalar itself. However, YAML's many syntaxes for scalar strings make this much harder in general. I'd need to deal with perturbations of coordinates due to comments, multiple forms of quoting, multiple forms of folding, etc.
In the general case, it seems that I need a mapping of source coordinates from parsed scalar values back to their original YAML locations. Such a mapping would need to faithfully represent all of the transformations that can happen as we go from the original YAML to the information that YAML represents. For example:
multiline:
once
upon
a
time
The parsed value of this string scalar is "once upon a time" (without quotes). A precise source mapping would let me know that the location of letter "t" on line 0, column 12 within "once upon a time" maps back to line 4, column 2 in the source YAML.
Can SnakeYAML provide source mappings with this level of detail? Is this something that SnakeYAML already provides? If not, is it feasible for me to build such a mapping using existing SnakeYAML APIs? Or is this requirement fundamentally outside the bounds of what SnakeYAML can do?
Thanks for any hints,
Ben