forked from purescript/purescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperators.hs
More file actions
51 lines (40 loc) · 1.13 KB
/
Operators.hs
File metadata and controls
51 lines (40 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
-- |
-- Operators fixity and associativity
--
module Language.PureScript.AST.Operators where
import Prelude.Compat
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 (Show, Eq, Ord)
showAssoc :: Associativity -> String
showAssoc Infixl = "infixl"
showAssoc Infixr = "infixr"
showAssoc Infix = "infix"
readAssoc :: String -> Associativity
readAssoc "infixl" = Infixl
readAssoc "infixr" = Infixr
readAssoc "infix" = Infix
readAssoc _ = internalError "readAssoc: no parse"
instance A.ToJSON Associativity where
toJSON = A.toJSON . showAssoc
instance A.FromJSON Associativity where
parseJSON = fmap readAssoc . A.parseJSON
-- |
-- Fixity data for infix operators
--
data Fixity = Fixity Associativity Precedence
deriving (Show, Eq, Ord)
instance A.ToJSON Fixity where
toJSON (Fixity associativity precedence) =
A.object [ "associativity" .= associativity
, "precedence" .= precedence
]