1

我正在使用 FParsec 实现一个将注释视为空白的解析器。似乎它需要一个简单的解析器转换,但我还不知道如何实现它。

这是我试图进行类型检查的代码 -

let whitespaceTextChars = " \t\r\n"

/// Read whitespace characters.
let whitespaceText = many (anyOf whitespaceTextChars)

/// Read a line comment.
let lineComment = pchar lineCommentChar >>. restOfLine true

/// Skip any white space characters.
let skipWhitespace = skipMany (lineComment <|> whitespaceText)

/// Skip at least one white space character.
let skipWhitespace1 = skipMany1 (lineComment <|> whitespaceText)

错误出现在两个运算符的第二个参数上<|>(over whitespaceText)。错误是 -

Error   1   Type mismatch. Expecting a     Parser<string,'a>     but given a     Parser<char list,'a>     The type 'string' does not match the type 'char list'
Error   2   Type mismatch. Expecting a     Parser<string,'a>     but given a     Parser<char list,'a>     The type 'string' does not match the type 'char list'

看来我需要将 a 转换Parser<char list, 'a>Parser<string, 'a>. 或者,由于我只是跳过它们,我可以将它们都转换为Parser<unit, 'a>. 但是,我不知道如何编写该代码。它是一些简单的 lambda 表达式吗?

干杯!

4

1 回答 1

2

let whitespaceText = manyChars (anyOf whitespaceTextChars)

或者

let whitespaceText = many (anyOf whitespaceTextChars) |>> fun cs -> System.String (Array.ofList cs)

于 2011-12-06T05:44:23.943 回答