module Agda.Syntax.Literal where
import Control.DeepSeq
import Data.Char
import Data.Word
import Data.Text (Text)
import qualified Data.Text as T
import Agda.Syntax.Position
import Agda.Syntax.Common
import Agda.Syntax.Abstract.Name
import {-# SOURCE #-} Agda.Syntax.TopLevelModuleName
(TopLevelModuleName)
import Agda.Utils.FileName
import Agda.Utils.Float ( doubleDenotEq, doubleDenotOrd )
import Agda.Utils.Pretty
type RLiteral = Ranged Literal
data Literal
= LitNat !Integer
| LitWord64 !Word64
| LitFloat !Double
| LitString !Text
| LitChar !Char
| LitQName !QName
| LitMeta !TopLevelModuleName !MetaId
deriving Int -> Literal -> ShowS
[Literal] -> ShowS
Literal -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Literal] -> ShowS
$cshowList :: [Literal] -> ShowS
show :: Literal -> String
$cshow :: Literal -> String
showsPrec :: Int -> Literal -> ShowS
$cshowsPrec :: Int -> Literal -> ShowS
Show
instance Pretty Literal where
pretty :: Literal -> Doc
pretty (LitNat Integer
n) = forall a. Pretty a => a -> Doc
pretty Integer
n
pretty (LitWord64 Word64
n) = forall a. Pretty a => a -> Doc
pretty Word64
n
pretty (LitFloat Double
d) = forall a. Pretty a => a -> Doc
pretty Double
d
pretty (LitString Text
s) = String -> Doc
text forall a b. (a -> b) -> a -> b
$ Text -> ShowS
showText Text
s String
""
pretty (LitChar Char
c) = String -> Doc
text forall a b. (a -> b) -> a -> b
$ String
"'" forall a. [a] -> [a] -> [a]
++ Char -> ShowS
showChar' Char
c String
"'"
pretty (LitQName QName
x) = forall a. Pretty a => a -> Doc
pretty QName
x
pretty (LitMeta TopLevelModuleName
_ MetaId
x) = forall a. Pretty a => a -> Doc
pretty MetaId
x
showText :: Text -> ShowS
showText :: Text -> ShowS
showText Text
s = String -> ShowS
showString String
"\""
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (Char -> a -> a) -> a -> Text -> a
T.foldr (\ Char
c -> (Char -> ShowS
showChar' Char
c forall b c a. (b -> c) -> (a -> b) -> a -> c
.)) forall a. a -> a
id Text
s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
"\""
showChar' :: Char -> ShowS
showChar' :: Char -> ShowS
showChar' Char
'"' = String -> ShowS
showString String
"\\\""
showChar' Char
c
| Char -> Bool
escapeMe Char
c = Char -> ShowS
showLitChar Char
c
| Bool
otherwise = String -> ShowS
showString [Char
c]
where
escapeMe :: Char -> Bool
escapeMe Char
c = Bool -> Bool
not (Char -> Bool
isPrint Char
c) Bool -> Bool -> Bool
|| Char
c forall a. Eq a => a -> a -> Bool
== Char
'\\'
instance Eq Literal where
LitNat Integer
n == :: Literal -> Literal -> Bool
== LitNat Integer
m = Integer
n forall a. Eq a => a -> a -> Bool
== Integer
m
LitWord64 Word64
n == LitWord64 Word64
m = Word64
n forall a. Eq a => a -> a -> Bool
== Word64
m
LitFloat Double
x == LitFloat Double
y = Double -> Double -> Bool
doubleDenotEq Double
x Double
y
LitString Text
s == LitString Text
t = Text
s forall a. Eq a => a -> a -> Bool
== Text
t
LitChar Char
c == LitChar Char
d = Char
c forall a. Eq a => a -> a -> Bool
== Char
d
LitQName QName
x == LitQName QName
y = QName
x forall a. Eq a => a -> a -> Bool
== QName
y
LitMeta TopLevelModuleName
f MetaId
x == LitMeta TopLevelModuleName
g MetaId
y = (TopLevelModuleName
f, MetaId
x) forall a. Eq a => a -> a -> Bool
== (TopLevelModuleName
g, MetaId
y)
Literal
_ == Literal
_ = Bool
False
instance Ord Literal where
LitNat Integer
n compare :: Literal -> Literal -> Ordering
`compare` LitNat Integer
m = Integer
n forall a. Ord a => a -> a -> Ordering
`compare` Integer
m
LitWord64 Word64
n `compare` LitWord64 Word64
m = Word64
n forall a. Ord a => a -> a -> Ordering
`compare` Word64
m
LitFloat Double
x `compare` LitFloat Double
y = Double -> Double -> Ordering
doubleDenotOrd Double
x Double
y
LitString Text
s `compare` LitString Text
t = Text
s forall a. Ord a => a -> a -> Ordering
`compare` Text
t
LitChar Char
c `compare` LitChar Char
d = Char
c forall a. Ord a => a -> a -> Ordering
`compare` Char
d
LitQName QName
x `compare` LitQName QName
y = QName
x forall a. Ord a => a -> a -> Ordering
`compare` QName
y
LitMeta TopLevelModuleName
f MetaId
x `compare` LitMeta TopLevelModuleName
g MetaId
y = (TopLevelModuleName
f, MetaId
x) forall a. Ord a => a -> a -> Ordering
`compare` (TopLevelModuleName
g, MetaId
y)
compare LitNat{} Literal
_ = Ordering
LT
compare Literal
_ LitNat{} = Ordering
GT
compare LitWord64{} Literal
_ = Ordering
LT
compare Literal
_ LitWord64{} = Ordering
GT
compare LitFloat{} Literal
_ = Ordering
LT
compare Literal
_ LitFloat{} = Ordering
GT
compare LitString{} Literal
_ = Ordering
LT
compare Literal
_ LitString{} = Ordering
GT
compare LitChar{} Literal
_ = Ordering
LT
compare Literal
_ LitChar{} = Ordering
GT
compare LitQName{} Literal
_ = Ordering
LT
compare Literal
_ LitQName{} = Ordering
GT
instance KillRange Literal where
killRange :: Literal -> Literal
killRange (LitNat Integer
x) = Integer -> Literal
LitNat Integer
x
killRange (LitWord64 Word64
x) = Word64 -> Literal
LitWord64 Word64
x
killRange (LitFloat Double
x) = Double -> Literal
LitFloat Double
x
killRange (LitString Text
x) = Text -> Literal
LitString Text
x
killRange (LitChar Char
x) = Char -> Literal
LitChar Char
x
killRange (LitQName QName
x) = forall a b. KillRange a => (a -> b) -> a -> b
killRange1 QName -> Literal
LitQName QName
x
killRange (LitMeta TopLevelModuleName
m MetaId
x) = TopLevelModuleName -> MetaId -> Literal
LitMeta (forall a. KillRange a => KillRangeT a
killRange TopLevelModuleName
m) MetaId
x
instance NFData Literal where
rnf :: Literal -> ()
rnf (LitNat Integer
_ ) = ()
rnf (LitWord64 Word64
_ ) = ()
rnf (LitFloat Double
_ ) = ()
rnf (LitString Text
_ ) = ()
rnf (LitChar Char
_ ) = ()
rnf (LitQName QName
a ) = forall a. NFData a => a -> ()
rnf QName
a
rnf (LitMeta TopLevelModuleName
m MetaId
_ ) = forall a. NFData a => a -> ()
rnf TopLevelModuleName
m