如何使用 FParsec 解析注释
我正在尝试使用 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 解析永远不会成功。由于这是一个组合器,我无法设置断点或分析它为什么不起作用。
有什么想法为什么它不起作用吗?
I'm attempting to parse lisp-style comments from an s-expression language with FParsec. I got a bit of help with parsing single-line comments in this previous thread - How to convert an FParsec parser to parse whitespace
While that was resolved, I still need to parse multiline comments. Here's the current code -
/// 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
Unfortunately, the multilineComment parse never succeeds. Since this is a combinator, I can't put breakpoints or analyze why it won't work.
Any ideas why it won't work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将
closeMultilineCommentStr
的 bool 参数更改为 false,否则它将跳过
closeMultilineCommentStr
字符串。使其与嵌套注释一起使用
Try changing the bool argument for
closeMultilineCommentStr
to falseOtherwise it will skip over the
closeMultilineCommentStr
string.To make it work with nested comments