HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Moduleparsers-0.12.12Haskell2010

Text.Parser.Expression

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

  • 3 types
  • 1 value
  • Packageparsers-0.12.12
  • Exports4
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceExpression.hs
datadata Assoc
#

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

Instances8Bounded, Enum, Eq, Data, Ord, Read, …
  • Bounded AssocDefined in parsers-0.12.12 · Text.Parser.Expression
  • Enum AssocDefined in parsers-0.12.12 · Text.Parser.Expression
  • Eq AssocDefined in parsers-0.12.12 · Text.Parser.Expression
  • Data AssocDefined in parsers-0.12.12 · Text.Parser.Expression
  • Ord AssocDefined in parsers-0.12.12 · Text.Parser.Expression
  • Read AssocDefined in parsers-0.12.12 · Text.Parser.Expression
  • Show AssocDefined in parsers-0.12.12 · Text.Parser.Expression
  • Ix AssocDefined in parsers-0.12.12 · Text.Parser.Expression
datadata Operator (m :: Type -> Type) a
#

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

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

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

valuebuildExpressionParser
  1. :: (Parsing m, Applicative m)
  2. => OperatorTable m a
  3. -> m a
  4. -> m a
#

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