Scala:如何组合来自不同对象的解析器组合器
给定一系列实现解析器组合器的对象,我如何组合解析器?由于 Parsers.Parser
是一个内部类,在 Scala 中 内部类绑定到 < em>外部对象,故事变得稍微复杂一些。
下面是一个尝试组合来自不同对象的两个解析器的示例。
import scala.util.parsing.combinator._
class BinaryParser extends JavaTokenParsers {
def anyrep: Parser[Any] = rep(any)
def any: Parser[Any] = zero | one
def zero: Parser[Any] = "0"
def one: Parser[Any] = "1"
}
object LongChainParser extends BinaryParser {
def parser1: Parser[Any] = zero~zero~one~one
}
object ShortChainParser extends BinaryParser {
def parser2: Parser[Any] = zero~zero
}
object ExampleParser extends BinaryParser {
def parser: Parser[Any] = (LongChainParser.parser1
||| ShortChainParser.parser2) ~ anyrep
def main(args: Array[String]) {
println(parseAll(parser, args(0) ))
}
}
这会导致以下错误:
<console>:11: error: type mismatch;
found : ShortChainParser.Parser[Any]
required: LongChainParser.Parser[?]
def parser: Parser[Any] = (LongChainParser.parser1
||| ShortChainParser.parser2) ~ anyrep
我已经找到了这个问题的解决方案,但是自从它被提出以来 最近在 scala-user ML 上(将一个解析器注入另一个解析器时出现问题),可能也值得将其放在这里。
Given a family of objects that implement parser combinators, how do I combine the parsers? Since Parsers.Parser
is an inner class, and in Scala inner classes are bound to the outer object, the story becomes slightly complicated.
Here's an example that attempts to combine two parsers from different objects.
import scala.util.parsing.combinator._
class BinaryParser extends JavaTokenParsers {
def anyrep: Parser[Any] = rep(any)
def any: Parser[Any] = zero | one
def zero: Parser[Any] = "0"
def one: Parser[Any] = "1"
}
object LongChainParser extends BinaryParser {
def parser1: Parser[Any] = zero~zero~one~one
}
object ShortChainParser extends BinaryParser {
def parser2: Parser[Any] = zero~zero
}
object ExampleParser extends BinaryParser {
def parser: Parser[Any] = (LongChainParser.parser1
||| ShortChainParser.parser2) ~ anyrep
def main(args: Array[String]) {
println(parseAll(parser, args(0) ))
}
}
This results to the following error:
<console>:11: error: type mismatch;
found : ShortChainParser.Parser[Any]
required: LongChainParser.Parser[?]
def parser: Parser[Any] = (LongChainParser.parser1
||| ShortChainParser.parser2) ~ anyrep
I've found the solution to this problem already, but since it was brought up
recently on scala-user ML (Problem injecting one parser into another), it's probably worth putting it here too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
快速的答案是使用
trait
而不是在object
中托管解析器:因为像
~
和| 这样的组合运算符
是针对内部类编写的,通过说BinaryParser#Parser[_]
将解析器引用升级到类级别对您没有任何好处。使用特征可以解决所有内部类问题,因为LongChainParser
和ShortChainParser
中的Parser[Any]
现在都引用的内部类ExampleParser
对象。The quick answer is to use the
trait
s instead of hosting the parsers inobject
s:Because the combinator operators like
~
and|
are written against the inner class, escalating the parser references to class-level by sayingBinaryParser#Parser[_]
doesn't do you any good. Using traits solves all that inner-class issues since bothParser[Any]
fromLongChainParser
andShortChainParser
now refer to the inner class ofExampleParser
object.