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 に答える