Agda-2.5.2: A dependently typed functional programming language and proof assistant

Safe Haskell | None
Language | Haskell2010

Agda.Utils.Parser.MemoisedCPS

Description

Parser combinators with support for left recursion, following Johnson's "Memoization in Top-Down Parsing".

This implementation is based on an implementation due to Atkey (attached to an edlambda-members mailing list message from 2011-02-15 titled 'Slides for "Introduction to Parser Combinators"').

Note that non-memoised left recursion is not guaranteed to work.

The code contains an important deviation from Johnson's paper: the check for subsumed results is not included. This means that one can get the same result multiple times when parsing using ambiguous grammars. As an example, parsing the empty string using `S ∷= ε | ε` succeeds twice. This change also means that parsing fails to terminate for some cyclic grammars that would otherwise be handled successfully, such as `S ∷= S | ε`. However, the library is not intended to handle infinitely ambiguous grammars. (It is unclear to the author of this module whether the change leads to more non-termination for grammars that are not cyclic.)

Synopsis

- class (Functor p, Applicative p, Alternative p, Monad p) => ParserClass p k r tok | p -> k, p -> r, p -> tok where
- data Parser k r tok a
- data ParserWithGrammar k r tok a

# Documentation

class (Functor p, Applicative p, Alternative p, Monad p) => ParserClass p k r tok | p -> k, p -> r, p -> tok where #

Minimal complete definition

parse, grammar, token, sat, tok, annotate, memoise, memoiseIfPrinting

Methods

parse :: p a -> [tok] -> [a] #

Runs the parser.

grammar :: p a -> Doc #

Tries to print the parser, or returns `empty`, depending on the implementation. This function might not terminate.

token :: p tok #

Parses a single token.

sat :: (tok -> Bool) -> p tok #

Parses a token satisfying the given predicate.

tok :: (Eq tok, Show tok) => tok -> p tok #

Parses a given token.

annotate :: (Doc -> Doc) -> p a -> p a #

Uses the given function to modify the printed representation (if any) of the given parser.

memoise :: (Eq k, Hashable k, Show k) => k -> p r -> p r #

Memoises the given parser.

Every memoised parser must be annotated with a unique key. (Parametrised parsers must use distinct keys for distinct inputs.)

memoiseIfPrinting :: (Eq k, Hashable k, Show k) => k -> p r -> p r #

Memoises the given parser, but only if printing, not if parsing.

Every memoised parser must be annotated with a unique key. (Parametrised parsers must use distinct keys for distinct inputs.)

Instances

ParserClass (ParserWithGrammar k r tok) k r tok # |
Methods parse :: ParserWithGrammar k r tok a -> [tok] -> [a] # grammar :: ParserWithGrammar k r tok a -> Doc # token :: ParserWithGrammar k r tok tok # sat :: (tok -> Bool) -> ParserWithGrammar k r tok tok # tok :: tok -> ParserWithGrammar k r tok tok # annotate :: (Doc -> Doc) -> ParserWithGrammar k r tok a -> ParserWithGrammar k r tok a # memoise :: k -> ParserWithGrammar k r tok r -> ParserWithGrammar k r tok r # memoiseIfPrinting :: k -> ParserWithGrammar k r tok r -> ParserWithGrammar k r tok r #
ParserClass (Parser k r tok) k r tok # |
Methods parse :: Parser k r tok a -> [tok] -> [a] # grammar :: Parser k r tok a -> Doc # token :: Parser k r tok tok # sat :: (tok -> Bool) -> Parser k r tok tok # tok :: tok -> Parser k r tok tok # annotate :: (Doc -> Doc) -> Parser k r tok a -> Parser k r tok a # memoise :: k -> Parser k r tok r -> Parser k r tok r # memoiseIfPrinting :: k -> Parser k r tok r -> Parser k r tok r #

data Parser k r tok a #

The parser type.

The parameters of the type `Parser k r tok a` have the following meanings:

`k`
  Type used for memoisation keys.
`r`
  The type of memoised values. (Yes, all memoised values have to have the same type.)
`tok`
  The token type.
`a`
  The result type.

Instances

Monad (Parser k r tok) # |
Methods (>>=) :: Parser k r tok a -> (a -> Parser k r tok b) -> Parser k r tok b # (>>) :: Parser k r tok a -> Parser k r tok b -> Parser k r tok b # return :: a -> Parser k r tok a # fail :: String -> Parser k r tok a #
Functor (Parser k r tok) # |
Methods fmap :: (a -> b) -> Parser k r tok a -> Parser k r tok b # (<$) :: a -> Parser k r tok b -> Parser k r tok a #
Applicative (Parser k r tok) # |
Methods pure :: a -> Parser k r tok a # (<*>) :: Parser k r tok (a -> b) -> Parser k r tok a -> Parser k r tok b # (*>) :: Parser k r tok a -> Parser k r tok b -> Parser k r tok b # (<*) :: Parser k r tok a -> Parser k r tok b -> Parser k r tok a #
Alternative (Parser k r tok) # |
Methods empty :: Parser k r tok a # (<|>) :: Parser k r tok a -> Parser k r tok a -> Parser k r tok a # some :: Parser k r tok a -> Parser k r tok [a] # many :: Parser k r tok a -> Parser k r tok [a] #
ParserClass (Parser k r tok) k r tok # |
Methods parse :: Parser k r tok a -> [tok] -> [a] # grammar :: Parser k r tok a -> Doc # token :: Parser k r tok tok # sat :: (tok -> Bool) -> Parser k r tok tok # tok :: tok -> Parser k r tok tok # annotate :: (Doc -> Doc) -> Parser k r tok a -> Parser k r tok a # memoise :: k -> Parser k r tok r -> Parser k r tok r # memoiseIfPrinting :: k -> Parser k r tok r -> Parser k r tok r #

data ParserWithGrammar k r tok a #

An extended parser type, with some support for printing parsers.

Instances

Monad (ParserWithGrammar k r tok) # |
Methods (>>=) :: ParserWithGrammar k r tok a -> (a -> ParserWithGrammar k r tok b) -> ParserWithGrammar k r tok b # (>>) :: ParserWithGrammar k r tok a -> ParserWithGrammar k r tok b -> ParserWithGrammar k r tok b # return :: a -> ParserWithGrammar k r tok a # fail :: String -> ParserWithGrammar k r tok a #
Functor (ParserWithGrammar k r tok) # |
Methods fmap :: (a -> b) -> ParserWithGrammar k r tok a -> ParserWithGrammar k r tok b # (<$) :: a -> ParserWithGrammar k r tok b -> ParserWithGrammar k r tok a #
Applicative (ParserWithGrammar k r tok) # |
Methods pure :: a -> ParserWithGrammar k r tok a # (<*>) :: ParserWithGrammar k r tok (a -> b) -> ParserWithGrammar k r tok a -> ParserWithGrammar k r tok b # (*>) :: ParserWithGrammar k r tok a -> ParserWithGrammar k r tok b -> ParserWithGrammar k r tok b # (<*) :: ParserWithGrammar k r tok a -> ParserWithGrammar k r tok b -> ParserWithGrammar k r tok a #
Alternative (ParserWithGrammar k r tok) # |
Methods empty :: ParserWithGrammar k r tok a # (<|>) :: ParserWithGrammar k r tok a -> ParserWithGrammar k r tok a -> ParserWithGrammar k r tok a # some :: ParserWithGrammar k r tok a -> ParserWithGrammar k r tok [a] # many :: ParserWithGrammar k r tok a -> ParserWithGrammar k r tok [a] #
ParserClass (ParserWithGrammar k r tok) k r tok # |
Methods parse :: ParserWithGrammar k r tok a -> [tok] -> [a] # grammar :: ParserWithGrammar k r tok a -> Doc # token :: ParserWithGrammar k r tok tok # sat :: (tok -> Bool) -> ParserWithGrammar k r tok tok # tok :: tok -> ParserWithGrammar k r tok tok # annotate :: (Doc -> Doc) -> ParserWithGrammar k r tok a -> ParserWithGrammar k r tok a # memoise :: k -> ParserWithGrammar k r tok r -> ParserWithGrammar k r tok r # memoiseIfPrinting :: k -> ParserWithGrammar k r tok r -> ParserWithGrammar k r tok r #