-- |
-- Operators fixity and associativity
--
module Language.PureScript.AST.Operators where

import Prelude.Compat

import Codec.Serialise (Serialise)
import GHC.Generics (Generic)
import Control.DeepSeq (NFData)
import Data.Aeson ((.=))
import qualified Data.Aeson as A

import Language.PureScript.Crash

-- |
-- A precedence level for an infix operator
--
type Precedence = Integer

-- |
-- Associativity for infix operators
--
data Associativity = Infixl | Infixr | Infix
  deriving (Int -> Associativity -> ShowS
[Associativity] -> ShowS
Associativity -> String
(Int -> Associativity -> ShowS)
-> (Associativity -> String)
-> ([Associativity] -> ShowS)
-> Show Associativity
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Associativity] -> ShowS
$cshowList :: [Associativity] -> ShowS
show :: Associativity -> String
$cshow :: Associativity -> String
showsPrec :: Int -> Associativity -> ShowS
$cshowsPrec :: Int -> Associativity -> ShowS
Show, Associativity -> Associativity -> Bool
(Associativity -> Associativity -> Bool)
-> (Associativity -> Associativity -> Bool) -> Eq Associativity
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Associativity -> Associativity -> Bool
$c/= :: Associativity -> Associativity -> Bool
== :: Associativity -> Associativity -> Bool
$c== :: Associativity -> Associativity -> Bool
Eq, Eq Associativity
Eq Associativity
-> (Associativity -> Associativity -> Ordering)
-> (Associativity -> Associativity -> Bool)
-> (Associativity -> Associativity -> Bool)
-> (Associativity -> Associativity -> Bool)
-> (Associativity -> Associativity -> Bool)
-> (Associativity -> Associativity -> Associativity)
-> (Associativity -> Associativity -> Associativity)
-> Ord Associativity
Associativity -> Associativity -> Bool
Associativity -> Associativity -> Ordering
Associativity -> Associativity -> Associativity
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Associativity -> Associativity -> Associativity
$cmin :: Associativity -> Associativity -> Associativity
max :: Associativity -> Associativity -> Associativity
$cmax :: Associativity -> Associativity -> Associativity
>= :: Associativity -> Associativity -> Bool
$c>= :: Associativity -> Associativity -> Bool
> :: Associativity -> Associativity -> Bool
$c> :: Associativity -> Associativity -> Bool
<= :: Associativity -> Associativity -> Bool
$c<= :: Associativity -> Associativity -> Bool
< :: Associativity -> Associativity -> Bool
$c< :: Associativity -> Associativity -> Bool
compare :: Associativity -> Associativity -> Ordering
$ccompare :: Associativity -> Associativity -> Ordering
$cp1Ord :: Eq Associativity
Ord, (forall x. Associativity -> Rep Associativity x)
-> (forall x. Rep Associativity x -> Associativity)
-> Generic Associativity
forall x. Rep Associativity x -> Associativity
forall x. Associativity -> Rep Associativity x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Associativity x -> Associativity
$cfrom :: forall x. Associativity -> Rep Associativity x
Generic)

instance NFData Associativity
instance Serialise Associativity

showAssoc :: Associativity -> String
showAssoc :: Associativity -> String
showAssoc Associativity
Infixl = String
"infixl"
showAssoc Associativity
Infixr = String
"infixr"
showAssoc Associativity
Infix  = String
"infix"

readAssoc :: String -> Associativity
readAssoc :: String -> Associativity
readAssoc String
"infixl" = Associativity
Infixl
readAssoc String
"infixr" = Associativity
Infixr
readAssoc String
"infix"  = Associativity
Infix
readAssoc String
_ = String -> Associativity
forall a. HasCallStack => String -> a
internalError String
"readAssoc: no parse"

instance A.ToJSON Associativity where
  toJSON :: Associativity -> Value
toJSON = String -> Value
forall a. ToJSON a => a -> Value
A.toJSON (String -> Value)
-> (Associativity -> String) -> Associativity -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Associativity -> String
showAssoc

instance A.FromJSON Associativity where
  parseJSON :: Value -> Parser Associativity
parseJSON = (String -> Associativity) -> Parser String -> Parser Associativity
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> Associativity
readAssoc (Parser String -> Parser Associativity)
-> (Value -> Parser String) -> Value -> Parser Associativity
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Value -> Parser String
forall a. FromJSON a => Value -> Parser a
A.parseJSON

-- |
-- Fixity data for infix operators
--
data Fixity = Fixity Associativity Precedence
  deriving (Int -> Fixity -> ShowS
[Fixity] -> ShowS
Fixity -> String
(Int -> Fixity -> ShowS)
-> (Fixity -> String) -> ([Fixity] -> ShowS) -> Show Fixity
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Fixity] -> ShowS
$cshowList :: [Fixity] -> ShowS
show :: Fixity -> String
$cshow :: Fixity -> String
showsPrec :: Int -> Fixity -> ShowS
$cshowsPrec :: Int -> Fixity -> ShowS
Show, Fixity -> Fixity -> Bool
(Fixity -> Fixity -> Bool)
-> (Fixity -> Fixity -> Bool) -> Eq Fixity
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Fixity -> Fixity -> Bool
$c/= :: Fixity -> Fixity -> Bool
== :: Fixity -> Fixity -> Bool
$c== :: Fixity -> Fixity -> Bool
Eq, Eq Fixity
Eq Fixity
-> (Fixity -> Fixity -> Ordering)
-> (Fixity -> Fixity -> Bool)
-> (Fixity -> Fixity -> Bool)
-> (Fixity -> Fixity -> Bool)
-> (Fixity -> Fixity -> Bool)
-> (Fixity -> Fixity -> Fixity)
-> (Fixity -> Fixity -> Fixity)
-> Ord Fixity
Fixity -> Fixity -> Bool
Fixity -> Fixity -> Ordering
Fixity -> Fixity -> Fixity
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Fixity -> Fixity -> Fixity
$cmin :: Fixity -> Fixity -> Fixity
max :: Fixity -> Fixity -> Fixity
$cmax :: Fixity -> Fixity -> Fixity
>= :: Fixity -> Fixity -> Bool
$c>= :: Fixity -> Fixity -> Bool
> :: Fixity -> Fixity -> Bool
$c> :: Fixity -> Fixity -> Bool
<= :: Fixity -> Fixity -> Bool
$c<= :: Fixity -> Fixity -> Bool
< :: Fixity -> Fixity -> Bool
$c< :: Fixity -> Fixity -> Bool
compare :: Fixity -> Fixity -> Ordering
$ccompare :: Fixity -> Fixity -> Ordering
$cp1Ord :: Eq Fixity
Ord, (forall x. Fixity -> Rep Fixity x)
-> (forall x. Rep Fixity x -> Fixity) -> Generic Fixity
forall x. Rep Fixity x -> Fixity
forall x. Fixity -> Rep Fixity x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Fixity x -> Fixity
$cfrom :: forall x. Fixity -> Rep Fixity x
Generic)

instance NFData Fixity
instance Serialise Fixity

instance A.ToJSON Fixity where
  toJSON :: Fixity -> Value
toJSON (Fixity Associativity
associativity Precedence
precedence) =
    [Pair] -> Value
A.object [ Text
"associativity" Text -> Associativity -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Associativity
associativity
             , Text
"precedence" Text -> Precedence -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Precedence
precedence
             ]