Earley-0.8.3: Parsing all context-free grammars using Earley's algorithm.

Safe HaskellNone
LanguageHaskell2010

Text.Earley.Internal

Contents

Description

This module exposes the internals of the package: its API may change independently of the PVP-compliant version number.

Synopsis

Concrete rules and productions

data Rule s r e t a Source

The concrete rule type that the parser uses

Constructors

Rule 

Fields

ruleProd :: ProdR s r e t a
 
ruleNullable :: !(STRef s (Maybe [a]))
 
ruleConts :: !(STRef s (STRef s [Cont s r e t a r]))
 

type ProdR s r e t a = Prod (Rule s r) e t a Source

nullable :: Rule s r e t a -> ST s [a] Source

nullableProd :: ProdR s r e t a -> ST s [a] Source

resetConts :: Rule s r e t a -> ST s () Source

type Args s f a = f -> ST s [a] Source

If we have something of type f, Args s f a is what we need to do to f to produce as.

noArgs :: Args s a a Source

funArg :: (f -> a) -> Args s f a Source

pureArg :: x -> Args s f a -> Args s (x -> f) a Source

impureArgs :: ST s [x] -> Args s f a -> Args s (x -> f) a Source

mapArgs :: (a -> b) -> Args s f a -> Args s f b Source

composeArgs :: Args s a b -> Args s b c -> Args s a c Source

States and continuations

type Pos = Int Source

data State s r e t a where Source

An Earley state with result type a.

Constructors

State :: !Pos -> !(ProdR s r e t f) -> !(Args s f b) -> !(Conts s r e t b a) -> State s r e t a 
Final :: f -> Args s f a -> State s r e t a 

data Cont s r e t a b where Source

A continuation accepting an a and producing a b.

Constructors

Cont :: !Pos -> !(Args s a b) -> !(ProdR s r e t (b -> c)) -> !(Args s c d) -> !(Conts s r e t d e') -> Cont s r e t a e' 
FinalCont :: Args s a c -> Cont s r e t a c 

data Conts s r e t a c Source

Constructors

Conts 

Fields

conts :: !(STRef s [Cont s r e t a c])
 
contsArgs :: !(STRef s (Maybe (STRef s (ST s [a]))))
 

newConts :: STRef s [Cont s r e t a c] -> ST s (Conts s r e t a c) Source

contraMapCont :: Args s b a -> Cont s r e t a c -> Cont s r e t b c Source

contToState :: ST s [a] -> Cont s r e t a c -> State s r e t c Source

simplifyCont :: Conts s r e t b a -> ST s [Cont s r e t b a] Source

Strings of non-ambiguous continuations can be optimised by removing indirections.

Grammars

grammar :: Grammar (Rule s r) e a -> ST s a Source

Interpret an abstract Grammar.

initialState :: ProdR s a e t a -> ST s (State s a e t a) Source

Given a grammar, construct an initial state.

Parsing

data Report e i Source

A parsing report, which contains fields that are useful for presenting errors to the user if a parse is deemed a failure. Note however that we get a report even when we successfully parse something.

Constructors

Report 

Fields

position :: Int

The final position in the input (0-based) that the parser reached.

expected :: [e]

The named productions processed at the final position.

unconsumed :: i

The part of the input string that was not consumed, which may be empty.

Instances

(Eq e, Eq i) => Eq (Report e i) Source 
(Ord e, Ord i) => Ord (Report e i) Source 
(Read e, Read i) => Read (Report e i) Source 
(Show e, Show i) => Show (Report e i) Source 

data Result s e i a Source

The result of a parse.

Constructors

Ended (Report e i)

The parser ended.

Parsed (ST s [a]) Int i (ST s (Result s e i a))

The parser parsed a number of as. These are given as a computation, ST s [a] that constructs the as when run. We can thus save some work by ignoring this computation if we do not care about the results. The Int is the position in the input where these results were obtained, the i the rest of the input, and the last component is the continuation.

Instances

safeHead :: ListLike i t => i -> Maybe t Source

safeTail :: ListLike i t => i -> i Source

parse Source

Arguments

:: ListLike i t 
=> [State s a e t a]

States to process at this position

-> [ST s [a]]

Results ready to be reported (when this position has been processed)

-> [State s a e t a]

States to process at the next position

-> ST s ()

Computation that resets the continuation refs of productions

-> [e]

Named productions encountered at this position

-> Pos

The current position in the input string

-> i

The input string

-> ST s (Result s e i a) 

The internal parsing routine

parser :: ListLike i t => (forall r. Grammar r e (Prod r e t a)) -> i -> ST s (Result s e i a) Source

Create a parser from the given grammar.

allParses :: (forall s. ST s (Result s e i a)) -> ([(a, Int)], Report e i) Source

Return all parses from the result of a given parser. The result may contain partial parses. The Ints are the position at which a result was produced.

fullParses :: ListLike i t => (forall s. ST s (Result s e i a)) -> ([a], Report e i) Source

Return all parses that reached the end of the input from the result of a given parser.

report :: ListLike i t => (forall s. ST s (Result s e i a)) -> Report e i Source

See e.g. how far the parser is able to parse the input string before it fails. This can be much faster than getting the parse results for highly ambiguous grammars.