{-# LANGUAGE CPP          #-}
{-# LANGUAGE ViewPatterns #-}
module Ide.Plugin.Literals (
    collectLiterals
    , Literal(..)
    , getSrcText
    , getSrcSpan
) where

import           Data.Maybe                    (maybeToList)
import           Data.Text                     (Text)
import qualified Data.Text                     as T
#if __GLASGOW_HASKELL__ >= 908
import qualified Data.Text.Encoding            as T
#endif
import           Development.IDE.GHC.Compat    hiding (getSrcSpan)
import           Development.IDE.Graph.Classes (NFData (rnf))
import           Generics.SYB                  (Data, everything, extQ)
import qualified GHC.Generics                  as GHC

-- data type to capture what type of literal we are dealing with
-- provides location and possibly source text (for OverLits) as well as it's value
-- we currently don't have any use for PrimLiterals. They never have source text so we always drop them
-- | Captures a Numeric Literals Location, Source Text, and Value.
data Literal = IntLiteral  LiteralSrcSpan Text Integer
             | FracLiteral LiteralSrcSpan Text Rational
             deriving ((forall x. Literal -> Rep Literal x)
-> (forall x. Rep Literal x -> Literal) -> Generic Literal
forall x. Rep Literal x -> Literal
forall x. Literal -> Rep Literal x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Literal -> Rep Literal x
from :: forall x. Literal -> Rep Literal x
$cto :: forall x. Rep Literal x -> Literal
to :: forall x. Rep Literal x -> Literal
GHC.Generic, Int -> Literal -> ShowS
[Literal] -> ShowS
Literal -> String
(Int -> Literal -> ShowS)
-> (Literal -> String) -> ([Literal] -> ShowS) -> Show Literal
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Literal -> ShowS
showsPrec :: Int -> Literal -> ShowS
$cshow :: Literal -> String
show :: Literal -> String
$cshowList :: [Literal] -> ShowS
showList :: [Literal] -> ShowS
Show, Eq Literal
Eq Literal =>
(Literal -> Literal -> Ordering)
-> (Literal -> Literal -> Bool)
-> (Literal -> Literal -> Bool)
-> (Literal -> Literal -> Bool)
-> (Literal -> Literal -> Bool)
-> (Literal -> Literal -> Literal)
-> (Literal -> Literal -> Literal)
-> Ord Literal
Literal -> Literal -> Bool
Literal -> Literal -> Ordering
Literal -> Literal -> Literal
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
$ccompare :: Literal -> Literal -> Ordering
compare :: Literal -> Literal -> Ordering
$c< :: Literal -> Literal -> Bool
< :: Literal -> Literal -> Bool
$c<= :: Literal -> Literal -> Bool
<= :: Literal -> Literal -> Bool
$c> :: Literal -> Literal -> Bool
> :: Literal -> Literal -> Bool
$c>= :: Literal -> Literal -> Bool
>= :: Literal -> Literal -> Bool
$cmax :: Literal -> Literal -> Literal
max :: Literal -> Literal -> Literal
$cmin :: Literal -> Literal -> Literal
min :: Literal -> Literal -> Literal
Ord, Literal -> Literal -> Bool
(Literal -> Literal -> Bool)
-> (Literal -> Literal -> Bool) -> Eq Literal
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Literal -> Literal -> Bool
== :: Literal -> Literal -> Bool
$c/= :: Literal -> Literal -> Bool
/= :: Literal -> Literal -> Bool
Eq, Typeable Literal
Typeable Literal =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> Literal -> c Literal)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c Literal)
-> (Literal -> Constr)
-> (Literal -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c Literal))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Literal))
-> ((forall b. Data b => b -> b) -> Literal -> Literal)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> Literal -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> Literal -> r)
-> (forall u. (forall d. Data d => d -> u) -> Literal -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> Literal -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> Literal -> m Literal)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Literal -> m Literal)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Literal -> m Literal)
-> Data Literal
Literal -> Constr
Literal -> DataType
(forall b. Data b => b -> b) -> Literal -> Literal
forall a.
Typeable a =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> Literal -> u
forall u. (forall d. Data d => d -> u) -> Literal -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Literal -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Literal -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Literal -> m Literal
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Literal -> m Literal
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Literal
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Literal -> c Literal
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Literal)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Literal)
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Literal -> c Literal
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Literal -> c Literal
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Literal
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Literal
$ctoConstr :: Literal -> Constr
toConstr :: Literal -> Constr
$cdataTypeOf :: Literal -> DataType
dataTypeOf :: Literal -> DataType
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Literal)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Literal)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Literal)
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Literal)
$cgmapT :: (forall b. Data b => b -> b) -> Literal -> Literal
gmapT :: (forall b. Data b => b -> b) -> Literal -> Literal
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Literal -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Literal -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Literal -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Literal -> r
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> Literal -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> Literal -> [u]
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Literal -> u
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Literal -> u
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Literal -> m Literal
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Literal -> m Literal
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Literal -> m Literal
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Literal -> m Literal
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Literal -> m Literal
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Literal -> m Literal
Data)

newtype LiteralSrcSpan = LiteralSrcSpan { LiteralSrcSpan -> RealSrcSpan
unLit :: RealSrcSpan }
                        deriving ((forall x. LiteralSrcSpan -> Rep LiteralSrcSpan x)
-> (forall x. Rep LiteralSrcSpan x -> LiteralSrcSpan)
-> Generic LiteralSrcSpan
forall x. Rep LiteralSrcSpan x -> LiteralSrcSpan
forall x. LiteralSrcSpan -> Rep LiteralSrcSpan x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. LiteralSrcSpan -> Rep LiteralSrcSpan x
from :: forall x. LiteralSrcSpan -> Rep LiteralSrcSpan x
$cto :: forall x. Rep LiteralSrcSpan x -> LiteralSrcSpan
to :: forall x. Rep LiteralSrcSpan x -> LiteralSrcSpan
GHC.Generic, Int -> LiteralSrcSpan -> ShowS
[LiteralSrcSpan] -> ShowS
LiteralSrcSpan -> String
(Int -> LiteralSrcSpan -> ShowS)
-> (LiteralSrcSpan -> String)
-> ([LiteralSrcSpan] -> ShowS)
-> Show LiteralSrcSpan
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> LiteralSrcSpan -> ShowS
showsPrec :: Int -> LiteralSrcSpan -> ShowS
$cshow :: LiteralSrcSpan -> String
show :: LiteralSrcSpan -> String
$cshowList :: [LiteralSrcSpan] -> ShowS
showList :: [LiteralSrcSpan] -> ShowS
Show, Eq LiteralSrcSpan
Eq LiteralSrcSpan =>
(LiteralSrcSpan -> LiteralSrcSpan -> Ordering)
-> (LiteralSrcSpan -> LiteralSrcSpan -> Bool)
-> (LiteralSrcSpan -> LiteralSrcSpan -> Bool)
-> (LiteralSrcSpan -> LiteralSrcSpan -> Bool)
-> (LiteralSrcSpan -> LiteralSrcSpan -> Bool)
-> (LiteralSrcSpan -> LiteralSrcSpan -> LiteralSrcSpan)
-> (LiteralSrcSpan -> LiteralSrcSpan -> LiteralSrcSpan)
-> Ord LiteralSrcSpan
LiteralSrcSpan -> LiteralSrcSpan -> Bool
LiteralSrcSpan -> LiteralSrcSpan -> Ordering
LiteralSrcSpan -> LiteralSrcSpan -> LiteralSrcSpan
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
$ccompare :: LiteralSrcSpan -> LiteralSrcSpan -> Ordering
compare :: LiteralSrcSpan -> LiteralSrcSpan -> Ordering
$c< :: LiteralSrcSpan -> LiteralSrcSpan -> Bool
< :: LiteralSrcSpan -> LiteralSrcSpan -> Bool
$c<= :: LiteralSrcSpan -> LiteralSrcSpan -> Bool
<= :: LiteralSrcSpan -> LiteralSrcSpan -> Bool
$c> :: LiteralSrcSpan -> LiteralSrcSpan -> Bool
> :: LiteralSrcSpan -> LiteralSrcSpan -> Bool
$c>= :: LiteralSrcSpan -> LiteralSrcSpan -> Bool
>= :: LiteralSrcSpan -> LiteralSrcSpan -> Bool
$cmax :: LiteralSrcSpan -> LiteralSrcSpan -> LiteralSrcSpan
max :: LiteralSrcSpan -> LiteralSrcSpan -> LiteralSrcSpan
$cmin :: LiteralSrcSpan -> LiteralSrcSpan -> LiteralSrcSpan
min :: LiteralSrcSpan -> LiteralSrcSpan -> LiteralSrcSpan
Ord, LiteralSrcSpan -> LiteralSrcSpan -> Bool
(LiteralSrcSpan -> LiteralSrcSpan -> Bool)
-> (LiteralSrcSpan -> LiteralSrcSpan -> Bool) -> Eq LiteralSrcSpan
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: LiteralSrcSpan -> LiteralSrcSpan -> Bool
== :: LiteralSrcSpan -> LiteralSrcSpan -> Bool
$c/= :: LiteralSrcSpan -> LiteralSrcSpan -> Bool
/= :: LiteralSrcSpan -> LiteralSrcSpan -> Bool
Eq, Typeable LiteralSrcSpan
Typeable LiteralSrcSpan =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> LiteralSrcSpan -> c LiteralSrcSpan)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c LiteralSrcSpan)
-> (LiteralSrcSpan -> Constr)
-> (LiteralSrcSpan -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c LiteralSrcSpan))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c LiteralSrcSpan))
-> ((forall b. Data b => b -> b)
    -> LiteralSrcSpan -> LiteralSrcSpan)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> LiteralSrcSpan -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> LiteralSrcSpan -> r)
-> (forall u.
    (forall d. Data d => d -> u) -> LiteralSrcSpan -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> LiteralSrcSpan -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> LiteralSrcSpan -> m LiteralSrcSpan)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> LiteralSrcSpan -> m LiteralSrcSpan)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> LiteralSrcSpan -> m LiteralSrcSpan)
-> Data LiteralSrcSpan
LiteralSrcSpan -> Constr
LiteralSrcSpan -> DataType
(forall b. Data b => b -> b) -> LiteralSrcSpan -> LiteralSrcSpan
forall a.
Typeable a =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u.
Int -> (forall d. Data d => d -> u) -> LiteralSrcSpan -> u
forall u. (forall d. Data d => d -> u) -> LiteralSrcSpan -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> LiteralSrcSpan -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> LiteralSrcSpan -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> LiteralSrcSpan -> m LiteralSrcSpan
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> LiteralSrcSpan -> m LiteralSrcSpan
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c LiteralSrcSpan
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> LiteralSrcSpan -> c LiteralSrcSpan
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c LiteralSrcSpan)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c LiteralSrcSpan)
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> LiteralSrcSpan -> c LiteralSrcSpan
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> LiteralSrcSpan -> c LiteralSrcSpan
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c LiteralSrcSpan
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c LiteralSrcSpan
$ctoConstr :: LiteralSrcSpan -> Constr
toConstr :: LiteralSrcSpan -> Constr
$cdataTypeOf :: LiteralSrcSpan -> DataType
dataTypeOf :: LiteralSrcSpan -> DataType
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c LiteralSrcSpan)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c LiteralSrcSpan)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c LiteralSrcSpan)
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c LiteralSrcSpan)
$cgmapT :: (forall b. Data b => b -> b) -> LiteralSrcSpan -> LiteralSrcSpan
gmapT :: (forall b. Data b => b -> b) -> LiteralSrcSpan -> LiteralSrcSpan
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> LiteralSrcSpan -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> LiteralSrcSpan -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> LiteralSrcSpan -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> LiteralSrcSpan -> r
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> LiteralSrcSpan -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> LiteralSrcSpan -> [u]
$cgmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> LiteralSrcSpan -> u
gmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> LiteralSrcSpan -> u
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> LiteralSrcSpan -> m LiteralSrcSpan
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> LiteralSrcSpan -> m LiteralSrcSpan
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> LiteralSrcSpan -> m LiteralSrcSpan
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> LiteralSrcSpan -> m LiteralSrcSpan
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> LiteralSrcSpan -> m LiteralSrcSpan
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> LiteralSrcSpan -> m LiteralSrcSpan
Data)

instance NFData LiteralSrcSpan where
    rnf :: LiteralSrcSpan -> ()
rnf LiteralSrcSpan
x = LiteralSrcSpan
x LiteralSrcSpan -> () -> ()
forall a b. a -> b -> b
`seq` ()

instance NFData Literal


-- | Return a Literal's Source representation
getSrcText :: Literal -> Text
getSrcText :: Literal -> Text
getSrcText = \case
  IntLiteral LiteralSrcSpan
_ Text
txt Integer
_  -> Text
txt
  FracLiteral LiteralSrcSpan
_ Text
txt Rational
_ -> Text
txt

-- | Return a Literal's Real Source location
getSrcSpan :: Literal -> RealSrcSpan
getSrcSpan :: Literal -> RealSrcSpan
getSrcSpan = \case
    IntLiteral LiteralSrcSpan
ss Text
_ Integer
_  -> LiteralSrcSpan -> RealSrcSpan
unLit LiteralSrcSpan
ss
    FracLiteral LiteralSrcSpan
ss Text
_ Rational
_ -> LiteralSrcSpan -> RealSrcSpan
unLit LiteralSrcSpan
ss

-- | Find all literals in a Parsed Source File
collectLiterals :: Data ast => ast -> [Literal]
collectLiterals :: forall ast. Data ast => ast -> [Literal]
collectLiterals = ([Literal] -> [Literal] -> [Literal])
-> (forall ast. Data ast => ast -> [Literal])
-> forall ast. Data ast => ast -> [Literal]
forall r. (r -> r -> r) -> GenericQ r -> GenericQ r
everything [Literal] -> [Literal] -> [Literal]
forall a. Semigroup a => a -> a -> a
(<>) (Maybe Literal -> [Literal]
forall a. Maybe a -> [a]
maybeToList (Maybe Literal -> [Literal])
-> (a -> Maybe Literal) -> a -> [Literal]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe Literal -> a -> Maybe Literal
forall a b. a -> b -> a
const Maybe Literal
forall a. Maybe a
Nothing (a -> Maybe Literal)
-> (GenLocated SrcSpanAnnA (HsExpr GhcPs) -> Maybe Literal)
-> a
-> Maybe Literal
forall a b r.
(Typeable a, Typeable b) =>
(a -> r) -> (b -> r) -> a -> r
`extQ` LHsExpr GhcPs -> Maybe Literal
GenLocated SrcSpanAnnA (HsExpr GhcPs) -> Maybe Literal
getLiteral (a -> Maybe Literal)
-> (GenLocated SrcSpanAnnA (Pat GhcPs) -> Maybe Literal)
-> a
-> Maybe Literal
forall a b r.
(Typeable a, Typeable b) =>
(a -> r) -> (b -> r) -> a -> r
`extQ` LPat GhcPs -> Maybe Literal
GenLocated SrcSpanAnnA (Pat GhcPs) -> Maybe Literal
getPattern))


-- | Translate from HsLit and HsOverLit Types to our Literal Type
getLiteral :: LHsExpr GhcPs -> Maybe Literal
getLiteral :: LHsExpr GhcPs -> Maybe Literal
getLiteral (L (SrcSpanAnnA -> SrcSpan
forall a. SrcSpanAnn' a -> SrcSpan
locA -> (RealSrcSpan RealSrcSpan
sSpan Maybe BufSpan
_)) HsExpr GhcPs
expr) = case HsExpr GhcPs
expr of
    HsLit XLitE GhcPs
_ HsLit GhcPs
lit         -> HsLit GhcPs -> RealSrcSpan -> Maybe Literal
forall p. HsLit p -> RealSrcSpan -> Maybe Literal
fromLit HsLit GhcPs
lit RealSrcSpan
sSpan
    HsOverLit XOverLitE GhcPs
_ HsOverLit GhcPs
overLit -> HsOverLit GhcPs -> RealSrcSpan -> Maybe Literal
forall p. HsOverLit p -> RealSrcSpan -> Maybe Literal
fromOverLit HsOverLit GhcPs
overLit RealSrcSpan
sSpan
    HsExpr GhcPs
_                   -> Maybe Literal
forall a. Maybe a
Nothing
getLiteral LHsExpr GhcPs
_ = Maybe Literal
forall a. Maybe a
Nothing

-- | Destructure Patterns to unwrap any Literals
getPattern :: LPat GhcPs -> Maybe Literal
getPattern :: LPat GhcPs -> Maybe Literal
getPattern (L (SrcSpanAnnA -> SrcSpan
forall a. SrcSpanAnn' a -> SrcSpan
locA -> (RealSrcSpan RealSrcSpan
patSpan Maybe BufSpan
_)) Pat GhcPs
pat) = case Pat GhcPs
pat of
    LitPat XLitPat GhcPs
_ HsLit GhcPs
lit -> case HsLit GhcPs
lit of
        HsInt XHsInt GhcPs
_ IntegralLit
val   -> RealSrcSpan -> IntegralLit -> Maybe Literal
fromIntegralLit RealSrcSpan
patSpan IntegralLit
val
        HsRat XHsRat GhcPs
_ FractionalLit
val Type
_ -> RealSrcSpan -> FractionalLit -> Maybe Literal
fromFractionalLit RealSrcSpan
patSpan FractionalLit
val
        HsLit GhcPs
_             -> Maybe Literal
forall a. Maybe a
Nothing
#if __GLASGOW_HASKELL__ == 902
    NPat _ (L (RealSrcSpan sSpan _) overLit) _ _ -> fromOverLit overLit sSpan
    NPlusKPat _ _ (L (RealSrcSpan sSpan _) overLit1) _ _ _ -> fromOverLit overLit1 sSpan
#else
    NPat XNPat GhcPs
_ (L (SrcSpanAnn' (EpAnn NoEpAnns) -> SrcSpan
forall a. SrcSpanAnn' a -> SrcSpan
locA -> (RealSrcSpan RealSrcSpan
sSpan Maybe BufSpan
_)) HsOverLit GhcPs
overLit) Maybe (SyntaxExpr GhcPs)
_ SyntaxExpr GhcPs
_ -> HsOverLit GhcPs -> RealSrcSpan -> Maybe Literal
forall p. HsOverLit p -> RealSrcSpan -> Maybe Literal
fromOverLit HsOverLit GhcPs
overLit RealSrcSpan
sSpan
    NPlusKPat XNPlusKPat GhcPs
_ LIdP GhcPs
_ (L (SrcSpanAnn' (EpAnn NoEpAnns) -> SrcSpan
forall a. SrcSpanAnn' a -> SrcSpan
locA -> (RealSrcSpan RealSrcSpan
sSpan Maybe BufSpan
_)) HsOverLit GhcPs
overLit1) HsOverLit GhcPs
_ SyntaxExpr GhcPs
_ SyntaxExpr GhcPs
_ -> HsOverLit GhcPs -> RealSrcSpan -> Maybe Literal
forall p. HsOverLit p -> RealSrcSpan -> Maybe Literal
fromOverLit HsOverLit GhcPs
overLit1 RealSrcSpan
sSpan
#endif
    Pat GhcPs
_ -> Maybe Literal
forall a. Maybe a
Nothing
getPattern LPat GhcPs
_ = Maybe Literal
forall a. Maybe a
Nothing

fromLit :: HsLit p -> RealSrcSpan -> Maybe Literal
fromLit :: forall p. HsLit p -> RealSrcSpan -> Maybe Literal
fromLit HsLit p
lit RealSrcSpan
sSpan = case HsLit p
lit of
        HsInt XHsInt p
_ IntegralLit
val   -> RealSrcSpan -> IntegralLit -> Maybe Literal
fromIntegralLit RealSrcSpan
sSpan IntegralLit
val
        HsRat XHsRat p
_ FractionalLit
val Type
_ -> RealSrcSpan -> FractionalLit -> Maybe Literal
fromFractionalLit RealSrcSpan
sSpan FractionalLit
val
        HsLit p
_             -> Maybe Literal
forall a. Maybe a
Nothing

fromOverLit :: HsOverLit p -> RealSrcSpan -> Maybe Literal
fromOverLit :: forall p. HsOverLit p -> RealSrcSpan -> Maybe Literal
fromOverLit OverLit{XOverLit p
OverLitVal
ol_ext :: XOverLit p
ol_val :: OverLitVal
ol_ext :: forall p. HsOverLit p -> XOverLit p
ol_val :: forall p. HsOverLit p -> OverLitVal
..} RealSrcSpan
sSpan = case OverLitVal
ol_val of
        HsIntegral IntegralLit
il   -> RealSrcSpan -> IntegralLit -> Maybe Literal
fromIntegralLit RealSrcSpan
sSpan IntegralLit
il
        HsFractional FractionalLit
fl -> RealSrcSpan -> FractionalLit -> Maybe Literal
fromFractionalLit RealSrcSpan
sSpan FractionalLit
fl
        OverLitVal
_               -> Maybe Literal
forall a. Maybe a
Nothing
fromOverLit HsOverLit p
_ RealSrcSpan
_ = Maybe Literal
forall a. Maybe a
Nothing

fromIntegralLit :: RealSrcSpan -> IntegralLit -> Maybe Literal
fromIntegralLit :: RealSrcSpan -> IntegralLit -> Maybe Literal
fromIntegralLit RealSrcSpan
s IL{Bool
Integer
SourceText
il_text :: SourceText
il_neg :: Bool
il_value :: Integer
il_text :: IntegralLit -> SourceText
il_neg :: IntegralLit -> Bool
il_value :: IntegralLit -> Integer
..} = (Text -> Literal) -> Maybe Text -> Maybe Literal
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Text
txt' -> LiteralSrcSpan -> Text -> Integer -> Literal
IntLiteral (RealSrcSpan -> LiteralSrcSpan
LiteralSrcSpan RealSrcSpan
s) Text
txt' Integer
il_value) (SourceText -> Maybe Text
fromSourceText SourceText
il_text)

fromFractionalLit  :: RealSrcSpan -> FractionalLit -> Maybe Literal
fromFractionalLit :: RealSrcSpan -> FractionalLit -> Maybe Literal
fromFractionalLit RealSrcSpan
s fl :: FractionalLit
fl@FL{SourceText
fl_text :: SourceText
fl_text :: FractionalLit -> SourceText
fl_text} = (Text -> Literal) -> Maybe Text -> Maybe Literal
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Text
txt' -> LiteralSrcSpan -> Text -> Rational -> Literal
FracLiteral (RealSrcSpan -> LiteralSrcSpan
LiteralSrcSpan RealSrcSpan
s) Text
txt' (FractionalLit -> Rational
rationalFromFractionalLit FractionalLit
fl)) (SourceText -> Maybe Text
fromSourceText SourceText
fl_text)

fromSourceText :: SourceText -> Maybe Text
fromSourceText :: SourceText -> Maybe Text
fromSourceText = \case
#if __GLASGOW_HASKELL__ >= 908
  SourceText s -> Just $ T.decodeUtf8 $ bytesFS s
#else
  SourceText String
s -> Text -> Maybe Text
forall a. a -> Maybe a
Just (Text -> Maybe Text) -> Text -> Maybe Text
forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack String
s
#endif
  SourceText
NoSourceText -> Maybe Text
forall a. Maybe a
Nothing