2011-05-10 95 views
9

什么是使我的解析器尊重(忽略)C风格注释的最简单方法。我对这两种评论类型都很感兴趣,尽管只有一种类型的解决方案也是受欢迎的。忽略Scala组合器解析器中的C风格注释

我目前只是简单地扩展JavaTokenParsers。

+0

你使用哪个解析器库? – 2011-05-10 15:49:36

回答

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) 
+0

@ziggystar好的,等一下... – 2011-05-10 20:02:33