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.
- Source
- registers.scala
- Since
2.2.0
- Grouped
- Alphabetic
- By Inheritance
- registers
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Type Members
- class Reg[A] extends AnyRef
This class is used to index registers within the mutable state.
This class is used to index registers within the mutable state.
- 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.
- implicit final class RegisterMaker[A] extends AnyRef
This class, when in scope, enables a method to create and fill a register with a given value.
- implicit final class RegisterMethods[P, A] extends AnyRef
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.
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 clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native()
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable])
- 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 usinginit
,cond
,step
andbody
as parsers which control the loop itself. First, a registerr
is created and initialised withinit
. Thencond
is parsed, producing the functionpred
. Ifr.gets(pred)
returns true, thenbody
is parsed, thenr
is modified with the result of parsingstep
. This repeats untilr.gets(pred)
returns false. This is useful for performing certain context sensitive tasks.- init
the initial value of the induction variable.
- cond
the condition by which the loop terminates.
- step
the change in induction variable on each iteration.
- body
the body of the loop performed each iteration.
- returns
a parser that initialises some state with
init
and then parses body untilcond
is true, modifying the state each iteration withstep
.
the classic context sensitive grammar of
anbncn
can be matched usingforP
: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'}
- See also
forYieldP
for a version that returns the results of eachbody
parse.
Example: - 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 usinginit
,cond
,step
andbody
as parsers which control the loop itself. First, a registerr
is created and initialised withinit
. Thencond
is parsed, producing the functionpred
. Ifr.gets(pred)
returns true, thenbody
is parsed, thenr
is modified with the result of parsingstep
. This repeats untilr.gets(pred)
returns false. This is useful for performing certain context sensitive tasks.- init
the initial value of the induction variable.
- cond
the condition by which the loop terminates.
- step
the change in induction variable on each iteration.
- body
the body of the loop performed each iteration, which has access to the current value of the state.
- returns
a parser that initialises some state with
init
and then parses body untilcond
is true, modifying the state each iteration withstep
.
the classic context sensitive grammar of
anbncn
can be matched usingforP_
: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'}
- See also
forYieldP_
for a version that returns the results of eachbody
parse.
Example: - 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 usinginit
,cond
,step
andbody
as parsers which control the loop itself. First, a registerr
is created and initialised withinit
. Thencond
is parsed, producing the functionpred
. Ifr.gets(pred)
returns true, thenbody
is parsed, thenr
is modified with the result of parsingstep
. This repeats untilr.gets(pred)
returns false. This is useful for performing certain context sensitive tasks. UnlikeforP
the results of the body invokations are returned in a list.- init
the initial value of the induction variable.
- cond
the condition by which the loop terminates.
- step
the change in induction variable on each iteration.
- body
the body of the loop performed each iteration.
- returns
a parser that initialises some state with
init
and then parses body untilcond
is true, modifying the state each iteration withstep
. The results of the iterations are returned in a list.
the classic context sensitive grammar of
anbncn
can be matched usingforP
: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.- See also
forP
for a version that ignores the results.
Example: - 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 usinginit
,cond
,step
andbody
as parsers which control the loop itself. First, a registerr
is created and initialised withinit
. Thencond
is parsed, producing the functionpred
. Ifr.gets(pred)
returns true, thenbody
is parsed, thenr
is modified with the result of parsingstep
. This repeats untilr.gets(pred)
returns false. This is useful for performing certain context sensitive tasks. UnlikeforP_
the results of the body invokations are returned in a list.- init
the initial value of the induction variable.
- cond
the condition by which the loop terminates.
- step
the change in induction variable on each iteration.
- body
the body of the loop performed each iteration, which has access to the current value of the state.
- returns
a parser that initialises some state with
init
and then parses body untilcond
is true, modifying the state each iteration withstep
.
the classic context sensitive grammar of
anbncn
can be matched usingforP_
: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.- See also
forP_
for a version that ignores the results of the body.
Example: - final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- 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()
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toString(): String
- Definition Classes
- AnyRef → Any
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()
- object Reg
This object allows for the construction of a register via its
make
function.
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.
Register-Based Combinators
Some combinators are made much more efficient in the presence of registers and they can be found here.
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.