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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
module Atom ( SrcLoc(..), showLoc
, Ident, ident, identAt, prime, string, isPrimed, identSrcLoc
, Literal, numeral, charLit, stringLit, numeralAt, charLitAt, stringLitAt, litSrcLoc )
where
import Prettier hiding ( string )
import qualified Prettier as PP
data SrcLoc = At Int Int
| Unknown
deriving (Show)
instance Pretty SrcLoc where
prettyPrec _d (At ln col) = PP.string (showLoc ln col)
prettyPrec _d Unknown = PP.string "<unknown>"
showLoc :: Int -> Int -> String
showLoc line col = "(line " ++ show line ++ ", column " ++ show col ++ ")"
data Ident = Ident String SrcLoc
| Prime Ident
instance Eq Ident where
Ident s1 _ == Ident s2 _ = s1 == s2
Prime i1 == Prime i2 = i1 == i2
_ == _ = False
instance Ord Ident where
compare (Ident s1 _) (Ident s2 _)
= compare s1 s2
compare (Ident _ _) (Prime _)
= LT
compare (Prime _) (Ident _ _)
= GT
compare (Prime i1) (Prime i2)
= compare i1 i2
instance Show Ident where
showsPrec d (Ident s _) = showParen (d > 9) (showString "ident " . shows s)
showsPrec d (Prime i) = showParen (d > 9) (showString "prime " . showsPrec 10 i)
instance Pretty Ident where
prettyPrec _d (Ident s _) = PP.string s
prettyPrec d (Prime i) = prettyPrec d i <> char '\''
ident :: String -> Ident
ident s = Ident s Unknown
identAt :: String -> SrcLoc -> Ident
identAt s loc = Ident s loc
prime :: Ident -> Ident
prime i = Prime i
string :: Ident -> String
string (Ident s _) = s
string (Prime i) = string i
isPrimed :: Ident -> Bool
isPrimed (Prime _i) = True
isPrimed _ = False
identSrcLoc :: Ident -> SrcLoc
identSrcLoc (Ident _ loc) = loc
identSrcLoc (Prime i) = identSrcLoc i
data Literal = Numeral String SrcLoc
| Char String SrcLoc
| String String SrcLoc
str :: Literal -> String
str (Numeral s _) = s
str (Char s _) = s
str (String s _) = s
instance Eq Literal where
l1 == l2 = str l1 == str l2
instance Ord Literal where
compare l1 l2 = compare (str l1) (str l2)
instance Show Literal where
showsPrec d (Numeral s _) = showParen (d > 9) (showString "numeral " . shows s)
showsPrec d (Char s _) = showParen (d > 9) (showString "charLit " . shows s)
showsPrec d (String s _) = showParen (d > 9) (showString "stringLit " . shows s)
instance Pretty Literal where
prettyPrec _d (Numeral s _) = PP.string s
prettyPrec _d (Char s _) = PP.string s
prettyPrec _d (String s _) = PP.string s
numeral, charLit, stringLit :: String -> Literal
numeral s = Numeral s Unknown
charLit s = Char s Unknown
stringLit s = String s Unknown
numeralAt, charLitAt, stringLitAt :: String -> SrcLoc -> Literal
numeralAt s loc = Numeral s loc
charLitAt s loc = Char s loc
stringLitAt s loc = String s loc
litSrcLoc :: Literal -> SrcLoc
litSrcLoc (Numeral _ loc) = loc
litSrcLoc (Char _ loc) = loc
litSrcLoc (String _ loc) = loc
|