/FastParsers

Fast Parser Combinators

Primary LanguageScalaMIT LicenseMIT

FastParsers Build Status

FastParsers is a Scala parser library which uses macros to transform easy-to- write parser combinators into efficient recursive-descent backtracking parsers. The generated parsers are about 20x faster than Scala's parser combinator library even though its interface stay about the same.

Example

Here is an example of a basic JSON parser

val jsonParser = FastParser {
  def value: Parser[Any] = (obj | arr | stringLit |
                            decimalNumber | "null" | "true" | "false")
  def obj: Parser[Any] = lit("{") ~> repsep (member, ",") <~ "}"
  def arr: Parser[Any] = lit("[") ~> repsep (value , ",") <~ "]"
  def member: Parser[Any] = stringLit ~ (lit(":") ~> value)
}

This generates a Parser Object which allows you to call any of the transformed rules.

val cnt = "{\"firstName \": \"John\" , \"age\": 25}"
jsonParser.value(cnt) match {
  case Success(result) =>
    println ("success: " + result )
  case Failure(error) =>
    println (" failure: " + error )
}

How to use

A snapshot is available on Sonatype. To use it with SBT, add the following lines in your build:

libraryDependencies += "com.github.begeric" % "fastparsers_2.11" % "0.1-SNAPSHOT"
resolvers += Resolver.sonatypeRepo("snapshots")

More information

For a more comprehensive presentation please take a look at the presentation slides.

To have a better understanding of the internals please read the paper describing the implementation.