never executed always true always false
1 {-# LANGUAGE TypeFamilies #-}
2 {-# LANGUAGE DataKinds #-}
3 {-# LANGUAGE DeriveGeneric #-}
4 module ElmFormat.AST.Shared where
5
6 import Data.Coapplicative
7 import Data.Int (Int64)
8 import GHC.Generics
9 import Data.Text (Text)
10 import qualified Data.Text as Text
11 import qualified Data.Tuple as Tuple
12 import qualified Data.Char as Char
13
14 {-| This module contains types that are used by multiple versions of the Elm AST.
15 -}
16
17
18 type List a = [a]
19
20
21 newtype LowercaseIdentifier =
22 LowercaseIdentifier String
23 deriving (Eq, Ord)
24
25 instance Show LowercaseIdentifier where
26 show (LowercaseIdentifier name) = name
27
28
29 newtype UppercaseIdentifier =
30 UppercaseIdentifier String
31 deriving (Eq, Ord, Show)
32
33
34 newtype SymbolIdentifier =
35 SymbolIdentifier String
36 deriving (Eq, Ord, Show)
37
38
39 data Commented c a =
40 C c a
41 deriving (Eq, Ord, Functor, Show) -- TODO: is Ord needed?
42
43 instance Coapplicative (Commented c) where
44 extract (C _ a) = a
45 {-# INLINE extract #-}
46
47
48 data IntRepresentation
49 = DecimalInt
50 | HexadecimalInt
51 deriving (Eq, Show, Generic)
52
53
54 data FloatRepresentation
55 = DecimalFloat
56 | ExponentFloat
57 deriving (Eq, Show, Generic)
58
59
60 data StringRepresentation
61 = SingleQuotedString
62 | TripleQuotedString
63 deriving (Eq, Show, Generic)
64
65
66 data LiteralValue
67 = IntNum Int64 IntRepresentation
68 | FloatNum Double FloatRepresentation
69 | Chr Char
70 | Str String StringRepresentation
71 | Boolean Bool
72 deriving (Eq, Show)
73
74
75 data Ref ns
76 = VarRef ns LowercaseIdentifier
77 | TagRef ns UppercaseIdentifier
78 | OpRef SymbolIdentifier
79 deriving (Eq, Ord, Show, Functor)
80
81 refFromText :: Text -> Maybe (Ref ())
82 refFromText text =
83 case Tuple.fst <$> Text.uncons text of
84 Just first | Char.isUpper first ->
85 Just $ TagRef () (UppercaseIdentifier $ Text.unpack text)
86
87 Just first | Char.isLower first ->
88 Just $ VarRef () (LowercaseIdentifier $ Text.unpack text)
89
90 Just _ ->
91 Just $ OpRef (SymbolIdentifier $ Text.unpack text)
92
93 Nothing ->
94 Nothing
95
96
97 data UnaryOperator =
98 Negative
99 deriving (Eq, Show)