简体   繁体   中英

Using Parsers with PackratParsers in scala parser combinators

This Parser object Test runs out of stack, although it has PackratParsers mixed in. The problem arises with rep1(a | f ) in the second parser rule. The usual method parse is not availlable, because it is subclass of Parser - which shouldnt be changed - and probably isn t the reason for the problem.

Why is left not handled by the PackratParser here?

import scala.util.parsing.combinator._
import scala.util.parsing.input._
import java.io._

sealed class T
case class F(val t: List[T]) extends T
case class L(val t: Char) extends T

object Test extends Parsers  with PackratParsers{
  override type Elem = Char
  type PP[T] = PackratParser[T]
  lazy val a : PP[L]= elem("1", { x: Elem => x == '1' }) ^^ { case x => L(x) }
  lazy val f: PP[F] = rep1(a | f ) ^^ { case x => F(x) }
  def main(args: Array[String]) {
    phrase(f)(new PackratReader(StreamReader(new InputStreamReader(new FileInputStream(args(0))))))
  }
}

Well, StreamReader was the wrong choice for PackratParsers, because it cannot work on Streams. Eg a CharSequenceReader is the right choice, because the same position in it can be read more than once.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM