| Portability | non-portable | 
|---|---|
| Stability | experimental | 
| Maintainer | ekmett@gmail.com | 
| Safe Haskell | None | 
Text.Parser.Expression
Description
A helper module to parse "expressions". Builds a parser given a table of operators and associativities.
- data  Assoc 
- = AssocNone
 - | AssocLeft
 - | AssocRight
 
 - data Operator m a
 - type OperatorTable m a = [[Operator m a]]
 - buildExpressionParser :: forall m a. (Parsing m, Applicative m) => OperatorTable m a -> m a -> m a
 
Documentation
This data type specifies the associativity of operators: left, right or none.
Constructors
| AssocNone | |
| AssocLeft | |
| AssocRight | 
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.
type OperatorTable m 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 :: forall m a. (Parsing m, Applicative m) => OperatorTable m a -> m a -> m aSource
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.
  expr    = buildExpressionParser table term
          <?> "expression"
  term    =  parens expr
          <|> natural
          <?> "simple expression"
  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)