HsYAML icon indicating copy to clipboard operation
HsYAML copied to clipboard

Laziness of parser

Open jgm opened this issue 5 years ago • 2 comments

If I do

case decode inp of
  Right (opt :: Opt : _) -> doSomethingWith opt
  Right [] -> oneKindOfError
  Left (pos, err) -> anotherKindOfError

and inp is something like

---
foo: bar
...

baz

then I get an error when it hits baz, even though it can successfully parse the one YAML document I'm asking for. Is this expected? Could the parser be made lazier, so it returns the result after having parsed the first YAML document in the stream, and doesn't worry about the rest unless I ask for it?

Alternatively, could there be an option telling it to ignore non-YAML content after the first YAML document, if you just use decode1?

jgm avatar Oct 12 '19 18:10 jgm

Sorry, my question was ill-posed. I see why it can't be lazy if it's returning an Either value. I guess the real question is whether another function might be made available that lazily returns a list of YAML documents (with no error return values).

jgm avatar Oct 12 '19 19:10 jgm

@jgm that's definitely possible; we could still report errors with a document granularity. i.e. something like (pseudo-code):

decodeDocsLazy :: ByteString -> YamlDocs

where

data YamlDocs = YamlDoc Doc YamlDocs 
                          | YamlError ErrInfo

hvr avatar Dec 31 '19 08:12 hvr