object combinator
This module contains a huge number of pre-made combinators that are very useful for a variety of purposes.
In particular, it contains combinators for: performing a parser iteratively, collecting all the results; querying whether or not any input is left; optionally performing parsers; parsing delimited constructions; handling multiple possible alternatives or parsers to sequence; handling more complex conditional execution; and more.
- Source
- combinator.scala
- Since
2.2.0
- Grouped
- Alphabetic
- By Inheritance
- combinator
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
attemptChoice[A](ps: Parsley[A]*): Parsley[A]
This combinator tries to parse each of the parsers
psin order, until one of them succeeds.This combinator tries to parse each of the parsers
psin order, until one of them succeeds.Finds the first parser in
pswhich succeeds, returning its result. If none of the parsers succeed, then this combinator fails. This combinator will always try and parse each of the combinators until one succeeds, regardless of how they fail. The last argument will not be wrapped inattempt, as this is not necessary.- ps
the parsers to try, in order.
- returns
a parser that tries to parse one of
ps.
scala> import parsley.combinator.attemptChoice scala> import parsley.character.string scala> val p = attemptChoice(string("abc"), string("ab"), string("bc"), string("d")) scala> p.parse("abc") val res0 = Success("abc") scala> p.parse("ab") val res1 = Success("ab") scala> p.parse("bc") val res2 = Success("bc") scala> p.parse("x") val res3 = Failure(..)
- Note
this combinator is not particularly efficient, because it may unnecessarily backtrack for each alternative.
- See also
Example: -
def
between[A](open: Parsley[_], close: ⇒ Parsley[_], p: ⇒ Parsley[A]): Parsley[A]
This combinator parses
open, followed byp, and thenclose.This combinator parses
open, followed byp, and thenclose.First parse
open, ignore its result, then parse,p, producingx. Finally, parseclose, ignoring its result. Ifopen,p, andcloseall succeeded, then returnx. If any of them failed, this combinator fails.- open
the first parser to parse.
- close
the last parser to parse.
- p
the parser to parse between the other two.
- returns
a parser that reads
open, thenp, thencloseand returns the result ofp.
def braces[A](p: Parsley[A]) = between(char('{'), char('}'), p)
Example: -
def
choice[A](ps: Parsley[A]*): Parsley[A]
This combinator tries to parse each of the parsers
psin order, until one of them succeeds.This combinator tries to parse each of the parsers
psin order, until one of them succeeds.Finds the first parser in
pswhich 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.- ps
the parsers to try, in order.
- returns
a parser that tries to parse one of
ps.
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(..)
- See also
Example: -
def
clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native()
-
def
decide[A](p: Parsley[Option[A]], q: ⇒ Parsley[A]): Parsley[A]
This combinator parses
qdepending only ifpreturns aNone.This combinator parses
qdepending only ifpreturns aNone.First parses
p. IfpreturnedSome(x), thenxis returned. Otherwise, ifpreturnedNonethenqis parsed, producingy, andyis returned. Ifporqfails, the combinator fails.- p
the first parser, which returns an
Optionto eliminate.- q
a parser to execute when
preturnsNone, to provide a value of typeA.- returns
a parser that either just parses
por bothpandqin order to return anA.
decide(option(p), q) = p <|> q
Example: -
def
decide[A](p: Parsley[Option[A]]): Parsley[A]
This combinator can eliminate an
Optionfrom the result of the parserp.This combinator can eliminate an
Optionfrom the result of the parserp.First parse
p, if it succeeds returningSome(x), then returnx. However, ifpfails, or returnedNone, then this combinator fails.- p
the parser to parse and extract the result from.
- returns
a parser that tries to extract the result from
p.
decide(option(p)) = p
Example: -
def
endBy[A](p: Parsley[A], sep: ⇒ Parsley[_]): Parsley[List[A]]
This combinator parses zero or more occurrences of
p, separated and ended bysep.This combinator parses zero or more occurrences of
p, separated and ended bysep.Behaves just like
endBy1, except does not require an initialpandsep, returning the empty list instead.- p
the parser whose results are collected into a list.
- sep
the delimiter that must be parsed between every
p.- returns
a parser that parses
pdelimited bysep, returning the list ofp's results.
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))
Example: -
def
endBy1[A](p: Parsley[A], sep: ⇒ Parsley[_]): Parsley[List[A]]
This combinator parses one or more occurrences of
p, separated and ended bysep.This combinator parses one or more occurrences of
p, separated and ended bysep.Parses
pfollowed bysepone or more times. The results of thep's,x1throughxn, are returned asList(x1, .., xn). Ifporsepfails having consumed input, the whole parser fails.- p
the parser whose results are collected into a list.
- sep
the delimiter that must be parsed between every
p.- returns
a parser that parses
pdelimited bysep, returning the list ofp's results.
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))
Example: -
val
eof: Parsley[Unit]
This parser only succeeds at the end of the input.
This parser only succeeds at the end of the input.
Equivalent to
notFollowedBy(item).scala> import parsley.combinator.eof scala> eof.parse("a") val res0 = Failure(..) scala> eof.parse("") val res1 = Success(())
Example: -
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
exactly[A](n: Int, p: Parsley[A]): Parsley[List[A]]
This combinator parses exactly
noccurrences ofp, returning thesenresults in a list.This combinator parses exactly
noccurrences ofp, returning thesenresults in a list.Parses
prepeatedly up tontimes. Ifpfails beforenis reached, then this combinator fails. It is not required forpto fail after thenth parse. The results produced byp,x1throughxn, are returned asList(x1, .., xn).- n
the number of times to repeat
p.- p
the parser to repeat.
- returns
a parser that parses
pexactlyntimes, returning a list of the results.
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'))
- Since
4.0.0
Example: -
def
finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
def
ifP[A](condP: Parsley[Boolean], thenP: ⇒ Parsley[A], elseP: ⇒ Parsley[A]): Parsley[A]
This combinator parses one of
thenPorelsePdepending on the result of parsingcondP.This combinator parses one of
thenPorelsePdepending on the result of parsingcondP.This is a lifted
if-statement. First, parsecondP: if it is successful and returnstrue, then parsethenP; else, if it returnedfalse, parseelseP; or, ifcondPfailed then fail. If either ofthenPorelsePfail, then this combinator also fails.Most useful in conjunction with Registers, as this allows for decisions to be made based on state.
- condP
the parser that yields the condition value.
- thenP
the parser to execute if the condition is
true.- elseP
the parser to execute if the condition is
false.- returns
a parser that conditionally parses
thenPorelsePaftercondP.
ifP(pure(true), p, _) == p ifP(pure(false), _, p) == p
- Since
4.0.0
Example: -
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
def
many[A](p: Parsley[A]): Parsley[List[A]]
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. Ifpfailed having consumed input, this combinator fails. Otherwise whenpfails without consuming input, this combinator will return all of the results,x1throughxn(withn >= 0), in a list:List(x1, .., xn). Ifpwas never successful, the empty list is returned.- p
the parser to execute multiple times.
- returns
a parser that parses
puntil it fails, returning the list of all the successful results.
scala> import parsley.character.string scala> import parsley.combinator.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(..)
- Since
2.2.0
Example: -
def
manyN[A](n: Int, p: Parsley[A]): Parsley[List[A]]
This combinator repeatedly parses a given parser
nor more times, collecting the results into a list.This combinator repeatedly parses a given parser
nor more times, collecting the results into a list.Parses a given parser,
p, repeatedly until it fails. Ifpfailed having consumed input, this combinator fails. Otherwise whenpfails without consuming input, this combinator will return all of the results,x1throughxm(withm >= n), in a list:List(x1, .., xm). Ifpwas not successful at leastntimes, this combinator fails.- n
the minimum number of
ps required.- p
the parser to execute multiple times.
- returns
a parser that parses
puntil it fails, returning the list of all the successful results.
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(..)
- Note
many(p) == many(0, p)andsome(p) == many(1, p).
Example: -
def
manyUntil[A](p: Parsley[A], end: Parsley[_]): Parsley[List[A]]
This combinator repeatedly parses a given parser zero or more times, until the
endparser succeeds, collecting the results into a list.This combinator repeatedly parses a given parser zero or more times, until the
endparser succeeds, collecting the results into a list.First tries to parse
end, if it fails without consuming input, then parsesp, which must succeed. This repeats untilendsucceeds. Whenenddoes succeed, this combinator will return all of the results generated byp,x1throughxn(withn >= 0), in a list:List(x1, .., xn). Ifendcould be parsed immediately, the empty list is returned.- p
the parser to execute multiple times.
- end
the parser that stops the parsing of
p.- returns
a parser that parses
puntilendsucceeds, returning the list of all the successful results.
This can be useful for scanning comments:
scala> import parsley.character.{string, item, endOfLine} scala> import parsley.combinator.many scala> val comment = string("//") *> manyUntil(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)
Example: -
val
more: Parsley[Unit]
This parser only succeeds if there is still more input.
This parser only succeeds if there is still more input.
Equivalent to
lookAhead(item).void.scala> import parsley.combinator.more scala> more.parse("") val res0 = Failure(..) scala> more.parse("a") val res1 = Success(())
Example: -
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
def
option[A](p: Parsley[A]): Parsley[Option[A]]
This combinator tries to parse
p, wrapping its result in aSomeif it succeeds, or returnsNoneif it fails.This combinator tries to parse
p, wrapping its result in aSomeif it succeeds, or returnsNoneif it fails.Tries to parse
p. Ifpsucceeded, producingx, thenSome(x)is returned. Otherwise, ifpfailed without consuming input, thenNoneis returned instead.- p
the parser to try to parse.
- returns
a parser that tries to parse
p, but can still succeed withNoneif that was not possible.
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(..)
Example: -
def
optional(p: Parsley[_]): Parsley[Unit]
This combinator will parse
pif possible, otherwise will do nothing.This combinator will parse
pif possible, otherwise will do nothing.Tries to parse
p. Ifpsucceeds, or fails without consuming input then this combinator is successful. Otherwise, ifpfailed having consumed input, this combinator fails.- p
the parser to try to parse.
- returns
a parser that tries to parse
p.
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(..)
- Note
equivalent to
optionalAs(p, ()).
Example: -
def
optionalAs[A](p: Parsley[_], x: A): Parsley[A]
This combinator will parse
pif possible, otherwise will do nothing.This combinator will parse
pif possible, otherwise will do nothing.Tries to parse
p. Ifpsucceeds, or fails without consuming input then this combinator is successful and returnsx. Otherwise, ifpfailed having consumed input, this combinator fails.- p
the parser to try to parse.
- x
the value to return regardless of how
pperforms.- returns
a parser that tries to parse
p, returningxregardless of success or failure.
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(..)
Example: -
def
sepBy[A](p: Parsley[A], sep: ⇒ Parsley[_]): Parsley[List[A]]
This combinator parses zero or more occurrences of
p, separated bysep.This combinator parses zero or more occurrences of
p, separated bysep.Behaves just like
sepBy1, except does not require an initialp, returning the empty list instead.- p
the parser whose results are collected into a list.
- sep
the delimiter that must be parsed between every
p.- returns
a parser that parses
pdelimited bysep, returning the list ofp's results.
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
Example: -
def
sepBy1[A](p: Parsley[A], sep: ⇒ Parsley[_]): Parsley[List[A]]
This combinator parses one or more occurrences of
p, separated bysep.This combinator parses one or more occurrences of
p, separated bysep.First parses a
p. Then parsessepfollowed bypuntil there are no moreseps. The results of thep's,x1throughxn, are returned asList(x1, .., xn). Ifporsepfails having consumed input, the whole parser fails. Requires at least onepto have been parsed.- p
the parser whose results are collected into a list.
- sep
the delimiter that must be parsed between every
p.- returns
a parser that parses
pdelimited bysep, returning the list ofp's results.
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
Example: -
def
sepEndBy[A](p: Parsley[A], sep: ⇒ Parsley[_]): Parsley[List[A]]
This combinator parses zero or more occurrences of
p, separated and optionally ended bysep.This combinator parses zero or more occurrences of
p, separated and optionally ended bysep.Behaves just like
sepEndBy1, except does not require an initialp, returning the empty list instead.- p
the parser whose results are collected into a list.
- sep
the delimiter that must be parsed between every
p.- returns
a parser that parses
pdelimited bysep, returning the list ofp's results.
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))
Example: -
def
sepEndBy1[A](p: Parsley[A], sep: ⇒ Parsley[_]): Parsley[List[A]]
This combinator parses one or more occurrences of
p, separated and optionally ended bysep.This combinator parses one or more occurrences of
p, separated and optionally ended bysep.First parses a
p. Then parsessepfollowed bypuntil there are no more: if a finalsepexists, this is parsed. The results of thep's,x1throughxn, are returned asList(x1, .., xn). Ifporsepfails having consumed input, the whole parser fails. Requires at least onepto have been parsed.- p
the parser whose results are collected into a list.
- sep
the delimiter that must be parsed between every
p.- returns
a parser that parses
pdelimited bysep, returning the list ofp's results.
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))
Example: -
def
sequence[A](ps: Parsley[A]*): Parsley[List[A]]
This combinator will parse each of
psin order, collecting the results.This combinator will parse each of
psin order, collecting the results.Given the parsers
ps, consisting ofp1throughpn, parses each in order. If they all succeed, producing the resultsx1throughxn, thenList(x1, .., xn)is returned. If any of the parsers fail, then the whole combinator fails.- ps
parsers to be sequenced.
- returns
a parser that parses each of
ps, returning the results in a list
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(..)
- Since
4.0.0
- Note
be aware that all of the arguments to this combinator are in strict positions.
- See also
Example: -
def
skip(p: Parsley[_], ps: Parsley[_]*): Parsley[Unit]
This combinator will parse each of
psin order, discarding the results.This combinator will parse each of
psin order, discarding the results.Given the parsers
ps, consisting ofp1throughpn, parses each in order. If they all succeed, this combinator succeeds. If any of the parsers fail, then the whole combinator fails.- p
first parser to be sequenced
- ps
parsers to be sequenced.
- returns
a parser that parses each of
ps, returning().
scala> import parsley.combinator.skip scala> import parsley.character.{char, item} scala> val p = skip(char('a'), item, char('c')) scala> p.parse("abc") val res0 = Success(()) scala> p.parse("ab") val res1 = Failure(..)
- Note
be aware that all of the arguments to this combinator are in strict positions.
- See also
Example: -
def
skipMany(p: Parsley[_]): Parsley[Unit]
This combinator repeatedly parses a given parser zero or more times, ignoring the results.
This combinator repeatedly parses a given parser zero or more times, ignoring the results.
Parses a given parser,
p, repeatedly until it fails. Ifpfailed having consumed input, this combinator fails. Otherwise whenpfails without consuming input, this combinator will succeed.- p
the parser to execute multiple times.
- returns
a parser that parses
puntil it fails, returning unit.
scala> import parsley.character.string scala> import parsley.combinator.skipMany scala> val p = skipMany(string("ab")) scala> p.parse("") val res0 = Success(()) scala> p.parse("ab") val res1 = Success(()) scala> p.parse("abababab") val res2 = Success(()) scala> p.parse("aba") val res3 = Failure(..)
- Since
2.2.0
Example: -
def
skipManyN(n: Int, p: Parsley[_]): Parsley[Unit]
This combinator repeatedly parses a given parser
nor more times, ignoring the results.This combinator repeatedly parses a given parser
nor more times, ignoring the results.Parses a given parser,
p, repeatedly until it fails. Ifpfailed having consumed input, this combinator fails. Otherwise whenpfails without consuming input, this combinator will succeed. The parserpmust succeed at leastntimes.- p
the parser to execute multiple times.
- returns
a parser that parses
puntil it fails, returning unit.
scala> import parsley.character.string scala> import parsley.combinator.skipManyN scala> val p = skipManyN(2, string("ab")) scala> p.parse("") val res0 = Failure(..) scala> p.parse("ab") val res1 = Failure(..) scala> p.parse("abababab") val res2 = Success(()) scala> p.parse("aba") val res3 = Failure(..)
Example: -
def
skipSome(p: Parsley[_]): Parsley[Unit]
This combinator repeatedly parses a given parser one or more times, ignoring the results.
This combinator repeatedly parses a given parser one or more times, ignoring the results.
Parses a given parser,
p, repeatedly until it fails. Ifpfailed having consumed input, this combinator fails. Otherwise whenpfails without consuming input, this combinator will succeed. The parserpmust succeed at least once.- p
the parser to execute multiple times.
- returns
a parser that parses
puntil it fails, returning unit.
scala> import parsley.character.string scala> import parsley.combinator.skipSome scala> val p = skipSome(string("ab")) scala> p.parse("") val res0 = Failure(..) scala> p.parse("ab") val res1 = Success(()) scala> p.parse("abababab") val res2 = Success(()) scala> p.parse("aba") val res3 = Failure(..)
Example: -
def
some[A](p: Parsley[A]): Parsley[List[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. Ifpfailed having consumed input, this combinator fails. Otherwise whenpfails without consuming input, this combinator will return all of the results,x1throughxn(withn >= 1), in a list:List(x1, .., xn). Ifpwas not successful at least one time, this combinator fails.- p
the parser to execute multiple times.
- returns
a parser that parses
puntil it fails, returning the list of all the successful results.
scala> import parsley.character.string scala> import parsley.combinator.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(..)
Example: -
def
someUntil[A](p: Parsley[A], end: Parsley[_]): Parsley[List[A]]
This combinator repeatedly parses a given parser one or more times, until the
endparser succeeds, collecting the results into a list.This combinator repeatedly parses a given parser one or more times, until the
endparser succeeds, collecting the results into a list.First ensures that trying to parse
endfails, then tries to parsep. If it succeed then it will repeatedly: try to parseend, if it fails without consuming input, then parsesp, which must succeed. Whenenddoes succeed, this combinator will return all of the results generated byp,x1throughxn(withn >= 1), in a list:List(x1, .., xn). The parserpmust succeed at least once beforeendsucceeds.- p
the parser to execute multiple times.
- end
the parser that stops the parsing of
p.- returns
a parser that parses
puntilendsucceeds, returning the list of all the successful results.
This can be useful for scanning comments:
scala> import parsley.character.{string, item, endOfLine} scala> import parsley.combinator.many 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'))
Example: -
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toString(): String
- Definition Classes
- AnyRef → Any
-
def
traverse[A, B](f: (A) ⇒ Parsley[B], xs: A*): Parsley[List[B]]
This combinator will parse each of the parsers generated by applying
ftoxs, in order, collecting the results.This combinator will parse each of the parsers generated by applying
ftoxs, in order, collecting the results.Given the values
xs, consisting ofx1throughxn, first creates the parsesf(x1)throughf(xn)and then calledsequenceon them.- f
the function used to generate parsers for each values
- xs
the values to turn into parsers and sequence.
- returns
a parser that sequences the parsers generated from applying
fto each ofxs.
// this is an OK implementation for `string`, which is common in Haskell. def string(str: String) = { traverse(char, str: _*).map(_.mkString) }
- Since
4.0.0
- Note
be aware that all of the arguments to this combinator are in strict positions.
- See also
Example: -
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native()
-
def
when(condP: Parsley[Boolean], thenP: ⇒ Parsley[Unit]): Parsley[Unit]
This combinator conditionally parses
thenPdepending on the result of parsingcondP.This combinator conditionally parses
thenPdepending on the result of parsingcondP.This is a lifted
if-statement. First, parsecondP: if it is successful and returnstrue, then parsethenP; else, if it returnedfalsedo nothing; or, ifcondPfailed then fail. IfthenPfails, then this combinator also fails.Most useful in conjunction with Registers, as this allows for decisions to be made based on state.
- condP
the parser that yields the condition value.
- thenP
the parser to execute if the condition is
true.- returns
a parser that conditionally parses
thenPorelsePaftercondP.
when(pure(true), p) == p when(pure(false), _) == unit
Example: -
def
whileP(p: Parsley[Boolean]): Parsley[Unit]
This combinator repeatedly parses
pso long as it returnstrue.This combinator repeatedly parses
pso long as it returnstrue.This is a lifted
while-loop. First, parsep: if it is successful and returnstrue, then repeat; else if it returnedfalsestop; 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
forPcombinator.- p
the parser to repeatedly parse.
- returns
a parser that continues to parse
puntil it returnsfalse.
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) *> when(_cond, whileP(body *> _step *> _cond)) }
Example:
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. Depending on the combinator, all of the results produced by the
repeated execution of the parser may be returned in a List. These are almost essential for any practical parsing
task.
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.
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.
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 ps 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.
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).