gpt4 book ai didi

parsing - 在Scala组合器解析器中忽略C样式的注释

转载 作者:行者123 更新时间:2023-12-04 03:46:10 25 4
gpt4 key购买 nike

使我的解析器尊重(忽略)C样式注释的最简单方法是什么。我对这两种注释类型都感兴趣,尽管也欢迎仅针对一种类型的解决方案。

我目前只是在扩展JavaTokenParsers。

最佳答案

只要不嵌套注释,就可以使用简单的正则表达式。把它放在whiteSpace里面:

scala> object T extends JavaTokenParsers {
| protected override val whiteSpace = """(\s|//.*|(?m)/\*(\*(?!/)|[^*])*\*/)+""".r
| def simpleParser = ident+
| }
defined module T

scala> val testString = """ident // comment to the end of line
| another ident /* start comment
| end comment */ final ident"""
testString: java.lang.String =
ident // comment to the end of line
another ident /* start comment
end comment */ final ident

scala> T.parseAll(T.simpleParser, testString)
res0: T.ParseResult[List[String]] = [3.27] parsed: List(ident, another, ident, final, ident)

关于parsing - 在Scala组合器解析器中忽略C样式的注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5952720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com