Search code examples
.netparsingf#fparsec

FParsec - How to parse from standard input stream


I can't seem to successfully parse from standard input stream with FParsec. I reduced my case to this very simple code :

match (runParserOnStream (pstring "test" .>> FParsec.CharParsers.newline) () "stdin" (Console.OpenStandardInput ()) Console.InputEncoding) with
    | Success(result, _, _)   -> printfn "Success: %A" result
    | Failure(errorMsg, perr, _) -> printfn "Failure: %s" errorMsg

But when i run the program, enter the string test, and then press Enter, it hangs there, and i can't seem to figure out why ..

What would be the solution ?


Solution

  • For performance reasons and simplicity, FParsec reads input streams block-wise (or reads the complete stream into a string before starting to parse). See e.g. this answer for some more details: Chunked Parsing with FParsec

    If you want to parse the input from a REPL with FParsec, you could implement a simple scanner that waits for a terminator in the input stream (e.g. a ";;" followed by a newline, like in the FSI console) and then, when it encounters such a terminator, copies the input up to the terminator into a string and hands it over to an FParsec parser for evaluation.