Reg

parsley.registers$.Reg
See theReg companion object
class Reg[A]

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

Members list

Concise view

Getters

These combinators allow for the retrieval of the stateful value of a register, and injecting it into the parsing context. Does not modify the contents of the register itself.

def get: Parsley[A]

This combinator injects the value stored in this register into a parser.

This combinator injects the value stored in this register into a parser.

Allows for the value stored in this register to be purely injected into the parsing context. No input is consumed in this process, and it cannot fail.

Attributes

Returns:

a parser that returns the value stored in this register.

Since:

3.2.0

Example:

Get-Get Law:

r.get *> r.get == r.get
r.get <~> r.get == r.get.map(x => (x, x))
Source:
registers.scala
def gets[B](f: A => B): Parsley[B]

This combinator injects the value stored in this register into a parser after applying a function to it.

This combinator injects the value stored in this register into a parser after applying a function to it.

Allows for the value stored in this register to be purely injected into the parsing context but the function f is applied first. No input is consumed in this process, and it cannot fail.

Attributes

B

the desired result type.

f

the function used to transform the value in this register.

Returns:

the value stored in this register applied to f.

Since:

3.2.0

Source:
registers.scala
def gets[B](pf: Parsley[A => B]): Parsley[B]

This combinator injects the value stored in this register into a parser after applying a function obtained from a parser to it.

This combinator injects the value stored in this register into a parser after applying a function obtained from a parser to it.

First, pf is parsed, producing the function f on success. Then, the value stored in this register x is applied to the function f. The combinator returns f(x). Only pf is allowed to consume input. If pf fails, the combinator fails, otherwise it will succeed.

Attributes

B

the desired result type.

pf

the parser that produces the function used to transform the value in this register.

Returns:

the value stored in this register applied to a function generated from pf.

Since:

3.2.0

Source:
registers.scala

Setters

These combinators directly update the value contained within a register. This new value can be provided directly or sourced from a parser.

def put(x: A): Parsley[Unit]

This combinator stores a new value into this register.

This combinator stores a new value into this register.

Without any other effect, the value x will be placed into this register.

Attributes

x

the value to place in the register.

Since:

3.2.0

Example:

Put-Get Law:

r.put(x) *> r.get == r.put(x) #> x

Put-Put Law:

r.put(x) *> r.put(y) == r.put(y)
Source:
registers.scala
def put(p: Parsley[A]): Parsley[Unit]

This combinator stores a new value into this register.

This combinator stores a new value into this register.

First, parse p to obtain its result x. Then store x into this register without any further effect. If p fails this combinator fails.

Attributes

p

the parser that produces the value to store in the register.

Since:

3.2.0

Example:

Get-Put Law:

r.put(r.get) == unit

Put-Put Law:

// only when `q` does not inspect the value of `r`!
r.put(p) *> r.put(q) == p *> r.put(q)
Source:
registers.scala
def puts[B](p: Parsley[B], f: B => A): Parsley[Unit]

This combinator stores a new value into this register.

This combinator stores a new value into this register.

First, parse p to obtain its result x. Then store f(x) into this register without any further effect. If p fails this combinator fails.

Equivalent to

this.put(p.map(f))

Attributes

f

a function which adapts the result of p so that it can fit into this register.

p

the parser that produces the value to store in the register.

Since:

3.0.0

Source:
registers.scala

Modification

These combinators modify the value stored within a register by using a function. The function used can be provided directly or sourced from a parser.

def modify(f: A => A): Parsley[Unit]

This combinator modifies the value stored in this register with a function.

This combinator modifies the value stored in this register with a function.

Without any other effect, get the value stored in this register, x, and put back f(x).

Equivalent to

this.put(this.gets(f))

Attributes

f

the function used to modify this register's value.

Since:

3.2.0

Source:
registers.scala
def modify(pf: Parsley[A => A]): Parsley[Unit]

This combinator modifies the value stored in this register with a function.

This combinator modifies the value stored in this register with a function.

First, parse pf to obtain its result f. Then get the value stored in this register, x, and put back f(x). If p fails this combinator fails.

Equivalent to

this.put(this.gets(pf))

Attributes

pf

the parser that produces the function used to transform the value in this register.

Since:

3.2.0

Source:
registers.scala

Local Modification

These combinators allow for some form of local stateful modification. This means that any changes to the register may be reverted after the execution of the parser: this may be on the parsers success, but it could also involve the parsers failure.

def local[B](x: A)(p: Parsley[B]): Parsley[B]

This combinator changed the value stored in this register for the duration of a given parser, resetting it afterwards.

This combinator changed the value stored in this register for the duration of a given parser, resetting it afterwards.

First get the current value in this register xold, then place x into this register without any further effect. Then, parse p, producing result y on success. Finally, put xold back into this register and return y. If p fails, the whole combinator fails and the state is not restored.

Attributes

p

the parser to execute with the adjusted state.

x

the value to place into this register.

Returns:

the parser that performs p with the modified state x.

Since:

3.2.0

Example:

Put-Put Law:

r.put(x) *> r.local(y)(p) == r.put(y) *> p <* r.put(x)
Source:
registers.scala
def local[B](p: Parsley[A])(q: => Parsley[B]): Parsley[B]

This combinator changed the value stored in this register for the duration of a given parser, resetting it afterwards.

This combinator changed the value stored in this register for the duration of a given parser, resetting it afterwards.

First get the current value in this register xold, then parse p to get the result x, placing it into this register without any further effect. Then, parse q, producing result y on success. Finally, put xold back into this register and return y. If p or q fail, the whole combinator fails and the state is not restored.

Attributes

p

the parser whose return value is placed in this register.

q

the parser to execute with the adjusted state.

Returns:

the parser that performs q with the modified state.

Since:

3.2.0

Source:
registers.scala
def local[B](f: A => A)(p: Parsley[B]): Parsley[B]

This combinator changed the value stored in this register for the duration of a given parser, resetting it afterwards.

This combinator changed the value stored in this register for the duration of a given parser, resetting it afterwards.

First get the current value in this register xold, then place f(xold) into this register without any further effect. Then, parse p, producing result y on success. Finally, put xold back into this register and return y. If p fails, the whole combinator fails and the state is not restored.

Attributes

f

the function used to modify the value in this register.

p

the parser to execute with the adjusted state.

Returns:

the parser that performs p with the modified state.

Since:

3.2.0

Example:

Put-Put Law and Put-Get Law:

r.put(x) *> r.local(f)(p) == r.put(f(x)) *> p <* r.put(x)
Source:
registers.scala
def rollback[B](p: Parsley[B]): Parsley[B]

This combinator rolls-back any changes to this register made by a given parser if it fails.

This combinator rolls-back any changes to this register made by a given parser if it fails.

First get the current value in this register xold. Then parse p, if it succeeds, producing y, then y is returned and this register retains its value post-p. Otherwise, if p failed without consuming input, xold is placed back into this register and this combinator fails.

This can be used in conjunction with local to make an almost unconditional state restore:

// `r`'s state is always rolled back after `p` unless it fails having consumed input.
r.rollback(r.local(x)(p))

Attributes

p

the parser to perform.

Returns:

the result of the parser p, if any.

Since:

3.2.0

Source:
registers.scala