{-# LANGUAGE OverloadedStrings #-}

-- | Interface to the Futhark parser.
module Language.Futhark.Parser
  ( parseFuthark,
    parseExp,
    parseModExp,
    parseType,
    parseValue,
    parseValues,
    parseDecOrExpIncrM,
    SyntaxError (..),
  )
where

import qualified Data.Text as T
import Language.Futhark.Parser.Parser
import Language.Futhark.Prop
import Language.Futhark.Syntax

-- | Parse an entire Futhark program from the given 'T.Text', using
-- the 'FilePath' as the source name for error messages.
parseFuthark ::
  FilePath ->
  T.Text ->
  Either SyntaxError UncheckedProg
parseFuthark :: FilePath -> Text -> Either SyntaxError UncheckedProg
parseFuthark = ParserMonad UncheckedProg
-> FilePath -> Text -> Either SyntaxError UncheckedProg
forall a. ParserMonad a -> FilePath -> Text -> Either SyntaxError a
parse ParserMonad UncheckedProg
prog

-- | Parse an Futhark expression from the given 'String', using the
-- 'FilePath' as the source name for error messages.
parseExp ::
  FilePath ->
  T.Text ->
  Either SyntaxError UncheckedExp
parseExp :: FilePath -> Text -> Either SyntaxError UncheckedExp
parseExp = ParserMonad UncheckedExp
-> FilePath -> Text -> Either SyntaxError UncheckedExp
forall a. ParserMonad a -> FilePath -> Text -> Either SyntaxError a
parse ParserMonad UncheckedExp
expression

-- | Parse a Futhark module expression from the given 'String', using the
-- 'FilePath' as the source name for error messages.
parseModExp ::
  FilePath ->
  T.Text ->
  Either SyntaxError (ModExpBase NoInfo Name)
parseModExp :: FilePath -> Text -> Either SyntaxError (ModExpBase NoInfo Name)
parseModExp = ParserMonad (ModExpBase NoInfo Name)
-> FilePath -> Text -> Either SyntaxError (ModExpBase NoInfo Name)
forall a. ParserMonad a -> FilePath -> Text -> Either SyntaxError a
parse ParserMonad (ModExpBase NoInfo Name)
modExpression

-- | Parse an Futhark type from the given 'String', using the
-- 'FilePath' as the source name for error messages.
parseType ::
  FilePath ->
  T.Text ->
  Either SyntaxError UncheckedTypeExp
parseType :: FilePath -> Text -> Either SyntaxError UncheckedTypeExp
parseType = ParserMonad UncheckedTypeExp
-> FilePath -> Text -> Either SyntaxError UncheckedTypeExp
forall a. ParserMonad a -> FilePath -> Text -> Either SyntaxError a
parse ParserMonad UncheckedTypeExp
futharkType

-- | Parse any Futhark value from the given 'String', using the 'FilePath'
-- as the source name for error messages.
parseValue ::
  FilePath ->
  T.Text ->
  Either SyntaxError Value
parseValue :: FilePath -> Text -> Either SyntaxError Value
parseValue = ParserMonad Value -> FilePath -> Text -> Either SyntaxError Value
forall a. ParserMonad a -> FilePath -> Text -> Either SyntaxError a
parse ParserMonad Value
anyValue

-- | Parse several Futhark values (separated by anything) from the given
-- 'String', using the 'FilePath' as the source name for error
-- messages.
parseValues ::
  FilePath ->
  T.Text ->
  Either SyntaxError [Value]
parseValues :: FilePath -> Text -> Either SyntaxError [Value]
parseValues = ParserMonad [Value]
-> FilePath -> Text -> Either SyntaxError [Value]
forall a. ParserMonad a -> FilePath -> Text -> Either SyntaxError a
parse ParserMonad [Value]
anyValues

-- | Parse an Futhark expression incrementally from monadic actions, using the
-- 'FilePath' as the source name for error messages.
parseExpIncrM ::
  Monad m =>
  m T.Text ->
  FilePath ->
  T.Text ->
  m (Either SyntaxError UncheckedExp)
parseExpIncrM :: m Text -> FilePath -> Text -> m (Either SyntaxError UncheckedExp)
parseExpIncrM m Text
fetch FilePath
file Text
program =
  m Text
-> ReadLineMonad (Either SyntaxError UncheckedExp)
-> m (Either SyntaxError UncheckedExp)
forall (m :: * -> *) a. Monad m => m Text -> ReadLineMonad a -> m a
getLinesFromM m Text
fetch (ReadLineMonad (Either SyntaxError UncheckedExp)
 -> m (Either SyntaxError UncheckedExp))
-> ReadLineMonad (Either SyntaxError UncheckedExp)
-> m (Either SyntaxError UncheckedExp)
forall a b. (a -> b) -> a -> b
$ ParserMonad UncheckedExp
-> FilePath
-> Text
-> ReadLineMonad (Either SyntaxError UncheckedExp)
forall a.
ParserMonad a
-> FilePath -> Text -> ReadLineMonad (Either SyntaxError a)
parseInMonad ParserMonad UncheckedExp
expression FilePath
file Text
program

-- | Parse either an expression or a declaration incrementally;
-- favouring declarations in case of ambiguity.
parseDecOrExpIncrM ::
  Monad m =>
  m T.Text ->
  FilePath ->
  T.Text ->
  m (Either SyntaxError (Either UncheckedDec UncheckedExp))
parseDecOrExpIncrM :: m Text
-> FilePath
-> Text
-> m (Either SyntaxError (Either UncheckedDec UncheckedExp))
parseDecOrExpIncrM m Text
fetch FilePath
file Text
input =
  case ParserMonad UncheckedDec
-> FilePath
-> Text
-> ReadLineMonad (Either SyntaxError UncheckedDec)
forall a.
ParserMonad a
-> FilePath -> Text -> ReadLineMonad (Either SyntaxError a)
parseInMonad ParserMonad UncheckedDec
declaration FilePath
file Text
input of
    Value Left {} -> (UncheckedExp -> Either UncheckedDec UncheckedExp)
-> Either SyntaxError UncheckedExp
-> Either SyntaxError (Either UncheckedDec UncheckedExp)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap UncheckedExp -> Either UncheckedDec UncheckedExp
forall a b. b -> Either a b
Right (Either SyntaxError UncheckedExp
 -> Either SyntaxError (Either UncheckedDec UncheckedExp))
-> m (Either SyntaxError UncheckedExp)
-> m (Either SyntaxError (Either UncheckedDec UncheckedExp))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m Text -> FilePath -> Text -> m (Either SyntaxError UncheckedExp)
forall (m :: * -> *).
Monad m =>
m Text -> FilePath -> Text -> m (Either SyntaxError UncheckedExp)
parseExpIncrM m Text
fetch FilePath
file Text
input
    Value (Right UncheckedDec
d) -> Either SyntaxError (Either UncheckedDec UncheckedExp)
-> m (Either SyntaxError (Either UncheckedDec UncheckedExp))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either SyntaxError (Either UncheckedDec UncheckedExp)
 -> m (Either SyntaxError (Either UncheckedDec UncheckedExp)))
-> Either SyntaxError (Either UncheckedDec UncheckedExp)
-> m (Either SyntaxError (Either UncheckedDec UncheckedExp))
forall a b. (a -> b) -> a -> b
$ Either UncheckedDec UncheckedExp
-> Either SyntaxError (Either UncheckedDec UncheckedExp)
forall a b. b -> Either a b
Right (Either UncheckedDec UncheckedExp
 -> Either SyntaxError (Either UncheckedDec UncheckedExp))
-> Either UncheckedDec UncheckedExp
-> Either SyntaxError (Either UncheckedDec UncheckedExp)
forall a b. (a -> b) -> a -> b
$ UncheckedDec -> Either UncheckedDec UncheckedExp
forall a b. a -> Either a b
Left UncheckedDec
d
    GetLine Maybe Text -> ReadLineMonad (Either SyntaxError UncheckedDec)
_ -> do
      Text
l <- m Text
fetch
      m Text
-> FilePath
-> Text
-> m (Either SyntaxError (Either UncheckedDec UncheckedExp))
forall (m :: * -> *).
Monad m =>
m Text
-> FilePath
-> Text
-> m (Either SyntaxError (Either UncheckedDec UncheckedExp))
parseDecOrExpIncrM m Text
fetch FilePath
file (Text -> m (Either SyntaxError (Either UncheckedDec UncheckedExp)))
-> Text
-> m (Either SyntaxError (Either UncheckedDec UncheckedExp))
forall a b. (a -> b) -> a -> b
$ Text
input Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\n" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
l