本文介绍了FParsec-如何从标准输入流进行解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法使用FParsec从标准输入流中成功解析.我将案例简化为以下非常简单的代码:

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

但是当我运行程序时,输入字符串测试,然后按Enter,它挂在那里,我似乎无法弄清楚为什么..

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 ..

有什么解决方案?

推荐答案

出于性能原因和简单性考虑,FParsec逐块读取输入流(或在开始解析之前将完整的流读取为字符串).参见例如此答案以获取更多详细信息:使用FParsec进行分块分析

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

如果要使用FParsec解析来自REPL的输入,则可以实现一个简单的扫描程序,该扫描程序等待输入流中的终止符(例如,";;"后跟换行符,例如FSI控制台中的)然后,当遇到这样的终结符时,将直到终结符的输入复制到字符串中,然后将其交给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.

这篇关于FParsec-如何从标准输入流进行解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 00:34