Skip to content

Instantly share code, notes, and snippets.

@lihaoyi
Created May 12, 2015 06:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lihaoyi/4dfc76939844da6e1db8 to your computer and use it in GitHub Desktop.
Save lihaoyi/4dfc76939844da6e1db8 to your computer and use it in GitHub Desktop.
Monadic Parsers
@ load.ivy("com.lihaoyi" %% "fastparse" % "0.1.4")
@ import fastparse._
import fastparse._
@ val parser = P( "a".rep1.!.flatMap(s => ("b" * s.length) ~ ("c" * s.length)) )
parser: fastparse.Parser[Unit] = parser
@ parser.parse("aaa")
res4: fastparse.core.Result[Unit] = Failure(parser:0 / "bbb":3 ..."", false)
@ parser.parse("aaabbbcc")
res5: fastparse.core.Result[Unit] = Failure(parser:0 / "ccc":6 ..."cc", false)
@ parser.parse("aaaabcccc")
res6: fastparse.core.Result[Unit] = Failure(parser:0 / "bbbb":4 ..."bcccc", false)
@ parser.parse("aaaaabbbbbccccc")
res7: fastparse.core.Result[Unit] = Success((), 15)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment