How to convert an FParsec parser to parse whitespace

扶醉桌前 提交于 2019-12-02 14:13:34

问题


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!


回答1:


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

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