9

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

我目前只是在扩展 JavaTokenParsers。

4

1 回答 1

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)
于 2011-05-10T18:45:15.553 回答