Domanda

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 ?

È stato utile?

Soluzione

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.

Altri suggerimenti

Since FParsec's source code is available it was easy enough to step through it and see that it reads the input stream until the buffer is full or end of stream is signaled.

Alternatively, you could read a line at a time:

let rec parseConsoleInput() =
  let parser = pstring "text" .>> eof
  Console.Write("> ")
  match Console.ReadLine() with
  | null | "" -> ()
  | input -> 
    match run parser input with
    | Success(result, _, _) -> printfn "Success: %A" result
    | Failure(msg, _, _) -> printfn "Failure: %s" msg
    parseConsoleInput()
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top