Skip to content

Instantly share code, notes, and snippets.

@gszeliga
Last active August 29, 2015 14:01
Show Gist options
  • Save gszeliga/9b00f91195b6dc6234a7 to your computer and use it in GitHub Desktop.
Save gszeliga/9b00f91195b6dc6234a7 to your computer and use it in GitHub Desktop.
def natural = rep1(single_digit) ^^ (_.mkString.toInt) named ("natural")
def signedInt = new Parser[Int] {
def apply(in: Input) = {
'-'(in) match {
case Success(_, rest) => natural(rest) map (_ * -1)
case _ => natural(in)
}
}
} named ("signed_int")
def rawbyte = new Parser[Byte] {
def apply(in: Input) = {
if (in.atEnd) Failure("End of input reached", in)
else Success(in.first, in.rest)
}
} named ("raw_bytes")
def string: Parser[BString] =
natural ~ ':' >> {
case size ~ _ => repN(size, rawbyte) ^^ (BString(_))
} named ("string")
def int: Parser[BInt] = {
delimitedBy(NUMBER_BEGIN, NUMBER_END)(signedInt) ^^ (BInt(_)) named ("int")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment