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.
This is the documentation for Parsley.
Package structure
The parsley package contains the
Parsley
class, as well as theResult
,Success
, andFailure
types. In addition to these, it also contains the following packages and "modules" (a module is defined as being an object which mocks a package):parsley.Parsley
contains the bulk of the core "function-style" combinators.parsley.combinator
contains many helpful combinators that simplify some common parser patterns.parsley.character
contains the combinators needed to read characters and strings, as well as combinators to match specific sub-sets of characters.parsley.debug
contains debugging combinators, helpful for identifying faults in parsers.parsley.extension
contains syntactic sugar combinators exposed as implicit classes.parsley.io
contains extension methods to run parsers with input sourced from IO sources.parsley.expr
contains the following sub modules:parsley.expr.chain
contains combinators used in expression parsingparsley.expr.precedence
is a builder for expression parsers built on a precedence table.parsley.expr.infix
contains combinators used in expression parsing, but with more permissive types than their equivalents inchain
.parsley.expr.mixed
contains combinators that can be used for expression parsing, but where different fixities may be mixed on the same level: this is rare in practice.parsley.implicits
contains several implicits to add syntactic sugar to the combinators. These are sub-categorised into the following sub modules:parsley.implicits.character
contains implicits to allow you to use character and string literals as parsers.parsley.implicits.combinator
contains implicits related to combinators, such as the ability to make any parser into aParsley[Unit]
automatically.parsley.implicits.lift
enables postfix application of the lift combinator onto a function (or value).parsley.implicits.zipped
enables boths a reversed form of lift where the function appears on the right and is applied on a tuple (useful when type inference has failed) as well as a.zipped
method for building tuples out of several combinators.parsley.errors
contains modules to deal with error messages, their refinement and generation.parsley.errors.combinator
provides combinators that can be used to either produce more detailed errors as well as refine existing errors.parsley.errors.tokenextractors
provides mixins for common token extraction strategies during error message generation: these can be used to avoid implementingunexpectedToken
in theErrorBuilder
.parsley.lift
contains functions which lift functions that work on regular types to those which now combine the results of parsers returning those same types. these are ubiquitous.parsley.ap
contains functions which allow for the application of a parser returning a function to several parsers returning each of the argument types.parsley.registers
contains combinators that interact with the context-sensitive functionality in the form of registers.parsley.token
contains theLexer
class that provides a host of helpful lexing combinators when provided with the description of a language.parsley.position
contains parsers for extracting position information.parsley.genericbridges
contains some basic implementations of the Parser Bridge pattern (see Design Patterns for Parser Combinators in Scala, or the parsley wiki): these can be used before more specialised generic bridge traits can be constructed.