Skip to content

Instantly share code, notes, and snippets.

@tpolecat
Last active December 14, 2015 06:48
Show Gist options
  • Select an option

  • Save tpolecat/5045146 to your computer and use it in GitHub Desktop.

Select an option

Save tpolecat/5045146 to your computer and use it in GitHub Desktop.
object stuff {
import scala.util.parsing.combinator._
class Parser extends RegexParsers {
def parseTop(s: String): Stuff = parse(top(""), s) match {
case Success(value, _) => value
case _ => throw new RuntimeException("...")
}
def top(indent:String): Parser[Stuff] =
single(indent) | top(indent + " ")
def single(indent:String): Parser[Stuff] =
(indent + "a") ^^ { s => Stuff(s.length) }
override def skipWhitespace = false
}
case class Stuff(i: Int)
}
println((new stuff.Parser).parseTop(" a").i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment