0xDEADBEEF

RSS odkazy english edition

Scala kombinátory parserů

7. 9. 2020
import scala.util.parsing.input._
import scala.util.parsing.combinator._

class DropReader[T](xs: Seq[T], offset: Int) extends Reader[T] {
  def first = xs.head
  def rest  = new DropReader(xs.tail, offset+1)
  override def drop(n: Int) = new DropReader(xs.drop(n), offset+n)
  def atEnd = xs.isEmpty
  def pos = new Pos(offset)
}

class PointReader[T](xs: IndexedSeq[T], offset: Int) extends Reader[T] {
  def first = xs(offset)
  def rest = new PointReader(xs, offset+1)
  override def drop(n: Int) = new PointReader(xs, offset+n)
  def atEnd = offset >= xs.size
  def pos = new Pos(offset)
}

class Pos(val column: Int) extends Position {
  val line = 0
  val lineContents = ""
}

def readerOf[T](xs: Seq[T]) = xs match {
  case xs: IndexedSeq[T] => new PointReader(xs, 0)
  case xs                => new DropReader(xs, 0)
}
píše k47 (@kaja47, k47)