2015-05-14 84 views
2

我在寫一個cron解析器,但編譯器抱怨非法規則組合,parboiled2非法規則組合

我的解析器出了什麼問題?

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} 
} 

回答

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等人是想彈出值從堆棧中的規則,而這些無法與~組成。

相關問題