{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Dhall.Parser (
exprFromText
, exprAndHeaderFromText
, censor
, createHeader
, expr, exprA
, Header(..)
, Src(..)
, SourcedException(..)
, ParseError(..)
, Parser(..)
) where
import Control.Applicative (many)
import Control.Exception (Exception)
import Data.Text (Text)
import Data.Void (Void)
import Dhall.Src (Src (..))
import Dhall.Syntax
import Text.Megaparsec (ParseErrorBundle (..), PosState (..))
import qualified Data.Text as Text
import qualified Dhall.Core as Core
import qualified Text.Megaparsec
import Dhall.Parser.Combinators
import Dhall.Parser.Expression
import Dhall.Parser.Token hiding (text)
expr :: Parser (Expr Src Import)
expr :: Parser (Expr Src Import)
expr = forall a. Parser a -> Parser (Expr Src a)
exprA (forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
Text.Megaparsec.try Parser Import
import_)
exprA :: Parser a -> Parser (Expr Src a)
exprA :: forall a. Parser a -> Parser (Expr Src a)
exprA = forall a. Parser a -> Parser (Expr Src a)
completeExpression
{-# DEPRECATED exprA "Support for parsing custom imports will be dropped in a future release" #-}
data ParseError = ParseError {
ParseError -> ParseErrorBundle Text Void
unwrap :: Text.Megaparsec.ParseErrorBundle Text Void
, ParseError -> Text
input :: Text
}
censor :: ParseError -> ParseError
censor :: ParseError -> ParseError
censor ParseError
parseError =
ParseError
parseError
{ unwrap :: ParseErrorBundle Text Void
unwrap =
(ParseError -> ParseErrorBundle Text Void
unwrap ParseError
parseError)
{ bundlePosState :: PosState Text
bundlePosState =
(forall s e. ParseErrorBundle s e -> PosState s
bundlePosState (ParseError -> ParseErrorBundle Text Void
unwrap ParseError
parseError))
{ pstateInput :: Text
pstateInput =
Text -> Text
Core.censorText
(forall s. PosState s -> s
pstateInput (forall s e. ParseErrorBundle s e -> PosState s
bundlePosState (ParseError -> ParseErrorBundle Text Void
unwrap ParseError
parseError)))
}
}
}
instance Show ParseError where
show :: ParseError -> String
show (ParseError {Text
ParseErrorBundle Text Void
input :: Text
unwrap :: ParseErrorBundle Text Void
input :: ParseError -> Text
unwrap :: ParseError -> ParseErrorBundle Text Void
..}) =
String
"\n\ESC[1;31mError\ESC[0m: Invalid input\n\n" forall a. Semigroup a => a -> a -> a
<> forall s e.
(VisualStream s, TraversableStream s, ShowErrorComponent e) =>
ParseErrorBundle s e -> String
Text.Megaparsec.errorBundlePretty ParseErrorBundle Text Void
unwrap
instance Exception ParseError
exprFromText
:: String
-> Text
-> Either ParseError (Expr Src Import)
exprFromText :: String -> Text -> Either ParseError (Expr Src Import)
exprFromText String
delta Text
text = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> b
snd (String -> Text -> Either ParseError (Header, Expr Src Import)
exprAndHeaderFromText String
delta Text
text)
newtype = Text deriving Int -> Header -> ShowS
[Header] -> ShowS
Header -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Header] -> ShowS
$cshowList :: [Header] -> ShowS
show :: Header -> String
$cshow :: Header -> String
showsPrec :: Int -> Header -> ShowS
$cshowsPrec :: Int -> Header -> ShowS
Show
createHeader :: Text -> Header
Text
text = Text -> Header
Header (Text
prefix forall a. Semigroup a => a -> a -> a
<> Text
newSuffix)
where
isWhitespace :: Char -> Bool
isWhitespace Char
c = Char
c forall a. Eq a => a -> a -> Bool
== Char
' ' Bool -> Bool -> Bool
|| Char
c forall a. Eq a => a -> a -> Bool
== Char
'\n' Bool -> Bool -> Bool
|| Char
c forall a. Eq a => a -> a -> Bool
== Char
'\r' Bool -> Bool -> Bool
|| Char
c forall a. Eq a => a -> a -> Bool
== Char
'\t'
prefix :: Text
prefix = (Char -> Bool) -> Text -> Text
Text.dropAround Char -> Bool
isWhitespace Text
text
newSuffix :: Text
newSuffix
| Text -> Bool
Text.null Text
prefix = Text
""
| Bool
otherwise = Text
"\n"
exprAndHeaderFromText
:: String
-> Text
-> Either ParseError (Header, Expr Src Import)
exprAndHeaderFromText :: String -> Text -> Either ParseError (Header, Expr Src Import)
exprAndHeaderFromText String
delta Text
text = case Either (ParseErrorBundle Text Void) (Text, Expr Src Import)
result of
Left ParseErrorBundle Text Void
errInfo -> forall a b. a -> Either a b
Left (ParseError { unwrap :: ParseErrorBundle Text Void
unwrap = ParseErrorBundle Text Void
errInfo, input :: Text
input = Text
text })
Right (Text
txt, Expr Src Import
r) -> forall a b. b -> Either a b
Right (Text -> Header
createHeader Text
txt, Expr Src Import
r)
where
parser :: Parser (Tokens Text, Expr Src Import)
parser = do
(Tokens Text
bytes, ()
_) <- forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> m (Tokens s, a)
Text.Megaparsec.match (forall (f :: * -> *) a. Alternative f => f a -> f [a]
many Parser ()
shebang forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser ()
whitespace)
Expr Src Import
r <- Parser (Expr Src Import)
expr
forall e s (m :: * -> *). MonadParsec e s m => m ()
Text.Megaparsec.eof
forall (m :: * -> *) a. Monad m => a -> m a
return (Tokens Text
bytes, Expr Src Import
r)
result :: Either (ParseErrorBundle Text Void) (Text, Expr Src Import)
result = forall e s a.
Parsec e s a -> String -> s -> Either (ParseErrorBundle s e) a
Text.Megaparsec.parse (forall a. Parser a -> Parsec Void Text a
unParser Parser (Text, Expr Src Import)
parser) String
delta Text
text