Grempa-0.1.3: Embedded grammar DSL and LALR parser generator

Data.Parser.Grempa.Static

Description

Create parsers from grammars statically (at compile time).

Synopsis

Documentation

mkStaticParserSource

Arguments

:: (Typeable a, ToPat t, Token t, Lift t) 
=> Grammar t a

The grammar

-> ExpQ

The Template Haskell representation of the grammar

-> ExpQ

The representation of a parser of type Parser t a

Make a static parser from a grammar.

Example usage:

 g :: Grammar s a
 gparser = $(mkStaticParser g [|g|])

Note that gparser must be in a different module than g, due to Template Haskell restrictions. The token type of the grammar must also be an instance of ToPat, and the result type an instance of Typeable (the GHC extension DeriveDataTypeable may be useful for this).

If there are conflicts in the parsing tables, they will be displayed as warnings when compiling the parser.

class ToPat a whereSource

Make a Template Haskell pattern from a value. This is used to create a case expression from a parsing table when using mkStaticParser, and it is thus required that the token type that the parser is to operate on is an instance of this class.

The parser will behave differently depending on how its ToPat instance works. If only comparing constructors (toConstrPat), it will regard Just 1 as the same compared to Just 2.

toConstrPat and Language.Haskell.TH can help in creating an instance.

Methods

toPat :: a -> PatQSource

Instances

ToPat Char 
ToPat Int 
ToPat a => ToPat [a] 
ToPat a => ToPat (Tok a) 
(ToPat a, ToPat b) => ToPat (a, b) 

toConstrPat :: (Token t, Lift t) => t -> PatQSource

Automatically create a ToPat instance which only compares the constructor of the token type. For example, the pattern yielded from using this on the value Just 3 is the pattern Just _.

Example usage:

 instance ToPat TokenType where
     toPat = toConstrPat

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.