f# - 如何使用 FParsec 解析评论

标签 f# fparsec

我正在尝试使用 FParsec 从 s 表达式语言解析 lisp 样式的注释。我在之前的帖子中在解析单行注释方面得到了一些帮助 - How to convert an FParsec parser to parse whitespace

虽然解决了,但我仍然需要解析多行注释。这是当前的代码 -

/// Read whitespace character as a string.
let spaceAsStr = anyOf whitespaceChars |>> fun chr -> string chr

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

/// Read a multiline comment.
/// TODO: make multiline comments nest.
let multilineComment =
    between
        (pstring openMultilineCommentStr)
        (pstring closeMultilineCommentStr)
        (charsTillString closeMultilineCommentStr true System.Int32.MaxValue)

/// Read whitespace text.
let whitespace =
    lineComment <|>
    multilineComment <|>
    spaceAsStr

/// Skip any white space characters.
let skipWhitespace = skipMany whitespace

/// Skip at least one white space character.
let skipWhitespace1 = skipMany1 whitespace

不幸的是, multilineComment 解析永远不会成功。由于这是一个组合器,我无法放置断点或分析它为什么不起作用。

任何想法为什么它不起作用?

最佳答案

尝试更改 closeMultilineCommentStr 的 bool 参数假的

(charsTillString closeMultilineCommentStr false System.Int32.MaxValue)

否则它会跳过 closeMultilineCommentStr字符串。

使其与嵌套注释一起使用
let rec multilineComment o=
    let ign x = charsTillString x false System.Int32.MaxValue
    between
        (pstring openMultilineCommentStr)
        (pstring closeMultilineCommentStr)
        (attempt (ign openMultilineCommentStr >>. multilineComment >>. ign closeMultilineCommentStr) <|> 
        ign closeMultilineCommentStr) <|o

关于f# - 如何使用 FParsec 解析评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8405032/

相关文章:

generics - Seq.iter 比 native 映射慢 : any generic solution?

f# - 用Mono运行F#代码

parsing - 如何使用 FParsec 解析字符串值

f# - 有人可以举一个在 FParsec 中使用 chainl1 的例子吗?

performance - 为什么F#中的printf这么慢?

list - 在 F# 中,map2 如何处理不均匀的列表长度?

postgresql - F# SQLProvider 列顺序与表中的顺序不匹配

f# - 如何将 FParsec 结果值提取到 FSI 中的变量

f# - fparsec 解析字符串序列