| Safe Haskell | None |
|---|---|
| Language | GHC2021 |
GreedyParser
Description
This module contains a simple greedy parser for path grammars.
The grammar is provided by an evaluator (Eval).
In addition, the parser takes a policy function
that picks a reduction option in each step.
Synopsis
- data GreedyState tr tr' slc op
- showFrozen :: (Show slc, Show tr') => Path tr' slc -> String
- showOpen :: (Show slc, Show tr) => Path tr slc -> String
- showOps :: Show o => [o] -> String
- gsOps :: GreedyState tr tr' slc op -> [op]
- data SingleParent slc tr = SingleParent !(StartStop slc) !tr !(StartStop slc)
- data ActionSingle slc tr s f = ActionSingle (SingleParent slc tr) (LeftmostSingle s f)
- data DoubleParent slc tr = DoubleParent !(StartStop slc) !tr !slc !tr !(StartStop slc)
- data ActionDouble slc tr s f h = ActionDouble (DoubleParent slc tr) (LeftmostDouble s f h)
- type Action slc tr s f h = Either (ActionSingle slc tr s f) (ActionDouble slc tr s f h)
- actionGoesLeft :: Action slc tr s f h -> Maybe Bool
- opGoesLeft :: Leftmost s f h -> Maybe Bool
- parseGreedy :: forall (m :: Type -> Type) tr tr' slc slc' s f h. (Monad m, MonadIO m, Show tr', Show slc, Show tr, Show s, Show f, Show h) => Eval tr tr' slc slc' h (Leftmost s f h) -> ([Action slc tr s f h] -> ExceptT String m (Action slc tr s f h)) -> Path slc' tr' -> ExceptT String m (Analysis s f h tr slc)
- initParseState :: Eval tr tr' slc slc' h v -> Path slc' tr' -> GreedyState tr tr' slc op
- parseStep :: forall (m :: Type -> Type) tr tr' slc slc' s f h. Monad m => Eval tr tr' slc slc' h (Leftmost s f h) -> ([Action slc tr s f h] -> ExceptT String m (Action slc tr s f h)) -> GreedyState tr tr' slc (Leftmost s f h) -> ExceptT String m (Either (GreedyState tr tr' slc (Leftmost s f h)) (tr, [Leftmost s f h]))
- getActions :: forall {k} (m :: k) tr tr' slc slc' s f h. Eval tr tr' slc slc' h (Leftmost s f h) -> GreedyState tr tr' slc (Leftmost s f h) -> [Action slc tr s f h]
- lastWasLeft :: [Leftmost s f h] -> Bool
- collectAllThawLeft :: Eval tr tr' slc slc' h (Leftmost s f h) -> Path (Maybe tr') slc -> slc -> tr -> StartStop slc -> [ActionDouble slc tr s f h]
- collectThawSingle :: Eval tr tr' slc slc' h (Leftmost s f h) -> StartStop slc -> Maybe tr' -> StartStop slc -> [ActionSingle slc tr s f]
- collectThawLeft :: Eval tr tr' slc slc' h (Leftmost s f h) -> StartStop slc -> Maybe tr' -> slc -> tr -> StartStop slc -> [ActionDouble slc tr s f h]
- collectUnsplitSingle :: Eval tr tr' slc slc' h (Leftmost s f h) -> StartStop slc -> tr -> slc -> tr -> StartStop slc -> [ActionSingle slc tr s f]
- collectUnsplitLeft :: Eval tr tr' slc slc' h (Leftmost s f h) -> StartStop slc -> tr -> slc -> tr -> slc -> tr -> StartStop slc -> [ActionDouble slc tr s f h]
- collectUnsplitRight :: Eval tr tr' slc slc' h (Leftmost s f h) -> StartStop slc -> tr -> slc -> tr -> slc -> tr -> StartStop slc -> Bool -> [ActionDouble slc tr s f h]
- collectUnspreads :: Eval tr tr' slc slc' h (Leftmost s f h) -> StartStop slc -> tr -> slc -> tr -> slc -> tr -> StartStop slc -> [ActionDouble slc tr s f h]
- collectDoubles :: Eval tr tr' slc slc' h (Leftmost s f h) -> StartStop slc -> tr -> slc -> tr -> slc -> Path tr slc -> Bool -> [ActionDouble slc tr s f h]
- pickRandom :: forall g (m :: Type -> Type) slc. StatefulGen g m => g -> [slc] -> ExceptT String m slc
- applyAction :: forall {k1} {k2} (m :: k1) tr tr' slc (slc' :: k2) s f h. GreedyState tr tr' slc (Leftmost s f h) -> Action slc tr s f h -> Either String (Either (GreedyState tr tr' slc (Leftmost s f h)) (tr, [Leftmost s f h]))
- parseRandom :: (Show tr', Show slc, Show tr, Show s, Show f, Show h) => Eval tr tr' slc slc' h (Leftmost s f h) -> Path slc' tr' -> ExceptT String IO (Analysis s f h tr slc)
- parseRandom' :: (Show tr', Show slc, Show tr, Show s, Show f, Show h, StatefulGen g IO) => g -> Eval tr tr' slc slc' h (Leftmost s f h) -> Path slc' tr' -> ExceptT String IO (Analysis s f h tr slc)
Parsing State
data GreedyState tr tr' slc op Source #
The state of the greedy parse between steps. Generally, the current reduction consists of frozen transitions between the ⋊ and the current location and open transitions between the current location and ⋉.
⋊==[1]==[2]==[3]——[4]——[5]——⋉
└ frozen ┘ | └ open ┘
midSlice (current position)
frozen: ==[2]==[1]==
midSlice: [3]
open: ——[4]——[5]——This is the GSSemiOpen case:
The slice at the current pointer ([3])
is represented as an individual slice (midSlice).
The frozen part is represented by a Path of frozen transitions (tr') and slices (slc).
in reverse direction, i.e. from midslice back to ⋊ (excluding ⋊).
The open part is a Path of open transitions (tr) and slices (slc)
in forward direction from midSlice up to ⋉.
There are two special cases.
All transitions can be frozen (GSFrozen),
in which case state only contains the backward Path of frozen transitions
(excluding ⋊ and ⋉):
⋊==[1]==[2]==[3]==⋉
└ current position
represented as: ==[3]==[2]==[1]==Or all transitions can be open (GSOpen),
in which case the state is just the forward path of open transitions:
⋊——[1]——[2]——[3]——⋉ └ current position represented as: ——[1]——[2]——[3]——
The open and semiopen case additionally have a list of operations in generative order, and a flag that indicates whether the previous step was a left operation, which would prevent continuing with a right unsplit.
Constructors
| GSFrozen !(Path (Maybe tr') slc) | |
| GSSemiOpen | |
Fields
| |
| GSOpen !(Path tr slc) ![op] | |
Instances
| (Show tr, Show tr', Show slc, Show o) => Show (GreedyState tr tr' slc o) Source # | |
Defined in GreedyParser Methods showsPrec :: Int -> GreedyState tr tr' slc o -> ShowS # show :: GreedyState tr tr' slc o -> String # showList :: [GreedyState tr tr' slc o] -> ShowS # | |
showFrozen :: (Show slc, Show tr') => Path tr' slc -> String Source #
Helper function for showing the frozen part of a piece.
showOpen :: (Show slc, Show tr) => Path tr slc -> String Source #
Helper function for showing the open part of a piece.
gsOps :: GreedyState tr tr' slc op -> [op] Source #
Parsing Actions
data SingleParent slc tr Source #
Single parent of a parsing action
Constructors
| SingleParent !(StartStop slc) !tr !(StartStop slc) |
Instances
| (Show slc, Show tr) => Show (SingleParent slc tr) Source # | |
Defined in GreedyParser Methods showsPrec :: Int -> SingleParent slc tr -> ShowS # show :: SingleParent slc tr -> String # showList :: [SingleParent slc tr] -> ShowS # | |
data ActionSingle slc tr s f Source #
A parsing action (reduction step) with a single parent transition. Combines the parent elements with a single-transition derivation operation.
Constructors
| ActionSingle | |
Fields
| |
Instances
| (Show slc, Show tr, Show s, Show f) => Show (ActionSingle slc tr s f) Source # | |
Defined in GreedyParser Methods showsPrec :: Int -> ActionSingle slc tr s f -> ShowS # show :: ActionSingle slc tr s f -> String # showList :: [ActionSingle slc tr s f] -> ShowS # | |
data DoubleParent slc tr Source #
Single parent of a parsing action
Constructors
| DoubleParent !(StartStop slc) !tr !slc !tr !(StartStop slc) |
Instances
| (Show slc, Show tr) => Show (DoubleParent slc tr) Source # | |
Defined in GreedyParser Methods showsPrec :: Int -> DoubleParent slc tr -> ShowS # show :: DoubleParent slc tr -> String # showList :: [DoubleParent slc tr] -> ShowS # | |
data ActionDouble slc tr s f h Source #
A parsing action (reduction step) with two parent transitions. Combines the parent elements with a double-transition derivation operation.
Constructors
| ActionDouble | |
Fields
| |
Instances
| (Show slc, Show tr, Show f, Show s, Show h) => Show (ActionDouble slc tr s f h) Source # | |
Defined in GreedyParser Methods showsPrec :: Int -> ActionDouble slc tr s f h -> ShowS # show :: ActionDouble slc tr s f h -> String # showList :: [ActionDouble slc tr s f h] -> ShowS # | |
type Action slc tr s f h = Either (ActionSingle slc tr s f) (ActionDouble slc tr s f h) Source #
An alias that combines ActionSingle and ActionDouble, representing all possible reduction steps.
actionGoesLeft :: Action slc tr s f h -> Maybe Bool Source #
A helper function that checks whether an action:
- - is a double action and goes left ('Just True')
- - is a double action and goes right ('Just False')
- - is a single action (Nothing, doesn't have to choose).
- (See opGoesLeft.)
opGoesLeft :: Leftmost s f h -> Maybe Bool Source #
A helper function that checks whether a derivation operation:
- - is a double op and goes left ('Just True')
- - is a double op and goes right ('Just False')
- - is a single op (Nothing, doesn't have to choose).
- (See actionGoesLeft.)
Parsing Algorithm
Arguments
| :: forall (m :: Type -> Type) tr tr' slc slc' s f h. (Monad m, MonadIO m, Show tr', Show slc, Show tr, Show s, Show f, Show h) | |
| => Eval tr tr' slc slc' h (Leftmost s f h) | the evaluator of the grammar to be used |
| -> ([Action slc tr s f h] -> ExceptT String m (Action slc tr s f h)) | the policy: picks a parsing action from a list of options
(determines the |
| -> Path slc' tr' | the input piece |
| -> ExceptT String m (Analysis s f h tr slc) | the full parse or an error message |
Parse a piece in a greedy fashion.
At each step, a policy chooses from the possible reduction actions,
the reduction is applied, and parsing continues
until the piece is fully reduced or no more reduction operations are available.
Returns the full derivation from the top (⋊——⋉) or an error message.
initParseState :: Eval tr tr' slc slc' h v -> Path slc' tr' -> GreedyState tr tr' slc op Source #
Initializes a parse state. Takes an evaluator and a (frozen) input path. Returns the parsing state that corresponds to the unparsed input.
Arguments
| :: forall (m :: Type -> Type) tr tr' slc slc' s f h. Monad m | |
| => Eval tr tr' slc slc' h (Leftmost s f h) | the evaluator of the grammar to be used |
| -> ([Action slc tr s f h] -> ExceptT String m (Action slc tr s f h)) | the policy: picks a parsing action from a list of options
(determines the |
| -> GreedyState tr tr' slc (Leftmost s f h) | the current parsing state |
| -> ExceptT String m (Either (GreedyState tr tr' slc (Leftmost s f h)) (tr, [Leftmost s f h])) | either the next state or the result of the parse. |
A single greedy parse step.
Enumerates a list of possible actions in the current state
and applies a policy function to select an action.
The resulting state is returned,
wrapped in a monad transformer stack containing
String exceptions and the monad of the policy.
Arguments
| :: forall {k} (m :: k) tr tr' slc slc' s f h. Eval tr tr' slc slc' h (Leftmost s f h) | the evaluator of the grammar to be used |
| -> GreedyState tr tr' slc (Leftmost s f h) | the current parsing state |
| -> [Action slc tr s f h] | the list of possible actions |
Enumerates the list of possible actions in the current state
lastWasLeft :: [Leftmost s f h] -> Bool Source #
collectAllThawLeft :: Eval tr tr' slc slc' h (Leftmost s f h) -> Path (Maybe tr') slc -> slc -> tr -> StartStop slc -> [ActionDouble slc tr s f h] Source #
collectThawSingle :: Eval tr tr' slc slc' h (Leftmost s f h) -> StartStop slc -> Maybe tr' -> StartStop slc -> [ActionSingle slc tr s f] Source #
collectThawLeft :: Eval tr tr' slc slc' h (Leftmost s f h) -> StartStop slc -> Maybe tr' -> slc -> tr -> StartStop slc -> [ActionDouble slc tr s f h] Source #
collectUnsplitSingle :: Eval tr tr' slc slc' h (Leftmost s f h) -> StartStop slc -> tr -> slc -> tr -> StartStop slc -> [ActionSingle slc tr s f] Source #
collectUnsplitLeft :: Eval tr tr' slc slc' h (Leftmost s f h) -> StartStop slc -> tr -> slc -> tr -> slc -> tr -> StartStop slc -> [ActionDouble slc tr s f h] Source #
collectUnsplitRight :: Eval tr tr' slc slc' h (Leftmost s f h) -> StartStop slc -> tr -> slc -> tr -> slc -> tr -> StartStop slc -> Bool -> [ActionDouble slc tr s f h] Source #
collectUnspreads :: Eval tr tr' slc slc' h (Leftmost s f h) -> StartStop slc -> tr -> slc -> tr -> slc -> tr -> StartStop slc -> [ActionDouble slc tr s f h] Source #
collectDoubles :: Eval tr tr' slc slc' h (Leftmost s f h) -> StartStop slc -> tr -> slc -> tr -> slc -> Path tr slc -> Bool -> [ActionDouble slc tr s f h] Source #
pickRandom :: forall g (m :: Type -> Type) slc. StatefulGen g m => g -> [slc] -> ExceptT String m slc Source #
A policy that picks the next action at random.
Must be partially applied with a random generator before passing to parseGreedy.
Applying actions
Arguments
| :: forall {k1} {k2} (m :: k1) tr tr' slc (slc' :: k2) s f h. GreedyState tr tr' slc (Leftmost s f h) | the current parsing state |
| -> Action slc tr s f h | the action to be applied |
| -> Either String (Either (GreedyState tr tr' slc (Leftmost s f h)) (tr, [Leftmost s f h])) | either the next state or an error message |
Apply an action to a parsing state.
Entry Points
Arguments
| :: (Show tr', Show slc, Show tr, Show s, Show f, Show h) | |
| => Eval tr tr' slc slc' h (Leftmost s f h) | the grammar's evaluator |
| -> Path slc' tr' | the input piece |
| -> ExceptT String IO (Analysis s f h tr slc) | a random reduction of the piece (or an error message) |
Parse a piece randomly using a fresh random number generator.
Arguments
| :: (Show tr', Show slc, Show tr, Show s, Show f, Show h, StatefulGen g IO) | |
| => g | a random number generator |
| -> Eval tr tr' slc slc' h (Leftmost s f h) | the grammar's evaluator |
| -> Path slc' tr' | the input piece |
| -> ExceptT String IO (Analysis s f h tr slc) | a random reduction of the piece (or an error message) |
Parse a piece randomly using an existing random number generator.