parsers-0.12.12: Parsing combinators

Copyright | (c) Edward Kmett 2011-2012 (c) Paolo Martini 2007 (c) Daan Leijen 1999-2001
License | BSD-style (see the LICENSE file)
Maintainer | ekmett@gmail.com
Stability | experimental
Portability | non-portable
Safe Haskell | None
Language | Haskell2010

Text.Parser.Expression

Description

A helper module to parse "expressions". Builds a parser given a table of operators and associativities.

Synopsis

- data Assoc
  - = AssocNone
  - | AssocLeft
  - | AssocRight
- data Operator (m :: Type -> Type) a
  - = Infix (m (a -> a -> a)) Assoc
  - | Prefix (m (a -> a))
  - | Postfix (m (a -> a))
- type OperatorTable (m :: Type -> Type) a = [[Operator m a]]
- buildExpressionParser :: (Parsing m, Applicative m) => OperatorTable m a -> m a -> m a

# Documentation

data Assoc Source #

This data type specifies the associativity of operators: left, right or none.

Constructors

AssocNone |
AssocLeft |
AssocRight |

#### Instances

Instances details

Data Assoc Source # |

Instance details

Defined in Text.Parser.Expression

Methods

gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Assoc -> c Assoc Source #

gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c Assoc Source #

toConstr :: Assoc -> Constr Source #

dataTypeOf :: Assoc -> DataType Source #

dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c Assoc) Source #

dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Assoc) Source #

gmapT :: (forall b. Data b => b -> b) -> Assoc -> Assoc Source #

gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Assoc -> r Source #

gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Assoc -> r Source #

gmapQ :: (forall d. Data d => d -> u) -> Assoc -> [u] Source #

gmapQi :: Int -> (forall d. Data d => d -> u) -> Assoc -> u Source #

gmapM :: Monad m => (forall d. Data d => d -> m d) -> Assoc -> m Assoc Source #

gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Assoc -> m Assoc Source #

gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Assoc -> m Assoc Source #

Bounded Assoc Source # |
Instance details Defined in Text.Parser.Expression Methods minBound :: Assoc Source # maxBound :: Assoc Source #
Enum Assoc Source # |
Instance details Defined in Text.Parser.Expression Methods succ :: Assoc -> Assoc Source # pred :: Assoc -> Assoc Source # toEnum :: Int -> Assoc Source # fromEnum :: Assoc -> Int Source # enumFrom :: Assoc -> [Assoc] Source # enumFromThen :: Assoc -> Assoc -> [Assoc] Source # enumFromTo :: Assoc -> Assoc -> [Assoc] Source # enumFromThenTo :: Assoc -> Assoc -> Assoc -> [Assoc] Source #
Ix Assoc Source # |
Instance details Defined in Text.Parser.Expression Methods range :: (Assoc, Assoc) -> [Assoc] Source # index :: (Assoc, Assoc) -> Assoc -> Int Source # unsafeIndex :: (Assoc, Assoc) -> Assoc -> Int Source # inRange :: (Assoc, Assoc) -> Assoc -> Bool Source # rangeSize :: (Assoc, Assoc) -> Int Source # unsafeRangeSize :: (Assoc, Assoc) -> Int Source #
Read Assoc Source # |
Instance details Defined in Text.Parser.Expression Methods readsPrec :: Int -> ReadS Assoc Source # readList :: ReadS [Assoc] Source # readPrec :: ReadPrec Assoc Source # readListPrec :: ReadPrec [Assoc] Source #
Show Assoc Source # |
Instance details Defined in Text.Parser.Expression Methods showsPrec :: Int -> Assoc -> ShowS Source # show :: Assoc -> String Source # showList :: [Assoc] -> ShowS Source #
Eq Assoc Source # |
Instance details Defined in Text.Parser.Expression Methods (==) :: Assoc -> Assoc -> Bool Source # (/=) :: Assoc -> Assoc -> Bool Source #
Ord Assoc Source # |
Instance details Defined in Text.Parser.Expression Methods compare :: Assoc -> Assoc -> Ordering Source # (<) :: Assoc -> Assoc -> Bool Source # (<=) :: Assoc -> Assoc -> Bool Source # (>) :: Assoc -> Assoc -> Bool Source # (>=) :: Assoc -> Assoc -> Bool Source # max :: Assoc -> Assoc -> Assoc Source # min :: Assoc -> Assoc -> Assoc Source #

data Operator (m :: Type -> Type) a Source #

This data type specifies operators that work on values of type `a`. An operator is either binary infix or unary prefix or postfix. A binary operator has also an associated associativity.

Constructors

Infix (m (a -> a -> a)) Assoc |
Prefix (m (a -> a)) |
Postfix (m (a -> a)) |

type OperatorTable (m :: Type -> Type) a = [[Operator m a]] Source #

An `OperatorTable m a` is a list of `Operator m a` lists. The list is ordered in descending precedence. All operators in one list have the same precedence (but may have a different associativity).

buildExpressionParser :: (Parsing m, Applicative m) => OperatorTable m a -> m a -> m a Source #

`buildExpressionParser table term` builds an expression parser for terms `term` with operators from `table`, taking the associativity and precedence specified in `table` into account. Prefix and postfix operators of the same precedence can only occur once (i.e. `--2` is not allowed if `-` is prefix negate). Prefix and postfix operators of the same precedence associate to the left (i.e. if `++` is postfix increment, than `-2++` equals `-1`, not `-3`).

The `buildExpressionParser` takes care of all the complexity involved in building expression parser. Here is an example of an expression parser that handles prefix signs, postfix increment and basic arithmetic.

     import Control.Applicative ((<|>))
     import Text.Parser.Combinators ((<?>))
     import Text.Parser.Expression
     import Text.Parser.Token (TokenParsing, natural, parens, reserve)
     import Text.Parser.Token.Style (emptyOps)

     expr   :: (Monad m, TokenParsing m) => m Integer
     expr    = buildExpressionParser table term
             <?> "expression"

     term   :: (Monad m, TokenParsing m) => m Integer
     term    =  parens expr
             <|> natural
             <?> "simple expression"

     table  :: (Monad m, TokenParsing m) => [[Operator m Integer]]
     table   = [ [prefix "-" negate, prefix "+" id ]
               , [postfix "++" (+1)]
               , [binary "*" (*) AssocLeft, binary "/" (div) AssocLeft ]
               , [binary "+" (+) AssocLeft, binary "-" (-)   AssocLeft ]
               ]

     binary  name fun assoc = Infix (fun <$ reservedOp name) assoc
     prefix  name fun       = Prefix (fun <$ reservedOp name)
     postfix name fun       = Postfix (fun <$ reservedOp name)

     reservedOp name = reserve emptyOps name