{-# LANGUAGE OverloadedStrings #-}
{- |
   Module      : Text.Pandoc.Lua.SourcePos
   Copyright   : © 2024 Albert Krewinkel
   License     : GPL-2.0-or-later
   Maintainer  : Albert Krewinkel <albert+pandoc@tarleb.com>

Helper function to retrieve the 'SourcePos' in a Lua script.
-}
module Text.Pandoc.Lua.SourcePos
  ( luaSourcePos
  ) where

import HsLua
import Text.Parsec.Pos (SourcePos, newPos)
import Text.Read (readMaybe)
import qualified Data.Text as T
import qualified HsLua.Core.Utf8 as UTF8

-- | Returns the current position in a Lua script.
--
-- The reporting level is the level of the call stack, for which the
-- position should be reported. There might not always be a position
-- available, e.g., in C functions.
luaSourcePos :: LuaError e
             => Int                -- ^ reporting level
             -> LuaE e (Maybe SourcePos)
luaSourcePos :: forall e. LuaError e => Int -> LuaE e (Maybe SourcePos)
luaSourcePos Int
lvl = do
  -- reporting levels:
  -- 0: this hook,
  -- 1: userdata wrapper function for the hook,
  -- 2: warn,
  -- 3: function calling warn.
  Int -> LuaE e ()
forall e. Int -> LuaE e ()
where' Int
lvl
  Text
locStr <- ByteString -> Text
UTF8.toText (ByteString -> Text) -> LuaE e ByteString -> LuaE e Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> StackIndex -> LuaE e ByteString
forall e. LuaError e => StackIndex -> LuaE e ByteString
tostring' StackIndex
top
  Maybe SourcePos -> LuaE e (Maybe SourcePos)
forall a. a -> LuaE e a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe SourcePos -> LuaE e (Maybe SourcePos))
-> Maybe SourcePos -> LuaE e (Maybe SourcePos)
forall a b. (a -> b) -> a -> b
$ do
    (Text
prfx, Text
sfx) <- HasCallStack => Text -> Text -> (Text, Text)
Text -> Text -> (Text, Text)
T.breakOnEnd Text
":" (Text -> (Text, Text)) -> Maybe Text -> Maybe (Text, Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Text -> Maybe Text
T.stripSuffix Text
": " Text
locStr
    (Text
source, Char
_) <- Text -> Maybe (Text, Char)
T.unsnoc Text
prfx
    Int
line <- String -> Maybe Int
forall a. Read a => String -> Maybe a
readMaybe (Text -> String
T.unpack Text
sfx)
    -- We have no column information, so always use column 1
    SourcePos -> Maybe SourcePos
forall a. a -> Maybe a
Just (SourcePos -> Maybe SourcePos) -> SourcePos -> Maybe SourcePos
forall a b. (a -> b) -> a -> b
$ String -> Int -> Int -> SourcePos
newPos (Text -> String
T.unpack Text
source) Int
line Int
1