state

parsley.state
object state

This module contains all the functionality and operations for using and manipulating references.

These often have a role in performing context-sensitive parsing tasks, where a Turing-powerful system is required. While flatMap is capable of such parsing, it is much less efficient than the use of references, though slightly more flexible. In particular, the persist combinator enabled by StateCombinators can serve as a drop-in replacement for flatMap in many scenarios.

Attributes

Since

4.5.0

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

Members list

Grouped members

References

The Ref type is used to describe pieces of state that are threaded through a parser. The creation and basic combinators of references are found within Ref and its companion object.

class Ref[A]

This class is used to index references within the mutable state.

This class is used to index references within the mutable state.

Attributes

Note

it is undefined behaviour to use a reference in multiple different independent parsers. You should be careful to parameterise the references in shared parsers and allocate fresh ones for each "top-level" parser you will run.

Companion
object
Source
state.scala
Supertypes
class Object
trait Matchable
class Any
object Ref

This object allows for the construction of a reference via its make function.

This object allows for the construction of a reference via its make function.

Attributes

Companion
class
Source
state.scala
Supertypes
class Object
trait Matchable
class Any
Self type
Ref.type

Reference-Based Combinators

Some combinators are made much more efficient in the presence of references and they can be found here.

def forP[A](init: Parsley[A], cond: => Parsley[A => Boolean], step: => Parsley[A => A])(body: => Parsley[_]): Parsley[Unit]

This combinator allows for the repeated execution of a parser in a stateful loop.

This combinator allows for the repeated execution of a parser in a stateful loop.

forP(init, cond, step)(body) behaves much like a traditional for loop using init, cond, step and body as parsers which control the loop itself. First, a reference r is created and initialised with init. Then cond is parsed, producing the function pred. If r.gets(pred) returns true, then body is parsed, then r is modified with the result of parsing step. This repeats until r.gets(pred) returns false. This is useful for performing certain context sensitive tasks.

Value parameters

body

the body of the loop performed each iteration.

cond

the condition by which the loop terminates.

init

the initial value of the induction variable.

step

the change in induction variable on each iteration.

Attributes

Returns

a parser that initialises some state with init and then parses body until cond is true, modifying the state each iteration with step.

See also

forYieldP for a version that returns the results of each body parse.

Example

the classic context sensitive grammar of anbncn can be matched using forP:

val r = Ref.make[Int]
r.set(0) *>
many('a' *> r.update(_+1)) *>
forP[Int](r.get, pure(_ != 0), pure(_ - 1)){'b'} *>
forP[Int](r.get, pure(_ != 0), pure(_ - 1)){'c'}
Source
state.scala
def forP_[A](init: Parsley[A], cond: => Parsley[A => Boolean], step: => Parsley[A => A])(body: Parsley[A] => Parsley[_]): Parsley[Unit]

This combinator allows for the repeated execution of a parser body in a stateful loop, body will have access to the current value of the state.

This combinator allows for the repeated execution of a parser body in a stateful loop, body will have access to the current value of the state.

forP_(init, cond, step)(body) behaves much like a traditional for loop using init, cond, step and body as parsers which control the loop itself. First, a reference r is created and initialised with init. Then cond is parsed, producing the function pred. If r.gets(pred) returns true, then body is parsed, then r is modified with the result of parsing step. This repeats until r.gets(pred) returns false. This is useful for performing certain context sensitive tasks.

Value parameters

body

the body of the loop performed each iteration, which has access to the current value of the state.

cond

the condition by which the loop terminates.

init

the initial value of the induction variable.

step

the change in induction variable on each iteration.

Attributes

Returns

a parser that initialises some state with init and then parses body until cond is true, modifying the state each iteration with step.

See also

forYieldP_ for a version that returns the results of each body parse.

Example

the classic context sensitive grammar of anbncn can be matched using forP_:

val r = Ref.make[Int]
r.set(0) *>
many('a' *> r.update(_+1)) *>
forP_[Int](r.get, pure(_ != 0), pure(_ - 1)){_ => 'b'} *>
forP_[Int](r.get, pure(_ != 0), pure(_ - 1)){_ => 'c'}
Source
state.scala
def forYieldP[A, B](init: Parsley[A], cond: => Parsley[A => Boolean], step: => Parsley[A => A])(body: => Parsley[B]): Parsley[List[B]]

This combinator allows for the repeated execution of a parser in a stateful loop.

This combinator allows for the repeated execution of a parser in a stateful loop.

forYieldP(init, cond, step)(body) behaves much like a traditional for comprehension using init, cond, step and body as parsers which control the loop itself. First, a reference r is created and initialised with init. Then cond is parsed, producing the function pred. If r.gets(pred) returns true, then body is parsed, then r is modified with the result of parsing step. This repeats until r.gets(pred) returns false. This is useful for performing certain context sensitive tasks. Unlike forP the results of the body invokations are returned in a list.

Value parameters

body

the body of the loop performed each iteration.

cond

the condition by which the loop terminates.

init

the initial value of the induction variable.

step

the change in induction variable on each iteration.

Attributes

Returns

a parser that initialises some state with init and then parses body until cond is true, modifying the state each iteration with step. The results of the iterations are returned in a list.

See also

forP for a version that ignores the results.

Example

the classic context sensitive grammar of anbncn can be matched using forP:

val r = Ref.make[Int]
r.set(0) *>
many('a' *> r.update(_+1)) *>
forYieldP[Int, Char](r.get, pure(_ != 0), pure(_ - 1)){'b'} *>
forYieldP[Int, Char](r.get, pure(_ != 0), pure(_ - 1)){'c'}

This will return a list n 'c' characters.

Source
state.scala
def forYieldP_[A, B](init: Parsley[A], cond: => Parsley[A => Boolean], step: => Parsley[A => A])(body: Parsley[A] => Parsley[B]): Parsley[List[B]]

This combinator allows for the repeated execution of a parser body in a stateful loop, body will have access to the current value of the state.

This combinator allows for the repeated execution of a parser body in a stateful loop, body will have access to the current value of the state.

forP_(init, cond, step)(body) behaves much like a traditional for comprehension using init, cond, step and body as parsers which control the loop itself. First, a reference r is created and initialised with init. Then cond is parsed, producing the function pred. If r.gets(pred) returns true, then body is parsed, then r is modified with the result of parsing step. This repeats until r.gets(pred) returns false. This is useful for performing certain context sensitive tasks. Unlike forP_ the results of the body invokations are returned in a list.

Value parameters

body

the body of the loop performed each iteration, which has access to the current value of the state.

cond

the condition by which the loop terminates.

init

the initial value of the induction variable.

step

the change in induction variable on each iteration.

Attributes

Returns

a parser that initialises some state with init and then parses body until cond is true, modifying the state each iteration with step.

See also

forP_ for a version that ignores the results of the body.

Example

the classic context sensitive grammar of anbncn can be matched using forP_:

val r = Ref.make[Int]
r.set(0) *>
many('a' *> r.update(_+1)) *>
forYieldP_[Int, Char](r.get, pure(_ != 0), pure(_ - 1)){_ => 'b'} *>
forYieldP_[Int, Char](r.get, pure(_ != 0), pure(_ - 1)){_ => 'c'}

This will return a list n 'c' characters.

Source
state.scala

Reference Extension Combinators

These are implicit classes that, when in scope, enable additional combinators on parsers that interact with the reference system in some way.

final implicit class RefMaker[A](val x: A) extends AnyVal

This class, when in scope, enables a method to create and fill a reference with a given value.

This class, when in scope, enables a method to create and fill a reference with a given value.

Value parameters

x

the value to initialise a reference with.

Attributes

Constructor

This constructor should not be called manually, it is designed to be used via Scala's implicit resolution.

Source
state.scala
Supertypes
class AnyVal
trait Matchable
class Any
final implicit def RefMaker[A](x: A): RefMaker[A]

This class, when in scope, enables a method to create and fill a reference with a given value.

This class, when in scope, enables a method to create and fill a reference with a given value.

Value parameters

x

the value to initialise a reference with.

Attributes

Constructor

This constructor should not be called manually, it is designed to be used via Scala's implicit resolution.

Source
state.scala
final implicit class StateCombinators[P, A](p: P)(implicit con: P => Parsley[A])

This class, when in scope, enables the use of combinators directly on parsers that interact with the reference system to store and persist results so they can be used multiple times.

This class, when in scope, enables the use of combinators directly on parsers that interact with the reference system to store and persist results so they can be used multiple times.

Value parameters

con

a conversion that allows values convertible to parsers to be used.

p

the value that this class is enabling methods on.

Attributes

Constructor

This constructor should not be called manually, it is designed to be used via Scala's implicit resolution.

Source
state.scala
Supertypes
class Object
trait Matchable
class Any
final implicit def StateCombinators[P, A](p: P)(implicit con: P => Parsley[A]): StateCombinators[P, A]

This class, when in scope, enables the use of combinators directly on parsers that interact with the reference system to store and persist results so they can be used multiple times.

This class, when in scope, enables the use of combinators directly on parsers that interact with the reference system to store and persist results so they can be used multiple times.

Value parameters

con

a conversion that allows values convertible to parsers to be used.

p

the value that this class is enabling methods on.

Attributes

Constructor

This constructor should not be called manually, it is designed to be used via Scala's implicit resolution.

Source
state.scala