我正在尝试使用 FParsec 从 s 表达式语言解析 lisp 风格的注释。我在上一个线程中解析单行注释时得到了一些帮助 -如何转换 FParsec 解析器来解析空格
虽然这个问题已经解决,但我仍然需要解析多行注释。这是当前的代码 -
/// 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 解析永远不会成功。由于这是一个组合器,我无法设置断点或分析它为什么不起作用。
有什么想法为什么它不起作用吗?
尝试更改 bool 参数closeMultilineCommentStr
为假
(charsTillString closeMultilineCommentStr false System.Int32.MaxValue)
否则会跳过closeMultilineCommentStr
string.
使其与嵌套注释一起使用
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)