Grempa-0.1.3: Embedded grammar DSL and LALR parser generator

Data.Parser.Grempa.Dynamic

Description

Create parsers from grammars dynamically (at runtime).

Synopsis

Documentation

mkDynamicParserSource

Arguments

:: (Token t, Token t', Typeable a) 
=> (t -> t', t' -> t)

Token wrapper and unwrapper

-> Grammar t a

Language grammar

-> Parser t a 

Make a parser at runtime given a grammar

constrWrapper :: (t -> CTok t, CTok t -> t)Source

Wrap the input tokens in the CTok datatype, which has Eq and Ord instances which only look at the constructors of the input values. This is for use as an argument to mkDynamicParser.

Example, which will evaluate to True:

 CTok (Just 1) == CTok (Just 2)

This is useful when using a lexer that may give back a list of something like:

 data Token = Ident String | Number Integer | LParen | RParen | Plus | ...

If you want to specify a grammar that accepts any Ident and any Number and not just specific ones, use constrWrapper.

idWrapper :: (t -> t, t -> t)Source

Don't wrap the input tokens. This is for use as an argument to mkDynamicParser. An example usage of idWrapper is if the parser operates directly on String.

type ParseResult t a = Either (ParseError t) aSource

The result of running a parser

type Parser t a = [t] -> ParseResult t aSource

The type of a parser generated by Grempa

data ParseError t Source

The different kinds of errors that can occur

Constructors

ParseError

The parser did not get an accepted string of tokens.

Fields

expectedTokens :: [Tok t]

A list of tokens that would have been acceptable inputs when the error occured.

position :: Integer

The position (index into the input token list) at which the error occured.

InternalParserError

This should not happen. Please file a bug report if it does.

Fields

position :: Integer

The position (index into the input token list) at which the error occured.

Instances

showError :: Show t => ParseError t -> StringSource

Make a prettier error string from a ParseError. This shows the position as an index into the input string of tokens, which may not always be preferable, as that position may differ to the position in the input if it is first processed by a lexer. It also shows the expected tokens.

parse :: Show t => Parser t a -> [t] -> aSource

Throw away the Either from the ParseResult and throw an exception using showError if something went wrong.