{-# LANGUAGE OverloadedStrings #-}
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
luaSourcePos :: LuaError e
=> Int
-> LuaE e (Maybe SourcePos)
luaSourcePos :: forall e. LuaError e => Int -> LuaE e (Maybe SourcePos)
luaSourcePos Int
lvl = do
Int -> LuaE e ()
forall e. Int -> LuaE e ()
where' Int
lvl
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
return $ do
(prfx, sfx) <- T.breakOnEnd ":" <$> T.stripSuffix ": " locStr
(source, _) <- T.unsnoc prfx
line <- readMaybe (T.unpack sfx)
Just $ newPos (T.unpack source) line 1