本文介绍了在Scala组合器解析器中忽略C样式的注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What is the most simple way to make my parser respect (ignore) C-style comments. I'm interested in both comment types, though a solution for only one type is also welcome.

我目前只是在扩展JavaTokenParsers.

I'm currently simply extending JavaTokenParsers.

推荐答案

只要不嵌套注释,就可以使用简单的正则表达式.将其放入whiteSpace:

You can use a simple regular expression, as long as you don't nest comments. Put it inside 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)

这篇关于在Scala组合器解析器中忽略C样式的注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 04:37