Skip to content

Instantly share code, notes, and snippets.

@AlecZorab
Created May 12, 2015 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AlecZorab/e9b4360c033bbdae75f9 to your computer and use it in GitHub Desktop.
Save AlecZorab/e9b4360c033bbdae75f9 to your computer and use it in GitHub Desktop.
Experiments in fastparse
object ParseGist extends App {
import fastparse._
val space = P(CharsWhile(" \n".contains(_)).?)
val digits = P(CharsWhile('0' to '9' contains _))
val exponent = P(CharIn("eE") ~ CharIn("+-").? ~ digits)
val fractional = P("." ~ digits)
val integral = P("0" | CharIn('1' to '9') ~ digits.?)
val integer = space ~ P(CharIn("+-").? ~ integral).!.map(_.toInt)
val double = space ~ P(CharIn("+-").? ~ integral ~ fractional.? ~ exponent.?).!.map(_.toDouble)
//why do I get this type?
val p4Header:Parser[((Int, Int, Int, Double, Double, Int, Int, Double, Double), Double, Int, Int)] =
P(integer ~ integer ~ integer ~ double ~ double ~ integer ~ integer ~ double ~ double ~ double ~ integer ~ integer)
val res = p4Header.parse( """2 27 0 3.000000 3.000000 1 3 0.080000 0.000000 0.000000 0 1002""")
println(res)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment