0

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 ?

raph.amiard
  • 2,755
  • 2
  • 20
  • 23

2 Answers2

6

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.

Community
  • 1
  • 1
Stephan Tolksdorf
  • 3,062
  • 21
  • 28
  • Hey stephan, that's what i did on my own because i didn't want to spend too much time pulling hairs over it. Thanks ! – raph.amiard Oct 12 '12 at 09:59
2

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()
Daniel
  • 47,404
  • 11
  • 101
  • 179
  • In a grammar defined by A -> "test", the only valid input is "test", so Fparsec *could* stop after it has parsed said string. If there are remaining characters in the stream, then input is incorrect according to the grammar. – raph.amiard Oct 11 '12 at 19:18
  • Moreover, what is a parseable chunk is *defined* by the grammar, and not in any other way. Run F# repl for an example of what i mean. – raph.amiard Oct 11 '12 at 19:19
  • 1
    In the F# REPL, you have have to terminate the input with ";;". If you wanted to parse the input from such a REPL with FParsec, you could simply scan the input for ";;", copy the chunks separated by ";;"s into strings and then individually parse these strings with FParsec. – Stephan Tolksdorf Oct 11 '12 at 22:55
  • 1
    @raph.amiard - In F# there is no good way of knowing when a "parseable chunk" ends. In particular, I can separate lines by arbitrary amounts of blank lines without affecting the meaning of the code. This is why you need to add `;;` to signify when your "parseable chunk" ends – John Palmer Oct 12 '12 at 04:30