Scala编程:解析器组合器与GUI开发
1. 解析器组合器基础
在解析器组合器中, lastFailure 字段初始化为 None ,在 Failure 类的构造函数中更新:
case class Failure(msg: String, in: Input)
extends ParseResult[Nothing] {
if (lastFailure.isDefined &&
lastFailure.get.in.pos <= in.pos)
lastFailure = Some(this)
}
phrase 方法会读取该字段,如果解析器失败则发出最终错误消息。以下是 Parsers 特质中 phrase 方法的实现:
def phrase[T](p: Parser[T]) = new Parser[T] {
lastFailure = None
def apply(in: Input) = p(in) match {
case s @ Success(out, in1) =>
if (in1.atEnd) s
else Failure("end of input expected", in1)
case f : Failure =>
Scala解析器与GUI编程整合
超级会员免费看
订阅专栏 解锁全文
66

被折叠的 条评论
为什么被折叠?



