License | BSD-3-Clause |
---|---|
Maintainer | Jamie Willis |
Stability | experimental |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
Extensions |
|
This module contains all the relevant operations for the evaluation of a machine. These are used by Parsley.Internal.Backend.Machine.Eval to provide the various instruction interpretations.
Since: 1.0.0.0
Synopsis
- dup :: (?flags :: Flags) => Defunc x -> (Defunc x -> Code r) -> Code r
- returnST :: forall s a. a -> ST s a
- sat :: (?flags :: Flags) => (Defunc Char -> Defunc Bool) -> Code Char -> (Defunc Char -> Code b) -> Code b -> Code b
- emitLengthCheck :: (?ops :: InputOps (StaRep o)) => Int -> Int -> Maybe (Code Char -> Code a -> Code a) -> (Offset o -> [(Code Char, Offset o)] -> Code a) -> Code a -> Offset o -> (Offset o -> StaRep o) -> Code a
- fetch :: (?ops :: InputOps (StaRep o)) => Offset o -> (Code Char -> Offset o -> Code b) -> Code b
- newΣ :: (?flags :: Flags) => ΣVar x -> Access -> Defunc x -> (Ctx s o a -> Code (ST s r)) -> Ctx s o a -> Code (ST s r)
- writeΣ :: (?flags :: Flags) => ΣVar x -> Access -> Defunc x -> (Ctx s o a -> Code (ST s r)) -> Ctx s o a -> Code (ST s r)
- readΣ :: (?flags :: Flags) => ΣVar x -> Access -> (Defunc x -> Ctx s o a -> Code (ST s r)) -> Ctx s o a -> Code (ST s r)
- fatal :: AugmentedStaHandler s o a
- raise :: (DynOps o, ?flags :: Flags) => Γ s o xs (Succ n) r a -> Code (ST s (Maybe a))
- buildHandler :: DynOps o => Γ s o xs n r a -> (Γ s o (o ': xs) n r a -> Code (ST s (Maybe a))) -> Word -> StaHandlerBuilder s o a
- buildYesHandler :: Γ s o xs n r a -> (Γ s o xs n r a -> Code (ST s (Maybe a))) -> StaYesHandler s o a
- buildIterYesHandler :: DynOps o => Γ s o xs n r a -> (Γ s o xs n r a -> Code (ST s (Maybe a))) -> Word -> StaHandler s o a
- bindAlwaysHandler :: forall s o xs n r a b. HandlerOps o => Γ s o xs n r a -> Bool -> StaHandlerBuilder s o a -> (Γ s o xs (Succ n) r a -> Code b) -> Code b
- bindSameHandler :: forall s o xs n r a b. (HandlerOps o, PositionOps (StaRep o), DynOps o) => Γ s o xs n r a -> Bool -> StaYesHandler s o a -> Bool -> StaHandlerBuilder s o a -> (Γ s o xs (Succ n) r a -> Code b) -> Code b
- halt :: StaCont s o a a
- noreturn :: StaCont s o a Void
- resume :: (DynOps o, ?flags :: Flags) => StaCont s o a x -> Γ s o (x ': xs) n r a -> Code (ST s (Maybe a))
- callWithContinuation :: (MarshalOps o, DynOps o) => StaSubroutine s o a x -> StaCont s o a x -> Input o -> Vec (Succ n) (AugmentedStaHandler s o a) -> Code (ST s (Maybe a))
- callCC :: forall s o xs n r a x. (MarshalOps o, DynOps o, ?flags :: Flags) => Word -> StaSubroutine s o a x -> (Γ s o (x ': xs) (Succ n) r a -> Code (ST s (Maybe a))) -> Γ s o xs (Succ n) r a -> Code (ST s (Maybe a))
- suspend :: (?flags :: Flags) => (Γ s o (x ': xs) n r a -> Code (ST s (Maybe a))) -> Γ s o xs n r a -> (Input# o -> Input o) -> StaCont s o a x
- setupJoinPoint :: forall s o xs n r a x. (JoinBuilder o, DynOps o, ?flags :: Flags) => ΦVar x -> Machine s o (x ': xs) n r a -> Machine s o xs n r a -> MachineMonad s o xs n r a
- bindIterAlways :: forall s o a. (RecBuilder o, DynOps o) => Ctx s o a -> MVar Void -> Machine s o '[] One Void a -> Bool -> StaHandlerBuilder s o a -> Input o -> Word -> Code (ST s (Maybe a))
- bindIterSame :: forall s o a. (RecBuilder o, HandlerOps o, PositionOps (StaRep o), DynOps o) => Ctx s o a -> MVar Void -> Machine s o '[] One Void a -> Bool -> StaHandler s o a -> Bool -> StaHandlerBuilder s o a -> Input o -> Word -> Code (ST s (Maybe a))
- buildRec :: forall rs s o a r. (RecBuilder o, DynOps o) => MVar r -> Regs rs -> Ctx s o a -> Machine s o '[] One r a -> Metadata -> DynFunc rs s o a r
- dynHandler :: forall s o a. MarshalOps o => AugmentedStaHandler s o a -> InputCharacteristic -> DynHandler s o a
- dynCont :: forall s o a x. MarshalOps o => StaCont s o a x -> DynCont s o a x
- logHandler :: (?ops :: InputOps (StaRep o), LogHandler o, ?flags :: Flags) => String -> Ctx s o a -> Γ s o xs (Succ n) ks a -> Word -> StaHandlerBuilder s o a
- preludeString :: forall s o xs n r a. (?ops :: InputOps (StaRep o), LogHandler o) => String -> Char -> Γ s o xs n r a -> Ctx s o a -> String -> Code String
- type Ops o = (HandlerOps o, JoinBuilder o, RecBuilder o, PositionOps (StaRep o), MarshalOps o, LogOps (StaRep o), DynOps o)
- type LogHandler o = (PositionOps (StaRep o), LogOps (StaRep o), DynOps o)
- type StaHandlerBuilder s o a = Input o -> StaHandler s o a
- type StaYesHandler s o a = Input o -> Code (ST s (Maybe a))
- class HandlerOps o
- class JoinBuilder o
- class RecBuilder o
- class PositionOps rep
- class MarshalOps o
- class LogOps rep
Core Machine Operations
dup :: (?flags :: Flags) => Defunc x -> (Defunc x -> Code r) -> Code r Source #
Creates a let-binding that allows the same value to be used multiple times without re-computation.
Since: 1.0.0.0
returnST :: forall s a. a -> ST s a Source #
This is just plain ol' return
. It is given a concrete
type here so that "Ambiuous Type Error" is avoided in the
generated code.
Since: 1.0.0.0
Abstracted Input Operations
:: (?flags :: Flags) | |
=> (Defunc Char -> Defunc Bool) | Predicate to test the character with. |
-> Code Char | The character to test against. |
-> (Defunc Char -> Code b) | Code to execute on success. |
-> Code b | Code to execute on failure. |
-> Code b |
Given a predicate, a continuation that accepts an updated state Γ
,
code to execute on failure, and a state γ
, tries to read a character
from the input within γ
, executing the failure code if it does not
exist or does not match.
Since: 2.1.0.0
:: (?ops :: InputOps (StaRep o)) | |
=> Int | The number of required characters \(n\). |
-> Int | The number of characters to prefetch \(m\). |
-> Maybe (Code Char -> Code a -> Code a) | An optional check for the head character. |
-> (Offset o -> [(Code Char, Offset o)] -> Code a) | The good continuation if \(n\) characters are available. |
-> Code a | The bad continuation if the characters are unavailable. |
-> Offset o | The input to test on. |
-> (Offset o -> StaRep o) | |
-> Code a |
Emits a length check for a number of characters \(n\) in the most efficient
way it can. It takes two continuations a good
and a bad
: the good
is used
when the \(n\) characters are available and the bad
when they are not.
Since: 2.3.0.0
fetch :: (?ops :: InputOps (StaRep o)) => Offset o -> (Code Char -> Offset o -> Code b) -> Code b Source #
Consumes the next character and adjusts the offset to match.
Since: 1.8.0.0
Register Operations
newΣ :: (?flags :: Flags) => ΣVar x -> Access -> Defunc x -> (Ctx s o a -> Code (ST s r)) -> Ctx s o a -> Code (ST s r) Source #
writeΣ :: (?flags :: Flags) => ΣVar x -> Access -> Defunc x -> (Ctx s o a -> Code (ST s r)) -> Ctx s o a -> Code (ST s r) Source #
Depending on the access type, either generates the code for a write to a register (and caching that result) or updates the cache with the register's new value.
Since: 1.0.0.0
readΣ :: (?flags :: Flags) => ΣVar x -> Access -> (Defunc x -> Ctx s o a -> Code (ST s r)) -> Ctx s o a -> Code (ST s r) Source #
Depending on the access type, either generates a read from a register or fetches the value from the cache and feeds it to a continuation.
Since: 1.0.0.0
Handler Operations
Basic handlers and operations
fatal :: AugmentedStaHandler s o a Source #
This is the root-most handler, when it is executed the parser fails immediately
by returning Nothing
.
Since: 1.2.0.0
raise :: (DynOps o, ?flags :: Flags) => Γ s o xs (Succ n) r a -> Code (ST s (Maybe a)) Source #
Fails by evaluating the next handler with the current input. Makes
use of staHandlerEval
to make use of static information available
about the state of the input (since 1.4.0.0).
Since: 1.0.0.0
Handler preparation
:: DynOps o | |
=> Γ s o xs n r a | State to execute the handler with. |
-> (Γ s o (o ': xs) n r a -> Code (ST s (Maybe a))) | Partial parser accepting the modified state. |
-> Word | The unique identifier for the offset on failure. |
-> StaHandlerBuilder s o a |
Converts a partially evaluated parser into a handler: this is done by
completing the evaluation in the context of a future offset, and taking
a captured offset and pushing it to the stack. Returns a StaHandlerBuilder
,
which takes the captured offset as the first argument.
Since: 1.2.0.0
buildYesHandler :: Γ s o xs n r a -> (Γ s o xs n r a -> Code (ST s (Maybe a))) -> StaYesHandler s o a Source #
Converts a partially evaluated parser into a "yes" handler: this means that
the handler always knows that the inputs are equal, so does not require
both a captured and a current offset. Otherwise, is similar to buildHandler
.
Since: 2.1.0.0
buildIterYesHandler :: DynOps o => Γ s o xs n r a -> (Γ s o xs n r a -> Code (ST s (Maybe a))) -> Word -> StaHandler s o a Source #
Converts a partially evaluated parser into a "yes" handler: this means that
the handler always knows that the inputs are equal, so does not require
both a captured and a current offset. Otherwise, is similar to buildHandler
.
Since: 2.1.0.0
Handler binding
:: forall s o xs n r a b. HandlerOps o | |
=> Γ s o xs n r a | The state from which to capture the offset. |
-> Bool | Whether or not a binding is required |
-> StaHandlerBuilder s o a | The handler waiting to receive the captured offset and be bound. |
-> (Γ s o xs (Succ n) r a -> Code b) | The parser to receive the binding. |
-> Code b |
Wraps around bindHandler#
to create a binding for "always" handlers, which always
perform the same action regardless of if the captured and current offsets match or
not.
Since: 1.4.0.0
:: forall s o xs n r a b. (HandlerOps o, PositionOps (StaRep o), DynOps o) | |
=> Γ s o xs n r a | The state from which to capture the offset. |
-> Bool | Is a binding required for the matching handler? |
-> StaYesHandler s o a | The handler that handles matching input. |
-> Bool | Is a binding required for the mismatched handler? |
-> StaHandlerBuilder s o a | The handler that handles mismatched input. |
-> (Γ s o xs (Succ n) r a -> Code b) | The parser to receive the composite handler. |
-> Code b |
Wraps around bindHandler#
to create three bindings for a handler that acts
differently depending on whether inputs match or not. The three bindings are
for the case where they are the same, the case where they differ, and the case
where they are unknown (which is defined in terms of the previous two).
Since: 2.1.0.0
Continuation Operations
Basic continuations and operations
halt :: StaCont s o a a Source #
The root-most return continuation, this is used when the top-level
parser returns: it returns the result with Just
and terminates
the entire parser.
Since: 1.2.0.0
noreturn :: StaCont s o a Void Source #
This continuation is used for binding that never return, which is
enforced by the Void
in the type. This signifies that a binding
may only exit on failure, which is the case with iterating parsers.
Since: 1.2.0.0
resume :: (DynOps o, ?flags :: Flags) => StaCont s o a x -> Γ s o (x ': xs) n r a -> Code (ST s (Maybe a)) Source #
Executes a given continuation (which may be a return continuation or a
join point) taking the required components from the state Γ
.
Since: 1.2.0.0
:: (MarshalOps o, DynOps o) | |
=> StaSubroutine s o a x | The subroutine |
-> StaCont s o a x | The return continuation for the subroutine. |
-> Input o | The input to feed to |
-> Vec (Succ n) (AugmentedStaHandler s o a) | The stack from which to obtain the handler to pass to |
-> Code (ST s (Maybe a)) |
A form of callCC
, this calls a subroutine with a given return continuation
passed to it. This may be the current continuation, but also may just be a
previous return continuation in the case of a tail call.
Since: 1.8.0.0
:: forall s o xs n r a x. (MarshalOps o, DynOps o, ?flags :: Flags) | |
=> Word | |
-> StaSubroutine s o a x | The subroutine |
-> (Γ s o (x ': xs) (Succ n) r a -> Code (ST s (Maybe a))) | The return continuation to generate |
-> Γ s o xs (Succ n) r a | |
-> Code (ST s (Maybe a)) |
Combines suspend
and callWithContinuation
, simultaneously performing
an optimisation on the offset if the subroutine has known input characteristics.
Since: 1.5.0.0
Continuation preparation
:: (?flags :: Flags) | |
=> (Γ s o (x ': xs) n r a -> Code (ST s (Maybe a))) | The partial parser to turn into a return continuation. |
-> Γ s o xs n r a | The state to execute the continuation with. |
-> (Input# o -> Input o) | Function used to generate the offset |
-> StaCont s o a x |
Converts a partial parser into a return continuation in a manner similar
to buildHandler
.
Since: 1.8.0.0
Join Point Operations
:: forall s o xs n r a x. (JoinBuilder o, DynOps o, ?flags :: Flags) | |
=> ΦVar x | The name of the binding. |
-> Machine s o (x ': xs) n r a | The definition of the binding. |
-> Machine s o xs n r a | The scope within which the binding is valid. |
-> MachineMonad s o xs n r a |
Wraps around setupJoinPoint#
to make a join point and register it
into the Ctx
.
Since: 1.4.0.0
Iteration Operations
:: forall s o a. (RecBuilder o, DynOps o) | |
=> Ctx s o a | The context to keep the binding |
-> MVar Void | The name of the binding. |
-> Machine s o '[] One Void a | The body of the loop. |
-> Bool | Does loop exit require a binding? |
-> StaHandlerBuilder s o a | What to do after the loop exits (by failing) |
-> Input o | The initial offset to provide to the loop |
-> Word | The unique name for captured offset and iteration offset |
-> Code (ST s (Maybe a)) |
Uses bindIterHandler#
and bindIter#
to create an iterated parser
from its loop body and return continuation. The exit of a loop is done
using failure, and this failure does not discriminate whether or not
the loop consumed input in its final iteration.
Since: 1.8.0.0
:: forall s o a. (RecBuilder o, HandlerOps o, PositionOps (StaRep o), DynOps o) | |
=> Ctx s o a | The context to store the binding in. |
-> MVar Void | The name of the binding. |
-> Machine s o '[] One Void a | The loop body. |
-> Bool | Is a binding required for the matching handler? |
-> StaHandler s o a | The handler when input is the same. |
-> Bool | Is a binding required for the differing handler? |
-> StaHandlerBuilder s o a | The handler when input differs. |
-> Input o | The initial offset of the loop. |
-> Word | The unique name of the captured offsets and the iteration offset. |
-> Code (ST s (Maybe a)) |
Similar to bindIterAlways
, but builds a handler that performs in
the same way as bindSameHandler
.
Since: 2.1.0.0
Recursion Operations
:: forall rs s o a r. (RecBuilder o, DynOps o) | |
=> MVar r | The name of the binding. |
-> Regs rs | The registered required by the binding. |
-> Ctx s o a | The context to re-insert the register-less binding |
-> Machine s o '[] One r a | The body of the binding. |
-> Metadata | The metadata associated with the binding |
-> DynFunc rs s o a r |
Wraps around bindRec#
to produce a recursive parser binding. This function
also provides all the free-registers which are closed over by the binding.
This eliminates recursive calls from having to pass all of the same registers
each time round.
Since: 1.5.0.0
Marshalling Operations
dynHandler :: forall s o a. MarshalOps o => AugmentedStaHandler s o a -> InputCharacteristic -> DynHandler s o a Source #
Wraps around dynHandler#
, but ensures that if the StaHandler
originated from a DynHandler
itself, that no work is performed.
Takes in an InputCharacteristic
, which is used to refine the
handler given knowledge about how it might be used.
Since: 1.5.0.0
Log Operations
logHandler :: (?ops :: InputOps (StaRep o), LogHandler o, ?flags :: Flags) => String -> Ctx s o a -> Γ s o xs (Succ n) ks a -> Word -> StaHandlerBuilder s o a Source #
The specialised handler for the debug
combinator. It will fail again after
having printed the debug information.
Since: 1.2.0.0
:: forall s o xs n r a. (?ops :: InputOps (StaRep o), LogHandler o) | |
=> String | The name as per the debug combinator |
-> Char | Either |
-> Γ s o xs n r a | |
-> Ctx s o a | |
-> String | String that represents the current status |
-> Code String |
Used for the debug instructions and handler, produces the debugging information string.
Since: 1.2.0.0
Convenience Types
type Ops o = (HandlerOps o, JoinBuilder o, RecBuilder o, PositionOps (StaRep o), MarshalOps o, LogOps (StaRep o), DynOps o) Source #
A convience bundle of all of the type class constraints.
Since: 1.0.0.0
type LogHandler o = (PositionOps (StaRep o), LogOps (StaRep o), DynOps o) Source #
The constraints needed to build a logHandler
.
Since: 1.0.0.0
type StaHandlerBuilder s o a = Input o -> StaHandler s o a Source #
A StaHandler
that has not yet captured its offset.
Since: 1.2.0.0
type StaYesHandler s o a = Input o -> Code (ST s (Maybe a)) Source #
A "yes-handler" that has not yet captured its offset
Since: 2.1.0.0
Re-exports from Parsley.Internal.Backend.Machine.InputOps
class HandlerOps o Source #
Used to generate a binding for a handler.
Since: 1.4.0.0
Instances
class JoinBuilder o Source #
Generates join-point bindings.
Since: 1.4.0.0
Instances
JoinBuilder ByteString Source # | |
Defined in Parsley.Internal.Backend.Machine.BindingOps setupJoinPoint# :: StaCont# s ByteString a x -> (DynCont s ByteString a x -> Code b) -> Code b Source # | |
JoinBuilder ByteString Source # | |
Defined in Parsley.Internal.Backend.Machine.BindingOps setupJoinPoint# :: StaCont# s ByteString a x -> (DynCont s ByteString a x -> Code b) -> Code b Source # | |
JoinBuilder CharList Source # | |
Defined in Parsley.Internal.Backend.Machine.BindingOps | |
JoinBuilder Stream Source # | |
Defined in Parsley.Internal.Backend.Machine.BindingOps | |
JoinBuilder Text16 Source # | |
Defined in Parsley.Internal.Backend.Machine.BindingOps | |
JoinBuilder Text Source # | |
Defined in Parsley.Internal.Backend.Machine.BindingOps | |
JoinBuilder String Source # | |
Defined in Parsley.Internal.Backend.Machine.BindingOps | |
JoinBuilder (UArray Int Char) Source # | |
class RecBuilder o Source #
Various functions for creating bindings for recursive parsers.
Since: 1.4.0.0
Instances
class PositionOps rep Source #
Defines operations for manipulating offsets for regular use. These are not tied to the original captured input but instead to the representation of its offset.
Since: 1.0.0.0
Instances
PositionOps PartialStaOffset Source # | |
Defined in Parsley.Internal.Backend.Machine.InputOps same :: PartialStaOffset -> PartialStaOffset -> Code Bool Source # | |
PositionOps PartialStaText Source # | |
Defined in Parsley.Internal.Backend.Machine.InputOps same :: PartialStaText -> PartialStaText -> Code Bool Source # | |
PositionOps (PartialStaOffWith ts) Source # | |
Defined in Parsley.Internal.Backend.Machine.InputOps same :: PartialStaOffWith ts -> PartialStaOffWith ts -> Code Bool Source # | |
PositionOps (Code UnpackedLazyByteString) Source # | |
Defined in Parsley.Internal.Backend.Machine.InputOps |
class MarshalOps o Source #
These operations are responsible for materialising the static handlers and continuations into dynamic forms that can be passed into other bindings at runtime.
Since: 1.4.0.0
Instances
Defines operation used for debugging operations.
Since: 1.0.0.0