忽略Scala组合器解析器中的C风格注释

zig*_*tar 9 parsing scala parser-combinators

什么是使我的解析器尊重(忽略)C风格注释的最简单方法.我对两种评论类型都感兴趣,但也欢迎只有一种类型的解决方案.

我目前只是在扩展JavaTokenParsers.

Dan*_*ral 11

只要不嵌套注释,就可以使用简单的正则表达式.把它放进去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)
Run Code Online (Sandbox Code Playgroud)