Expr.hs 1.36 KB
Newer Older
DerekElkins's avatar
DerekElkins committed
1 2 3 4 5
-----------------------------------------------------------------------------
-- |
-- Module      :  Text.ParserCombinators.Parsec.Expr
-- Copyright   :  (c) Paolo Martini 2007
-- License     :  BSD-style (see the LICENSE file)
6
--
7
-- Maintainer  :  derek.a.elkins@gmail.com
DerekElkins's avatar
DerekElkins committed
8 9
-- Stability   :  provisional
-- Portability :  portable
10
--
DerekElkins's avatar
DerekElkins committed
11
-- Parsec compatibility module
12
--
DerekElkins's avatar
DerekElkins committed
13 14 15
-----------------------------------------------------------------------------

module Text.ParserCombinators.Parsec.Expr
16 17 18 19
    ( Assoc (AssocNone,AssocLeft,AssocRight),
      Operator(..),
      OperatorTable,
      buildExpressionParser
DerekElkins's avatar
DerekElkins committed
20 21
    ) where

22 23 24 25 26 27 28 29
import Text.Parsec.Expr(Assoc(..))
import qualified Text.Parsec.Expr as N
import Text.ParserCombinators.Parsec(GenParser)

import Control.Monad.Identity

data Operator tok st a   = Infix  (GenParser tok st (a -> a -> a)) Assoc
                         | Prefix (GenParser tok st (a -> a))
30
                         | Postfix (GenParser tok st (a -> a))
31 32 33 34 35 36

type OperatorTable tok st a = [[Operator tok st a]]

convert :: Operator tok st a -> N.Operator [tok] st Identity a
convert (Infix p a) = N.Infix p a
convert (Prefix p)  = N.Prefix p
37
convert (Postfix p)  = N.Postfix p
38 39 40 41 42

buildExpressionParser :: OperatorTable tok st a
                      -> GenParser tok st a
                      -> GenParser tok st a
buildExpressionParser = N.buildExpressionParser . map (map convert)