FParsec - How to parse from standard input stream

强颜欢笑 提交于 2019-12-12 03:56:29

问题


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 ?


回答1:


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.




回答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()


来源:https://stackoverflow.com/questions/12846706/fparsec-how-to-parse-from-standard-input-stream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!