2011-05-10 73 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

相關問題