如何转换FParsec解析器来解析空格

时间:2011-12-06 03:53:37

标签: f# fparsec

我正在实现一个解析器,它使用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)

错误出现在<|>运算符的两个参数上(超过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'

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

干杯!

1 个答案:

答案 0 :(得分:2)

let whitespaceText = manyChars (anyOf whitespaceTextChars)

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