I'm implementing a parser that treats comments as white space with FParsec. It seems like it requires a trivial parser conversion, but I don't yet know how to implement that.
Here's the code I'm trying to get to type-check -
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)
The error is on the second argument of both the <|>
operators (over whitespaceText
). The errors are -
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'
It seems I need to convert a Parser<char list, 'a>
to a Parser<string, 'a>
. Or, since I'm just skipping them, I could convert them both to Parser<unit, 'a>
. However, I don't know how to write that code. Is it some simple lambda expression?
Cheers!
let whitespaceText = manyChars (anyOf whitespaceTextChars)
or
let whitespaceText = many (anyOf whitespaceTextChars) |>> fun cs -> System.String (Array.ofList cs)
来源:https://stackoverflow.com/questions/8395139/how-to-convert-an-fparsec-parser-to-parse-whitespace