1

这看起来很简单!

class SeparatedParser(val input: ParserInput, val delimiter: String = ",") extends Parser {

  def pipedField = rule { (zeroOrMore(field).separatedBy("|")) }
  def field = rule { capture(zeroOrMore(noneOf(delimiter))) }

  def d = delimiter

  def record = rule {
    field ~ d ~ pipedField ~ d ~ field ~ EOI
  }
}

我尝试:

val parser = new SeparatedParser("""49798,piped1|piped2,sklw""")
val parsed = parser.record.run()
parsed match {
  case Success(rel) => println(rel)
  case Failure(pe:ParseError) =>println(parser.formatError(pe)) 
}

但我得到:

49798 :: Vector(piped1|piped2) :: sklw :: HNil

我希望 Vector 有两个单独的元素:piped1piped2.

我犯了什么愚蠢的错误?

4

0 回答 0