我正在编写一个cron解析器,但编译器抱怨非法规则组合,
我的解析器出了什么问题?
import org.parboiled2._
sealed trait Part
case class Fixed(points: Seq[Int]) extends Part
case class Range(start: Int, end: Int) extends Part
case class Every(start: Int, interval: Int) extends Part
case object Full extends Part
case object Ignore extends Part
class CronParser(val input: ParserInput) extends Parser {
def number = rule { capture(digits) ~> (_.toInt) }
def digits = rule { oneOrMore(CharPredicate.Digit) }
def fixed = rule { oneOrMore(number).separatedBy(",") ~> Fixed }
def range = rule { digits ~ '-' ~ digits ~> Range }
def every= rule { digits ~ '/' ~ digits ~> Every }
def full= rule { '*' ~ push(Full) }
def ignore = rule { '?' ~ push(Ignore) }
def part = rule { fixed | range | every | full | ignore }
def expr = rule { part ~ part ~ part ~ part ~ part}
}
答案 0 :(得分:2)
您正在使用digits
,我认为您希望使用number
。以下应该可以正常工作:
class CronParser(val input: ParserInput) extends Parser {
def number = rule { capture(digits) ~> (_.toInt) }
def digits = rule { oneOrMore(CharPredicate.Digit) }
def fixed = rule { oneOrMore(number).separatedBy(",") ~> Fixed }
def range = rule { number ~ '-' ~ number ~> Range }
def every = rule { number ~ '/' ~ number ~> Every }
def full = rule { '*' ~ push(Full) }
def ignore = rule { '?' ~ push(Ignore) }
def part = rule { fixed | range | every | full | ignore }
def expr = rule { part ~ part ~ part ~ part ~ part }
}
问题是digits
没有推送值,这意味着range
等是想要从堆栈中弹出值的规则,而这些规则无法组合与~
。