combinator

parsley.cats.combinator
object combinator

This module contains pre-made combinators that are very useful for a variety of purposes, specialised to cats.

In particular, it contains functionality found normally in parsley.combinator, but returning the cats NonEmptyList instead of a regular Scala List.

Attributes

Since

1.2.0

Source
combinator.scala
Graph
Supertypes
class Object
trait Matchable
class Any
Self type
combinator.type

Members list

Value members

Concrete methods

def endBy1[A](p: Parsley[A], sep: => Parsley[_]): Parsley[NonEmptyList[A]]

This combinator parses one or more occurrences of p, separated and ended by sep.

This combinator parses one or more occurrences of p, separated and ended by sep.

Parses p followed by sep one or more times. The results of the p's, x1 through xn, are returned as NonEmptyList.of(x1, .., xn). If p or sep fails having consumed input, the whole parser fails.

Value parameters

p

the parser whose results are collected into a list.

sep

the delimiter that must be parsed between every p.

Attributes

Returns

a parser that parses p delimited by sep, returning the non-empty list of p's results.

Since

1.2.0

Example

scala> ...
scala> val args = endBy1(int, string(";\n"))
scala> args.parse("7;\n3;\n2")
val res0 = Failure(..)
scala> args.parse("")
val res1 = Failure(..)
scala> args.parse("1;\n")
val res2 = Success(NonEmptyList.of(1))
scala> args.parse("1;\n2;\n")
val res3 = Success(NonEmptyList.of(1, 2))
Source
combinator.scala
def sepBy1[A](p: Parsley[A], sep: => Parsley[_]): Parsley[NonEmptyList[A]]

This combinator parses one or more occurrences of p, separated by sep.

This combinator parses one or more occurrences of p, separated by sep.

First parses a p. Then parses sep followed by p until there are no more seps. The results of the p's, x1 through xn, are returned as NonEmptyList.of(x1, .., xn). If p or sep fails having consumed input, the whole parser fails. Requires at least one p to have been parsed.

Value parameters

p

the parser whose results are collected into a list.

sep

the delimiter that must be parsed between every p.

Attributes

Returns

a parser that parses p delimited by sep, returning the non-empty list of p's results.

Since

1.2.0

Example

scala> ...
scala> val args = sepBy1(int, string(", "))
scala> args.parse("7, 3, 2")
val res0 = Success(NonEmptyList.of(7, 3, 2))
scala> args.parse("")
val res1 = Failure(..)
scala> args.parse("1")
val res2 = Success(NonEmptyList.of(1))
scala> args.parse("1, 2, ")
val res3 = Failure(..) // no trailing comma allowed
Source
combinator.scala
def sepEndBy1[A](p: Parsley[A], sep: => Parsley[_]): Parsley[NonEmptyList[A]]

This combinator parses one or more occurrences of p, separated and optionally ended by sep.

This combinator parses one or more occurrences of p, separated and optionally ended by sep.

First parses a p. Then parses sep followed by p until there are no more: if a final sep exists, this is parsed. The results of the p's, x1 through xn, are returned as NonEmptyList.of(x1, .., xn). If p or sep fails having consumed input, the whole parser fails. Requires at least one p to have been parsed.

Value parameters

p

the parser whose results are collected into a list.

sep

the delimiter that must be parsed between every p.

Attributes

Returns

a parser that parses p delimited by sep, returning the non-empty list of p's results.

Since

1.2.0

Example

scala> ...
scala> val args = sepEndBy1(int, string(";\n"))
scala> args.parse("7;\n3;\n2")
val res0 = Success(NonEmptyList.of(7, 3, 2))
scala> args.parse("")
val res1 = Failure(..)
scala> args.parse("1")
val res2 = Success(NonEmptyList.of(1))
scala> args.parse("1;\n2;\n")
val res3 = Success(NonEmptyList.of(1, 2))
Source
combinator.scala
def some[A](p: Parsley[A]): Parsley[NonEmptyList[A]]

This combinator repeatedly parses a given parser one or more times, collecting the results into a list.

This combinator repeatedly parses a given parser one or more times, collecting the results into a list.

Parses a given parser, p, repeatedly until it fails. If p failed having consumed input, this combinator fails. Otherwise when p fails without consuming input, this combinator will return all of the results, x1 through xn (with n >= 1), in a non-empty list: NonEmptyList.of(x1, .., xn). If p was not successful at least one time, this combinator fails.

Value parameters

p

the parser to execute multiple times.

Attributes

Returns

a parser that parses p until it fails, returning the non-empty list of all the successful results.

Since

1.2.0

Example

scala> import parsley.character.string
scala> import parsley.cats.combinator.some
scala> val p = some(string("ab"))
scala> p.parse("")
val res0 = Failure(..)
scala> p.parse("ab")
val res1 = Success(NonEmptyList.of("ab"))
scala> p.parse("abababab")
val res2 = Success(NonEmptyList.of("ab", "ab", "ab", "ab"))
scala> p.parse("aba")
val res3 = Failure(..)
Source
combinator.scala
def someTill[A](p: Parsley[A], end: Parsley[_]): Parsley[NonEmptyList[A]]

This combinator repeatedly parses a given parser one or more times, until the end parser succeeds, collecting the results into a list.

This combinator repeatedly parses a given parser one or more times, until the end parser succeeds, collecting the results into a list.

First ensures that trying to parse end fails, then tries to parse p. If it succeed then it will repeatedly: try to parse end, if it fails without consuming input, then parses p, which must succeed. When end does succeed, this combinator will return all of the results generated by p, x1 through xn (with n >= 1), in a non-empty list: NonEmptyList.of(x1, .., xn). The parser p must succeed at least once before end succeeds.

Value parameters

end

the parser that stops the parsing of p.

p

the parser to execute multiple times.

Attributes

Returns

a parser that parses p until end succeeds, returning the non-empty list of all the successful results.

Since

1.3.0

Example

This can be useful for scanning comments:

scala> import parsley.character.{string, item, endOfLine}
scala> import parsley.cats.combinator.someTill
scala> val comment = string("//") *> someTill(item, endOfLine)
scala> p.parse("//hello world")
val res0 = Failure(..)
scala> p.parse("//hello world\n")
val res1 = Success(NonEmptyList.of('h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'))
scala> p.parse("//\n")
val res2 = Failure(..)
scala> p.parse("//a\n")
val res3 = Success(NonEmptyList.of('a'))
Source
combinator.scala

Deprecated methods

def someUntil[A](p: Parsley[A], end: Parsley[_]): Parsley[NonEmptyList[A]]

This combinator repeatedly parses a given parser one or more times, until the end parser succeeds, collecting the results into a list.

This combinator repeatedly parses a given parser one or more times, until the end parser succeeds, collecting the results into a list.

First ensures that trying to parse end fails, then tries to parse p. If it succeed then it will repeatedly: try to parse end, if it fails without consuming input, then parses p, which must succeed. When end does succeed, this combinator will return all of the results generated by p, x1 through xn (with n >= 1), in a non-empty list: NonEmptyList.of(x1, .., xn). The parser p must succeed at least once before end succeeds.

Value parameters

end

the parser that stops the parsing of p.

p

the parser to execute multiple times.

Attributes

Returns

a parser that parses p until end succeeds, returning the non-empty list of all the successful results.

Since

1.2.0

Example

This can be useful for scanning comments:

scala> import parsley.character.{string, item, endOfLine}
scala> import parsley.cats.combinator.someUntil
scala> val comment = string("//") *> someUntil(item, endOfLine)
scala> p.parse("//hello world")
val res0 = Failure(..)
scala> p.parse("//hello world\n")
val res1 = Success(NonEmptyList.of('h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'))
scala> p.parse("//\n")
val res2 = Failure(..)
scala> p.parse("//a\n")
val res3 = Success(NonEmptyList.of('a'))
Deprecated
true
Source
combinator.scala