2018-04-07 21:02:50 +02:00
|
|
|
{-# LANGUAGE ConstraintKinds #-}
|
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
2018-04-24 11:14:27 +02:00
|
|
|
{-# LANGUAGE GADTs #-}
|
2018-04-07 21:02:50 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
|
|
|
{-# LANGUAGE TypeApplications #-}
|
|
|
|
{-# LANGUAGE TypeFamilies #-}
|
2018-04-24 11:14:27 +02:00
|
|
|
{-# LANGUAGE TypeOperators #-}
|
2018-04-07 21:02:50 +02:00
|
|
|
|
2018-04-24 11:14:27 +02:00
|
|
|
module Nix.Render where
|
2018-04-07 21:02:50 +02:00
|
|
|
|
2018-04-10 18:03:24 +02:00
|
|
|
import Data.ByteString (ByteString)
|
|
|
|
import Data.List.NonEmpty (NonEmpty((:|)))
|
|
|
|
import qualified Data.Set as Set
|
|
|
|
import Data.Void
|
|
|
|
import Nix.Expr.Types.Annotated
|
2018-05-07 21:06:56 +02:00
|
|
|
import Text.Megaparsec.Error
|
|
|
|
import Text.Megaparsec.Pos (SourcePos(..))
|
|
|
|
import Text.PrettyPrint.ANSI.Leijen
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
|
|
class Monad m => MonadFile m where
|
|
|
|
readFile :: FilePath -> m ByteString
|
|
|
|
|
2018-04-21 07:36:40 +02:00
|
|
|
posAndMsg :: SourcePos -> Doc -> ParseError t Void
|
|
|
|
posAndMsg beg msg =
|
2018-04-18 02:25:59 +02:00
|
|
|
FancyError (beg :| [])
|
2018-04-21 07:36:40 +02:00
|
|
|
(Set.fromList [ErrorFail (show msg) :: ErrorFancy Void])
|
2018-04-18 02:25:59 +02:00
|
|
|
|
2018-04-24 11:14:27 +02:00
|
|
|
renderLocation :: MonadFile m => SrcSpan -> Doc -> m Doc
|
2018-04-21 07:36:40 +02:00
|
|
|
renderLocation (SrcSpan beg@(SourcePos "<string>" _ _) _) msg =
|
2018-04-28 01:36:24 +02:00
|
|
|
return $ text $ init $ parseErrorPretty @Char (posAndMsg beg msg)
|
2018-04-18 02:25:59 +02:00
|
|
|
|
|
|
|
renderLocation (SrcSpan beg@(SourcePos path _ _) _) msg = do
|
2018-04-24 11:14:27 +02:00
|
|
|
contents <- Nix.Render.readFile path
|
2018-04-28 01:36:24 +02:00
|
|
|
return $ text $ init $ parseErrorPretty' contents (posAndMsg beg msg)
|