{-# OPTIONS_GHC -Wunused-imports #-}
module Agda.Utils.CallStack.Base (
SrcLocPackage
, SrcLocModule
, SrcFun
, SrcLocFile
, SrcLocLine
, SrcLocCol
, CallSite
, CallSiteFilter
, prettySrcLoc
, prettyCallSite
, prettyCallStack
, filterCallStack
, headCallSite
, overCallSites
, popnCallStack
, truncatedCallStack
, withCallerCallStack
, withCurrentCallStack
, withNBackCallStack
, CallStack
, callStack
, fromCallSiteList
, getCallStack
, HasCallStack
, SrcLoc(..)
)
where
import Data.List ( intercalate )
import Data.Maybe ( listToMaybe )
import GHC.Stack
( callStack
, CallStack
, emptyCallStack
, fromCallSiteList
, getCallStack
, HasCallStack
, popCallStack
, prettySrcLoc
, SrcLoc(..)
)
type SrcLocPackage = String
type SrcLocModule = String
type SrcFun = String
type SrcLocFile = String
type SrcLocLine = Int
type SrcLocCol = Int
type CallSite = (SrcFun, SrcLoc)
type CallSiteFilter = CallSite -> Bool
prettyCallSite :: CallSite -> String
prettyCallSite :: CallSite -> [Char]
prettyCallSite ([Char]
fun, SrcLoc
loc) = [Char]
fun [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
", called at " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ SrcLoc -> [Char]
prettySrcLoc SrcLoc
loc
prettyCallStack :: CallStack -> String
prettyCallStack :: CallStack -> [Char]
prettyCallStack CallStack
cs = case (CallSite -> [Char]) -> [CallSite] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map CallSite -> [Char]
prettyCallSite (CallStack -> [CallSite]
getCallStack CallStack
cs) of
[] -> [Char]
"(empty CallStack)"
[Char]
firstLoc : [[Char]]
restLocs -> [Char] -> [[Char]] -> [Char]
forall a. [a] -> [[a]] -> [a]
intercalate [Char]
"\n" ([Char]
firstLoc [Char] -> [[Char]] -> [[Char]]
forall a. a -> [a] -> [a]
: (([Char] -> [Char]) -> [[Char]] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map ([Char]
" " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++) [[Char]]
restLocs))
headCallSite :: CallStack -> Maybe CallSite
headCallSite :: CallStack -> Maybe CallSite
headCallSite = [CallSite] -> Maybe CallSite
forall a. [a] -> Maybe a
listToMaybe ([CallSite] -> Maybe CallSite)
-> (CallStack -> [CallSite]) -> CallStack -> Maybe CallSite
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CallStack -> [CallSite]
getCallStack
truncatedCallStack :: CallStack -> CallStack
truncatedCallStack :: CallStack -> CallStack
truncatedCallStack CallStack
cs = CallStack -> (CallSite -> CallStack) -> Maybe CallSite -> CallStack
forall b a. b -> (a -> b) -> Maybe a -> b
maybe CallStack
emptyCallStack ([CallSite] -> CallStack
fromCallSiteList ([CallSite] -> CallStack)
-> (CallSite -> [CallSite]) -> CallSite -> CallStack
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CallSite -> [CallSite]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure) (CallStack -> Maybe CallSite
headCallSite CallStack
cs)
overCallSites :: ([CallSite] -> [CallSite]) -> CallStack -> CallStack
overCallSites :: ([CallSite] -> [CallSite]) -> CallStack -> CallStack
overCallSites [CallSite] -> [CallSite]
f = [CallSite] -> CallStack
fromCallSiteList ([CallSite] -> CallStack)
-> (CallStack -> [CallSite]) -> CallStack -> CallStack
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [CallSite] -> [CallSite]
f ([CallSite] -> [CallSite])
-> (CallStack -> [CallSite]) -> CallStack -> [CallSite]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CallStack -> [CallSite]
getCallStack
filterCallStack :: CallSiteFilter -> CallStack -> CallStack
filterCallStack :: CallSiteFilter -> CallStack -> CallStack
filterCallStack = ([CallSite] -> [CallSite]) -> CallStack -> CallStack
overCallSites (([CallSite] -> [CallSite]) -> CallStack -> CallStack)
-> (CallSiteFilter -> [CallSite] -> [CallSite])
-> CallSiteFilter
-> CallStack
-> CallStack
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CallSiteFilter -> [CallSite] -> [CallSite]
forall a. (a -> Bool) -> [a] -> [a]
filter
popnCallStack :: Word -> CallStack -> CallStack
popnCallStack :: Word -> CallStack -> CallStack
popnCallStack Word
0 = CallStack -> CallStack
forall a. a -> a
id
popnCallStack Word
n = (Word -> CallStack -> CallStack
popnCallStack (Word
n Word -> Word -> Word
forall a. Num a => a -> a -> a
- Word
1)) (CallStack -> CallStack)
-> (CallStack -> CallStack) -> CallStack -> CallStack
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CallStack -> CallStack
popCallStack
withNBackCallStack :: HasCallStack => Word -> (CallStack -> b) -> b
withNBackCallStack :: forall b. HasCallStack => Word -> (CallStack -> b) -> b
withNBackCallStack Word
n CallStack -> b
f = CallStack -> b
f (Word -> CallStack -> CallStack
popnCallStack Word
n CallStack
from)
where
here :: CallStack
here = CallStack
HasCallStack => CallStack
callStack
from :: CallStack
from = CallStack -> CallStack
popCallStack CallStack
here
withCurrentCallStack :: HasCallStack => (CallStack -> b) -> b
withCurrentCallStack :: forall b. HasCallStack => (CallStack -> b) -> b
withCurrentCallStack = Word -> (CallStack -> b) -> b
forall b. HasCallStack => Word -> (CallStack -> b) -> b
withNBackCallStack Word
0
withCallerCallStack :: HasCallStack => (CallStack -> b) -> b
withCallerCallStack :: forall b. HasCallStack => (CallStack -> b) -> b
withCallerCallStack = Word -> (CallStack -> b) -> b
forall b. HasCallStack => Word -> (CallStack -> b) -> b
withNBackCallStack Word
1