flyx / NimYAML

YAML implementation for Nim
https://nimyaml.org
Other
186 stars 36 forks source link

Comment after block/multi-line text #106

Closed theamarin closed 2 years ago

theamarin commented 2 years ago

Hi, thanks to @flyx for your great work! I just found that the following yaml snippet won't parse with NimYAML. A minimum non-working example:

sequence:
  - id: first
    multi: |+
        Multi-line text

  # Comment
  - id: second

It raises: Error: unhandled exception: Illegal token (expected block sequence indicator): Indentation [YamlParserError]

Call stack:

yaml/dom.nim(205) loadDom
yaml/dom.nim(192) compose
yaml/dom.nim(158) composeNode
yaml/dom.nim(170) composeNode
yaml/stream.nim(113) peek
yaml/stream.nim(99) next
yaml/parser.nim(160) :anonymous
yaml/parser.nim(682) inBlockSeq

I am using the latest commit in the devel branch (89e18ce).

Do you have any ideas how to fix this?

flyx commented 2 years ago

That's a parser bug. I suggest not having the comment there for now until I figure out how to fix this.

There is special handling necessary for comments after block scalars, which allows comments there that are less indented than the scalar (otherwise they would be scalar content) but more than the surrounding level. I assume the code implementing that is broken somehow, which causes this problem. Or perhaps it's the code handling + indicator that doesn't eat as many tokens from the stream as it should. Possibly a combination of both.

I had a quick look on the code but this doesn't seem to be a trivial fix. It might take some time as this is a low-prio project for me, sorry about that.

theamarin commented 2 years ago

Thank you so much for your quick and thorough reply! The issue remains the same without the + indicator.

Indenting the comment so it is in line with the - id:first sequence:

sequence:
  - id: first
    multi: |+
        Multi-line text

    # Comment
  - id: second

leads to the stack trace ending with

yaml/parser.nim(712) beforeBlockMapKey

Maybe this helps. Thanks again!