Parser

cats.parse.Parser
See theParser companion class
object Parser

Attributes

Companion
class
Source
Parser.scala
Graph
Supertypes
trait Sum
trait Mirror
class Object
trait Matchable
class Any
Self type
Parser.type

Members list

Type members

Classlikes

class Error(val input: Option[String], val failedAtOffset: Int, val expected: NonEmptyList[Expectation]) extends Product, Serializable

Represents where a failure occurred and all the expectations that were broken

Represents where a failure occurred and all the expectations that were broken

Attributes

Companion
object
Source
Parser.scala
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
object Error extends AbstractFunction2[Int, NonEmptyList[Expectation], Error], Serializable

Attributes

Companion
class
Source
Parser.scala
Supertypes
trait Serializable
class AbstractFunction2[Int, NonEmptyList[Expectation], Error]
trait (Int, NonEmptyList[Expectation]) => Error
class Object
trait Matchable
class Any
Show all
Self type
Error.type

Attributes

Source
Parser.scala
Supertypes
class Object
trait Matchable
class Any
Self type
sealed abstract class Expectation

An expectation reports the kind or parsing error and where it occured.

An expectation reports the kind or parsing error and where it occured.

Attributes

Companion
object
Source
Parser.scala
Supertypes
class Object
trait Matchable
class Any
Known subtypes
class EndOfString
class Fail
class FailWith
class InRange
class Length
class OneOfStr
class WithContext
Show all
object Expectation

Attributes

Companion
class
Source
Parser.scala
Supertypes
trait Sum
trait Mirror
class Object
trait Matchable
class Any
Self type
final class Soft[+A](parser: Parser[A]) extends Soft0[A]

If we can parse this then that, do so, if we fail that without consuming, rewind before this without consuming.

If we can parse this then that, do so, if we fail that without consuming, rewind before this without consuming. If either consume 1 or more, do not rewind

Attributes

Source
Parser.scala
Supertypes
class Soft0[A]
class Object
trait Matchable
class Any
sealed class Soft0[+A](parser: Parser0[A])

If we can parse this then that, do so, if we fail that without consuming, rewind before this without consuming.

If we can parse this then that, do so, if we fail that without consuming, rewind before this without consuming. If either consume 1 or more, do not rewind

Attributes

Source
Parser.scala
Supertypes
class Object
trait Matchable
class Any
Known subtypes
class Soft[A]
final class Soft01[+A](val parser: Parser0[A]) extends AnyVal

If we can parse this then that, do so, if we fail that without consuming, rewind before this without consuming.

If we can parse this then that, do so, if we fail that without consuming, rewind before this without consuming. If either consume 1 or more, do not rewind

Attributes

Source
Parser.scala
Supertypes
class AnyVal
trait Matchable
class Any
final class With1[+A](val parser: Parser0[A]) extends AnyVal

Enables syntax to access product01, product and flatMap01 This helps us build Parser instances when starting from a Parser0

Enables syntax to access product01, product and flatMap01 This helps us build Parser instances when starting from a Parser0

Attributes

Source
Parser.scala
Supertypes
class AnyVal
trait Matchable
class Any

Inherited types

type MirroredElemLabels <: Tuple

The names of the product elements

The names of the product elements

Attributes

Inherited from:
Mirror
Source
Mirror.scala
type MirroredLabel <: String

The name of the type

The name of the type

Attributes

Inherited from:
Mirror
Source
Mirror.scala

Value members

Concrete methods

def anyChar: Parser[Char]

Parse 1 character from the string

Parse 1 character from the string

Attributes

Source
Parser.scala
def as[B](pa: Parser[Any], b: B): Parser[B]

Replaces parsed values with the given value.

Replaces parsed values with the given value.

Attributes

Source
Parser.scala
def as0[B](pa: Parser0[Any], b: B): Parser0[B]

Replaces parsed values with the given value.

Replaces parsed values with the given value.

Attributes

Source
Parser.scala
def backtrack[A](pa: Parser[A]): Parser[A]

If we fail, rewind the offset back so that we can try other branches.

If we fail, rewind the offset back so that we can try other branches. This tends to harm debuggability and ideally should be minimized

Attributes

Source
Parser.scala
def backtrack0[A](pa: Parser0[A]): Parser0[A]

If we fail, rewind the offset back so that we can try other branches.

If we fail, rewind the offset back so that we can try other branches. This tends to harm debuggability and ideally should be minimized

Attributes

Source
Parser.scala

return the current Caret (offset, line, column) this is a bit more expensive that just the index

return the current Caret (offset, line, column) this is a bit more expensive that just the index

Attributes

Source
Parser.scala
def char(c: Char): Parser[Unit]

parse a single character

parse a single character

Attributes

Source
Parser.scala
def charIn(cs: Iterable[Char]): Parser[Char]

An empty iterable is the same as fail

An empty iterable is the same as fail

Attributes

Source
Parser.scala
def charIn(c0: Char, cs: Char*): Parser[Char]

parse one of a given set of characters

parse one of a given set of characters

Attributes

Source
Parser.scala
def charWhere(fn: Char => Boolean): Parser[Char]

parse one character that matches a given function

parse one character that matches a given function

Attributes

Source
Parser.scala
def charsWhile(fn: Char => Boolean): Parser[String]

Parse a string while the given function is true parses at least one character

Parse a string while the given function is true parses at least one character

Attributes

Source
Parser.scala
def charsWhile0(fn: Char => Boolean): Parser0[String]

Parse a string while the given function is true

Parse a string while the given function is true

Attributes

Source
Parser.scala
def defer[A](pa: => Parser[A]): Parser[A]

Lazily create a Parser This is useful to create some recursive parsers see Defer0[Parser].fix

Lazily create a Parser This is useful to create some recursive parsers see Defer0[Parser].fix

Attributes

Source
Parser.scala
def defer0[A](pa: => Parser0[A]): Parser0[A]

Lazily create a Parser0 This is useful to create some recursive parsers see Defer0[Parser].fix

Lazily create a Parser0 This is useful to create some recursive parsers see Defer0[Parser].fix

Attributes

Source
Parser.scala
def eitherOr[A, B](first: Parser[B], second: Parser[A]): Parser[Either[A, B]]

If the first parser fails to parse its input with an epsilon error, try the second parser instead.

If the first parser fails to parse its input with an epsilon error, try the second parser instead.

If the first parser fails with an arresting error, the second parser won't be tried.

Backtracking may be used on the first parser to allow the second one to pick up after any error, resetting any state that was modified by the first parser.

Attributes

Source
Parser.scala
def eitherOr0[A, B](first: Parser0[B], second: Parser0[A]): Parser0[Either[A, B]]

If the first parser fails to parse its input with an epsilon error, try the second parser instead.

If the first parser fails to parse its input with an epsilon error, try the second parser instead.

If the first parser fails with an arresting error, the second parser won't be tried.

Backtracking may be used on the first parser to allow the second one to pick up after any error, resetting any state that was modified by the first parser.

Attributes

Source
Parser.scala
def end: Parser0[Unit]

succeeds when we are at the end

succeeds when we are at the end

Attributes

Source
Parser.scala
def fail[A]: Parser[A]

A parser that always fails with an epsilon failure

A parser that always fails with an epsilon failure

Attributes

Source
Parser.scala
def failWith[A](message: String): Parser[A]

A parser that always fails with an epsilon failure and a given message this is generally used with flatMap to validate a result beyond the literal parsing.

A parser that always fails with an epsilon failure and a given message this is generally used with flatMap to validate a result beyond the literal parsing.

e.g. parsing a number then validate that it is bounded.

Attributes

Source
Parser.scala
def flatMap0[A, B](pa: Parser0[A])(fn: A => Parser0[B]): Parser0[B]

Standard monadic flatMap Avoid this function if possible.

Standard monadic flatMap Avoid this function if possible. If you can instead use product, ~, *>, or <* use that. flatMap always has to allocate a parser, and the parser is less amenable to optimization

Attributes

Source
Parser.scala
def flatMap01[A, B](pa: Parser0[A])(fn: A => Parser[B]): Parser[B]

Standard monadic flatMap where you end with a Parser Avoid this function if possible.

Standard monadic flatMap where you end with a Parser Avoid this function if possible. If you can instead use product, ~, *>, or <* use that. flatMap always has to allocate a parser, and the parser is less amenable to optimization

Attributes

Source
Parser.scala
def flatMap10[A, B](pa: Parser[A])(fn: A => Parser0[B]): Parser[B]

Standard monadic flatMap where you start with a Parser Avoid this function if possible.

Standard monadic flatMap where you start with a Parser Avoid this function if possible. If you can instead use product, ~, *>, or <* use that. flatMap always has to allocate a parser, and the parser is less amenable to optimization

Attributes

Source
Parser.scala
def fromCharMap[A](charMap: Map[Char, A]): Parser[A]

Convert a Map[Char, A] to Parser[A] first match a character, then map it to a value A

Convert a Map[Char, A] to Parser[A] first match a character, then map it to a value A

Attributes

Source
Parser.scala
def fromStringMap[A](stringMap: Map[String, A]): Parser[A]

Convert a Map[String, A] to Parser[A] first match a character, then map it to a value A This throws if any of the keys are the empty string

Convert a Map[String, A] to Parser[A] first match a character, then map it to a value A This throws if any of the keys are the empty string

Attributes

Source
Parser.scala
def fromStringMap0[A](stringMap: Map[String, A]): Parser0[A]

Convert a Map[String, A] to Parser[A] first match a character, then map it to a value A

Convert a Map[String, A] to Parser[A] first match a character, then map it to a value A

Attributes

Source
Parser.scala
def ignoreCase(str: String): Parser[Unit]

Parse a given string, in a case-insensitive manner, or fail.

Parse a given string, in a case-insensitive manner, or fail. This backtracks on failure this is an error if the string is empty

Attributes

Source
Parser.scala
def ignoreCase0(str: String): Parser0[Unit]

Parse a potentially empty string, in a case-insensitive manner, or fail.

Parse a potentially empty string, in a case-insensitive manner, or fail. This backtracks on failure

Attributes

Source
Parser.scala
def ignoreCaseChar(c: Char): Parser[Unit]

Ignore the case of a single character If you want to know if it is upper or lower, use .string to capture the string and then map to process the result.

Ignore the case of a single character If you want to know if it is upper or lower, use .string to capture the string and then map to process the result.

Attributes

Source
Parser.scala
def ignoreCaseCharIn(cs: Iterable[Char]): Parser[Char]

Parse any single character in a set of characters as lower or upper case

Parse any single character in a set of characters as lower or upper case

Attributes

Source
Parser.scala
def ignoreCaseCharIn(c0: Char, cs: Char*): Parser[Char]

Parse any single character in a set of characters as lower or upper case

Parse any single character in a set of characters as lower or upper case

Attributes

Source
Parser.scala
def index: Parser0[Int]

return the current position in the string we are parsing.

return the current position in the string we are parsing. This lets you record position information in your ASTs you are parsing

Attributes

Source
Parser.scala
def length(len: Int): Parser[String]

Parse the next len characters where len > 0 if (len < 1) throw IllegalArgumentException

Parse the next len characters where len > 0 if (len < 1) throw IllegalArgumentException

Attributes

Source
Parser.scala
def length0(len: Int): Parser0[String]

if len < 1, the same as pure("") else length(len)

if len < 1, the same as pure("") else length(len)

Attributes

Source
Parser.scala
def map[A, B](p: Parser[A])(fn: A => B): Parser[B]

transform a Parser result

transform a Parser result

Attributes

Source
Parser.scala
def map0[A, B](p: Parser0[A])(fn: A => B): Parser0[B]

transform a Parser0 result

transform a Parser0 result

Attributes

Source
Parser.scala
def not(pa: Parser0[Any]): Parser0[Unit]

returns a parser that succeeds if the current parser fails.

returns a parser that succeeds if the current parser fails. Note, this parser backtracks (never returns an arresting failure)

Attributes

Source
Parser.scala
def oneOf[A](parsers: List[Parser[A]]): Parser[A]

go through the list of parsers trying each as long as they are epsilon failures (don't advance) see @backtrack if you want to do backtracking.

go through the list of parsers trying each as long as they are epsilon failures (don't advance) see @backtrack if you want to do backtracking.

This is the same as parsers.foldLeft(fail)(_.orElse(_))

recommended style: oneOf(p1 :: p2 :: p3 :: Nil) rather than oneOf(List(p1, p2, p3))

Note: order matters here, since we don't backtrack by default.

Attributes

Source
Parser.scala
def oneOf0[A](ps: List[Parser0[A]]): Parser0[A]

go through the list of parsers trying each as long as they are epsilon failures (don't advance) see @backtrack if you want to do backtracking.

go through the list of parsers trying each as long as they are epsilon failures (don't advance) see @backtrack if you want to do backtracking.

This is the same as parsers.foldLeft(fail)(_.orElse(_))

recommended style: oneOf(p1 :: p2 :: p3 :: Nil) rather than oneOf(List(p1, p2, p3))

Note: order matters here, since we don't backtrack by default.

Attributes

Source
Parser.scala
def peek(pa: Parser0[Any]): Parser0[Unit]

a parser that consumes nothing when it succeeds, basically rewind on success

a parser that consumes nothing when it succeeds, basically rewind on success

Attributes

Source
Parser.scala
def product0[A, B](first: Parser0[A], second: Parser0[B]): Parser0[(A, B)]

parse first then second

parse first then second

Attributes

Source
Parser.scala
def product01[A, B](first: Parser0[A], second: Parser[B]): Parser[(A, B)]

product with the second argument being a Parser

product with the second argument being a Parser

Attributes

Source
Parser.scala
def product10[A, B](first: Parser[A], second: Parser0[B]): Parser[(A, B)]

product with the first argument being a Parser

product with the first argument being a Parser

Attributes

Source
Parser.scala
def pure[A](a: A): Parser0[A]

Don't advance in the parsed string, just return a This is used by the Applicative typeclass.

Don't advance in the parsed string, just return a This is used by the Applicative typeclass.

Attributes

Source
Parser.scala
def recursive[A](fn: Parser[A] => Parser[A]): Parser[A]

Build a recursive parser by assuming you have it Useful for parsing recursive structures like JSON.

Build a recursive parser by assuming you have it Useful for parsing recursive structures like JSON.

Attributes

Source
Parser.scala
def repAs[A, B](p1: Parser[A], min: Int)(implicit acc: Accumulator[A, B]): Parser[B]

Repeat the parser min or more times

Repeat the parser min or more times

The parser fails if it can't match at least min times

Attributes

Throws
Source
Parser.scala
def repAs[A, B](p1: Parser[A], min: Int, max: Int)(implicit acc: Accumulator[A, B]): Parser[B]

Repeat the parser min or more times, but no more than max

Repeat the parser min or more times, but no more than max

The parser fails if it can't match at least min times After repeating the parser max times, the parser completes succesfully

Attributes

Throws

java.lang.IllegalArgumentException if min < 1 or max < min

Source
Parser.scala
def repAs0[A, B](p1: Parser[A])(implicit acc: Accumulator0[A, B]): Parser0[B]

Repeat the parser 0 or more times

Repeat the parser 0 or more times

Attributes

Note

this can wind up parsing nothing

Source
Parser.scala
def repAs0[A, B](p1: Parser[A], max: Int)(implicit acc: Accumulator0[A, B]): Parser0[B]

Repeat the parser 0 or more times, but no more than max

Repeat the parser 0 or more times, but no more than max

It may seem weird to accept 0 here, but without, composing this method becomes more complex. Since and empty parse is possible for this method, we do allow max = 0

Attributes

Throws
Note

this can wind up parsing nothing

Source
Parser.scala
def repExactlyAs[A, B](p: Parser[A], times: Int)(implicit acc: Accumulator[A, B]): Parser[B]

Repeat the parser exactly times times

Repeat the parser exactly times times

Attributes

Throws
Source
Parser.scala
def repSep[A](p1: Parser[A], sep: Parser0[Any]): Parser[NonEmptyList[A]]

Repeat 1 or more times with a separator

Repeat 1 or more times with a separator

Attributes

Source
Parser.scala
def repSep[A](p1: Parser[A], min: Int, sep: Parser0[Any]): Parser[NonEmptyList[A]]

Repeat min or more times with a separator, at least once.

Repeat min or more times with a separator, at least once.

Attributes

Throws
Source
Parser.scala
def repSep[A](p1: Parser[A], min: Int, max: Int, sep: Parser0[Any]): Parser[NonEmptyList[A]]

Repeat min or more, up to max times with a separator, at least once.

Repeat min or more, up to max times with a separator, at least once.

Attributes

Throws

java.lang.IllegalArgumentException if min <= 0 or max < min

Source
Parser.scala
def repSep0[A](p1: Parser[A], sep: Parser0[Any]): Parser0[List[A]]

Repeat 0 or more times with a separator

Repeat 0 or more times with a separator

Attributes

Source
Parser.scala
def repSep0[A](p1: Parser[A], min: Int, sep: Parser0[Any]): Parser0[List[A]]

Repeat min or more times with a separator.

Repeat min or more times with a separator.

Attributes

Throws
Source
Parser.scala
def repSep0[A](p1: Parser[A], min: Int, max: Int, sep: Parser0[Any]): Parser0[List[A]]

Repeat min or more, up to max times with a separator.

Repeat min or more, up to max times with a separator.

Attributes

Throws

java.lang.IllegalArgumentException if min < 0 or max < min

Source
Parser.scala
def repUntil[A](p: Parser[A], end: Parser0[Any]): Parser[NonEmptyList[A]]

parse one or more times until Parser end succeeds.

parse one or more times until Parser end succeeds.

Attributes

Source
Parser.scala
def repUntil0[A](p: Parser[A], end: Parser0[Any]): Parser0[List[A]]

parse zero or more times until Parser end succeeds.

parse zero or more times until Parser end succeeds.

Attributes

Source
Parser.scala
def repUntilAs[A, B](p: Parser[A], end: Parser0[Any])(implicit acc: Accumulator[A, B]): Parser[B]

parse one or more times until Parser end succeeds.

parse one or more times until Parser end succeeds.

Attributes

Source
Parser.scala
def repUntilAs0[A, B](p: Parser[A], end: Parser0[Any])(implicit acc: Accumulator0[A, B]): Parser0[B]

parse zero or more times until Parser end succeeds.

parse zero or more times until Parser end succeeds.

Attributes

Source
Parser.scala
def select[A, B](p: Parser[Either[A, B]])(fn: Parser0[A => B]): Parser[B]

Parser version of select

Parser version of select

Attributes

Source
Parser.scala
def select0[A, B](p: Parser0[Either[A, B]])(fn: Parser0[A => B]): Parser0[B]

Parse p and if we get the Left side, parse fn This function name comes from seletive functors.

Parse p and if we get the Left side, parse fn This function name comes from seletive functors. This should be more efficient than flatMap since the fn Parser0 is evaluated once, not on every item parsed

Attributes

Source
Parser.scala
def softProduct0[A, B](first: Parser0[A], second: Parser0[B]): Parser0[(A, B)]

softProduct, a variant of product A soft product backtracks if the first succeeds and the second is an epsilon-failure.

softProduct, a variant of product A soft product backtracks if the first succeeds and the second is an epsilon-failure. By contrast product will be a failure in that case

see @Parser.soft

Attributes

Source
Parser.scala
def softProduct01[A, B](first: Parser0[A], second: Parser[B]): Parser[(A, B)]

softProduct with the second argument being a Parser A soft product backtracks if the first succeeds and the second is an epsilon-failure.

softProduct with the second argument being a Parser A soft product backtracks if the first succeeds and the second is an epsilon-failure. By contrast product will be a failure in that case

see @Parser.soft

Attributes

Source
Parser.scala
def softProduct10[A, B](first: Parser[A], second: Parser0[B]): Parser[(A, B)]

softProduct with the first argument being a Parser A soft product backtracks if the first succeeds and the second is an epsilon-failure.

softProduct with the first argument being a Parser A soft product backtracks if the first succeeds and the second is an epsilon-failure. By contrast product will be a failure in that case

see @Parser.soft

Attributes

Source
Parser.scala
def start: Parser0[Unit]

succeeds when we are at the start

succeeds when we are at the start

Attributes

Source
Parser.scala
def string(str: String): Parser[Unit]

Parse a given string or fail.

Parse a given string or fail. This backtracks on failure this is an error if the string is empty

Attributes

Source
Parser.scala
def string(pa: Parser[Any]): Parser[String]

Discard the result A and instead capture the matching string this is optimized to avoid internal allocations

Discard the result A and instead capture the matching string this is optimized to avoid internal allocations

Attributes

Source
Parser.scala
def string0(str: String): Parser0[Unit]

Parse a potentially empty string or fail.

Parse a potentially empty string or fail. This backtracks on failure

Attributes

Source
Parser.scala
def string0(pa: Parser0[Any]): Parser0[String]

Discard the result A and instead capture the matching string this is optimized to avoid internal allocations

Discard the result A and instead capture the matching string this is optimized to avoid internal allocations

Attributes

Source
Parser.scala
def stringIn(strings: Iterable[String]): Parser[String]

Parse the longest matching string between alternatives.

Parse the longest matching string between alternatives. The order of the strings does not matter.

If no string matches, this parser results in an epsilon failure.

It is an error to pass the empty string here, if you need that see stringIn0

Attributes

Source
Parser.scala
def stringIn0(strings: Iterable[String]): Parser0[String]

Version of stringIn that allows the empty string

Version of stringIn that allows the empty string

Attributes

Source
Parser.scala
def tailRecM[A, B](init: A)(fn: A => Parser[Either[A, B]]): Parser[B]

tail recursive monadic flatMaps on Parser This is a rarely used function, but needed to implement cats.FlatMap Avoid this function if possible.

tail recursive monadic flatMaps on Parser This is a rarely used function, but needed to implement cats.FlatMap Avoid this function if possible. If you can instead use product, ~, *>, or <* use that. flatMap always has to allocate a parser, and the parser is less amenable to optimization

Attributes

Source
Parser.scala
def tailRecM0[A, B](init: A)(fn: A => Parser0[Either[A, B]]): Parser0[B]

tail recursive monadic flatMaps This is a rarely used function, but needed to implement cats.FlatMap Avoid this function if possible.

tail recursive monadic flatMaps This is a rarely used function, but needed to implement cats.FlatMap Avoid this function if possible. If you can instead use product, ~, *>, or <* use that. flatMap always has to allocate a parser, and the parser is less amenable to optimization

Attributes

Source
Parser.scala
def until(p: Parser0[Any]): Parser[String]

parse one or more characters as long as they don't match p

parse one or more characters as long as they don't match p

Attributes

Source
Parser.scala
def until0(p: Parser0[Any]): Parser0[String]

parse zero or more characters as long as they don't match p this is useful for parsing comment strings, for instance.

parse zero or more characters as long as they don't match p this is useful for parsing comment strings, for instance.

Attributes

Source
Parser.scala
def void(pa: Parser[Any]): Parser[Unit]

discard the value in a Parser.

discard the value in a Parser. This is an optimization because we remove trailing map operations and don't allocate internal data structures This function is called internal to Functor.as and Apply.*> and Apply.<* so those are good uses.

Attributes

Source
Parser.scala
def void0(pa: Parser0[Any]): Parser0[Unit]

discard the value in a Parser.

discard the value in a Parser. This is an optimization because we remove trailing map operations and don't allocate internal data structures This function is called internal to Functor.as and Apply.*> and Apply.<* so those are good uses.

Attributes

Source
Parser.scala
def withContext[A](p: Parser[A], ctx: String): Parser[A]

Add a context string to Errors to aid debugging

Add a context string to Errors to aid debugging

Attributes

Source
Parser.scala
def withContext0[A](p0: Parser0[A], ctx: String): Parser0[A]

Add a context string to Errors to aid debugging

Add a context string to Errors to aid debugging

Attributes

Source
Parser.scala
def withString[A](pa: Parser[A]): Parser[(A, String)]

Return the value and the input string matched

Return the value and the input string matched

Attributes

Source
Parser.scala
def withString0[A](pa: Parser0[A]): Parser0[(A, String)]

Return the value and the input string matched

Return the value and the input string matched

Attributes

Source
Parser.scala

Concrete fields

val Fail: Parser[Nothing]

A parser that always fails with an epsilon failure

A parser that always fails with an epsilon failure

Attributes

Source
Parser.scala
val unit: Parser0[Unit]

A parser that returns unit

A parser that returns unit

Attributes

Source
Parser.scala

Implicits

Implicits