{-# LANGUAGE Safe #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  Text.ParserCombinators.Parsec.Expr
-- Copyright   :  (c) Paolo Martini 2007
-- License     :  BSD-style (see the LICENSE file)
--
-- Maintainer  :  derek.a.elkins@gmail.com
-- Stability   :  provisional
-- Portability :  portable
--
-- Parsec compatibility module
--
-----------------------------------------------------------------------------

module Text.ParserCombinators.Parsec.Expr
    ( Assoc (AssocNone,AssocLeft,AssocRight),
      Operator(..),
      OperatorTable,
      buildExpressionParser
    ) where

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))
                         | Postfix (GenParser tok st (a -> a))

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

convert :: Operator tok st a -> N.Operator [tok] st Identity a
convert :: forall tok st a. Operator tok st a -> Operator [tok] st Identity a
convert (Infix GenParser tok st (a -> a -> a)
p Assoc
a) = GenParser tok st (a -> a -> a)
-> Assoc -> Operator [tok] st Identity a
forall s u (m :: * -> *) a.
ParsecT s u m (a -> a -> a) -> Assoc -> Operator s u m a
N.Infix GenParser tok st (a -> a -> a)
p Assoc
a
convert (Prefix GenParser tok st (a -> a)
p)  = GenParser tok st (a -> a) -> Operator [tok] st Identity a
forall s u (m :: * -> *) a.
ParsecT s u m (a -> a) -> Operator s u m a
N.Prefix GenParser tok st (a -> a)
p
convert (Postfix GenParser tok st (a -> a)
p)  = GenParser tok st (a -> a) -> Operator [tok] st Identity a
forall s u (m :: * -> *) a.
ParsecT s u m (a -> a) -> Operator s u m a
N.Postfix GenParser tok st (a -> a)
p

buildExpressionParser :: OperatorTable tok st a
                      -> GenParser tok st a
                      -> GenParser tok st a
buildExpressionParser :: forall tok st a.
OperatorTable tok st a -> GenParser tok st a -> GenParser tok st a
buildExpressionParser = OperatorTable [tok] st Identity a
-> ParsecT [tok] st Identity a -> ParsecT [tok] st Identity a
forall s (m :: * -> *) t u a.
Stream s m t =>
OperatorTable s u m a -> ParsecT s u m a -> ParsecT s u m a
N.buildExpressionParser (OperatorTable [tok] st Identity a
 -> ParsecT [tok] st Identity a -> ParsecT [tok] st Identity a)
-> ([[Operator tok st a]] -> OperatorTable [tok] st Identity a)
-> [[Operator tok st a]]
-> ParsecT [tok] st Identity a
-> ParsecT [tok] st Identity a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Operator tok st a] -> [Operator [tok] st Identity a])
-> [[Operator tok st a]] -> OperatorTable [tok] st Identity a
forall a b. (a -> b) -> [a] -> [b]
map ((Operator tok st a -> Operator [tok] st Identity a)
-> [Operator tok st a] -> [Operator [tok] st Identity a]
forall a b. (a -> b) -> [a] -> [b]
map Operator tok st a -> Operator [tok] st Identity a
forall tok st a. Operator tok st a -> Operator [tok] st Identity a
convert)