registers

parsley.registers$
object registers

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

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 registers, though slightly more flexible. In particular, the persist combinator enabled by RegisterMethods can serve as a drop-in replacement for flatMap in many scenarios.

Attributes

Since:

2.2.0

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

Members list

Concise view

Registers

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

class Reg[A]

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

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

Attributes

Since:

2.2.0

Note:

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

Companion:
object
Source:
registers.scala
Graph
Supertypes
class Object
trait Matchable
class Any
object Reg

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

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

Attributes

Companion:
class
Source:
registers.scala
Graph
Supertypes
class Object
trait Matchable
class Any
Self type
Reg.type

Register-Based Combinators

Some combinators are made much more efficient in the presence of registers 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 register 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.

Attributes

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.

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 = Reg.make[Int]
r.put(0) *>
many('a' *> r.modify(_+1)) *>
forP[Int](r.get, pure(_ != 0), pure(_ - 1)){'b'} *>
forP[Int](r.get, pure(_ != 0), pure(_ - 1)){'c'}
Source:
registers.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 register 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.

Attributes

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.

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 = Reg.make[Int]
r.put(0) *>
many('a' *> r.modify(_+1)) *>
forP_[Int](r.get, pure(_ != 0), pure(_ - 1)){_ => 'b'} *>
forP_[Int](r.get, pure(_ != 0), pure(_ - 1)){_ => 'c'}
Source:
registers.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 register 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.

Attributes

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.

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 = Reg.make[Int]
r.put(0) *>
many('a' *> r.modify(_+1)) *>
forYieldP[Int](r.get, pure(_ != 0), pure(_ - 1)){'b'} *>
forYieldP[Int](r.get, pure(_ != 0), pure(_ - 1)){'c'}

This will return a list n 'c' characters.

Source:
registers.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 register 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.

Attributes

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.

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 = Reg.make[Int]
r.put(0) *>
many('a' *> r.modify(_+1)) *>
forYieldP_[Int](r.get, pure(_ != 0), pure(_ - 1)){_ => 'b'} *>
forYieldP_[Int](r.get, pure(_ != 0), pure(_ - 1)){_ => 'c'}

This will return a list n 'c' characters.

Source:
registers.scala

Register Extension Combinators

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

final implicit class RegisterMaker[A](x: A)

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

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

Attributes

x

the value to initialise a register with.

Constructor:

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

Source:
registers.scala
Graph
Supertypes
class Object
trait Matchable
class Any
final implicit def RegisterMaker[A](x: A): RegisterMaker[A]

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

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

Attributes

x

the value to initialise a register with.

Constructor:

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

Source:
registers.scala
final implicit class RegisterMethods[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 register 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 register system to store and persist results so they can be used multiple times.

Attributes

con

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

p

the value that this class is enabling methods on.

Constructor:

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

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

This class, when in scope, enables the use of combinators directly on parsers that interact with the register 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 register system to store and persist results so they can be used multiple times.

Attributes

con

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

p

the value that this class is enabling methods on.

Constructor:

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

Source:
registers.scala