{-# 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
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)
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