HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

ModuleAgda-2.7.0.1Haskell2010

Agda.Utils.Parser.MemoisedCPS

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.)

  • 3 types
  • 1 class
  • 9 values
  • PackageAgda-2.7.0.1
  • Exports13
  • LanguageHaskell2010
  • LicenceMIT
  • SourceMemoisedCPS.hs
classclass (Functor p, Applicative p, Alternative p, Monad p) => ParserClass (p :: Type -> Type) k r tok | p -> k, p -> r, p -> tok where
#

Methods

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

    Runs the parser.

  • grammar :: Show k => p a -> Doc

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

  • sat' :: (tok -> Maybe a) -> p a

    Parses a token satisfying the given predicate. The computed value is returned.

  • annotate :: (DocP -> DocP) -> 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.)

Instances2ParserClass
valuesat :: ParserClass p k r tok => (tok -> Bool) -> p tok
#

Parses a token satisfying the given predicate.

valuedoc :: ParserClass p k r tok => Doc -> p a -> p a
#

Uses the given document as the printed representation of the given parser. The document's precedence is taken to be atomP.

typetype DocP = (Doc, Int)
#

Documents paired with precedence levels.

valuestarP :: Int
#

Precedence of ⋆ and +.

newtypenewtype 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.

Instances5Monad, Functor, Applicative, Alternative, ParserClass
  • Monad (Parser k r tok)Defined in Agda-2.7.0.1 · Agda.Utils.Parser.MemoisedCPS
  • Functor (Parser k r tok)Defined in Agda-2.7.0.1 · Agda.Utils.Parser.MemoisedCPS
  • Applicative (Parser k r tok)Defined in Agda-2.7.0.1 · Agda.Utils.Parser.MemoisedCPS
  • Alternative (Parser k r tok)Defined in Agda-2.7.0.1 · Agda.Utils.Parser.MemoisedCPS
  • ParserClass (Parser k r tok) k r tokDefined in Agda-2.7.0.1 · Agda.Utils.Parser.MemoisedCPS
datadata ParserWithGrammar k r tok a
#

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

Instances5Monad, Functor, Applicative, Alternative, ParserClass