module Snail.IO where

import Data.Text.IO qualified as Text
import Snail.Lexer
import Text.Megaparsec

-- | Given a 'FilePath', attempt to parse 'SnailAst' from a file.
readSnailFile :: FilePath -> IO (Either String [SnailAst])
readSnailFile :: FilePath -> IO (Either FilePath [SnailAst])
readSnailFile FilePath
fp = do
    Text
contents <- FilePath -> IO Text
Text.readFile FilePath
fp
    forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ case forall e s a.
Parsec e s a -> FilePath -> s -> Either (ParseErrorBundle s e) a
parse Parser [SnailAst]
snailAst (forall a. Show a => a -> FilePath
show FilePath
fp) Text
contents of
        Left ParseErrorBundle Text Void
parseErrorBundle -> forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ forall s e.
(VisualStream s, TraversableStream s, ShowErrorComponent e) =>
ParseErrorBundle s e -> FilePath
errorBundlePretty ParseErrorBundle Text Void
parseErrorBundle
        Right [SnailAst]
sexprs -> forall a b. b -> Either a b
Right [SnailAst]
sexprs