quick
This serves as a quick way of importing everything from Parsley
, character
, position
, lift
, and ap
. Other packages are not included in this, but this should be a good baseline for any non-lexing/expression parsing work.
Attributes
- Since
-
5.0.0
- Source
- quick.scala
- Graph
-
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
quick.type
Members list
Grouped members
Primitive Combinators
These combinators are specific to parser combinators. In one way or another, they influence how a parser consumes input, or under what conditions a parser does or does not fail. These are really important for most practical parsing considerations, although lookAhead
is much less well used.
This combinator parses its argument p
, but rolls back any consumed input on failure.
This combinator parses its argument p
, but rolls back any consumed input on failure.
If the parser p
succeeds, then atomic(p)
has no effect. However, if p
failed, then any input that it consumed is rolled back. This ensures that the parser p
is all-or-nothing when consuming input. While there are many legimate uses for all-or-nothing behaviour, one notable, if discouraged, use is to allow the |
combinator to backtrack -- recall it can only parse its alternative if the first failed without consuming input. This is discouraged, however, as it can affect the complexity of the parser and harm error messages.
Value parameters
- p
-
the parser to execute, if it fails, it will not have consumed input.
Attributes
- Returns
-
a parser that tries
p
, but never consumes input if it fails. - Since
-
4.4.0
- Example
-
scala> import parsley.character.string, parsley.Parsley.atomic scala> (string("abc") | string("abd")).parse("abd") val res0 = Failure(..) // first parser consumed a, so no backtrack scala> (atomic(string("abc")) | string("abd")).parse("abd") val res1 = Success("abd") // first parser does not consume input on failure now
- Inherited from:
- ParsleyImpl (hidden)
- Source
- Parsley.scala
This combinator parses its argument p
, but does not consume input if it succeeds.
This combinator parses its argument p
, but does not consume input if it succeeds.
If the parser p
succeeds, then lookAhead(p)
will roll back any input consumed whilst parsing p
. If p
fails, however, then the whole combinator fails and any input consumed remains consumed. If this behaviour is not desirable, consider pairing lookAhead
with atomic
.
Value parameters
- p
-
the parser to execute, if it succeeds, it will not have consumed input.
Attributes
- Returns
-
a parser that parses
p
and never consumes input if it succeeds. - Example
-
scala> import parsley.Parsley.lookAhead, parsley.character.string scala> (lookAhead(string("aaa")) ~> string("aaa")).parse("aaa") val res0 = Success("aaa") scala> (lookAhead(string("abc")) | string("abd")).parse("abd") val res1 = Failure(..) // lookAhead does not roll back input consumed on failure
- Inherited from:
- ParsleyImpl (hidden)
- Source
- Parsley.scala
This combinator parses its argument p
, and succeeds when p
fails and vice-versa, never consuming input.
This combinator parses its argument p
, and succeeds when p
fails and vice-versa, never consuming input.
If the parser p
succeeds, then notFollowedBy(p)
will fail, consuming no input. Otherwise, should p
fail, then notFollowedBy(p)
will succeed, consuming no input and returning ()
.
Value parameters
- p
-
the parser to execute, it should fail in order for this combinator to succeed.
Attributes
- Returns
-
a parser which fails when
p
succeeds and succeeds otherwise, never consuming input. - Example
-
one use for this combinator is to allow for "longest-match" behaviour. For instance, keywords are normally only considered keywords if they are not part of some larger valid identifier (i.e. the keyword "if" should not parse successfully given "ifp"). This can be accomplished as follows:
import parsley.character.{string, letterOrDigit} import parsley.Parsley.notFollowedBy def keyword(kw: String): Parsley[Unit] = atomic { string(kw) ~> notFollowedBy(letterOrDigit) }
- Inherited from:
- ParsleyImpl (hidden)
- Source
- Parsley.scala
Core Combinators and Parsers
These are the most primitive combinators for consuming input capable of any input reading tasks.
This combinator tries to parse a single specific character c
from the input.
This combinator tries to parse a single specific character c
from the input.
Attempts to read the given character c
from the input stream at the current position. If this character can be found, it is consumed and returned. Otherwise, no input is consumed and this combinator will fail.
Value parameters
- c
-
the character to parse
Attributes
- Returns
-
a parser that tries to read a single
c
, or fails. - Note
-
this combinator can only handle 16-bit characters: for larger codepoints, consider using
string
orunicode.char
. - Example
-
scala> import parsley.character.char scala> char('a').parse("") val res0 = Failure(..) scala> char('a').parse("a") val res1 = Success('a') scala> char('a').parse("ba") val res2 = Failure(..)
- Inherited from:
- character (hidden)
- Source
- character.scala
This parser will parse any single character from the input, failing if there is no input remaining.
This parser will parse any single character from the input, failing if there is no input remaining.
Attributes
- Note
-
this combinator can only handle 16-bit characters: for larger codepoints, consider using
unicode.item
. - Inherited from:
- character (hidden)
- Source
- character.scala
This combinator tries to parse a single character from the input that matches the given predicate.
This combinator tries to parse a single character from the input that matches the given predicate.
Attempts to read a character from the input and tests it against the predicate pred
. If a character c
can be read and pred(c)
is true, then c
is consumed and returned. Otherwise, no input is consumed and this combinator will fail.
Value parameters
- pred
-
the predicate to test the next character against, should one exist.
Attributes
- Returns
-
a parser that tries to read a single character
c
, such thatpred(c)
is true, or fails. - Note
-
this combinator can only handle 16-bit characters: for larger codepoints, consider using
unicode.satisfy
. - Example
-
scala> import parsley.character.satisfy scala> satisfy(_.isDigit).parse("") val res0 = Failure(..) scala> satisfy(_.isDigit).parse("7") val res1 = Success('7') scala> satisfy(_.isDigit).parse("a5") val res2 = Failure(..) scala> def char(c: Char): Parsley[Char] = satisfy(_ == c)
- Inherited from:
- character (hidden)
- Source
- character.scala
This combinator tries to parse and process a character from the input if it is defined for the given function.
This combinator tries to parse and process a character from the input if it is defined for the given function.
Attempts to read a character from the input and tests to see if it is in the domain of f
. If a character c
can be read and f(c)
is defined, then c
is consumed and f(c)
is returned. Otherwise, no input is consumed and this combinator will fail.
Value parameters
- f
-
the function to test the next character against and transform it with, should one exist.
Attributes
- Returns
-
a parser that tries to read a single character
c
, such thatf(c)
is defined, and returnsf(c)
if so, or fails. - Since
-
4.4.0
- Note
-
this combinator can only handle 16-bit characters: for larger codepoints, consider using
unicode.satisfyMap
. - Example
-
scala> import parsley.character.satisfyMap scala> val digit = satisfyMap { case c if c.isDigit => c.asDigit } scala> digit.parse("") val res0 = Failure(..) scala> digit.parse("7") val res1 = Success(7) scala> digit.parse("a5") val res2 = Failure(..)
- Inherited from:
- character (hidden)
- Source
- character.scala
Consumptionless Parsers
These combinators and parsers do not consume input: they are the most primitive ways of producing successes and failures with the minimal possible effect on the parse. They are, however, reasonably useful; in particular, pure
and unit
can be put to good use in injecting results into a parser without needing to consume anything, or mapping another parser.
This combinator fails immediately, with a caret of the given width and no other information.
This combinator fails immediately, with a caret of the given width and no other information.
By producing basically no information, this combinator is principally for adjusting the caret-width of another error, rather than the value empty
, which is used to fail with no effect on error content.
Value parameters
- caretWidth
-
the width of the caret for the error produced by this combinator.
Attributes
- Returns
-
a parser that fails.
- Since
-
4.4.0
- Inherited from:
- ParsleyImpl (hidden)
- Source
- Parsley.scala
This parser fails immediately, with an unknown parse error.
This parser fails immediately, with an unknown parse error.
Attributes
- Returns
-
a parser that fails.
- Note
-
equivalent to
empty(0)
- Example
-
scala> import parsley.Parsley.empty scala> empty.parse("") val res0 = Failure(..)
- Inherited from:
- ParsleyImpl (hidden)
- Source
- Parsley.scala
This combinator produces a new value everytime it is parsed without having any other effect.
This combinator produces a new value everytime it is parsed without having any other effect.
When this combinator is ran, no input is required, nor consumed, and a new instance of the given value will always be successfully returned. It has no other effect on the state of the parser.
This is useful primarily if mutable data is being threaded around a parser: this should not be needed for the vast majority of parsers.
Value parameters
- x
-
the value to be returned.
Attributes
- Returns
-
a parser which consumes no input and produces a value
x
. - Since
-
4.0.0
- Example
-
scala> import parsley.Parsley.{pure, fresh} scala> val p = pure(new Object) scala> p.parse("") val res0 = Success(java.lang.Object@44a3ec6b) scala> p.parse("") val res1 = Success(java.lang.Object@44a3ec6b) scala> val q = fresh(new Object) scala> q.parse("") val res2 = Success(java.lang.Object@71623278) scala> q.parse("") val res3 = Success(java.lang.Object@768b970c)
- Inherited from:
- ParsleyImpl (hidden)
- Source
- Parsley.scala
This combinator produces a value without having any other effect.
This combinator produces a value without having any other effect.
When this combinator is ran, no input is required, nor consumed, and the given value will always be successfully returned. It has no other effect on the state of the parser.
Value parameters
- x
-
the value to be returned.
Attributes
- Returns
-
a parser which consumes no input and produces a value
x
. - Example
-
scala> import parsley.Parsley.pure scala> pure(7).parse("") val res0 = Success(7) scala> pure("hello!").parse("a") val res1 = Success("hello!")
- Inherited from:
- ParsleyImpl (hidden)
- Source
- Parsley.scala
This parser produces ()
without having any other effect.
This parser produces ()
without having any other effect.
When this parser is ran, no input is required, nor consumed, and the given value will always be successfully returned. It has no other effect on the state of the parser.
Value parameters
- x
-
the value to be returned.
Attributes
- Returns
-
a parser which consumes no input and produces
()
. - Note
-
defined as
pure(())
as a simple convenience. - Example
-
scala> import parsley.Parsley.unit scala> unit.parse("") val res0 = Success(()) scala> unit.parse("a") val res0 = Success(())
- Inherited from:
- ParsleyImpl (hidden)
- Source
- Parsley.scala
Iterative Combinators
These combinators all execute a given parser an unbounded number of times, until either it fails, or another parser succeeds, depending on the combinator. All of the results produced by the repeated execution of the parser are returned in a List
. These are almost essential for any practical parsing task.
This combinator repeatedly parses a given parser zero or more times, returning how many times it succeeded.
This combinator repeatedly parses a given parser zero or more times, returning how many times it succeeded.
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 succeed. The number of times p
succeeded is returned as the result.
Value parameters
- p
-
the parser to execute multiple times.
Attributes
- Returns
-
the number of times
p
successfully parses - Since
-
4.5.0
- Example
-
scala> import parsley.character.string scala> import parsley.combinator.countMany scala> val p = countMany(string("ab")) scala> p.parse("") val res0 = Success(0) scala> p.parse("ab") val res1 = Success(1) scala> p.parse("abababab") val res2 = Success(4) scala> p.parse("aba") val res3 = Failure(..)
- Inherited from:
- combinator (hidden)
- Source
- combinator.scala
This combinator repeatedly parses a given parser one or more times, returning how many times it succeeded.
This combinator repeatedly parses a given parser one or more times, returning how many times it succeeded.
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 succeed. The parser p
must succeed at least once. The number of times p
succeeded is returned as the result.
Value parameters
- p
-
the parser to execute multiple times.
Attributes
- Returns
-
the number of times
p
successfully parses - Since
-
4.5.0
- Example
-
scala> import parsley.character.string scala> import parsley.combinator.countSome scala> val p = countSome(string("ab")) scala> p.parse("") val res0 = Failure(..) scala> p.parse("ab") val res1 = Success(1) scala> p.parse("abababab") val res2 = Success(4) scala> p.parse("aba") val res3 = Failure(..)
- Inherited from:
- combinator (hidden)
- Source
- combinator.scala
This combinator repeatedly parses a given parser zero or more times, collecting the results into a list.
This combinator repeatedly parses a given parser zero 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 >= 0
), in a list: List(x1, .., xn)
. If p
was never successful, the empty list is returned.
Value parameters
- p
-
the parser to execute multiple times.
Attributes
- Returns
-
a parser that parses
p
until it fails, returning the list of all the successful results. - Since
-
4.5.0
- Example
-
scala> import parsley.character.string scala> import parsley.Parsley.many scala> val p = many(string("ab")) scala> p.parse("") val res0 = Success(Nil) scala> p.parse("ab") val res1 = Success(List("ab")) scala> p.parse("abababab") val res2 = Success(List("ab", "ab", "ab", "ab")) scala> p.parse("aba") val res3 = Failure(..)
- Inherited from:
- ParsleyImpl (hidden)
- Source
- Parsley.scala
This combinator repeatedly parses a given parser n
or more times, collecting the results into a list.
This combinator repeatedly parses a given parser n
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 xm
(with m >= n
), in a list: List(x1, .., xm)
. If p
was not successful at least n
times, this combinator fails.
Value parameters
- n
-
the minimum number of
p
s required. - p
-
the parser to execute multiple times.
Attributes
- Returns
-
a parser that parses
p
until it fails, returning the list of all the successful results. - Note
-
many(p) == many(0, p)
andsome(p) == many(1, p)
. - Example
-
scala> import parsley.character.string scala> import parsley.combinator.manyN scala> val p = manyN(2, string("ab")) scala> p.parse("") val res0 = Failure(..) scala> p.parse("ab") val res1 = Failure(..) scala> p.parse("abababab") val res2 = Success(List("ab", "ab", "ab", "ab")) scala> p.parse("aba") val res3 = Failure(..)
- Inherited from:
- combinator (hidden)
- Source
- combinator.scala
This combinator repeatedly parses a given parser zero or more times, until the end
parser succeeds, collecting the results into a list.
This combinator repeatedly parses a given parser zero or more times, until the end
parser succeeds, collecting the results into a list.
First tries to parse end
, if it fails without consuming input, then parses p
, which must succeed. This repeats until end
succeeds. When end
does succeed, this combinator will return all of the results generated by p
, x1
through xn
(with n >= 0
), in a list: List(x1, .., xn)
. If end
could be parsed immediately, the empty list is returned.
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
untilend
succeeds, returning the list of all the successful results. - Since
-
4.5.0
- Example
-
This can be useful for scanning comments:
scala> import parsley.character.{string, item, endOfLine} scala> import parsley.combinator.manyTill scala> val comment = string("//") ~> manyTill(item, endOfLine) scala> p.parse("//hello world") val res0 = Failure(..) scala> p.parse("//hello world\n") val res1 = Success(List('h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd')) scala> p.parse("//\n") val res2 = Success(Nil)
- Inherited from:
- combinator (hidden)
- Source
- combinator.scala
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 list: List(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 list of all the successful results. - Since
-
4.5.0
- Example
-
scala> import parsley.character.string scala> import parsley.Parsley.some scala> val p = some(string("ab")) scala> p.parse("") val res0 = Failure(..) scala> p.parse("ab") val res1 = Success(List("ab")) scala> p.parse("abababab") val res2 = Success(List("ab", "ab", "ab", "ab")) scala> p.parse("aba") val res3 = Failure(..)
- Inherited from:
- ParsleyImpl (hidden)
- Source
- Parsley.scala
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 list: List(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
untilend
succeeds, returning the list of all the successful results. - Since
-
4.5.0
- Example
-
This can be useful for scanning comments:
scala> import parsley.character.{string, item, endOfLine} scala> import parsley.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(List('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(List('a'))
- Inherited from:
- combinator (hidden)
- Source
- combinator.scala
Input Query Combinators
These combinators do not consume input, but they allow for querying of the input stream - specifically checking whether or not there is more input that can be consumed or not. In particular, most parsers should be making use of eof
to ensure that the parser consumes all the input available at the end of the parse.
This parser only succeeds at the end of the input.
This parser only succeeds at the end of the input.
Equivalent to notFollowedBy(item)
.
Attributes
- Since
-
4.5.0
- Example
-
scala> import parsley.combinator.eof scala> eof.parse("a") val res0 = Failure(..) scala> eof.parse("") val res1 = Success(())
- Inherited from:
- ParsleyImpl (hidden)
- Source
- Parsley.scala
Optional Parsing Combinators
These combinators allow for the possible parsing of some parser. If the parser succeeds, that is ok so long as it did not consume input. Be aware that the result of the success may be replaced with these combinators, with the exception of option
, which still preserves the result.
This combinator tries to parse p
, wrapping its result in a Some
if it succeeds, or returns None
if it fails.
This combinator tries to parse p
, wrapping its result in a Some
if it succeeds, or returns None
if it fails.
Tries to parse p
. If p
succeeded, producing x
, then Some(x)
is returned. Otherwise, if p
failed without consuming input, then None
is returned instead.
Value parameters
- p
-
the parser to try to parse.
Attributes
- Returns
-
a parser that tries to parse
p
, but can still succeed withNone
if that was not possible. - Example
-
scala> import parsley.combinator.option scala> import parsley.character.string scala> val p = option(string("abc")) scala> p.parse("") val res0 = Success(None) scala> p.parse("abc") val res1 = Success(Some("abc")) scala> p.parse("ab") val res2 = Failure(..)
- Inherited from:
- combinator (hidden)
- Source
- combinator.scala
This combinator will parse p
if possible, otherwise will do nothing.
This combinator will parse p
if possible, otherwise will do nothing.
Tries to parse p
. If p
succeeds, or fails without consuming input then this combinator is successful. Otherwise, if p
failed having consumed input, this combinator fails.
Value parameters
- p
-
the parser to try to parse.
Attributes
- Returns
-
a parser that tries to parse
p
. - Note
-
equivalent to
optionalAs(p, ())
. - Example
-
scala> import parsley.combinator.optional scala> import parsley.character.string scala> val p = optional(string("abc")) scala> p.parse("") val res0 = Success(()) scala> p.parse("abc") val res1 = Success(()) scala> p.parse("ab") val res2 = Failure(..)
- Inherited from:
- combinator (hidden)
- Source
- combinator.scala
This combinator will parse p
if possible, otherwise will do nothing.
This combinator will parse p
if possible, otherwise will do nothing.
Tries to parse p
. If p
succeeds, or fails without consuming input then this combinator is successful and returns x
. Otherwise, if p
failed having consumed input, this combinator fails.
Value parameters
- p
-
the parser to try to parse.
- x
-
the value to return regardless of how
p
performs.
Attributes
- Returns
-
a parser that tries to parse
p
, returningx
regardless of success or failure. - Example
-
scala> import parsley.combinator.optionalAs scala> import parsley.character.string scala> val p = optionalAs(string("abc"), 7) scala> p.parse("") val res0 = Success(7) scala> p.parse("abc") val res1 = Success(7) scala> p.parse("ab") val res2 = Failure(..)
- Inherited from:
- combinator (hidden)
- Source
- combinator.scala
Character Class Combinators
These combinators allow for working with character classes. This means that a set, or range, of characters can be specified, and the combinator will return a parser that matches one of those characters (or conversely, any character that is not in that set). The parsed character is always returned.
$noneOf
$noneOf
If the next character in the input is outside of the range of characters cs
, it is consumed and returned. Otherwise, no input is consumed and the combinator fails.
Value parameters
- cs
-
the range of characters to check.
Attributes
- Returns
-
a parser that parses a character outside the range
cs
. - See also
- Note
-
this combinator can only handle 16-bit characters: for larger codepoints, consider using
unicode.noneOf
. - Example
-
scala> import parsley.character.noneOf scala> val p = noneOf('a' to 'c') scala> p.parse("a") val res0 = Failure(..) scala> p.parse("b") val res1 = Failure(..) scala> p.parse("c") val res1 = Failure(..) scala> p.parse("xb") val res2 = Success('x') scala> p.parse("") val res3 = Failure(..)
- Inherited from:
- character (hidden)
- Source
- character.scala
$noneOf
$noneOf
If the next character in the input is not an element of the list of characters cs
, it is consumed and returned. Otherwise, no input is consumed and the combinator fails.
Value parameters
- cs
-
the set of characters to check.
Attributes
- Returns
-
a parser that parses one character that is not an element of
cs
. - See also
- Note
-
this combinator can only handle 16-bit characters: for larger codepoints, consider using
unicode.noneOf
. - Example
-
scala> import parsley.character.noneOf scala> val p = noneOf('a', 'b', 'c') scala> p.parse("a") val res0 = Failure(..) scala> p.parse("c") val res1 = Failure(..) scala> p.parse("xb") val res2 = Success('x') scala> p.parse("") val res3 = Failure(..)
- Inherited from:
- character (hidden)
- Source
- character.scala
$noneOf
$noneOf
If the next character in the input is not a member of the set cs
, it is consumed and returned. Otherwise, no input is consumed and the combinator fails.
Value parameters
- cs
-
the set of characters to check.
Attributes
- Returns
-
a parser that parses one character that is not a member of the set
cs
. - See also
- Note
-
this combinator can only handle 16-bit characters: for larger codepoints, consider using
unicode.noneOf
. - Example
-
scala> import parsley.character.noneOf scala> val p = noneOf(Set('a', 'b', 'c')) scala> p.parse("a") val res0 = Failure(..) scala> p.parse("c") val res1 = Failure(..) scala> p.parse("xb") val res2 = Success('x') scala> p.parse("") val res3 = Failure(..)
- Inherited from:
- character (hidden)
- Source
- character.scala
$oneOf
$oneOf
If the next character in the input is within the range of characters cs
, it is consumed and returned. Otherwise, no input is consumed and the combinator fails.
Value parameters
- cs
-
the range of characters to check.
Attributes
- Returns
-
a parser that parses a character within the range
cs
. - See also
- Note
-
this combinator can only handle 16-bit characters: for larger codepoints, consider using
unicode.oneOf
. - Example
-
scala> import parsley.character.oneOf scala> val p = oneOf('a' to 'c') scala> p.parse("a") val res0 = Success('a') scala> p.parse("b") val res1 = Success('b') scala> p.parse("c") val res1 = Success('c') scala> p.parse("xb") val res2 = Failure(..)
- Inherited from:
- character (hidden)
- Source
- character.scala
$oneOf
$oneOf
If the next character in the input is an element of the list of characters cs
, it is consumed and returned. Otherwise, no input is consumed and the combinator fails.
Value parameters
- cs
-
the characters to check.
Attributes
- Returns
-
a parser that parses one of the elements of
cs
. - See also
- Note
-
this combinator can only handle 16-bit characters: for larger codepoints, consider using
unicode.oneOf
. - Example
-
scala> import parsley.character.oneOf scala> val p = oneOf('a', 'b', 'c') scala> p.parse("a") val res0 = Success('a') scala> p.parse("c") val res1 = Success('c') scala> p.parse("xb") val res2 = Failure(..)
- Inherited from:
- character (hidden)
- Source
- character.scala
$oneOf
$oneOf
If the next character in the input is a member of the set cs
, it is consumed and returned. Otherwise, no input is consumed and the combinator fails.
Value parameters
- cs
-
the set of characters to check.
Attributes
- Returns
-
a parser that parses one of the member of the set
cs
. - See also
- Note
-
this combinator can only handle 16-bit characters: for larger codepoints, consider using
unicode.oneOf
. - Example
-
scala> import parsley.character.oneOf scala> val p = oneOf(Set('a', 'b', 'c')) scala> p.parse("a") val res0 = Success('a') scala> p.parse("c") val res1 = Success('c') scala> p.parse("xb") val res2 = Failure(..)
- Inherited from:
- character (hidden)
- Source
- character.scala
String Combinators
These combinators allow for working with, or building, strings. This means that they can parse specific strings, specific sets of strings, or can read characters repeatedly to generate strings. They are united in all returning String
as their result.
This combinator attempts to parse a given string from the input, and fails otherwise.
This combinator attempts to parse a given string from the input, and fails otherwise.
Attempts to read the given string completely from the input at the current position. If the string is present, then the parser succeeds, and the entire string is consumed from the input. Otherwise, if the input has too few characters remaining, or not all the characters matched, the parser fails. On failure, all the characters that were matched are consumed from the input.
Value parameters
- s
-
the string to be parsed from the input
Attributes
- Returns
-
a parser that either parses the string
s
or fails at the first mismatched character. - Note
-
the error messages generated by
string
do not reflect how far into the input it managed to get: this is because the error being positioned at the start of the string is more natural. However, input will still be consumed for purposes of backtracking. - Example
-
scala> import parsley.character.string scala> string("abc").parse("") val res0 = Failure(..) scala> string("abc").parse("abcd") val res1 = Success("abc") scala> string("abc").parse("xabc") val res2 = Failure(..)
- Inherited from:
- character (hidden)
- Source
- character.scala
This combinator parses characters matching the given predicate zero or more times, collecting the results into a string.
This combinator parses characters matching the given predicate zero or more times, collecting the results into a string.
Repeatly reads characters that satisfy the given predicate pred
. When no more characters can be successfully read, the results are stitched together into a String
and returned. This combinator can never fail, since satisfy
can never fail having consumed input.
Value parameters
- pred
-
the predicate to test characters against.
Attributes
- Returns
-
a parser that returns the span of characters satisfying
pred
- Since
-
4.4.0
- Note
-
this acts exactly like
stringOfMany(satisfy(pred))
, but may be more efficient.analogous to the
megaparsec
takeWhileP
combinator. - Example
-
scala> import parsley.character.{letter, stringOfMany} scala> import parsley.syntax.zipped._ scala> val ident = (letter, stringOfMany(_.isLetterOrDigit)).zipped((c, s) => s"$c$s") scala> ident.parse("abdc9d") val res0 = Success("abdc9d") scala> ident.parse("a") val res1 = Success("a") scala> ident.parse("9") val res2 = Failure(..)
- Inherited from:
- character (hidden)
- Source
- character.scala
This combinator parses pc
zero or more times, collecting its results into a string.
This combinator parses pc
zero or more times, collecting its results into a string.
Parses pc
repeatedly until it fails. The resulting characters are placed into a string, which is then returned. This is morally equivalent to many(pc).map(_.mkString)
, but it uses StringBuilder
, which makes it much more efficient.
Value parameters
- pc
-
the parser whose results make up the string
Attributes
- Returns
-
a parser that parses a string whose letters consist of results from
pc
. - Since
-
4.0.0
- Example
-
scala> import parsley.character.{letter, letterOrDigit, stringOfMany} scala> import parsley.syntax.zipped._ scala> val ident = (letter, stringOfMany(letterOrDigit)).zipped((c, s) => s"$c$s") scala> ident.parse("abdc9d") val res0 = Success("abdc9d") scala> ident.parse("a") val res1 = Success("a") scala> ident.parse("9") val res2 = Failure(..)
- Inherited from:
- character (hidden)
- Source
- character.scala
This combinator parses characters matching the given predicate one or more times, collecting the results into a string.
This combinator parses characters matching the given predicate one or more times, collecting the results into a string.
Repeatly reads characters that satisfy the given predicate pred
. When no more characters can be successfully read, the results are stitched together into a String
and returned. This combinator can never fail having consumed input, since satisfy
can never fail having consumed input.
Value parameters
- pred
-
the predicate to test characters against.
Attributes
- Returns
-
a parser that returns the span of characters satisfying
pred
- Since
-
4.4.0
- Note
-
this acts exactly like
stringOfSome(satisfy(pred))
, but may be more efficient.analogous to the
megaparsec
takeWhile1P
combinator. - Example
-
scala> import parsley.character.{stringOfSome} scala> val ident = stringOfSome(_.isLetter) scala> ident.parse("abdc9d") val res0 = Success("abdc") scala> ident.parse("") val res1 = Failure(..)
- Inherited from:
- character (hidden)
- Source
- character.scala
This combinator parses pc
one or more times, collecting its results into a string.
This combinator parses pc
one or more times, collecting its results into a string.
Parses pc
repeatedly until it fails. The resulting characters are placed into a string, which is then returned. This is morally equivalent to many(pc).map(_.mkString)
, but it uses StringBuilder
, which makes it much more efficient. The result string must have at least one character in it.
Value parameters
- pc
-
the parser whose results make up the string
Attributes
- Returns
-
a parser that parses a string whose letters consist of results from
pc
. - Since
-
4.0.0
- Example
-
scala> import parsley.character.{letter, stringOfSome} scala> val ident = stringOfSome(letter) scala> ident.parse("abdc9d") val res0 = Success("abdc") scala> ident.parse("") val res1 = Failure(..)
- Inherited from:
- character (hidden)
- Source
- character.scala
This combinator tries to parse each of the key-value pairs kvs
(and kv0
), until one of them succeeds.
This combinator tries to parse each of the key-value pairs kvs
(and kv0
), until one of them succeeds.
Each argument to this combinator is a pair of a string and a parser to perform if that string can be parsed. strings(s0 -> p0, ...)
can be thought of as atomicChoice(string(s0) ~> p0, ...)
, however, the given ordering of key-value pairs does not dictate the order in which the parses are tried. In particular, it will avoid keys that are the prefix of another key first, so that it has Longest Match semantics. It will try to minimise backtracking too, making it a much more efficient option than atomicChoice
.
Value parameters
- kv0
-
the first key-value pair to try to parse.
- kvs
-
the remaining key-value pairs to try to parse.
Attributes
- Returns
-
a parser that tries to parse all the given key-value pairs, returning the (possibly failing) result of the value that corresponds to the longest matching key.
- Since
-
4.0.0
- Note
-
the scope of any backtracking performed is isolated to the key itself, as it is assumed that once a key parses correctly, the branch has been committed to. Putting an
atomic
around the values will not affect this behaviour. - Example
-
scala> import parsley.character.strings scala> val p = strings("hell" -> pure(4), "hello" -> pure(5), "goodbye" -> pure(7), "g" -> pure(1), "abc" -> pure(3)) scala> p.parse("hell") val res0 = Success(4) scala> p.parse("hello") val res1 = Success(5) scala> p.parse("good") val res2 = Success(1) scala> p.parse("goodbye") val res3 = Success(7) scala> p.parse("a") val res4 = Failure(..)
- Inherited from:
- character (hidden)
- Source
- character.scala
This combinator tries to parse each of the strings strs
(and str0
), until one of them succeeds.
This combinator tries to parse each of the strings strs
(and str0
), until one of them succeeds.
Unlike choice
, or more accurately atomicChoice
, this combinator will not necessarily parse the strings in the order provided. It will avoid strings that have another string as a prefix first, so that it has Longest Match semantics. It will try to minimise backtracking too, making it a much more efficient option than atomicChoice
.
The longest succeeding string will be returned. If no strings match then the combinator fails.
Value parameters
- str0
-
the first string to try to parse.
- strs
-
the remaining strings to try to parse.
Attributes
- Returns
-
a parser that tries to parse all the given strings returning the longest one that matches.
- Since
-
4.0.0
- Example
-
scala> import parsley.character.strings scala> val p = strings("hell", "hello", "goodbye", "g", "abc") scala> p.parse("hell") val res0 = Success("hell") scala> p.parse("hello") val res1 = Success("hello") scala> p.parse("good") val res2 = Success("g") scala> p.parse("goodbye") val res3 = Success("goodbye") scala> p.parse("a") val res4 = Failure(..)
- Inherited from:
- character (hidden)
- Source
- character.scala
Separated Values Combinators
These combinators are concerned with delimited parsing, where one parser is repeated but delimited by another one. In each of these cases p
is the parser of interest and sep
is the delimeter. These combinators mainly differ in either the number of p
s they require, or exactly where the delimeters are allowed (only between, always trailing, or either). In all cases, they return the list of results generated by the repeated parses of p
.
This combinator parses zero or more occurrences of p
, separated and ended by sep
.
This combinator parses zero or more occurrences of p
, separated and ended by sep
.
Behaves just like endBy1
, except does not require an initial p
and sep
, returning the empty list instead.
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 bysep
, returning the list ofp
's results. - Example
-
scala> ... scala> val args = endBy(int, string(";\n")) scala> args.parse("7;\n3;\n2") val res0 = Failure(..) scala> args.parse("") val res1 = Success(Nil) scala> args.parse("1;\n") val res2 = Success(List(1)) scala> args.parse("1;\n2;\n") val res3 = Success(List(1, 2))
- Inherited from:
- combinator (hidden)
- Source
- combinator.scala
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 List(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 bysep
, returning the list ofp
's results. - 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(List(1)) scala> args.parse("1;\n2;\n") val res3 = Success(List(1, 2))
- Inherited from:
- combinator (hidden)
- Source
- combinator.scala
This combinator parses zero or more occurrences of p
, separated by sep
.
This combinator parses zero or more occurrences of p
, separated by sep
.
Behaves just like sepBy1
, except does not require an initial p
, returning the empty list instead.
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 bysep
, returning the list ofp
's results. - Example
-
scala> ... scala> val args = sepBy(int, string(", ")) scala> args.parse("7, 3, 2") val res0 = Success(List(7, 3, 2)) scala> args.parse("") val res1 = Success(Nil) scala> args.parse("1") val res2 = Success(List(1)) scala> args.parse("1, 2, ") val res3 = Failure(..) // no trailing comma allowed
- Inherited from:
- combinator (hidden)
- Source
- combinator.scala
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 sep
s. The results of the p
's, x1
through xn
, are returned as List(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 bysep
, returning the list ofp
's results. - Example
-
scala> ... scala> val args = sepBy1(int, string(", ")) scala> args.parse("7, 3, 2") val res0 = Success(List(7, 3, 2)) scala> args.parse("") val res1 = Failure(..) scala> args.parse("1") val res2 = Success(List(1)) scala> args.parse("1, 2, ") val res3 = Failure(..) // no trailing comma allowed
- Inherited from:
- combinator (hidden)
- Source
- combinator.scala
This combinator parses zero or more occurrences of p
, separated and optionally ended by sep
.
This combinator parses zero or more occurrences of p
, separated and optionally ended by sep
.
Behaves just like sepEndBy1
, except does not require an initial p
, returning the empty list instead.
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 bysep
, returning the list ofp
's results. - Example
-
scala> ... scala> val args = sepEndBy(int, string(";\n")) scala> args.parse("7;\n3;\n2") val res0 = Success(List(7, 3, 2)) scala> args.parse("") val res1 = Success(Nil) scala> args.parse("1") val res2 = Success(List(1)) scala> args.parse("1;\n2;\n") val res3 = Success(List(1, 2))
- Inherited from:
- combinator (hidden)
- Source
- combinator.scala
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 List(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 bysep
, returning the list ofp
's results. - Example
-
scala> ... scala> val args = sepEndBy1(int, string(";\n")) scala> args.parse("7;\n3;\n2") val res0 = Success(List(7, 3, 2)) scala> args.parse("") val res1 = Failure(..) scala> args.parse("1") val res2 = Success(List(1)) scala> args.parse("1;\n2;\n") val res3 = Success(List(1, 2))
- Inherited from:
- combinator (hidden)
- Source
- combinator.scala
Specific Character Parsers
These parsers are special cases of satisfy
or char
. They are worth using, as they are given special error labelling, producing nicer error messages than their primitive counterparts. This documentation assumes JDK 17. JDK 17 is compliant with Unicode® Specification 13.0. As such, the descriptions of the parsers in this section are accurate with respect to Unicode® Specification 13.0: using a different JDK may affect the precise definitions of the parsers below. If in doubt, check the documentation for java.lang.Character
to see which Unicode version is supported by your JVM. A table of the Unicode versions up to JDK 17 can be found here. These parsers are only able to parse unicode characters in the range '\u0000'
to '\uffff'
, known as the Basic Multilingual Plane (BMP). Unicode characters wider than a single 16-bit character should be parsed using multi-character combinators such as string
, or, alternatively, combinators found in unicode
.
This parser tries to parse a binary digit (bit) and returns it if successful.
This parser tries to parse a binary digit (bit) and returns it if successful.
A bit is either '0'
or '1'
.
Attributes
- Inherited from:
- character (hidden)
- Source
- character.scala
This parser tries to parse a CRLF
newline character pair, returning '\n'
if successful.
This parser tries to parse a CRLF
newline character pair, returning '\n'
if successful.
A CRLF
character is the pair of carriage return ('\r'
) and line feed ('\n'
). These two characters will be parsed together or not at all. The parser is made atomic using atomic
.
Attributes
- Inherited from:
- character (hidden)
- Source
- character.scala
This parser tries to parse a digit, and returns it if successful.
This parser tries to parse a digit, and returns it if successful.
A digit is any character c <= '\uffff'
whose Unicode Category Type is Decimal Number (Nd
). Examples of (inclusive) ranges within this category include:
-
the Latin digits
'0'
through'9'
-
the Arabic-Indic digits
'\u0660'
through'\u0669'
-
the Extended Arabic-Indic digits
'\u06f0'
through'\u06f9'
-
the Devangari digits
'\u0966'
through'\u096f'
-
the Fullwidth digits
'\uff10'
through'\uff19'
$categories
Attributes
- Inherited from:
- character (hidden)
- Source
- character.scala
This parser will parse either a line feed (LF
) or a CRLF
newline, returning '\n'
if successful.
This parser will parse either a line feed (LF
) or a CRLF
newline, returning '\n'
if successful.
Attributes
- See also
- Inherited from:
- character (hidden)
- Source
- character.scala
This parser tries to parse a hexadecimal digit, and returns it if successful.
This parser tries to parse a hexadecimal digit, and returns it if successful.
A hexadecimal digit is one of (all inclusive ranges):
-
the digits
'0'
through'9'
-
the letters
'a'
through'f'
-
the letters
'A'
through'Z'
Attributes
- See also
- Inherited from:
- character (hidden)
- Source
- character.scala
This parser tries to parse a letter, and returns it if successful.
This parser tries to parse a letter, and returns it if successful.
A letter is any character c <= '\uffff'
whose Unicode Category Type is any of the following:
-
Uppercase Letter (
Lu
) -
Lowercase Letter (
Ll
) -
Titlecase Letter (
Lt
) -
Modifier Letter (
Lm
) -
Other Letter (
Lo
)
$categories
Attributes
- Inherited from:
- character (hidden)
- Source
- character.scala
This parser tries to parse either a letter or a digit, and returns it if successful.
This parser tries to parse either a letter or a digit, and returns it if successful.
A letter or digit is anything that would parse in either letter
or digit
.
Attributes
- See also
- Inherited from:
- character (hidden)
- Source
- character.scala
This parser tries to parse a lowercase letter, and returns it if successful.
This parser tries to parse a lowercase letter, and returns it if successful.
A lowercase letter is any character c <= '\uffff'
whose Unicode Category Type is Lowercase Letter (Ll
). Examples of characters within this category include:
-
the Latin letters
'a'
through'z'
-
Latin special character such as
'é'
,'ß'
,'ð'
-
Cryillic letters
-
Greek letters
-
Coptic letters
$categories
Attributes
- Inherited from:
- character (hidden)
- Source
- character.scala
This parser tries to parse a line feed newline ('\n'
) character, and returns it if successful.
This parser tries to parse a line feed newline ('\n'
) character, and returns it if successful.
This parser will not accept a carriage return (CR
) character or CRLF
.
Attributes
- Inherited from:
- character (hidden)
- Source
- character.scala
This parser tries to parse an octal digit, and returns it if successful.
This parser tries to parse an octal digit, and returns it if successful.
An octal digit is one of '0'
to '7'
(inclusive).
Attributes
- See also
- Inherited from:
- character (hidden)
- Source
- character.scala
This parser tries to parse a space or tab character, and returns it if successful.
This parser tries to parse a space or tab character, and returns it if successful.
Attributes
- See also
- Inherited from:
- character (hidden)
- Source
- character.scala
This parser tries to parse a tab ('\t'
) character, and returns it if successful.
This parser tries to parse a tab ('\t'
) character, and returns it if successful.
This parser does not recognise vertical tabs, only horizontal ones.
Attributes
- Inherited from:
- character (hidden)
- Source
- character.scala
This parser tries to parse an uppercase letter, and returns it if successful.
This parser tries to parse an uppercase letter, and returns it if successful.
An uppercase letter is any character c <= '\uffff'
whose Unicode Category Type is Uppercase Letter (Lu
). Examples of characters within this category include:
-
the Latin letters
'A'
through'Z'
-
Latin special character such as
'Ã…'
,'Ç'
,'Õ'
-
Cryillic letters
-
Greek letters
-
Coptic letters
$categories
Attributes
- Inherited from:
- character (hidden)
- Source
- character.scala
This parser tries to parse a whitespace character, and returns it if successful.
This parser tries to parse a whitespace character, and returns it if successful.
A whitespace character is one of:
-
a space (
' '
) -
a tab (
'\t'
) -
a line feed (
'\n'
) -
a carriage return (
'\r'
) -
a form feed (
'\f'
) -
a vertical tab (
'\u000B'
)
Attributes
- Inherited from:
- character (hidden)
- Source
- character.scala
Conditional Combinators
These combinators will decide which branch to take next based on the result of another parser. This differs from combinators like |
which make decisions based on the success/failure of a parser: here the result of a successful parse will direct which option is done. These are sometimes known as "selective" combinators.
This combinator parses its first argument either
, and then parses either left
or right
depending on its result.
This combinator parses its first argument either
, and then parses either left
or right
depending on its result.
First, branch(either, left, right)
parses either
, which, if successful, will produce either a Left(x)
or a Right(y)
. If a Left(x)
is produced, the parser left
is executed to produce a function f
, and f(x)
is returned. Otherwise, if a Right(y)
is produced, the parser right
is executed to produce a function g
, and g(y)
is returned. If either of the two executed parsers fail, the entire combinator fails.
First introduced in Selective Applicative Functors (Mokhov et al. 2019).
Value parameters
- either
-
the first parser to execute, its result decides which parser to execute next.
- left
-
a parser to execute if
either
returns aLeft
. - right
-
a parser to execute if
either
returns aRight
.
Attributes
- Returns
-
a parser that will parse one of
left
orright
depending oneither
's result. - Example
-
def ifP[A](b: Parsley[Boolean], t: =>Parsley[A], e: =>Parsley[A]): Parsley[A] = { val cond = b.map { case true => Left(()) case false => Right(()) } branch(cond, t.map[Unit => A](x => _ => x), e.map[Unit => A](x => _ => x)) }
- Inherited from:
- ParsleyImpl (hidden)
- Source
- Parsley.scala
This combinator parses its first argument p
, then parses q
only if p
returns a Left
.
This combinator parses its first argument p
, then parses q
only if p
returns a Left
.
First, select(p, q)
parses p
, which, if successful, will produce either a Left(x)
or a Right(y)
. If a Left(x)
is produced, then the parser q
is executed to produce a function f
, and f(x)
is returned. Otherwise, if a Right(y)
is produced, y
is returned unmodified and q
is not parsed. If either p
or q
fails, the entire combinator fails. This is a special case of branch
where the right branch is pure(identity[B])
.
First introduced in Selective Applicative Functors (Mokhov et al. 2019).
Value parameters
- p
-
the first parser to execute, its result decides whether
q
is executed or not. - q
-
a parser to execute when
p
returns aLeft
.
Attributes
- Returns
-
a parser that will parse
p
then possibly parseq
to transformp
's result into aB
. - Example
-
def filter(pred: A => Boolean): Parsley[A] = { val p = this.map(x => if (pred(x)) Right(x) else Left(())) select(p, empty) }
- Inherited from:
- ParsleyImpl (hidden)
- Source
- Parsley.scala
Multiple Branching/Sequencing Combinators
These combinators allow for testing or sequencing a large number of parsers in one go. Be careful, however, these are variadic combinators and are necessarily (for compatibility with Scala 2) not lazy. In such a case where laziness is desired without resorting to the other lazier combinators, there is a neat trick: unroll the first iteration of the combinator, and use the corresponding regular combinator to do that (i.e. <::>
or *>
): since these will have a lazy right-hand side, the remaining variadic arguments will be kept lazily suspended until later. Alternatively, it is possible to use the prefix ~
combinator to make any individual arguments lazy as required, for example skip(p, ~q, r)
.
This combinator tries to parse each of the parsers ps
in order, until one of them succeeds.
This combinator tries to parse each of the parsers ps
in order, until one of them succeeds.
Finds the first parser in ps
which succeeds, returning its result. If none of the parsers succeed, then this combinator fails. If a parser fails having consumed input, this combinator fails immediately.
Value parameters
- ps
-
the parsers to try, in order.
Attributes
- Returns
-
a parser that tries to parse one of
ps
. - See also
- Example
-
scala> import parsley.combinator.choice scala> import parsley.character.string scala> val p = choice(string("abc"), string("ab"), string("bc"), string("d")) scala> p.parse("abc") val res0 = Success("abc") scala> p.parse("ab") val res1 = Failure(..) scala> p.parse("bc") val res2 = Success("bc") scala> p.parse("x") val res3 = Failure(..)
- Inherited from:
- combinator (hidden)
- Source
- combinator.scala
This combinator will parse each of ps
in order, collecting the results.
This combinator will parse each of ps
in order, collecting the results.
Given the parsers ps
, consisting of p1
through pn
, parses each in order. If they all succeed, producing the results x1
through xn
, then List(x1, .., xn)
is returned. If any of the parsers fail, then the whole combinator fails.
Value parameters
- p0
-
the first parser to be sequenced
- ps
-
parsers to be sequenced.
Attributes
- Returns
-
a parser that parses each of
ps
, returning the results in a list - See also
- Since
-
4.0.0
- Note
-
$strict
- Example
-
scala> import parsley.combinator.sequence scala> import parsley.character.{char, item} scala> val p = sequence(char('a'), item, char('c')) scala> p.parse("abc") val res0 = Success(List('a', 'b', 'c')) scala> p.parse("ab") val res1 = Failure(..)
- Inherited from:
- combinator (hidden)
- Source
- combinator.scala
This combinator will parse each of the parsers generated by applying f
to xs
, in order, collecting the results.
This combinator will parse each of the parsers generated by applying f
to xs
, in order, collecting the results.
Given the values xs
, consisting of x1
through xn
, first creates the parses f(x1)
through f(xn)
and then called sequence
on them.
Value parameters
- f
-
the function used to generate parsers for each values
- x0
-
the first value to turn into a parser and sequence.
- xs
-
the values to turn into parsers and sequence.
Attributes
- Returns
-
a parser that sequences the parsers generated from applying
f
to each ofxs
. - See also
- Since
-
4.0.0
- Note
-
$strict
- Example
-
// this is an OK implementation for `string`, which is common in Haskell. def string(str: String) = { traverse(str: _*)(char).span }
- Inherited from:
- combinator (hidden)
- Source
- combinator.scala
Range Combinators
These combinators allow for the parsing of a specific parser either a specific number of times, or between a certain amount of times.
This combinator parses between min
and max
occurrences of p
, returning the number of successes.
This combinator parses between min
and max
occurrences of p
, returning the number of successes.
Parses p
repeatedly a minimum of min
times and up to max
times both inclusive. If p
fails before min
is reached, then this combinator fails. It is not required for p
to fail after the max
th parse. The results are discarded and the number of successful parses of p
, n
, is returned instead, such that min <= n <= max
.
Value parameters
- max
-
the maximum number of times to repeat
p
, inclusive. - min
-
the minimum number of times to repeat
p
, inclusive. - p
-
the parser to repeat.
Attributes
- Returns
-
the number of times
p
parsed successfully. - Since
-
4.4.0
- Example
-
scala> import parsley.character.item scala> import parsley.combinator.count scala> val p = count(min=3, max=5)(item) scala> p.parse("ab") val res0 = Failure(..) scala> p.parse("abc") val res1 = Success(3) scala> p.parse("abcd") val res2 = Success(4) scala> p.parse("abcde") val res2 = Success(5) scala> p.parse("abcdef") val res2 = Success(5)
- Inherited from:
- combinator (hidden)
- Source
- combinator.scala
This combinator parses exactly n
occurrences of p
, returning these n
results in a list.
This combinator parses exactly n
occurrences of p
, returning these n
results in a list.
Parses p
repeatedly up to n
times. If p
fails before n
is reached, then this combinator fails. It is not required for p
to fail after the n
th parse. The results produced by p
, x1
through xn
, are returned as List(x1, .., xn)
.
Value parameters
- n
-
the number of times to repeat
p
. - p
-
the parser to repeat.
Attributes
- Returns
-
a parser that parses
p
exactlyn
times, returning a list of the results. - Since
-
4.0.0
- Example
-
scala> import parsley.character.item scala> import parsley.combinator.exactly scala> val p = exactly(3, item) scala> p.parse("ab") val res0 = Failure(..) scala> p.parse("abc") val res1 = Success(List('a', 'b', 'c')) scala> p.parse("abcd") val res2 = Success(List('a', 'b', 'c'))
- Inherited from:
- combinator (hidden)
- Source
- combinator.scala
This combinator parses between min
and max
occurrences of p
, returning these n
results in a list.
This combinator parses between min
and max
occurrences of p
, returning these n
results in a list.
Parses p
repeatedly a minimum of min
times and up to max
times both inclusive. If p
fails before min
is reached, then this combinator fails. It is not required for p
to fail after the max
th parse. The results produced by p
, xmin
through xmax
, are returned as List(xmin, .., xmax)
.
Value parameters
- max
-
the maximum number of times to repeat
p
, inclusive. - min
-
the minimum number of times to repeat
p
, inclusive. - p
-
the parser to repeat.
Attributes
- Returns
-
the results of the successful parses of
p
. - Since
-
4.4.0
- Example
-
scala> import parsley.character.item scala> import parsley.combinator.range scala> val p = range(min=3, max=5)(item) scala> p.parse("ab") val res0 = Failure(..) scala> p.parse("abc") val res1 = Success(List('a', 'b', 'c')) scala> p.parse("abcd") val res2 = Success(List('a', 'b', 'c', 'd')) scala> p.parse("abcde") val res2 = Success(List('a', 'b', 'c', 'd', 'e')) scala> p.parse("abcdef") val res2 = Success(List('a', 'b', 'c', 'd', 'e'))
- Inherited from:
- combinator (hidden)
- Source
- combinator.scala
Whitespace Skipping Parsers
These parsers are designed to skip chunks of whitespace, for very rudimentary lexing tasks. It is probably better to use the functionality of parsley.token.
This parser skips zero or more space characters using space
.
This parser skips zero or more space characters using space
.
Attributes
- Inherited from:
- character (hidden)
- Source
- character.scala
This parser skips zero or more space characters using whitespace
.
This parser skips zero or more space characters using whitespace
.
Attributes
- Inherited from:
- character (hidden)
- Source
- character.scala
Character Predicates
These are useful for providing to the sub-descriptions of a token.descriptions.LexicalDesc to specify behaviour for the lexer. Other than that, they aren't particularly useful.
This function returns true if a character is a hexadecimal digit.
This function returns true if a character is a hexadecimal digit.
A hexadecimal digit is one of (all inclusive ranges):
-
the digits
'0'
through'9'
-
the letters
'a'
through'f'
-
the letters
'A'
through'Z'
-
an equivalent from another charset
Attributes
- See also
- Inherited from:
- character (hidden)
- Source
- character.scala
This function returns true if a character is an octal digit.
This function returns true if a character is an octal digit.
An octal digit is one of '0'
to '7'
(inclusive).
Attributes
- See also
- Inherited from:
- character (hidden)
- Source
- character.scala
This function returns true if a character is either a space or a tab character.
This function returns true if a character is either a space or a tab character.
Attributes
- See also
- Inherited from:
- character (hidden)
- Source
- character.scala
condComp
This combinator parses q
depending only if p
returns a None
.
This combinator parses q
depending only if p
returns a None
.
First parses p
. If p
returned Some(x)
, then x
is returned. Otherwise, if p
returned None
then q
is parsed, producing y
, and y
is returned. If p
or q
fails, the combinator fails.
Value parameters
- p
-
the first parser, which returns an
Option
to eliminate. - q
-
a parser to execute when
p
returnsNone
, to provide a value of typeA
.
Attributes
- Returns
-
a parser that either just parses
p
or bothp
andq
in order to return anA
. - Example
-
decide(option(p), q) = p | q
- Inherited from:
- combinator (hidden)
- Source
- combinator.scala
This combinator can eliminate an Option
from the result of the parser p
.
This combinator can eliminate an Option
from the result of the parser p
.
First parse p
, if it succeeds returning Some(x)
, then return x
. However, if p
fails, or returned None
, then this combinator fails.
Value parameters
- p
-
the parser to parse and extract the result from.
Attributes
- Returns
-
a parser that tries to extract the result from
p
. - Example
-
decide(option(p)) = p
- Inherited from:
- combinator (hidden)
- Source
- combinator.scala
This combinator verfies that the given parser returns true
, or else fails.
This combinator verfies that the given parser returns true
, or else fails.
First, parse p
; if it succeeds then, so long at returns true
, this guard(p)
succeeds. Otherwise, if p
either fails, or returns false
, guard(p)
will fail.
Value parameters
- p
-
the parser that yields the condition value.
Attributes
- Example
-
guard(pure(true)) == unit guard(pure(false)) == empty when(p.map(!_), empty) == guardS(p)
- Inherited from:
- combinator (hidden)
- Source
- combinator.scala
This combinator parses one of thenP
or elseP
depending on the result of parsing condP
.
This combinator parses one of thenP
or elseP
depending on the result of parsing condP
.
This is a lifted if
-statement. First, parse condP
: if it is successful and returns true
, then parse thenP
; else, if it returned false
, parse elseP
; or, if condP
failed then fail. If either of thenP
or elseP
fail, then this combinator also fails.
Most useful in conjunction with Registers, as this allows for decisions to be made based on state.
Value parameters
- condP
-
the parser that yields the condition value.
- elseP
-
the parser to execute if the condition is
false.
- thenP
-
the parser to execute if the condition is
true
.
Attributes
- Returns
-
a parser that conditionally parses
thenP
orelseP
aftercondP
. - Since
-
4.5.0
- Example
-
ifS(pure(true), p, _) == p ifS(pure(false), _, p) == p
- Inherited from:
- combinator (hidden)
- Source
- combinator.scala
This combinator conditionally parses thenP
depending on the result of parsing condP
.
This combinator conditionally parses thenP
depending on the result of parsing condP
.
This is a lifted if
-statement. First, parse condP
: if it is successful and returns true
, then parse thenP
; else, if it returned false
do nothing; or, if condP
failed then fail. If thenP
fails, then this combinator also fails.
Most useful in conjunction with Registers, as this allows for decisions to be made based on state.
Value parameters
- condP
-
the parser that yields the condition value.
- thenP
-
the parser to execute if the condition is
true
.
Attributes
- Returns
-
a parser that conditionally parses
thenP
aftercondP
. - Example
-
whenS(pure(true), p) == p whenS(pure(false), _) == unit
- Inherited from:
- combinator (hidden)
- Source
- combinator.scala
This combinator repeatedly parses p
so long as it returns true
.
This combinator repeatedly parses p
so long as it returns true
.
This is a lifted while
-loop. First, parse p
: if it is successful and returns true
, then repeat; else if it returned false
stop; or, if it failed then this combinator fails.
Most useful in conjunction with Registers, as this allows for decisions to be made based on state. In particular, this can be used to define the forP
combinator.
Value parameters
- p
-
the parser to repeatedly parse.
Attributes
- Returns
-
a parser that continues to parse
p
until it returnsfalse
. - Example
-
def forP[A](init: Parsley[A], cond: =>Parsley[A => Boolean], step: =>Parsley[A => A])(body: =>Parsley[_]): Parsley[Unit] = { val reg = Reg.make[A] lazy val _cond = reg.gets(cond) lazy val _step = reg.modify(step) reg.put(init) ~> whenS(_cond, whileS(body ~> _step ~> _cond)) }
- Inherited from:
- combinator (hidden)
- Source
- combinator.scala
Type members
Types
Attributes
- Source
- quick.scala
Value members
Inherited methods
This combinator allows the function that results from one parser to be applied to the result of another parser.
This combinator allows the function that results from one parser to be applied to the result of another parser.
Effectively alias for <*>
, to be consistent with the other ap
variants.
Value parameters
- pf
-
the parser whose result is a function to map across the result of
p1
. - px
-
the second parser to perform.
Attributes
- Returns
-
a parser that applies the function
f
resulting frompf
to the resultx
of the parserp1
. - Inherited from:
- ap (hidden)
- Source
- ap.scala
$bodyAp
$bodyAp
Value parameters
- pf
-
$paramAp ten.
Attributes
- Returns
-
$returnAp
- Inherited from:
- ap (hidden)
- Source
- ap.scala
$bodyAp
$bodyAp
Value parameters
- pf
-
$paramAp eleven.
Attributes
- Returns
-
$returnAp
- Inherited from:
- ap (hidden)
- Source
- ap.scala
$bodyAp
$bodyAp
Value parameters
- pf
-
$paramAp twelve.
Attributes
- Returns
-
$returnAp
- Inherited from:
- ap (hidden)
- Source
- ap.scala
$bodyAp
$bodyAp
Value parameters
- pf
-
$paramAp thirteen.
Attributes
- Returns
-
$returnAp
- Inherited from:
- ap (hidden)
- Source
- ap.scala
$bodyAp
$bodyAp
Value parameters
- pf
-
$paramAp fourteen.
Attributes
- Returns
-
$returnAp
- Inherited from:
- ap (hidden)
- Source
- ap.scala
$bodyAp
$bodyAp
Value parameters
- pf
-
$paramAp fifteen.
Attributes
- Returns
-
$returnAp
- Inherited from:
- ap (hidden)
- Source
- ap.scala
$bodyAp
$bodyAp
Value parameters
- pf
-
$paramAp sixteen.
Attributes
- Returns
-
$returnAp
- Inherited from:
- ap (hidden)
- Source
- ap.scala
$bodyAp
$bodyAp
Value parameters
- pf
-
$paramAp seventeen.
Attributes
- Returns
-
$returnAp
- Inherited from:
- ap (hidden)
- Source
- ap.scala
$bodyAp
$bodyAp
Value parameters
- pf
-
$paramAp eighteen.
Attributes
- Returns
-
$returnAp
- Inherited from:
- ap (hidden)
- Source
- ap.scala
$bodyAp
$bodyAp
Value parameters
- pf
-
$paramAp nineteen.
Attributes
- Returns
-
$returnAp
- Inherited from:
- ap (hidden)
- Source
- ap.scala
$bodyAp
$bodyAp
Value parameters
- pf
-
$paramAp two.
Attributes
- Returns
-
$returnAp
- Inherited from:
- ap (hidden)
- Source
- ap.scala
$bodyAp
$bodyAp
Value parameters
- pf
-
$paramAp twenty.
Attributes
- Returns
-
$returnAp
- Inherited from:
- ap (hidden)
- Source
- ap.scala
$bodyAp
$bodyAp
Value parameters
- pf
-
$paramAp twenty-one.
Attributes
- Returns
-
$returnAp
- Inherited from:
- ap (hidden)
- Source
- ap.scala
$bodyAp
$bodyAp
Value parameters
- pf
-
$paramAp twenty-two.
Attributes
- Returns
-
$returnAp
- Inherited from:
- ap (hidden)
- Source
- ap.scala
$bodyAp
$bodyAp
Value parameters
- pf
-
$paramAp three.
Attributes
- Returns
-
$returnAp
- Inherited from:
- ap (hidden)
- Source
- ap.scala
$bodyAp
$bodyAp
Value parameters
- pf
-
$paramAp four.
Attributes
- Returns
-
$returnAp
- Inherited from:
- ap (hidden)
- Source
- ap.scala
$bodyAp
$bodyAp
Value parameters
- pf
-
$paramAp five.
Attributes
- Returns
-
$returnAp
- Inherited from:
- ap (hidden)
- Source
- ap.scala
$bodyAp
$bodyAp
Value parameters
- pf
-
$paramAp six.
Attributes
- Returns
-
$returnAp
- Inherited from:
- ap (hidden)
- Source
- ap.scala
$bodyAp
$bodyAp
Value parameters
- pf
-
$paramAp seven.
Attributes
- Returns
-
$returnAp
- Inherited from:
- ap (hidden)
- Source
- ap.scala
$bodyAp
$bodyAp
Value parameters
- pf
-
$paramAp eight.
Attributes
- Returns
-
$returnAp
- Inherited from:
- ap (hidden)
- Source
- ap.scala
$bodyAp
$bodyAp
Value parameters
- pf
-
$paramAp nine.
Attributes
- Returns
-
$returnAp
- Inherited from:
- ap (hidden)
- Source
- ap.scala
This combinator allows the result of a given parser to be changed using a given function.
This combinator allows the result of a given parser to be changed using a given function.
Effectively alias for map
, to be consistent with the other lift
variants.
Value parameters
- f
-
the function to map across the given parser
Attributes
- Returns
-
a parser that applies the function
f
to the result of the given parser. - Inherited from:
- lift (hidden)
- Source
- lift.scala
$bodyLift
$bodyLift
Value parameters
- f
-
$paramLift ten.
Attributes
- Returns
-
$returnLift
- Inherited from:
- lift (hidden)
- Source
- lift.scala
$bodyLift
$bodyLift
Value parameters
- f
-
$paramLift eleven.
Attributes
- Returns
-
$returnLift
- Inherited from:
- lift (hidden)
- Source
- lift.scala
$bodyLift
$bodyLift
Value parameters
- f
-
$paramLift twelve.
Attributes
- Returns
-
$returnLift
- Inherited from:
- lift (hidden)
- Source
- lift.scala
$bodyLift
$bodyLift
Value parameters
- f
-
$paramLift thirteen.
Attributes
- Returns
-
$returnLift
- Inherited from:
- lift (hidden)
- Source
- lift.scala
$bodyLift
$bodyLift
Value parameters
- f
-
$paramLift fourteen.
Attributes
- Returns
-
$returnLift
- Inherited from:
- lift (hidden)
- Source
- lift.scala
$bodyLift
$bodyLift
Value parameters
- f
-
$paramLift fifteen.
Attributes
- Returns
-
$returnLift
- Inherited from:
- lift (hidden)
- Source
- lift.scala
$bodyLift
$bodyLift
Value parameters
- f
-
$paramLift sixteen.
Attributes
- Returns
-
$returnLift
- Inherited from:
- lift (hidden)
- Source
- lift.scala
$bodyLift
$bodyLift
Value parameters
- f
-
$paramLift seventeen.
Attributes
- Returns
-
$returnLift
- Inherited from:
- lift (hidden)
- Source
- lift.scala
$bodyLift
$bodyLift
Value parameters
- f
-
$paramLift eighteen.
Attributes
- Returns
-
$returnLift
- Inherited from:
- lift (hidden)
- Source
- lift.scala
$bodyLift
$bodyLift
Value parameters
- f
-
$paramLift nineteen.
Attributes
- Returns
-
$returnLift
- Inherited from:
- lift (hidden)
- Source
- lift.scala
$bodyLift
$bodyLift
Value parameters
- f
-
$paramLift two.
Attributes
- Returns
-
$returnLift
- Inherited from:
- lift (hidden)
- Source
- lift.scala
$bodyLift
$bodyLift
Value parameters
- f
-
$paramLift twenty.
Attributes
- Returns
-
$returnLift
- Inherited from:
- lift (hidden)
- Source
- lift.scala
$bodyLift
$bodyLift
Value parameters
- f
-
$paramLift twenty-one.
Attributes
- Returns
-
$returnLift
- Inherited from:
- lift (hidden)
- Source
- lift.scala
$bodyLift
$bodyLift
Value parameters
- f
-
$paramLift twenty-two.
Attributes
- Returns
-
$returnLift
- Inherited from:
- lift (hidden)
- Source
- lift.scala
$bodyLift
$bodyLift
Value parameters
- f
-
$paramLift three.
Attributes
- Returns
-
$returnLift
- Inherited from:
- lift (hidden)
- Source
- lift.scala
$bodyLift
$bodyLift
Value parameters
- f
-
$paramLift four.
Attributes
- Returns
-
$returnLift
- Inherited from:
- lift (hidden)
- Source
- lift.scala
$bodyLift
$bodyLift
Value parameters
- f
-
$paramLift five.
Attributes
- Returns
-
$returnLift
- Inherited from:
- lift (hidden)
- Source
- lift.scala
$bodyLift
$bodyLift
Value parameters
- f
-
$paramLift six.
Attributes
- Returns
-
$returnLift
- Inherited from:
- lift (hidden)
- Source
- lift.scala
$bodyLift
$bodyLift
Value parameters
- f
-
$paramLift seven.
Attributes
- Returns
-
$returnLift
- Inherited from:
- lift (hidden)
- Source
- lift.scala
$bodyLift
$bodyLift
Value parameters
- f
-
$paramLift eight.
Attributes
- Returns
-
$returnLift
- Inherited from:
- lift (hidden)
- Source
- lift.scala
$bodyLift
$bodyLift
Value parameters
- f
-
$paramLift nine.
Attributes
- Returns
-
$returnLift
- Inherited from:
- lift (hidden)
- Source
- lift.scala
This combinator returns the result of a given parser and the number of characters it consumed.
This combinator returns the result of a given parser and the number of characters it consumed.
First records the initial offset
on entry to given parser p
, then executes p
. If p
succeeds, then the offset
is taken again, and the two values are subtracted to give width w
. The result of p
, x
is returned along with w
as (x, w)
. If p
fails, this combinator will also fail.
Value parameters
- p
-
the parser to compute the width for
Attributes
- Returns
-
a parser that pairs the result of the parser
p
with the number of characters it consumed - Since
-
4.4.0
- Note
-
the value returned is the number of 16-bit characters consumed, not unicode codepoints.
- Example
-
scala> import parsley.position.withWidth, parsley.character.string scala> withWidth(string("abc")).parse("abc") val res0 = Success(("abc", 3))
- Inherited from:
- position (hidden)
- Source
- position.scala
Inherited fields
This parser returns the current column number (starting at 1) of the input without having any other effect.
This parser returns the current column number (starting at 1) of the input without having any other effect.
When this combinator is ran, no input is required, nor consumed, and the current column number will always be successfully returned. It has no other effect on the state of the parser.
Attributes
- Returns
-
a parser that returns the column number the parser is currently at.
- Note
-
in the presence of wide unicode characters, the value returned may be inaccurate.
- Example
-
scala> import parsley.position.col, parsley.character.char scala> col.parse("") val res0 = Success(1) scala> (char('a') ~> col).parse("a") val res0 = Success(2) scala> (char('\n') ~> col).parse("\n") val res0 = Success(1)
- Inherited from:
- position (hidden)
- Source
- position.scala
This parser returns the current line number (starting at 1) of the input without having any other effect.
This parser returns the current line number (starting at 1) of the input without having any other effect.
When this combinator is ran, no input is required, nor consumed, and the current line number will always be successfully returned. It has no other effect on the state of the parser.
Attributes
- Returns
-
a parser that returns the line number the parser is currently at.
- Example
-
scala> import parsley.position.line, parsley.character.char scala> line.parse("") val res0 = Success(1) scala> (char('a') ~> line).parse("a") val res0 = Success(1) scala> (char('\n') ~> line).parse("\n") val res0 = Success(2)
- Inherited from:
- position (hidden)
- Source
- position.scala
This parser returns the current offset into the input (starting at 0) without having any other effect.
This parser returns the current offset into the input (starting at 0) without having any other effect.
When this combinator is ran, no input is required, nor consumed, and the current offset into the input will always be successfully returned. It has no other effect on the state of the parser.
Attributes
- Returns
-
a parser that returns the offset the parser is currently at.
- Note
-
offset does not take wide unicode codepoints into account.
- Example
-
scala> import parsley.position.offset, parsley.character.char scala> offset.parse("") val res0 = Success(0) scala> (char('a') ~> offset).parse("a") val res0 = Success(1) scala> (char('\n') ~> offset).parse("\n") val res0 = Success(1)
- Inherited from:
- position (hidden)
- Source
- position.scala
This parser returns the current line and column numbers (starting at 1) of the input without having any other effect.
This parser returns the current line and column numbers (starting at 1) of the input without having any other effect.
When this combinator is ran, no input is required, nor consumed, and the current line and column number will always be successfully returned. It has no other effect on the state of the parser.
Attributes
- Returns
-
a parser that returns the line and column number the parser is currently at.
- Note
-
in the presence of wide unicode characters, the column value returned may be inaccurate.
- Example
-
scala> import parsley.position.pos, parsley.character.char scala> pos.parse("") val res0 = Success((1, 1)) scala> (char('a') ~> pos).parse("a") val res0 = Success((1, 2)) scala> (char('\n') ~> pos).parse("\n") val res0 = Success((2, 1))
- Inherited from:
- position (hidden)
- Source
- position.scala