{-# OPTIONS_HADDOCK not-home #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
module Hedgehog.Internal.Source (
    LineNo(..)
  , ColumnNo(..)
  , Span(..)
  , getCaller

  -- * Re-exports from "GHC.Stack"
  , CallStack
  , HasCallStack
  , callStack
  , withFrozenCallStack
  ) where

import GHC.Stack (CallStack, HasCallStack, SrcLoc(..))
import GHC.Stack (callStack, getCallStack, withFrozenCallStack)


newtype LineNo =
  LineNo {
      LineNo -> Int
unLineNo :: Int
    } deriving (LineNo -> LineNo -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: LineNo -> LineNo -> Bool
$c/= :: LineNo -> LineNo -> Bool
== :: LineNo -> LineNo -> Bool
$c== :: LineNo -> LineNo -> Bool
Eq, Eq LineNo
LineNo -> LineNo -> Bool
LineNo -> LineNo -> Ordering
LineNo -> LineNo -> LineNo
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
min :: LineNo -> LineNo -> LineNo
$cmin :: LineNo -> LineNo -> LineNo
max :: LineNo -> LineNo -> LineNo
$cmax :: LineNo -> LineNo -> LineNo
>= :: LineNo -> LineNo -> Bool
$c>= :: LineNo -> LineNo -> Bool
> :: LineNo -> LineNo -> Bool
$c> :: LineNo -> LineNo -> Bool
<= :: LineNo -> LineNo -> Bool
$c<= :: LineNo -> LineNo -> Bool
< :: LineNo -> LineNo -> Bool
$c< :: LineNo -> LineNo -> Bool
compare :: LineNo -> LineNo -> Ordering
$ccompare :: LineNo -> LineNo -> Ordering
Ord, Integer -> LineNo
LineNo -> LineNo
LineNo -> LineNo -> LineNo
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
fromInteger :: Integer -> LineNo
$cfromInteger :: Integer -> LineNo
signum :: LineNo -> LineNo
$csignum :: LineNo -> LineNo
abs :: LineNo -> LineNo
$cabs :: LineNo -> LineNo
negate :: LineNo -> LineNo
$cnegate :: LineNo -> LineNo
* :: LineNo -> LineNo -> LineNo
$c* :: LineNo -> LineNo -> LineNo
- :: LineNo -> LineNo -> LineNo
$c- :: LineNo -> LineNo -> LineNo
+ :: LineNo -> LineNo -> LineNo
$c+ :: LineNo -> LineNo -> LineNo
Num, Int -> LineNo
LineNo -> Int
LineNo -> [LineNo]
LineNo -> LineNo
LineNo -> LineNo -> [LineNo]
LineNo -> LineNo -> LineNo -> [LineNo]
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: LineNo -> LineNo -> LineNo -> [LineNo]
$cenumFromThenTo :: LineNo -> LineNo -> LineNo -> [LineNo]
enumFromTo :: LineNo -> LineNo -> [LineNo]
$cenumFromTo :: LineNo -> LineNo -> [LineNo]
enumFromThen :: LineNo -> LineNo -> [LineNo]
$cenumFromThen :: LineNo -> LineNo -> [LineNo]
enumFrom :: LineNo -> [LineNo]
$cenumFrom :: LineNo -> [LineNo]
fromEnum :: LineNo -> Int
$cfromEnum :: LineNo -> Int
toEnum :: Int -> LineNo
$ctoEnum :: Int -> LineNo
pred :: LineNo -> LineNo
$cpred :: LineNo -> LineNo
succ :: LineNo -> LineNo
$csucc :: LineNo -> LineNo
Enum, Num LineNo
Ord LineNo
LineNo -> Rational
forall a. Num a -> Ord a -> (a -> Rational) -> Real a
toRational :: LineNo -> Rational
$ctoRational :: LineNo -> Rational
Real, Enum LineNo
Real LineNo
LineNo -> Integer
LineNo -> LineNo -> (LineNo, LineNo)
LineNo -> LineNo -> LineNo
forall a.
Real a
-> Enum a
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> (a, a))
-> (a -> a -> (a, a))
-> (a -> Integer)
-> Integral a
toInteger :: LineNo -> Integer
$ctoInteger :: LineNo -> Integer
divMod :: LineNo -> LineNo -> (LineNo, LineNo)
$cdivMod :: LineNo -> LineNo -> (LineNo, LineNo)
quotRem :: LineNo -> LineNo -> (LineNo, LineNo)
$cquotRem :: LineNo -> LineNo -> (LineNo, LineNo)
mod :: LineNo -> LineNo -> LineNo
$cmod :: LineNo -> LineNo -> LineNo
div :: LineNo -> LineNo -> LineNo
$cdiv :: LineNo -> LineNo -> LineNo
rem :: LineNo -> LineNo -> LineNo
$crem :: LineNo -> LineNo -> LineNo
quot :: LineNo -> LineNo -> LineNo
$cquot :: LineNo -> LineNo -> LineNo
Integral)

newtype ColumnNo =
  ColumnNo {
      ColumnNo -> Int
unColumnNo :: Int
    } deriving (ColumnNo -> ColumnNo -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ColumnNo -> ColumnNo -> Bool
$c/= :: ColumnNo -> ColumnNo -> Bool
== :: ColumnNo -> ColumnNo -> Bool
$c== :: ColumnNo -> ColumnNo -> Bool
Eq, Eq ColumnNo
ColumnNo -> ColumnNo -> Bool
ColumnNo -> ColumnNo -> Ordering
ColumnNo -> ColumnNo -> ColumnNo
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
min :: ColumnNo -> ColumnNo -> ColumnNo
$cmin :: ColumnNo -> ColumnNo -> ColumnNo
max :: ColumnNo -> ColumnNo -> ColumnNo
$cmax :: ColumnNo -> ColumnNo -> ColumnNo
>= :: ColumnNo -> ColumnNo -> Bool
$c>= :: ColumnNo -> ColumnNo -> Bool
> :: ColumnNo -> ColumnNo -> Bool
$c> :: ColumnNo -> ColumnNo -> Bool
<= :: ColumnNo -> ColumnNo -> Bool
$c<= :: ColumnNo -> ColumnNo -> Bool
< :: ColumnNo -> ColumnNo -> Bool
$c< :: ColumnNo -> ColumnNo -> Bool
compare :: ColumnNo -> ColumnNo -> Ordering
$ccompare :: ColumnNo -> ColumnNo -> Ordering
Ord, Integer -> ColumnNo
ColumnNo -> ColumnNo
ColumnNo -> ColumnNo -> ColumnNo
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
fromInteger :: Integer -> ColumnNo
$cfromInteger :: Integer -> ColumnNo
signum :: ColumnNo -> ColumnNo
$csignum :: ColumnNo -> ColumnNo
abs :: ColumnNo -> ColumnNo
$cabs :: ColumnNo -> ColumnNo
negate :: ColumnNo -> ColumnNo
$cnegate :: ColumnNo -> ColumnNo
* :: ColumnNo -> ColumnNo -> ColumnNo
$c* :: ColumnNo -> ColumnNo -> ColumnNo
- :: ColumnNo -> ColumnNo -> ColumnNo
$c- :: ColumnNo -> ColumnNo -> ColumnNo
+ :: ColumnNo -> ColumnNo -> ColumnNo
$c+ :: ColumnNo -> ColumnNo -> ColumnNo
Num, Int -> ColumnNo
ColumnNo -> Int
ColumnNo -> [ColumnNo]
ColumnNo -> ColumnNo
ColumnNo -> ColumnNo -> [ColumnNo]
ColumnNo -> ColumnNo -> ColumnNo -> [ColumnNo]
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: ColumnNo -> ColumnNo -> ColumnNo -> [ColumnNo]
$cenumFromThenTo :: ColumnNo -> ColumnNo -> ColumnNo -> [ColumnNo]
enumFromTo :: ColumnNo -> ColumnNo -> [ColumnNo]
$cenumFromTo :: ColumnNo -> ColumnNo -> [ColumnNo]
enumFromThen :: ColumnNo -> ColumnNo -> [ColumnNo]
$cenumFromThen :: ColumnNo -> ColumnNo -> [ColumnNo]
enumFrom :: ColumnNo -> [ColumnNo]
$cenumFrom :: ColumnNo -> [ColumnNo]
fromEnum :: ColumnNo -> Int
$cfromEnum :: ColumnNo -> Int
toEnum :: Int -> ColumnNo
$ctoEnum :: Int -> ColumnNo
pred :: ColumnNo -> ColumnNo
$cpred :: ColumnNo -> ColumnNo
succ :: ColumnNo -> ColumnNo
$csucc :: ColumnNo -> ColumnNo
Enum, Num ColumnNo
Ord ColumnNo
ColumnNo -> Rational
forall a. Num a -> Ord a -> (a -> Rational) -> Real a
toRational :: ColumnNo -> Rational
$ctoRational :: ColumnNo -> Rational
Real, Enum ColumnNo
Real ColumnNo
ColumnNo -> Integer
ColumnNo -> ColumnNo -> (ColumnNo, ColumnNo)
ColumnNo -> ColumnNo -> ColumnNo
forall a.
Real a
-> Enum a
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> (a, a))
-> (a -> a -> (a, a))
-> (a -> Integer)
-> Integral a
toInteger :: ColumnNo -> Integer
$ctoInteger :: ColumnNo -> Integer
divMod :: ColumnNo -> ColumnNo -> (ColumnNo, ColumnNo)
$cdivMod :: ColumnNo -> ColumnNo -> (ColumnNo, ColumnNo)
quotRem :: ColumnNo -> ColumnNo -> (ColumnNo, ColumnNo)
$cquotRem :: ColumnNo -> ColumnNo -> (ColumnNo, ColumnNo)
mod :: ColumnNo -> ColumnNo -> ColumnNo
$cmod :: ColumnNo -> ColumnNo -> ColumnNo
div :: ColumnNo -> ColumnNo -> ColumnNo
$cdiv :: ColumnNo -> ColumnNo -> ColumnNo
rem :: ColumnNo -> ColumnNo -> ColumnNo
$crem :: ColumnNo -> ColumnNo -> ColumnNo
quot :: ColumnNo -> ColumnNo -> ColumnNo
$cquot :: ColumnNo -> ColumnNo -> ColumnNo
Integral)

data Span =
  Span {
      Span -> FilePath
spanFile :: !FilePath
    , Span -> LineNo
spanStartLine :: !LineNo
    , Span -> ColumnNo
spanStartColumn :: !ColumnNo
    , Span -> LineNo
spanEndLine :: !LineNo
    , Span -> ColumnNo
spanEndColumn :: !ColumnNo
    } deriving (Span -> Span -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Span -> Span -> Bool
$c/= :: Span -> Span -> Bool
== :: Span -> Span -> Bool
$c== :: Span -> Span -> Bool
Eq, Eq Span
Span -> Span -> Bool
Span -> Span -> Ordering
Span -> Span -> Span
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
min :: Span -> Span -> Span
$cmin :: Span -> Span -> Span
max :: Span -> Span -> Span
$cmax :: Span -> Span -> Span
>= :: Span -> Span -> Bool
$c>= :: Span -> Span -> Bool
> :: Span -> Span -> Bool
$c> :: Span -> Span -> Bool
<= :: Span -> Span -> Bool
$c<= :: Span -> Span -> Bool
< :: Span -> Span -> Bool
$c< :: Span -> Span -> Bool
compare :: Span -> Span -> Ordering
$ccompare :: Span -> Span -> Ordering
Ord)

getCaller :: CallStack -> Maybe Span
getCaller :: CallStack -> Maybe Span
getCaller CallStack
stack =
  case CallStack -> [(FilePath, SrcLoc)]
getCallStack CallStack
stack of
    [] ->
      forall a. Maybe a
Nothing
    (FilePath
_, SrcLoc
x) : [(FilePath, SrcLoc)]
_ ->
      forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ FilePath -> LineNo -> ColumnNo -> LineNo -> ColumnNo -> Span
Span
        (SrcLoc -> FilePath
srcLocFile SrcLoc
x)
        (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ SrcLoc -> Int
srcLocStartLine SrcLoc
x)
        (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ SrcLoc -> Int
srcLocStartCol SrcLoc
x)
        (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ SrcLoc -> Int
srcLocEndLine SrcLoc
x)
        (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ SrcLoc -> Int
srcLocEndCol SrcLoc
x)

------------------------------------------------------------------------
-- Show instances

instance Show Span where
  showsPrec :: Int -> Span -> ShowS
showsPrec Int
p (Span FilePath
file LineNo
sl ColumnNo
sc LineNo
el ColumnNo
ec) =
    Bool -> ShowS -> ShowS
showParen (Int
p forall a. Ord a => a -> a -> Bool
> Int
10) forall a b. (a -> b) -> a -> b
$
      FilePath -> ShowS
showString FilePath
"Span " forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 FilePath
file forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      Char -> ShowS
showChar Char
' ' forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 LineNo
sl forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      Char -> ShowS
showChar Char
' ' forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 ColumnNo
sc forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      Char -> ShowS
showChar Char
' ' forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 LineNo
el forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      Char -> ShowS
showChar Char
' ' forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 ColumnNo
ec

instance Show LineNo where
  showsPrec :: Int -> LineNo -> ShowS
showsPrec Int
p (LineNo Int
x) =
    Bool -> ShowS -> ShowS
showParen (Int
p forall a. Ord a => a -> a -> Bool
> Int
10) forall a b. (a -> b) -> a -> b
$
      FilePath -> ShowS
showString FilePath
"LineNo " forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 Int
x

instance Show ColumnNo where
  showsPrec :: Int -> ColumnNo -> ShowS
showsPrec Int
p (ColumnNo Int
x) =
    Bool -> ShowS -> ShowS
showParen (Int
p forall a. Ord a => a -> a -> Bool
> Int
10) forall a b. (a -> b) -> a -> b
$
      FilePath -> ShowS
showString FilePath
"ColumnNo " forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 Int
x