如何转换FParsec解析器以解析空白 [英] How to convert an FParsec parser to parse whitespace

查看:51
本文介绍了如何转换FParsec解析器以解析空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

两个<|>运算符(在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'

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

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?

干杯!

推荐答案

let whitespaceText = manyChars (anyOf whitespaceTextChars)

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

这篇关于如何转换FParsec解析器以解析空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆