cats-parse

Continuous Integrationcodecov

A parsing library for the cats ecosystem.

To use in sbt add, the following to your libraryDependencies:

// use this snippet for the JVM
libraryDependencies += "org.typelevel" %% "cats-parse" % "1.0.0"

// use this snippet for JS, or cross-building
libraryDependencies += "org.typelevel" %%% "cats-parse" % "1.0.0"

The API docs are published.

Why another parsing library? See this blog post detailing the design. To reiterate, this library has a few goals:

  1. Compatibility: should work on all scala platforms and recent versions. Currently it supports JVM, JS on versions 2.11, 2.12, 2.13, and 3. The core library should have minimal dependencies. Currently this library only depends on cats.
  2. Excellent performance: should be as fast or faster than any parser combinator that has comparable scala version support.
  3. Cats friendliness: method names match cats style, and out of the box support for cats typeclasses.
  4. Precise errors: following the Haskell Trifecta parsing library, backtracking is opt-in vs opt-out. This design tends to make it easier to write parsers that point correctly to failure points.
  5. Safety: by separating Parser0, a parser that may consume no input, from Parser, a parser must consume at least one character on success. Most combinators and methods can be made safer to use and less prone to runtime errors.
  6. Stability: we are very reluctant to break compatibility between versions. We want to put a minimal tax on users to stay on the latest versions.

Tutorial

Simple parser

The library provides a set of simple parsers which might be combined to create any parsing logic. The simplest parser is Parser.anyChar which is successful where there is one char at the input. It has type Parser[Char] which means it returns one parsed char.

To provide any input to parser one need to use parse method.

import cats.parse.Parser

val p: Parser[Char] = Parser.anyChar
// p: Parser[Char] = AnyChar

p.parse("t")
// res1: Either[Parser.Error, (String, Char)] = Right(value = ("", 't'))

p.parse("")
// res2: Either[Parser.Error, (String, Char)] = Left(
//   value = Error(
//      = 0,
//      = NonEmptyList(
//       head = InRange(offset = 0, lower = '\u0000', upper = '￿'),
//       tail = List()
//     )
//   )
// )

p.parse("two")
// res3: Either[Parser.Error, (String, Char)] = Right(value = ("wo", 't'))

Notice the return type. Tuple2[String, Char] contains the rest of the input string and one parsed char if parsing was successful. It returns Left with error message if there was some parsing error.

Mapping output

The output of the parser might be processed with map method:

import cats.parse.Parser

case class CharWrapper(value: Char)

val p: Parser[CharWrapper] = Parser.anyChar.map(char => CharWrapper(char))
// p: Parser[CharWrapper] = Map(parser = AnyChar, fn = <function1>)

p.parse("t")
// res5: Either[Parser.Error, (String, CharWrapper)] = Right(
//   value = ("", CharWrapper(value = 't'))
// )

There are built-in methods for mapping the output to types String or Unit:

import cats.parse.Rfc5234.digit
import cats.parse.Parser

/* String */

val p2: Parser[String] = digit.map((c: Char) => c.toString)
// p2: Parser[String] = Map(
//   parser = CharIn(
//     min = 48,
//     bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
//     ranges = NonEmptyList(head = ('0', '9'), tail = List())
//   ),
//   fn = <function1>
// )
// is analog to
val p3: Parser[String] = digit.string
// p3: Parser[String] = StringP(
//   parser = CharIn(
//     min = 48,
//     bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
//     ranges = NonEmptyList(head = ('0', '9'), tail = List())
//   )
// )

p3.parse("1")
// res7: Either[Parser.Error, (String, String)] = Right(value = ("", "1"))

/* Unit */

val p4: Parser[Unit] = digit.map(_ => ())
// p4: Parser[Unit] = Map(
//   parser = CharIn(
//     min = 48,
//     bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
//     ranges = NonEmptyList(head = ('0', '9'), tail = List())
//   ),
//   fn = <function1>
// )
// is analog to
val p5: Parser[Unit] = digit.void
// p5: Parser[Unit] = Void(
//   parser = CharIn(
//     min = 48,
//     bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
//     ranges = NonEmptyList(head = ('0', '9'), tail = List())
//   )
// )

p5.parse("1")
// res8: Either[Parser.Error, (String, Unit)] = Right(value = ("", ()))

Combining parsers

The parsers might be combined through operators:

For this example we'll be using cats.parse.Rfc5234 package which contains such parsers as alpha (Latin alphabet) and sp (whitespace).

import cats.parse.Rfc5234.{sp, alpha, digit}
import cats.parse.Parser

/* Product */

// the sp parser won't return the whitespace, it just returns Unit if it successful
val p1: Parser[(Char, Unit)] = alpha ~ sp
// p1: Parser[(Char, Unit)] = Prod(
//   first = CharIn(
//     min = 65,
//     bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57},
//     ranges = NonEmptyList(head = ('A', 'Z'), tail = List(('a', 'z')))
//   ),
//   second = Void(
//     parser = CharIn(
//       min = 32,
//       bitSet = {0},
//       ranges = NonEmptyList(head = (' ', ' '), tail = List())
//     )
//   )
// )

p1.parse("t")
// res10: Either[Parser.Error, (String, (Char, Unit))] = Left(
//   value = Error(
//      = 1,
//      = NonEmptyList(
//       head = InRange(offset = 1, lower = ' ', upper = ' '),
//       tail = List()
//     )
//   )
// )

p1.parse("t ")
// res11: Either[Parser.Error, (String, (Char, Unit))] = Right(
//   value = ("", ('t', ()))
// )


/* productL, productR */

// The type is just Char because we dropping the space
// to drop the alphabet change the arrow side: alpha *> sp
val p2: Parser[Char] = alpha <* sp
// p2: Parser[Char] = Map(
//   parser = Prod(
//     first = CharIn(
//       min = 65,
//       bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57},
//       ranges = NonEmptyList(head = ('A', 'Z'), tail = List(('a', 'z')))
//     ),
//     second = Void(
//       parser = CharIn(
//         min = 32,
//         bitSet = {0},
//         ranges = NonEmptyList(head = (' ', ' '), tail = List())
//       )
//     )
//   ),
//   fn = cats.parse.Parser$$Lambda$10830/0x0000000102e57040@2dce2ff3
// )

// still error since we need the space even if we drop it
p2.parse("t")
// res12: Either[Parser.Error, (String, Char)] = Left(
//   value = Error(
//      = 1,
//      = NonEmptyList(
//       head = InRange(offset = 1, lower = ' ', upper = ' '),
//       tail = List()
//     )
//   )
// )

p2.parse("t ")
// res13: Either[Parser.Error, (String, Char)] = Right(value = ("", 't'))


/* surroundedBy */

val p4: Parser[Char] = sp *> alpha <* sp
// p4: Parser[Char] = Map(
//   parser = Prod(
//     first = Map(
//       parser = Prod(
//         first = Void(
//           parser = CharIn(
//             min = 32,
//             bitSet = {0},
//             ranges = NonEmptyList(head = (' ', ' '), tail = List())
//           )
//         ),
//         second = CharIn(
//           min = 65,
//           bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57},
//           ranges = NonEmptyList(head = ('A', 'Z'), tail = List(('a', 'z')))
//         )
//       ),
//       fn = cats.parse.Parser$$Lambda$10825/0x0000000102e4f840@2f0c58cd
//     ),
//     second = Void(
//       parser = CharIn(
//         min = 32,
//         bitSet = {0},
//         ranges = NonEmptyList(head = (' ', ' '), tail = List())
//       )
//     )
//   ),
//   fn = cats.parse.Parser$$Lambda$10830/0x0000000102e57040@2dce2ff3
// )
val p5: Parser[Char] = alpha.surroundedBy(sp)
// p5: Parser[Char] = Map(
//   parser = Prod(
//     first = Void(
//       parser = CharIn(
//         min = 32,
//         bitSet = {0},
//         ranges = NonEmptyList(head = (' ', ' '), tail = List())
//       )
//     ),
//     second = Prod(
//       first = CharIn(
//         min = 65,
//         bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57},
//         ranges = NonEmptyList(head = ('A', 'Z'), tail = List(('a', 'z')))
//       ),
//       second = Void(
//         parser = CharIn(
//           min = 32,
//           bitSet = {0},
//           ranges = NonEmptyList(head = (' ', ' '), tail = List())
//         )
//       )
//     )
//   ),
//   fn = cats.parse.Parser$$Lambda$10831/0x0000000102e56040@6f3ad327
// )

p4.parse(" a ")
// res14: Either[Parser.Error, (String, Char)] = Right(value = ("", 'a'))

p5.parse(" a ")
// res15: Either[Parser.Error, (String, Char)] = Right(value = ("", 'a'))


/* between */

val p6: Parser[Char] = sp *> alpha <* digit
// p6: Parser[Char] = Map(
//   parser = Prod(
//     first = Map(
//       parser = Prod(
//         first = Void(
//           parser = CharIn(
//             min = 32,
//             bitSet = {0},
//             ranges = NonEmptyList(head = (' ', ' '), tail = List())
//           )
//         ),
//         second = CharIn(
//           min = 65,
//           bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57},
//           ranges = NonEmptyList(head = ('A', 'Z'), tail = List(('a', 'z')))
//         )
//       ),
//       fn = cats.parse.Parser$$Lambda$10825/0x0000000102e4f840@2f0c58cd
//     ),
//     second = Void(
//       parser = CharIn(
//         min = 48,
//         bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
//         ranges = NonEmptyList(head = ('0', '9'), tail = List())
//       )
//     )
//   ),
//   fn = cats.parse.Parser$$Lambda$10830/0x0000000102e57040@2dce2ff3
// )
val p7: Parser[Char] = alpha.between(sp, digit)
// p7: Parser[Char] = Map(
//   parser = Prod(
//     first = Void(
//       parser = CharIn(
//         min = 32,
//         bitSet = {0},
//         ranges = NonEmptyList(head = (' ', ' '), tail = List())
//       )
//     ),
//     second = Prod(
//       first = CharIn(
//         min = 65,
//         bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57},
//         ranges = NonEmptyList(head = ('A', 'Z'), tail = List(('a', 'z')))
//       ),
//       second = Void(
//         parser = CharIn(
//           min = 48,
//           bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
//           ranges = NonEmptyList(head = ('0', '9'), tail = List())
//         )
//       )
//     )
//   ),
//   fn = cats.parse.Parser$$Lambda$10831/0x0000000102e56040@6f3ad327
// )

p6.parse(" a1")
// res16: Either[Parser.Error, (String, Char)] = Right(value = ("", 'a'))

p7.parse(" a1")
// res17: Either[Parser.Error, (String, Char)] = Right(value = ("", 'a'))


/* OrElse */

val p3: Parser[AnyVal] = alpha | sp
// p3: Parser[AnyVal] = OneOf(
//   all = List(
//     CharIn(
//       min = 65,
//       bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57},
//       ranges = NonEmptyList(head = ('A', 'Z'), tail = List(('a', 'z')))
//     ),
//     Void(
//       parser = CharIn(
//         min = 32,
//         bitSet = {0},
//         ranges = NonEmptyList(head = (' ', ' '), tail = List())
//       )
//     )
//   )
// )

p3.parse("t")
// res18: Either[Parser.Error, (String, AnyVal)] = Right(value = ("", 't'))

p3.parse(" ")
// res19: Either[Parser.Error, (String, AnyVal)] = Right(value = ("", ()))

Repeating parsers

Sometimes we need something to repeat zero or more times. The cats-parse have rep and rep0 methods for repeating values. rep means that the parser must be successful at least one time. rep0 means that the parser output might be empty.

import cats.data.NonEmptyList
import cats.parse.Rfc5234.alpha
import cats.parse.{Parser, Parser0}

val p1: Parser[NonEmptyList[Char]]  = alpha.rep
// p1: Parser[NonEmptyList[Char]] = Rep(
//   p1 = CharIn(
//     min = 65,
//     bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57},
//     ranges = NonEmptyList(head = ('A', 'Z'), tail = List(('a', 'z')))
//   ),
//   min = 1,
//   maxMinusOne = 2147483647,
//   acc1 = cats.parse.Accumulator$$anon$12@2675d67e
// )
val p2: Parser0[List[Char]] = alpha.rep0
// p2: Parser0[List[Char]] = OneOf0(
//   all = List(
//     Rep(
//       p1 = CharIn(
//         min = 65,
//         bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57},
//         ranges = NonEmptyList(head = ('A', 'Z'), tail = List(('a', 'z')))
//       ),
//       min = 1,
//       maxMinusOne = 2147483647,
//       acc1 = cats.parse.Accumulator0$$anon$9@2f9e022b
//     ),
//     Pure(result = List())
//   )
// )

p1.parse("")
// res21: Either[Parser.Error, (String, NonEmptyList[Char])] = Left(
//   value = Error(
//      = 0,
//      = NonEmptyList(
//       head = InRange(offset = 0, lower = 'A', upper = 'Z'),
//       tail = List(InRange(offset = 0, lower = 'a', upper = 'z'))
//     )
//   )
// )

p2.parse("")
// res22: Either[Parser.Error, (String, List[Char])] = Right(
//   value = ("", List())
// )

p2.parse("something")
// res23: Either[Parser.Error, (String, List[Char])] = Right(
//   value = ("", List('s', 'o', 'm', 'e', 't', 'h', 'i', 'n', 'g'))
// )

Notice the types of parsers. Parser type always means some non-empty output and the output of Parser0 might be empty.

One common task in this example is to parse a full line (or words) of text. In the example it is done by rep, and then it could be mapped to String in different ways:

import cats.data.NonEmptyList
import cats.parse.Rfc5234.alpha
import cats.parse.Parser

val p: Parser[String]  = alpha.rep.map((l: NonEmptyList[Char]) => l.toList.mkString)
// p: Parser[String] = Map(
//   parser = Rep(
//     p1 = CharIn(
//       min = 65,
//       bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57},
//       ranges = NonEmptyList(head = ('A', 'Z'), tail = List(('a', 'z')))
//     ),
//     min = 1,
//     maxMinusOne = 2147483647,
//     acc1 = cats.parse.Accumulator$$anon$12@28e3c458
//   ),
//   fn = <function1>
// )

val p2: Parser[String] = alpha.rep.string
// p2: Parser[String] = StringP(
//   parser = Rep(
//     p1 = CharIn(
//       min = 65,
//       bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57},
//       ranges = NonEmptyList(head = ('A', 'Z'), tail = List(('a', 'z')))
//     ),
//     min = 1,
//     maxMinusOne = 2147483647,
//     acc1 = cats.parse.Accumulator0$$anon$11@2a8c529f
//   )
// )
val p3: Parser[String] = alpha.repAs[String]
// p3: Parser[String] = Rep(
//   p1 = CharIn(
//     min = 65,
//     bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57},
//     ranges = NonEmptyList(head = ('A', 'Z'), tail = List(('a', 'z')))
//   ),
//   min = 1,
//   maxMinusOne = 2147483647,
//   acc1 = cats.parse.Accumulator0$$anon$7@428ec81e
// )

All three parsers will be identical in parsing results, but p2 and p3 are using built-in methods which will not create intermediate list. rep + map creates intermediate list which is mapped to string in this example.

Parsers with empty output

Some parsers never return a value. They have a type Parser0. One might get this type of parser when using rep0 or .? methods.

import cats.parse.Rfc5234.{alpha, sp}
import cats.parse.Parser

val p: Parser[String] = (alpha.rep <* sp.?).rep.string
// p: Parser[String] = StringP(
//   parser = Rep(
//     p1 = Prod(
//       first = Rep(
//         p1 = CharIn(
//           min = 65,
//           bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57},
//           ranges = NonEmptyList(head = ('A', 'Z'), tail = List(('a', 'z')))
//         ),
//         min = 1,
//         maxMinusOne = 2147483647,
//         acc1 = cats.parse.Accumulator0$$anon$11@2a8c529f
//       ),
//       second = OneOf0(
//         all = List(
//           CharIn(
//             min = 32,
//             bitSet = {0},
//             ranges = NonEmptyList(head = (' ', ' '), tail = List())
//           ),
//           Pure(result = ())
//         )
//       )
//     ),
//     min = 1,
//     maxMinusOne = 2147483647,
//     acc1 = cats.parse.Accumulator0$$anon$11@2a8c529f
//   )
// )

p.parse("hello world")
// res26: Either[Parser.Error, (String, String)] = Right(
//   value = ("", "hello world")
// )

Notice the type we got - Parser[String]. That is because we have rep outside and our alpha.rep parser with Parser type is on the left side of the clause. But what if we want to parse strings with spaces at the beginning?

val p = (sp.? *> alpha.rep <* sp.?).rep.string

We will get an error value rep is not a member of cats.parse.Parser0. This happens since we have the left-side parser as optional in sp.? *> alpha.rep <* sp.? clause. This clause has a type Parser0 which can't be repeated.

But this parser can't be empty because of alpha.rep parser, and we know it. For these types of parsers we need to use with1 wrapper method on the left side of the clause:

import cats.parse.Rfc5234.{alpha, sp}
import cats.parse.Parser


val p: Parser[String] = (sp.?.with1 *> alpha.rep <* sp.?).rep.string
// p: Parser[String] = StringP(
//   parser = Rep(
//     p1 = Prod(
//       first = OneOf0(
//         all = List(
//           CharIn(
//             min = 32,
//             bitSet = {0},
//             ranges = NonEmptyList(head = (' ', ' '), tail = List())
//           ),
//           Pure(result = ())
//         )
//       ),
//       second = Prod(
//         first = Rep(
//           p1 = CharIn(
//             min = 65,
//             bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57},
//             ranges = NonEmptyList(head = ('A', 'Z'), tail = List(('a', 'z')))
//           ),
//           min = 1,
//           maxMinusOne = 2147483647,
//           acc1 = cats.parse.Accumulator0$$anon$11@2a8c529f
//         ),
//         second = OneOf0(
//           all = List(
//             CharIn(
//               min = 32,
//               bitSet = {0},
//               ranges = NonEmptyList(head = (' ', ' '), tail = List())
//             ),
//             Pure(result = ())
//           )
//         )
//       )
//     ),
//     min = 1,
//     maxMinusOne = 2147483647,
//     acc1 = cats.parse.Accumulator0$$anon$11@2a8c529f
//   )
// )

p.parse("hello world")
// res28: Either[Parser.Error, (String, String)] = Right(
//   value = ("", "hello world")
// )

p.parse(" hello world")
// res29: Either[Parser.Error, (String, String)] = Right(
//   value = ("", " hello world")
// )

If we have multiple Parser0 parsers before the Parser - we'd need to use parenthesis like this: (sp.? ~ sp.?).with1 *> alpha.rep.

Error handling

Parser might be interrupted by parsing error. There are two kinds of errors:

import cats.parse.Rfc5234.{alpha, sp}
import cats.parse.Parser

val p1: Parser[Char] = alpha
// p1: Parser[Char] = CharIn(
//   min = 65,
//   bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57},
//   ranges = NonEmptyList(head = ('A', 'Z'), tail = List(('a', 'z')))
// )
val p2: Parser[Char] = sp *> alpha
// p2: Parser[Char] = Map(
//   parser = Prod(
//     first = Void(
//       parser = CharIn(
//         min = 32,
//         bitSet = {0},
//         ranges = NonEmptyList(head = (' ', ' '), tail = List())
//       )
//     ),
//     second = CharIn(
//       min = 65,
//       bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57},
//       ranges = NonEmptyList(head = ('A', 'Z'), tail = List(('a', 'z')))
//     )
//   ),
//   fn = cats.parse.Parser$$Lambda$10825/0x0000000102e4f840@2f0c58cd
// )

// epsilon failure
p1.parse("123")
// res31: Either[Parser.Error, (String, Char)] = Left(
//   value = Error(
//      = 0,
//      = NonEmptyList(
//       head = InRange(offset = 0, lower = 'A', upper = 'Z'),
//       tail = List(InRange(offset = 0, lower = 'a', upper = 'z'))
//     )
//   )
// )

// arresting failure
p2.parse(" 1")
// res32: Either[Parser.Error, (String, Char)] = Left(
//   value = Error(
//      = 1,
//      = NonEmptyList(
//       head = InRange(offset = 1, lower = 'A', upper = 'Z'),
//       tail = List(InRange(offset = 1, lower = 'a', upper = 'z'))
//     )
//   )
// )

We need to make this difference because the first type of error allows us to say that parser is not matching the input before we started to process it and the second error happens while parser processing the input.

Backtrack

Backtrack allows us to convert an arresting failure to epsilon failure. It also rewinds the input to the offset to that used before parsing began. The resulting parser might still be combined with others. Let's look at the example:

import cats.parse.Rfc5234.{digit, sp}

val p = sp *> digit <* sp
// p: cats.parse.Parser[Char] = Map(
//   parser = Prod(
//     first = Map(
//       parser = Prod(
//         first = Void(
//           parser = CharIn(
//             min = 32,
//             bitSet = {0},
//             ranges = NonEmptyList(head = (' ', ' '), tail = List())
//           )
//         ),
//         second = CharIn(
//           min = 48,
//           bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
//           ranges = NonEmptyList(head = ('0', '9'), tail = List())
//         )
//       ),
//       fn = cats.parse.Parser$$Lambda$10825/0x0000000102e4f840@2f0c58cd
//     ),
//     second = Void(
//       parser = CharIn(
//         min = 32,
//         bitSet = {0},
//         ranges = NonEmptyList(head = (' ', ' '), tail = List())
//       )
//     )
//   ),
//   fn = cats.parse.Parser$$Lambda$10830/0x0000000102e57040@2dce2ff3
// )

p.parse(" 1")
// res34: Either[cats.parse.Parser.Error, (String, Char)] = Left(
//   value = Error(
//      = 2,
//      = NonEmptyList(
//       head = InRange(offset = 2, lower = ' ', upper = ' '),
//       tail = List()
//     )
//   )
// )

Parser.Error contains two parameters:

final case class Error(input: String, failedAtOffset: Int, expected: NonEmptyList[Expectation])

case class InRange(offset: Int, lower: Char, upper: Char) extends Expectation

In the error message we see the failed offset and the expected value. There is a lot of expected error types which can be found in source code.

One thing we can do in this situation is providing a fallback parser which can be used in case of error. We can do this by using backtrack (which rewinds the input, so it will be passed to fallback parser as it was before the error) and combining it with orElse operator:

import cats.parse.Rfc5234.{digit, sp}

val p1 = sp *> digit <* sp
// p1: cats.parse.Parser[Char] = Map(
//   parser = Prod(
//     first = Map(
//       parser = Prod(
//         first = Void(
//           parser = CharIn(
//             min = 32,
//             bitSet = {0},
//             ranges = NonEmptyList(head = (' ', ' '), tail = List())
//           )
//         ),
//         second = CharIn(
//           min = 48,
//           bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
//           ranges = NonEmptyList(head = ('0', '9'), tail = List())
//         )
//       ),
//       fn = cats.parse.Parser$$Lambda$10825/0x0000000102e4f840@2f0c58cd
//     ),
//     second = Void(
//       parser = CharIn(
//         min = 32,
//         bitSet = {0},
//         ranges = NonEmptyList(head = (' ', ' '), tail = List())
//       )
//     )
//   ),
//   fn = cats.parse.Parser$$Lambda$10830/0x0000000102e57040@2dce2ff3
// )
val p2 = sp *> digit
// p2: cats.parse.Parser[Char] = Map(
//   parser = Prod(
//     first = Void(
//       parser = CharIn(
//         min = 32,
//         bitSet = {0},
//         ranges = NonEmptyList(head = (' ', ' '), tail = List())
//       )
//     ),
//     second = CharIn(
//       min = 48,
//       bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
//       ranges = NonEmptyList(head = ('0', '9'), tail = List())
//     )
//   ),
//   fn = cats.parse.Parser$$Lambda$10825/0x0000000102e4f840@2f0c58cd
// )

p1.backtrack.orElse(p2).parse(" 1")
// res36: Either[cats.parse.Parser.Error, (String, Char)] = Right(
//   value = ("", '1')
// )

(p1.backtrack | p2 ).parse(" 1")
// res37: Either[cats.parse.Parser.Error, (String, Char)] = Right(
//   value = ("", '1')
// )

Notice that (p1.backtrack | p2) clause is another parser by itself since we're still combining parsers by using orElse.

But we've already used orElse in example before without any backtrack operator, and it worked just fine. Why do we need backtrack now? Let's look at this example:

import cats.parse.Rfc5234.{digit, sp}

val p1 = sp *> digit <* sp
// p1: cats.parse.Parser[Char] = Map(
//   parser = Prod(
//     first = Map(
//       parser = Prod(
//         first = Void(
//           parser = CharIn(
//             min = 32,
//             bitSet = {0},
//             ranges = NonEmptyList(head = (' ', ' '), tail = List())
//           )
//         ),
//         second = CharIn(
//           min = 48,
//           bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
//           ranges = NonEmptyList(head = ('0', '9'), tail = List())
//         )
//       ),
//       fn = cats.parse.Parser$$Lambda$10825/0x0000000102e4f840@2f0c58cd
//     ),
//     second = Void(
//       parser = CharIn(
//         min = 32,
//         bitSet = {0},
//         ranges = NonEmptyList(head = (' ', ' '), tail = List())
//       )
//     )
//   ),
//   fn = cats.parse.Parser$$Lambda$10830/0x0000000102e57040@2dce2ff3
// )
val p2 = sp *> digit
// p2: cats.parse.Parser[Char] = Map(
//   parser = Prod(
//     first = Void(
//       parser = CharIn(
//         min = 32,
//         bitSet = {0},
//         ranges = NonEmptyList(head = (' ', ' '), tail = List())
//       )
//     ),
//     second = CharIn(
//       min = 48,
//       bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
//       ranges = NonEmptyList(head = ('0', '9'), tail = List())
//     )
//   ),
//   fn = cats.parse.Parser$$Lambda$10825/0x0000000102e4f840@2f0c58cd
// )
val p3 = digit
// p3: cats.parse.Parser[Char] = CharIn(
//   min = 48,
//   bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
//   ranges = NonEmptyList(head = ('0', '9'), tail = List())
// )

(p1 | p2).parse(" 1")
// res39: Either[cats.parse.Parser.Error, (String, Char)] = Left(
//   value = Error(
//      = 2,
//      = NonEmptyList(
//       head = InRange(offset = 2, lower = ' ', upper = ' '),
//       tail = List()
//     )
//   )
// )

(p1 | p2 | p3).parse("1")
// res40: Either[cats.parse.Parser.Error, (String, Char)] = Right(
//   value = ("", '1')
// )

The first parser combination is interrupted by arresting failures and the second parsing combination will only suffer from epsilon failures. The second parser works because orElse and | operators actually allows recovering from epsilon failures, but not from arresting failures.

So the backtrack helps us where the left side returns arresting failure.

Soft

This method might look similar to backtrack, but it allows us to proceed the parsing when the right side is returning an epsilon failure. It is really useful for ambiguous parsers when we can't really tell what exactly we are parsing before the end. Let's say we want to parse some input to the search engine which contains fields. This might look like "field:search_query". Let's try to write a parser for this:

import cats.parse.Rfc5234.{alpha, sp}
import cats.parse.Parser
import cats.parse.Parser.{char => pchar}

val searchWord = alpha.rep.string
// searchWord: Parser[String] = StringP(
//   parser = Rep(
//     p1 = CharIn(
//       min = 65,
//       bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57},
//       ranges = NonEmptyList(head = ('A', 'Z'), tail = List(('a', 'z')))
//     ),
//     min = 1,
//     maxMinusOne = 2147483647,
//     acc1 = cats.parse.Accumulator0$$anon$11@2a8c529f
//   )
// )

val fieldValue = alpha.rep.string ~ pchar(':')
// fieldValue: Parser[(String, Unit)] = Prod(
//   first = StringP(
//     parser = Rep(
//       p1 = CharIn(
//         min = 65,
//         bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57},
//         ranges = NonEmptyList(head = ('A', 'Z'), tail = List(('a', 'z')))
//       ),
//       min = 1,
//       maxMinusOne = 2147483647,
//       acc1 = cats.parse.Accumulator0$$anon$11@2a8c529f
//     )
//   ),
//   second = Void(
//     parser = CharIn(
//       min = 58,
//       bitSet = {0},
//       ranges = NonEmptyList(head = (':', ':'), tail = List())
//     )
//   )
// )

val p1 = fieldValue.? ~ (searchWord ~ sp.?).rep.string
// p1: cats.parse.Parser0[(Option[(String, Unit)], String)] = OneOf(
//   all = List(
//     Prod(
//       first = Map(
//         parser = Prod(
//           first = StringP(
//             parser = Rep(
//               p1 = CharIn(
//                 min = 65,
//                 bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57},
//                 ranges = NonEmptyList(
//                   head = ('A', 'Z'),
//                   tail = List(('a', 'z'))
//                 )
//               ),
//               min = 1,
//               maxMinusOne = 2147483647,
//               acc1 = cats.parse.Accumulator0$$anon$11@2a8c529f
//             )
//           ),
//           second = Void(
//             parser = CharIn(
//               min = 58,
//               bitSet = {0},
//               ranges = NonEmptyList(head = (':', ':'), tail = List())
//             )
//           )
//         ),
//         fn = cats.parse.Parser0$$Lambda$10817/0x0000000102e49840@645f1d29
//       ),
//       second = StringP(
//         parser = Rep(
//           p1 = Prod(
//             first = Rep(
//               p1 = CharIn(
//                 min = 65,
//                 bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57},
//                 ranges = NonEmptyList(
//                   head = ('A', 'Z'),
//                   tail = List(('a', 'z'))
//                 )
//               ),
//               min = 1,
//               maxMinusOne = 2147483647,
//               acc1 = cats.parse.Accumulator0$$anon$11@2a8c529f
// ...


p1.parse("title:The Wind Has Risen")
// res42: Either[Parser.Error, (String, (Option[(String, Unit)], String))] = Right(
//   value = ("", (Some(value = ("title", ())), "The Wind Has Risen"))
// )

p1.parse("The Wind Has Risen")
// res43: Either[Parser.Error, (String, (Option[(String, Unit)], String))] = Left(
//   value = Error(
//      = 3,
//      = NonEmptyList(
//       head = InRange(offset = 3, lower = ':', upper = ':'),
//       tail = List()
//     )
//   )
// )

This error happens because we can't really tell if we are parsing the fieldValue before we met a : char. We might do this with by writing two parsers, converting the first one's failure to epsilon failure by backtrack and then providing fallback parser by | operator (which allows the epsilon failures):

val p2 = fieldValue.? ~ (searchWord ~ sp.?).rep.string
// p2: cats.parse.Parser0[(Option[(String, Unit)], String)] = OneOf(
//   all = List(
//     Prod(
//       first = Map(
//         parser = Prod(
//           first = StringP(
//             parser = Rep(
//               p1 = CharIn(
//                 min = 65,
//                 bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57},
//                 ranges = NonEmptyList(
//                   head = ('A', 'Z'),
//                   tail = List(('a', 'z'))
//                 )
//               ),
//               min = 1,
//               maxMinusOne = 2147483647,
//               acc1 = cats.parse.Accumulator0$$anon$11@2a8c529f
//             )
//           ),
//           second = Void(
//             parser = CharIn(
//               min = 58,
//               bitSet = {0},
//               ranges = NonEmptyList(head = (':', ':'), tail = List())
//             )
//           )
//         ),
//         fn = cats.parse.Parser0$$Lambda$10817/0x0000000102e49840@645f1d29
//       ),
//       second = StringP(
//         parser = Rep(
//           p1 = Prod(
//             first = Rep(
//               p1 = CharIn(
//                 min = 65,
//                 bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57},
//                 ranges = NonEmptyList(
//                   head = ('A', 'Z'),
//                   tail = List(('a', 'z'))
//                 )
//               ),
//               min = 1,
//               maxMinusOne = 2147483647,
//               acc1 = cats.parse.Accumulator0$$anon$11@2a8c529f
// ...

val p3 = (searchWord ~ sp.?).rep.string
// p3: Parser[String] = StringP(
//   parser = Rep(
//     p1 = Prod(
//       first = Rep(
//         p1 = CharIn(
//           min = 65,
//           bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57},
//           ranges = NonEmptyList(head = ('A', 'Z'), tail = List(('a', 'z')))
//         ),
//         min = 1,
//         maxMinusOne = 2147483647,
//         acc1 = cats.parse.Accumulator0$$anon$11@2a8c529f
//       ),
//       second = OneOf0(
//         all = List(
//           CharIn(
//             min = 32,
//             bitSet = {0},
//             ranges = NonEmptyList(head = (' ', ' '), tail = List())
//           ),
//           Pure(result = ())
//         )
//       )
//     ),
//     min = 1,
//     maxMinusOne = 2147483647,
//     acc1 = cats.parse.Accumulator0$$anon$11@2a8c529f
//   )
// )

(p2.backtrack | p3).parse("title:The Wind Has Risen")
// res44: Either[Parser.Error, (String, Object with Serializable)] = Right(
//   value = ("", (Some(value = ("title", ())), "The Wind Has Risen"))
// )

(p2.backtrack | p3).parse("The Wind Has Risen")
// res45: Either[Parser.Error, (String, Object with Serializable)] = Right(
//   value = ("", "The Wind Has Risen")
// )

But this problem might be resolved with soft method inside the first parser since the right side of it actually returns an epsilon failure itself:

val fieldValueSoft = alpha.rep.string.soft ~ pchar(':')
// fieldValueSoft: Parser[(String, Unit)] = SoftProd(
//   first = StringP(
//     parser = Rep(
//       p1 = CharIn(
//         min = 65,
//         bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57},
//         ranges = NonEmptyList(head = ('A', 'Z'), tail = List(('a', 'z')))
//       ),
//       min = 1,
//       maxMinusOne = 2147483647,
//       acc1 = cats.parse.Accumulator0$$anon$11@2a8c529f
//     )
//   ),
//   second = Void(
//     parser = CharIn(
//       min = 58,
//       bitSet = {0},
//       ranges = NonEmptyList(head = (':', ':'), tail = List())
//     )
//   )
// )

val p4 = fieldValueSoft.? ~ (searchWord ~ sp.?).rep.string
// p4: cats.parse.Parser0[(Option[(String, Unit)], String)] = OneOf(
//   all = List(
//     Prod(
//       first = Map(
//         parser = SoftProd(
//           first = StringP(
//             parser = Rep(
//               p1 = CharIn(
//                 min = 65,
//                 bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57},
//                 ranges = NonEmptyList(
//                   head = ('A', 'Z'),
//                   tail = List(('a', 'z'))
//                 )
//               ),
//               min = 1,
//               maxMinusOne = 2147483647,
//               acc1 = cats.parse.Accumulator0$$anon$11@2a8c529f
//             )
//           ),
//           second = Void(
//             parser = CharIn(
//               min = 58,
//               bitSet = {0},
//               ranges = NonEmptyList(head = (':', ':'), tail = List())
//             )
//           )
//         ),
//         fn = cats.parse.Parser0$$Lambda$10817/0x0000000102e49840@645f1d29
//       ),
//       second = StringP(
//         parser = Rep(
//           p1 = Prod(
//             first = Rep(
//               p1 = CharIn(
//                 min = 65,
//                 bitSet = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57},
//                 ranges = NonEmptyList(
//                   head = ('A', 'Z'),
//                   tail = List(('a', 'z'))
//                 )
//               ),
//               min = 1,
//               maxMinusOne = 2147483647,
//               acc1 = cats.parse.Accumulator0$$anon$11@2a8c529f
// ...

p4.parse("title:The Wind Has Risen")
// res46: Either[Parser.Error, (String, (Option[(String, Unit)], String))] = Right(
//   value = ("", (Some(value = ("title", ())), "The Wind Has Risen"))
// )

p4.parse("The Wind Has Risen")
// res47: Either[Parser.Error, (String, (Option[(String, Unit)], String))] = Right(
//   value = ("", (None, "The Wind Has Risen"))
// )

So when the right side returns an epsilon failure the soft method allows us to rewind parsed input and try to proceed it's parsing with next parsers (without changing the parser itself!).

JSON parser example

Below is most of a json parser (the string unescaping is elided). This example can give you a feel for what it is like to use this library.

import cats.parse.{Parser0, Parser => P, Numbers}
import org.typelevel.jawn.ast._

object Json {
  private[this] val whitespace: P[Unit] = P.charIn(" \t\r\n").void
  private[this] val whitespaces0: Parser0[Unit] = whitespace.rep0.void

  val parser: P[JValue] = P.recursive[JValue] { recurse =>
    val pnull = P.string("null").as(JNull)
    val bool = P.string("true").as(JBool.True).orElse(P.string("false").as(JBool.False))
    val str = jsonString.map(JString(_))
    val num = Numbers.jsonNumber.map(JNum(_))

    val listSep: P[Unit] =
      P.char(',').soft.surroundedBy(whitespaces0).void

    def rep[A](pa: P[A]): Parser0[List[A]] =
      pa.repSep0(listSep).surroundedBy(whitespaces0)

    val list = rep(recurse).with1
      .between(P.char('['), P.char(']'))
      .map { vs => JArray.fromSeq(vs) }

    val kv: P[(String, JValue)] =
      jsonString ~ (P.char(':').surroundedBy(whitespaces0) *> recurse)

    val obj = rep(kv).with1
      .between(P.char('{'), P.char('}'))
      .map { vs => JObject.fromSeq(vs) }

    P.oneOf(str :: num :: list :: obj :: bool :: pnull :: Nil)
  }

  // any whitespace followed by json followed by whitespace followed by end
  val parserFile: P[JValue] = whitespaces0.with1 *> parser <* (whitespaces0 ~ P.end)
}

Performance

We have a benchmark suite that compares JSON parsing across several commonly used libraries. A recent (2021/11/05) result is below:

[info] Benchmark                         Mode  Cnt    Score    Error  Units
[info] BarBench.catsParseParse           avgt    4   ≈ 10⁻⁴           ms/op
[info] BarBench.fastparseParse           avgt    4   ≈ 10⁻⁴           ms/op
[info] BarBench.jawnParse                avgt    4   ≈ 10⁻⁴           ms/op
[info] BarBench.parboiled2Parse          avgt    4   ≈ 10⁻⁴           ms/op
[info] BarBench.parsleyParseCold         avgt    4    0.064 ±  0.001  ms/op
[info] Bla25Bench.catsParseParse         avgt    4   23.095 ±  0.174  ms/op
[info] Bla25Bench.fastparseParse         avgt    4   15.622 ±  0.414  ms/op
[info] Bla25Bench.jawnParse              avgt    4    7.501 ±  0.143  ms/op
[info] Bla25Bench.parboiled2Parse        avgt    4   18.423 ±  6.094  ms/op
[info] Bla25Bench.parsleyParseCold       avgt    4   30.752 ±  0.279  ms/op
[info] CountriesBench.catsParseParse     avgt    4    7.169 ±  0.041  ms/op
[info] CountriesBench.fastparseParse     avgt    4    5.023 ±  0.023  ms/op
[info] CountriesBench.jawnParse          avgt    4    1.235 ±  0.011  ms/op
[info] CountriesBench.parboiled2Parse    avgt    4    2.936 ±  0.008  ms/op
[info] CountriesBench.parsleyParseCold   avgt    4   11.800 ±  0.162  ms/op
[info] Qux2Bench.catsParseParse          avgt    4    7.031 ±  0.599  ms/op
[info] Qux2Bench.fastparseParse          avgt    4    6.597 ±  0.031  ms/op
[info] Qux2Bench.jawnParse               avgt    4    2.227 ±  0.014  ms/op
[info] Qux2Bench.parboiled2Parse         avgt    4    5.514 ±  0.472  ms/op
[info] Qux2Bench.parsleyParseCold        avgt    4   10.327 ±  0.293  ms/op
[info] StringInBenchmarks.oneOfParse     avgt    4   88.105 ±  2.658  ns/op
[info] StringInBenchmarks.stringInParse  avgt    4  129.246 ±  1.820  ns/op
[info] Ugh10kBench.catsParseParse        avgt    4   53.679 ±  1.385  ms/op
[info] Ugh10kBench.fastparseParse        avgt    4   45.165 ±  0.356  ms/op
[info] Ugh10kBench.jawnParse             avgt    4   11.404 ±  0.068  ms/op
[info] Ugh10kBench.parboiled2Parse       avgt    4   31.984 ±  0.748  ms/op
[info] Ugh10kBench.parsleyParseCold      avgt    4   77.150 ±  1.093  ms/op

Note that parboiled and fastparse both use macros that make them very difficult to port to Dotty. Jawn is a specialized and optimized JSON parser, so that can be considered an upper bound on performance. Keep in mind that parser performance depends both on the parsing library but also how the parser is written, but these results suggest that this library is already quite competitive.

Migrating from Fastparse

You should find all the Fastparse methods you are used to. If not, feel free to open an issue. There are a few things to keep in mind:

  1. In fastparse, you wrap a parser in P(...) to make the interior lazy. Following cats, to get a lazily constructed parser use Parser.defer or cats.Defer[Parser].defer.
  2. In fastparse the ~ operator does tuple concatenation. This can be nice, but also complex to see what the resulting type is. In cats-parse, ~ always returns a Tuple2 containing the parsed values from the left and right. To recover fastparse-like behavior, use cats syntax (pa, pb, pc...).tupled.
  3. In fastparse, backtracking is opt-out by using cuts. In cats-parse, backtracking is opt-in using .backtrack. Put another way, normal product operations in cats-parse are like ~/ in fastparse.
  4. In cats-parse, using *>, <*, and .void methods can be a significant optimization: if you don't need a result, communicate that to the library with those methods.

Getting and Giving Help

We welcome new contributors and new maintainers. Please feel free to open issues and PRs. If you have any problem using the library, an issue is the best way to ask a question until we flush out more documentation.