gdritter repos telml / master
a bit more clarity Getty Ritter 1 year, 3 months ago
1 changed file(s) with 44 addition(s) and 12 deletion(s). Collapse all Expand all
44 module Main (main) where
55
66 import qualified Control.Exception.Base as Exn
7 import qualified Control.Monad as Monad
78 import qualified Data.ByteString.Char8 as BS
89 import qualified Data.TeLML as TeLML
910 import qualified Data.Text as Text
2930 Left err -> do
3031 putStrLn err
3132 Sys.exitFailure
32 -- read the Lua source file, if provided
33 luaSource <- case optTagFile options of
34 Nothing -> return ""
35 Just f -> BS.readFile f
3633 -- run everything needed in the Lua context (i.e. evaluating the
3734 -- source and then using it to interpret tags)
38 result <- Lua.runEither (luaMain options luaSource telml)
35 result <- Lua.runEither (luaMain options telml)
3936 -- either print the result or print the error nicely
4037 case result of
4138 Right msg -> Text.putStr msg
5148
5249 -- | Evaluate the provided Lua source code and then use it to
5350 -- interpret the `TeLML.Document`.
54 luaMain :: Options -> BS.ByteString -> TeLML.Document -> LuaM Text.Text
55 luaMain opts luaSource doc = do
51 luaMain :: Options -> TeLML.Document -> LuaM Text.Text
52 luaMain opts doc = do
5653 -- load the basic libraries so we have access to stuff like `ipairs`
5754 Lua.openbase
5855 Lua.pop 1
6158 Lua.newtable
6259 Lua.setglobal "telml"
6360
64 -- evaluate the source file. (We don't care what it evaluates to.)
65 _ <- Lua.dostring luaSource
61 -- evaluate the source file.
62 case optTagFile opts of
63 Nothing -> return ()
64 Just f -> do
65 status <- Lua.dofile f
66 case status of
67 -- if it ran fine, then return
68 Lua.OK -> return ()
69 -- if it produced a runtime or syntax error, say so
70 Lua.ErrRun -> do
71 msg <- Lua.tostring (-1)
72 throw (LuaEvaluationError msg)
73 Lua.ErrSyntax -> do
74 msg <- Lua.tostring (-1)
75 throw (LuaEvaluationError msg)
76 _ ->
77 throw (BadEvaluationStatus status)
6678
6779 -- make sure that the user didn't do something funky like redefine
6880 -- the global `telml` to a string.
6981 telml <- Lua.getglobal "telml"
70 if telml /= Lua.TypeTable
71 then throw (RedefinedTable telml)
72 else return ()
82 Monad.when (telml /= Lua.TypeTable) $
83 throw (RedefinedTable telml)
7384
7485 -- walk over the document, evaluating as we go
7586 handleDoc opts doc
241252 " instead"
242253 ]
243254
255 -- | Whatever
256 data BadEvaluationStatus = BadEvaluationStatus { besStatus :: Lua.Status } deriving (Show)
257
258 instance Exn.Exception BadEvaluationStatus where
259 displayException bes = case besStatus bes of
260 Lua.Yield -> "Tag file yielded instead of exiting"
261 Lua.ErrMem -> "Tag file ran out of memory"
262 Lua.ErrErr -> "Tag file failed when running message handler"
263 Lua.ErrFile -> "Failed to open or read tag file"
264 rs -> error "[unreachable: should have handled status " ++ show rs ++" elsewhere]"
265
266 -- | An error for when running the tag file failed
267 data LuaEvaluationError = LuaEvaluationError { leeMessage :: Maybe BS.ByteString } deriving (Show)
268
269 instance Exn.Exception LuaEvaluationError where
270 displayException (LuaEvaluationError (Just m)) =
271 "Error loading tags: " ++ BS.unpack m
272 displayException (LuaEvaluationError Nothing) =
273 "Error loading tags, but could not get error message"
274
275
244276 -- | Print a Lua type nicely (for error message purposes)
245277 ppType :: Lua.Type -> String
246278 ppType Lua.TypeNil = "nil"