我正在尝试使用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参数更改为false
(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/