{-# LANGUAGE OverloadedStrings #-}

module File ( tcIO, tySrc
            , runStdin, runOnFile
            , exprEval
            ) where

import           A
import           A.E
import           A.I
import           Control.Applicative              ((<|>))
import           Control.Exception                (Exception, throw, throwIO)
import           Control.Monad.IO.Class           (liftIO)
import           Control.Monad.Trans.State.Strict (StateT, get, put, runState, runStateT)
import           Data.Bifunctor                   (second)
import qualified Data.ByteString                  as BS
import qualified Data.ByteString.Lazy             as BSL
import qualified Data.ByteString.Lazy.Char8       as ASCIIL
import           Data.Foldable                    (fold, traverse_)
import           Data.Functor                     (($>))
import qualified Data.Text                        as T
import           Data.Text.Encoding               (encodeUtf8)
import qualified Data.Text.IO                     as TIO
import           Data.Tuple                       (swap)
import qualified Data.Vector                      as V
import           Include
import           Jacinda.Backend.Const
import           Jacinda.Backend.T
import           Jacinda.Check.Field
import           Jacinda.Regex
import           L
import           Parser
import           Parser.Rw
import           Prettyprinter                    (Pretty (pretty))
import           R
import           Regex.Rure                       (RurePtr)
import           System.IO                        (stdin)
import           Text.CSV.Lazy.ByteString         (CSVField (..), parseCSV)
import           Ty

csvCtx :: BSL.ByteString -> [LineCtx]
csvCtx :: ByteString -> [LineCtx]
csvCtx = Maybe Int -> [Either [CSVError] [CSVField]] -> [LineCtx]
forall {a} {c}.
(Show a, Num c) =>
Maybe Int
-> [Either a [CSVField]]
-> [(StrictByteString, Vector StrictByteString, c)]
go Maybe Int
forall a. Maybe a
Nothing ([Either [CSVError] [CSVField]] -> [LineCtx])
-> (ByteString -> [Either [CSVError] [CSVField]])
-> ByteString
-> [LineCtx]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [Either [CSVError] [CSVField]]
parseCSV where
    go :: Maybe Int
-> [Either a [CSVField]]
-> [(StrictByteString, Vector StrictByteString, c)]
go Maybe Int
_ []                  = []
    go Maybe Int
_ (Left a
err:[Either a [CSVField]]
_)        = [Char] -> [(StrictByteString, Vector StrictByteString, c)]
forall a. (?callStack::CallStack) => [Char] -> a
error (a -> [Char]
forall a. Show a => a -> [Char]
show a
err)
    -- TODO: re-csv it?
    go (Just Int
n) (Right [CSVField]
r:[Either a [CSVField]]
rs) = let fs :: [StrictByteString]
fs=CSVField -> StrictByteString
mB(CSVField -> StrictByteString) -> [CSVField] -> [StrictByteString]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>[CSVField]
r in ([StrictByteString] -> StrictByteString
forall m. Monoid m => [m] -> m
forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold [StrictByteString]
fs, Int -> [StrictByteString] -> Vector StrictByteString
forall a. Int -> [a] -> Vector a
V.fromListN Int
n [StrictByteString]
fs, Int -> c
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([CSVField] -> Int
line [CSVField]
r))(StrictByteString, Vector StrictByteString, c)
-> [(StrictByteString, Vector StrictByteString, c)]
-> [(StrictByteString, Vector StrictByteString, c)]
forall a. a -> [a] -> [a]
:Maybe Int
-> [Either a [CSVField]]
-> [(StrictByteString, Vector StrictByteString, c)]
go (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
n) [Either a [CSVField]]
rs
    go Maybe Int
Nothing (Right [CSVField]
r:[Either a [CSVField]]
rs)  = let fs :: [StrictByteString]
fs=CSVField -> StrictByteString
mB(CSVField -> StrictByteString) -> [CSVField] -> [StrictByteString]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>[CSVField]
r; n :: Int
n=[StrictByteString] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [StrictByteString]
fs in ([StrictByteString] -> StrictByteString
forall m. Monoid m => [m] -> m
forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold [StrictByteString]
fs, Int -> [StrictByteString] -> Vector StrictByteString
forall a. Int -> [a] -> Vector a
V.fromListN Int
n [StrictByteString]
fs, Int -> c
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([CSVField] -> Int
line [CSVField]
r))(StrictByteString, Vector StrictByteString, c)
-> [(StrictByteString, Vector StrictByteString, c)]
-> [(StrictByteString, Vector StrictByteString, c)]
forall a. a -> [a] -> [a]
:Maybe Int
-> [Either a [CSVField]]
-> [(StrictByteString, Vector StrictByteString, c)]
go (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
n) [Either a [CSVField]]
rs
    mB :: CSVField -> StrictByteString
mB f :: CSVField
f@CSVField{} = ByteString -> StrictByteString
BSL.toStrict (CSVField -> ByteString
csvFieldContent CSVField
f)
    mB CSVField
f            = [Char] -> StrictByteString
forall a. (?callStack::CallStack) => [Char] -> a
error (CSVField -> [Char]
forall a. Show a => a -> [Char]
show CSVField
f)
    line :: [CSVField] -> Int
line (f :: CSVField
f@CSVField{}:[CSVField]
_) = CSVField -> Int
csvRowNum CSVField
f
    line (CSVField
f:[CSVField]
_)            = [Char] -> Int
forall a. (?callStack::CallStack) => [Char] -> a
error (CSVField -> [Char]
forall a. Show a => a -> [Char]
show CSVField
f)
    line []               = [Char] -> Int
forall a. (?callStack::CallStack) => [Char] -> a
error [Char]
"empty record in csv"

parseLib :: [FilePath] -> FilePath -> StateT AlexUserState IO [D AlexPosn]
parseLib :: [[Char]] -> [Char] -> StateT AlexUserState IO [D AlexPosn]
parseLib [[Char]]
incls [Char]
fp = do
    contents <- IO Text -> StateT AlexUserState IO Text
forall a. IO a -> StateT AlexUserState IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Text -> StateT AlexUserState IO Text)
-> IO Text -> StateT AlexUserState IO Text
forall a b. (a -> b) -> a -> b
$ [Char] -> IO Text
TIO.readFile ([Char] -> IO Text) -> IO [Char] -> IO Text
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< [[Char]] -> [Char] -> IO [Char]
resolveImport [[Char]]
incls [Char]
fp
    st <- get
    case parseLibWithCtx contents st of
        Left ParseError AlexPosn
err              -> IO [D AlexPosn] -> StateT AlexUserState IO [D AlexPosn]
forall a. IO a -> StateT AlexUserState IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (ParseError FPos -> IO [D AlexPosn]
forall e a. (?callStack::CallStack, Exception e) => e -> IO a
throwIO ([Char] -> AlexPosn -> FPos
FPos [Char]
fp(AlexPosn -> FPos) -> ParseError AlexPosn -> ParseError FPos
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>ParseError AlexPosn
err))
        Right (AlexUserState
st', ([], [D AlexPosn]
ds)) -> AlexUserState -> StateT AlexUserState IO ()
forall (m :: * -> *) s. Monad m => s -> StateT s m ()
put AlexUserState
st' StateT AlexUserState IO ()
-> [D AlexPosn] -> StateT AlexUserState IO [D AlexPosn]
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> (D AlexPosn -> D AlexPosn
forall a. D a -> D a
rwD (D AlexPosn -> D AlexPosn) -> [D AlexPosn] -> [D AlexPosn]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [D AlexPosn]
ds)
        Right (AlexUserState
st', ([[Char]]
is, [D AlexPosn]
ds)) -> do {AlexUserState -> StateT AlexUserState IO ()
forall (m :: * -> *) s. Monad m => s -> StateT s m ()
put AlexUserState
st'; dss <- ([Char] -> StateT AlexUserState IO [D AlexPosn])
-> [[Char]] -> StateT AlexUserState IO [[D AlexPosn]]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse ([[Char]] -> [Char] -> StateT AlexUserState IO [D AlexPosn]
parseLib [[Char]]
incls) [[Char]]
is; pure (concat dss ++ fmap rwD ds)}

parseP :: [FilePath] -> FilePath -> T.Text -> [(T.Text, Value)] -> StateT AlexUserState IO (Program AlexPosn)
parseP :: [[Char]]
-> [Char]
-> Text
-> [(Text, Text)]
-> StateT AlexUserState IO (Program AlexPosn)
parseP [[Char]]
incls [Char]
fn Text
src [(Text, Text)]
var = do
    st <- StateT AlexUserState IO AlexUserState
forall (m :: * -> *) s. Monad m => StateT s m s
get
    case parseWithCtx src var st of
        Left ParseError AlexPosn
err -> IO (Program AlexPosn) -> StateT AlexUserState IO (Program AlexPosn)
forall a. IO a -> StateT AlexUserState IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Program AlexPosn)
 -> StateT AlexUserState IO (Program AlexPosn))
-> IO (Program AlexPosn)
-> StateT AlexUserState IO (Program AlexPosn)
forall a b. (a -> b) -> a -> b
$ ParseError FPos -> IO (Program AlexPosn)
forall e a. (?callStack::CallStack, Exception e) => e -> IO a
throwIO ([Char] -> AlexPosn -> FPos
FPos [Char]
fn(AlexPosn -> FPos) -> ParseError AlexPosn -> ParseError FPos
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>ParseError AlexPosn
err)
        Right (AlexUserState
st', ([[Char]]
is, Program [D AlexPosn]
ds E AlexPosn
e)) -> do
            AlexUserState -> StateT AlexUserState IO ()
forall (m :: * -> *) s. Monad m => s -> StateT s m ()
put AlexUserState
st'
            dss <- ([Char] -> StateT AlexUserState IO [D AlexPosn])
-> [[Char]] -> StateT AlexUserState IO [[D AlexPosn]]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse ([[Char]] -> [Char] -> StateT AlexUserState IO [D AlexPosn]
parseLib [[Char]]
incls) [[Char]]
is
            pure $ Program (concat dss ++ fmap rwD ds) (rwE e)

-- | Parse + rename
parsePWithMax :: [FilePath] -> FilePath -> T.Text -> [(T.Text, T.Text)] -> IO (Program AlexPosn, Int)
parsePWithMax :: [[Char]]
-> [Char] -> Text -> [(Text, Text)] -> IO (Program AlexPosn, Int)
parsePWithMax [[Char]]
incls [Char]
fn Text
src [(Text, Text)]
vars = (Int -> Program AlexPosn -> (Program AlexPosn, Int))
-> (Int, Program AlexPosn) -> (Program AlexPosn, Int)
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int -> Program AlexPosn -> (Program AlexPosn, Int)
forall a. Int -> Program a -> (Program a, Int)
rP((Int, Program AlexPosn) -> (Program AlexPosn, Int))
-> ((Program AlexPosn, AlexUserState) -> (Int, Program AlexPosn))
-> (Program AlexPosn, AlexUserState)
-> (Program AlexPosn, Int)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Program AlexPosn, Int) -> (Int, Program AlexPosn)
forall a b. (a, b) -> (b, a)
swap((Program AlexPosn, Int) -> (Int, Program AlexPosn))
-> ((Program AlexPosn, AlexUserState) -> (Program AlexPosn, Int))
-> (Program AlexPosn, AlexUserState)
-> (Int, Program AlexPosn)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(AlexUserState -> Int)
-> (Program AlexPosn, AlexUserState) -> (Program AlexPosn, Int)
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second AlexUserState -> Int
forall {a} {b} {c}. (a, b, c) -> a
fst3 ((Program AlexPosn, AlexUserState) -> (Program AlexPosn, Int))
-> IO (Program AlexPosn, AlexUserState)
-> IO (Program AlexPosn, Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> StateT AlexUserState IO (Program AlexPosn)
-> AlexUserState -> IO (Program AlexPosn, AlexUserState)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT ([[Char]]
-> [Char]
-> Text
-> [(Text, Text)]
-> StateT AlexUserState IO (Program AlexPosn)
parseP [[Char]]
incls [Char]
fn Text
src [(Text, Text)]
vars) AlexUserState
alexInitUserState
    where fst3 :: (a, b, c) -> a
fst3 (a
x,b
_,c
_) = a
x

parseWithMax' :: T.Text -> Either (ParseError AlexPosn) (Program AlexPosn, Int)
parseWithMax' :: Text -> Either (ParseError AlexPosn) (Program AlexPosn, Int)
parseWithMax' = ((Int, File) -> (Program AlexPosn, Int))
-> Either (ParseError AlexPosn) (Int, File)
-> Either (ParseError AlexPosn) (Program AlexPosn, Int)
forall a b.
(a -> b)
-> Either (ParseError AlexPosn) a -> Either (ParseError AlexPosn) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Int -> Program AlexPosn -> (Program AlexPosn, Int))
-> (Int, Program AlexPosn) -> (Program AlexPosn, Int)
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int -> Program AlexPosn -> (Program AlexPosn, Int)
forall a. Int -> Program a -> (Program a, Int)
rP ((Int, Program AlexPosn) -> (Program AlexPosn, Int))
-> ((Int, File) -> (Int, Program AlexPosn))
-> (Int, File)
-> (Program AlexPosn, Int)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (File -> Program AlexPosn)
-> (Int, File) -> (Int, Program AlexPosn)
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second (Program AlexPosn -> Program AlexPosn
forall a. Program a -> Program a
rwP(Program AlexPosn -> Program AlexPosn)
-> (File -> Program AlexPosn) -> File -> Program AlexPosn
forall b c a. (b -> c) -> (a -> b) -> a -> c
.File -> Program AlexPosn
forall a b. (a, b) -> b
snd)) (Either (ParseError AlexPosn) (Int, File)
 -> Either (ParseError AlexPosn) (Program AlexPosn, Int))
-> (Text -> Either (ParseError AlexPosn) (Int, File))
-> Text
-> Either (ParseError AlexPosn) (Program AlexPosn, Int)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either (ParseError AlexPosn) (Int, File)
parseWithMax

type FileBS = BS.ByteString

data FPos = FPos { FPos -> [Char]
filen :: String, FPos -> AlexPosn
pos :: !AlexPosn }

instance Pretty FPos where pretty :: forall ann. FPos -> Doc ann
pretty (FPos [Char]
f AlexPosn
l) = [Char] -> Doc ann
forall ann. [Char] -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty [Char]
f Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
":" Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> AlexPosn -> Doc ann
forall a ann. Pretty a => a -> Doc ann
forall ann. AlexPosn -> Doc ann
pretty AlexPosn
l

tcompile :: Text -> RurePtr
tcompile=StrictByteString -> RurePtr
compileDefault(StrictByteString -> RurePtr)
-> (Text -> StrictByteString) -> Text -> RurePtr
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Text -> StrictByteString
encodeUtf8

compileR :: FileBS
         -> E T
         -> E T
compileR :: StrictByteString -> E T -> E T
compileR StrictByteString
fp = E T -> E T
r where
    r :: E T -> E T
r (RegexLit T
_ StrictByteString
rrϵ)  = RurePtr -> E T
forall a. RurePtr -> E a
RC (StrictByteString -> RurePtr
compileDefault StrictByteString
rrϵ)
    r (NB T
_ N
Fp)         = StrictByteString -> E T
mkStr StrictByteString
fp
    r e :: E T
e@Var{}           = E T
e
    r e :: E T
e@UB{}            = E T
e
    r e :: E T
e@NB{}            = E T
e
    r e :: E T
e@Lit{}           = E T
e
    r e :: E T
e@TB{}            = E T
e
    r e :: E T
e@BB{}            = E T
e
    r (Cond T
l E T
p E T
e0 E T
e1)  = T -> E T -> E T -> E T -> E T
forall a. a -> E a -> E a -> E a -> E a
Cond T
l (E T -> E T
r E T
p) (E T -> E T
r E T
e0) (E T -> E T
r E T
e1)
    r (OptionVal T
l Maybe (E T)
e)   = T -> Maybe (E T) -> E T
forall a. a -> Maybe (E a) -> E a
OptionVal T
l (E T -> E T
r(E T -> E T) -> Maybe (E T) -> Maybe (E T)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>Maybe (E T)
e)
    r (EApp T
l E T
e0 E T
e1)    = T -> E T -> E T -> E T
forall a. a -> E a -> E a -> E a
EApp T
l (E T -> E T
r E T
e0) (E T -> E T
r E T
e1)
    r e :: E T
e@Column{}        = E T
e
    r e :: E T
e@IParseCol{}     = E T
e
    r e :: E T
e@IParseAllCol{}  = E T
e
    r e :: E T
e@FParseAllCol{}  = E T
e
    r e :: E T
e@ParseAllCol{}   = E T
e
    r e :: E T
e@FParseCol{}     = E T
e
    r e :: E T
e@ParseCol{}      = E T
e
    r e :: E T
e@LastField{}     = E T
e
    r e :: E T
e@Field{}         = E T
e
    r e :: E T
e@FieldList{}     = E T
e
    r e :: E T
e@AllField{}      = E T
e
    r e :: E T
e@AllColumn{}     = E T
e
    r (Guarded T
l E T
p E T
e)   = T -> E T -> E T -> E T
forall a. a -> E a -> E a -> E a
Guarded T
l (E T -> E T
r E T
p) (E T -> E T
r E T
e)
    r (Implicit T
l E T
e)    = T -> E T -> E T
forall a. a -> E a -> E a
Implicit T
l (E T -> E T
r E T
e)
    r (Let T
l (Nm T
n, E T
eb) E T
e) = T -> (Nm T, E T) -> E T -> E T
forall a. a -> (Nm a, E a) -> E a -> E a
Let T
l (Nm T
n, E T -> E T
r E T
eb) (E T -> E T
r E T
e)
    r (Lam T
l Nm T
n E T
e)       = T -> Nm T -> E T -> E T
forall a. a -> Nm a -> E a -> E a
Lam T
l Nm T
n (E T -> E T
r E T
e)
    r (Tup T
l [E T]
es)        = T -> [E T] -> E T
forall a. a -> [E a] -> E a
Tup T
l (E T -> E T
r(E T -> E T) -> [E T] -> [E T]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>[E T]
es)
    r (Rec T
l [(Nm T, E T)]
es)        = T -> [(Nm T, E T)] -> E T
forall a. a -> [(Nm a, E a)] -> E a
Rec T
l ((E T -> E T) -> (Nm T, E T) -> (Nm T, E T)
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second E T -> E T
r((Nm T, E T) -> (Nm T, E T)) -> [(Nm T, E T)] -> [(Nm T, E T)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>[(Nm T, E T)]
es)
    r (Arr T
l Vector (E T)
es)        = T -> Vector (E T) -> E T
forall a. a -> Vector (E a) -> E a
Arr T
l (E T -> E T
r(E T -> E T) -> Vector (E T) -> Vector (E T)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>Vector (E T)
es)
    r (Anchor T
l [E T]
es)     = T -> [E T] -> E T
forall a. a -> [E a] -> E a
Anchor T
l (E T -> E T
r(E T -> E T) -> [E T] -> [E T]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>[E T]
es)
    r F{}               = [Char] -> E T
forall a. (?callStack::CallStack) => [Char] -> a
error [Char]
"impossible"
    r Dfn{}             = E T
forall a. a
desugar
    r Paren{}           = E T
forall a. a
desugar
    r ResVar{}          = E T
forall a. a
desugar
    r RwB{}             = E T
forall a. a
desugar
    r RwT{}             = E T
forall a. a
desugar
    r RC{}              = [Char] -> E T
forall a. (?callStack::CallStack) => [Char] -> a
error [Char]
"???"

exprEval :: T.Text -> E T
exprEval :: Text -> E T
exprEval Text
src =
    case Text -> Either (ParseError AlexPosn) (Program AlexPosn, Int)
parseWithMax' Text
src of
        Left ParseError AlexPosn
err -> ParseError AlexPosn -> E T
forall a e. (?callStack::CallStack, Exception e) => e -> a
throw ParseError AlexPosn
err
        Right (Program AlexPosn
ast, Int
m) ->
            let (Program T
typed, Int
i) = Either (Err AlexPosn) (Program T, Int) -> (Program T, Int)
forall e a. Exception e => Either e a -> a
yeet (Either (Err AlexPosn) (Program T, Int) -> (Program T, Int))
-> Either (Err AlexPosn) (Program T, Int) -> (Program T, Int)
forall a b. (a -> b) -> a -> b
$ Int
-> TyM AlexPosn (Program T)
-> Either (Err AlexPosn) (Program T, Int)
forall a b. Int -> TyM a b -> Either (Err a) (b, Int)
runTyM Int
m (Program AlexPosn -> TyM AlexPosn (Program T)
forall a. Ord a => Program a -> TyM a (Program T)
tyP Program AlexPosn
ast)
                (E T
inlined, Int
j) = Int -> Program T -> (E T, Int)
ib Int
i Program T
typed
            in Int -> E T -> E T
eB Int
j (StrictByteString -> E T -> E T
compileR ([Char] -> StrictByteString
forall a. (?callStack::CallStack) => [Char] -> a
error [Char]
"nf not defined.") E T
inlined)

compileFS :: Maybe T.Text -> RurePtr
compileFS :: Maybe Text -> RurePtr
compileFS = RurePtr -> (Text -> RurePtr) -> Maybe Text -> RurePtr
forall b a. b -> (a -> b) -> Maybe a -> b
maybe RurePtr
defaultRurePtr Text -> RurePtr
tcompile

runOnBytes :: [FilePath]
           -> FilePath -- ^ Data file name, for @nf@
           -> FilePath -- ^ For error locations
           -> T.Text -- ^ Program
           -> [(T.Text, Value)]
           -> Mode
           -> BSL.ByteString
           -> IO ()
runOnBytes :: [[Char]]
-> [Char]
-> [Char]
-> Text
-> [(Text, Text)]
-> Mode
-> ByteString
-> IO ()
runOnBytes [[Char]]
incls [Char]
fp [Char]
fn Text
src [(Text, Text)]
vars Mode
mode ByteString
contents = do
    incls' <- IO ([[Char]] -> [[Char]])
defaultIncludes IO ([[Char]] -> [[Char]]) -> IO [[Char]] -> IO [[Char]]
forall a b. IO (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [[Char]] -> IO [[Char]]
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [[Char]]
incls
    (ast, m) <- parsePWithMax incls' fn src vars
    (typed, i) <- yIO fn $ runTyM m (tyP ast)
    let (eI, j) = ib i typed
    m'Throw $ cF eI
    let (e', k) = runState (eta eI) j
        cont=Bool -> Int -> E T -> [LineCtx] -> IO ()
run (Program T -> Bool
forall a. Program a -> Bool
flushD Program T
typed) Int
k (StrictByteString -> E T -> E T
compileR (Text -> StrictByteString
encodeUtf8 (Text -> StrictByteString) -> Text -> StrictByteString
forall a b. (a -> b) -> a -> b
$ [Char] -> Text
T.pack [Char]
fp) E T
e')
    case (mode, getS ast) of
        (AWK Maybe Text
cliFS Maybe Text
cliRS, AWK Maybe Text
afs Maybe Text
ars) ->
            let r :: RurePtr
r=Maybe Text -> RurePtr
compileFS (Maybe Text
cliFS Maybe Text -> Maybe Text -> Maybe Text
forall a. Maybe a -> Maybe a -> Maybe a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Maybe Text
afs)
                bs :: [StrictByteString]
bs=case Maybe Text
cliRS Maybe Text -> Maybe Text -> Maybe Text
forall a. Maybe a -> Maybe a -> Maybe a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Maybe Text
ars of
                    Maybe Text
Nothing -> (ByteString -> StrictByteString)
-> [ByteString] -> [StrictByteString]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> StrictByteString
BSL.toStrict (ByteString -> [ByteString]
ASCIIL.lines ByteString
contents)
                    Just Text
rs -> RurePtr -> ByteString -> [StrictByteString]
lazySplit (Text -> RurePtr
tcompile Text
rs) ByteString
contents
                ctxs :: [LineCtx]
ctxs=((StrictByteString, Vector StrictByteString) -> Integer -> LineCtx)
-> [(StrictByteString, Vector StrictByteString)]
-> [Integer]
-> [LineCtx]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (\ ~(StrictByteString
x,Vector StrictByteString
y) Integer
z -> (StrictByteString
x,Vector StrictByteString
y,Integer
z)) [(StrictByteString
b, RurePtr -> StrictByteString -> Vector StrictByteString
splitBy RurePtr
r StrictByteString
b) | StrictByteString
b <- [StrictByteString]
bs] [Integer
1..]
            in [LineCtx] -> IO ()
cont [LineCtx]
ctxs
        (Mode
CSV, Mode
_) -> let ctxs :: [LineCtx]
ctxs = ByteString -> [LineCtx]
csvCtx ByteString
contents in [LineCtx] -> IO ()
cont [LineCtx]
ctxs
        (Mode
_, Mode
CSV) -> let ctxs :: [LineCtx]
ctxs = ByteString -> [LineCtx]
csvCtx ByteString
contents in [LineCtx] -> IO ()
cont [LineCtx]
ctxs

runStdin :: [FilePath]
         -> FilePath -- ^ For error location
         -> T.Text -- ^ Program
         -> [(T.Text, Value)]
         -> Mode
         -> IO ()
runStdin :: [[Char]] -> [Char] -> Text -> [(Text, Text)] -> Mode -> IO ()
runStdin [[Char]]
is [Char]
src Text
fn [(Text, Text)]
vars Mode
m = [[Char]]
-> [Char]
-> [Char]
-> Text
-> [(Text, Text)]
-> Mode
-> ByteString
-> IO ()
runOnBytes [[Char]]
is [Char]
"(stdin)" [Char]
src Text
fn [(Text, Text)]
vars Mode
m (ByteString -> IO ()) -> IO ByteString -> IO ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Handle -> IO ByteString
BSL.hGetContents Handle
stdin

runOnFile :: [FilePath]
          -> FilePath
          -> T.Text
          -> [(T.Text, Value)]
          -> Mode
          -> FilePath
          -> IO ()
runOnFile :: [[Char]]
-> [Char] -> Text -> [(Text, Text)] -> Mode -> [Char] -> IO ()
runOnFile [[Char]]
is [Char]
fn Text
e [(Text, Text)]
vs Mode
m [Char]
fp = [[Char]]
-> [Char]
-> [Char]
-> Text
-> [(Text, Text)]
-> Mode
-> ByteString
-> IO ()
runOnBytes [[Char]]
is [Char]
fp [Char]
fn Text
e [(Text, Text)]
vs Mode
m (ByteString -> IO ()) -> IO ByteString -> IO ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< [Char] -> IO ByteString
BSL.readFile [Char]
fp

tcIO :: [FilePath] -> FilePath -> T.Text -> IO ()
tcIO :: [[Char]] -> [Char] -> Text -> IO ()
tcIO [[Char]]
incls [Char]
fn Text
src = do
    incls' <- IO ([[Char]] -> [[Char]])
defaultIncludes IO ([[Char]] -> [[Char]]) -> IO [[Char]] -> IO [[Char]]
forall a b. IO (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [[Char]] -> IO [[Char]]
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [[Char]]
incls
    (ast, m) <- parsePWithMax incls' fn src []
    (pT, i) <- yIO fn $ runTyM m (tyP ast)
    let (eI, _) = ib i pT
    m'Throw $ cF eI

tySrc :: T.Text -> T
tySrc :: Text -> T
tySrc Text
src =
    case Text -> Either (ParseError AlexPosn) (Program AlexPosn, Int)
parseWithMax' Text
src of
        Right (Program AlexPosn
ast, Int
m) -> Either (Err AlexPosn) T -> T
forall e a. Exception e => Either e a -> a
yeet (Either (Err AlexPosn) T -> T) -> Either (Err AlexPosn) T -> T
forall a b. (a -> b) -> a -> b
$ (T, Int) -> T
forall a b. (a, b) -> a
fst ((T, Int) -> T)
-> Either (Err AlexPosn) (T, Int) -> Either (Err AlexPosn) T
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> TyM AlexPosn T -> Either (Err AlexPosn) (T, Int)
forall a b. Int -> TyM a b -> Either (Err a) (b, Int)
runTyM Int
m (E AlexPosn -> TyM AlexPosn T
forall a. Ord a => E a -> TyM a T
tyOf (Program AlexPosn -> E AlexPosn
forall a. Program a -> E a
expr Program AlexPosn
ast))
        Left ParseError AlexPosn
err       -> ParseError AlexPosn -> T
forall a e. (?callStack::CallStack, Exception e) => e -> a
throw ParseError AlexPosn
err

m'Throw :: Exception e => Maybe e -> IO ()
m'Throw :: forall e. Exception e => Maybe e -> IO ()
m'Throw = (e -> IO Any) -> Maybe e -> IO ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ e -> IO Any
forall e a. (?callStack::CallStack, Exception e) => e -> IO a
throwIO

yIO :: [Char] -> Either (f AlexPosn) a -> IO a
yIO [Char]
fp = (f AlexPosn -> IO a)
-> (a -> IO a) -> Either (f AlexPosn) a -> IO a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (f FPos -> IO a
forall e a. (?callStack::CallStack, Exception e) => e -> IO a
throwIO(f FPos -> IO a) -> (f AlexPosn -> f FPos) -> f AlexPosn -> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.([Char] -> AlexPosn -> FPos
FPos [Char]
fp(AlexPosn -> FPos) -> f AlexPosn -> f FPos
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>)) a -> IO a
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure

yeet :: Exception e => Either e a -> a
yeet :: forall e a. Exception e => Either e a -> a
yeet = (e -> a) -> (a -> a) -> Either e a -> a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either e -> a
forall a e. (?callStack::CallStack, Exception e) => e -> a
throw a -> a
forall a. a -> a
id

desugar :: a
desugar :: forall a. a
desugar = [Char] -> a
forall a. (?callStack::CallStack) => [Char] -> a
error [Char]
"Interal error: should have been desugared."