2018-04-06 06:10:06 +02:00
|
|
|
{-# LANGUAGE LambdaCase #-}
|
2018-03-30 00:35:12 +02:00
|
|
|
{-# LANGUAGE MultiWayIf #-}
|
2018-04-04 02:15:54 +02:00
|
|
|
-- {-# LANGUAGE QuasiQuotes #-}
|
2018-03-30 00:35:12 +02:00
|
|
|
|
2015-06-23 23:53:07 +02:00
|
|
|
module Main where
|
|
|
|
|
2018-04-10 17:34:21 +02:00
|
|
|
import Control.DeepSeq
|
|
|
|
import qualified Control.Exception as Exc
|
2018-04-03 23:21:33 +02:00
|
|
|
import Control.Monad
|
2018-04-04 22:33:53 +02:00
|
|
|
import Control.Monad.ST
|
2018-04-11 06:29:48 +02:00
|
|
|
import Data.Text (Text, pack)
|
2018-04-10 02:12:16 +02:00
|
|
|
import qualified Data.Text.IO as Text
|
2018-04-03 23:21:33 +02:00
|
|
|
import qualified Nix
|
2018-04-11 06:29:48 +02:00
|
|
|
import Nix.Expr
|
2018-04-03 23:21:33 +02:00
|
|
|
import Nix.Lint
|
|
|
|
import Nix.Parser
|
|
|
|
import Nix.Pretty
|
2018-04-10 21:25:34 +02:00
|
|
|
import Nix.Stack (NixException(..))
|
2018-04-03 23:40:49 +02:00
|
|
|
-- import Nix.TH
|
2018-04-03 23:21:33 +02:00
|
|
|
import Options.Applicative hiding (ParserResult(..))
|
|
|
|
import System.IO
|
|
|
|
import Text.PrettyPrint.ANSI.Leijen hiding ((<$>))
|
2015-06-23 23:53:07 +02:00
|
|
|
|
2018-03-28 06:59:27 +02:00
|
|
|
data Options = Options
|
2018-04-10 18:02:02 +02:00
|
|
|
{ verbose :: Bool
|
|
|
|
, debug :: Bool
|
|
|
|
, evaluate :: Bool
|
|
|
|
, check :: Bool
|
2018-04-11 02:42:35 +02:00
|
|
|
, parse :: Bool
|
2018-04-10 18:02:02 +02:00
|
|
|
, parseOnly :: Bool
|
|
|
|
, ignoreErrors :: Bool
|
|
|
|
, expression :: Maybe Text
|
2018-04-11 06:29:48 +02:00
|
|
|
, arg :: [NExpr]
|
|
|
|
, argstr :: [Text]
|
2018-04-10 18:02:02 +02:00
|
|
|
, fromFile :: Maybe FilePath
|
|
|
|
, filePaths :: [FilePath]
|
2018-03-28 06:59:27 +02:00
|
|
|
}
|
2015-06-23 23:53:07 +02:00
|
|
|
|
2018-03-28 06:59:27 +02:00
|
|
|
mainOptions :: Parser Options
|
|
|
|
mainOptions = Options
|
|
|
|
<$> switch
|
|
|
|
( short 'v'
|
|
|
|
<> long "verbose"
|
|
|
|
<> help "Verbose output")
|
|
|
|
<*> switch
|
|
|
|
( short 'd'
|
|
|
|
<> long "debug"
|
|
|
|
<> help "Debug output")
|
|
|
|
<*> switch
|
|
|
|
( long "eval"
|
|
|
|
<> help "Whether to evaluate, or just pretty-print")
|
2018-03-29 01:27:35 +02:00
|
|
|
<*> switch
|
|
|
|
( long "check"
|
|
|
|
<> help "Whether to check for syntax errors after parsing")
|
2018-04-11 02:42:35 +02:00
|
|
|
<*> switch
|
|
|
|
( long "parse"
|
|
|
|
<> help "Whether to parse the file (also the default right now)")
|
2018-04-10 17:34:21 +02:00
|
|
|
<*> switch
|
|
|
|
( long "parse-only"
|
|
|
|
<> help "Whether to parse only, no pretty printing or checking")
|
2018-04-10 18:02:02 +02:00
|
|
|
<*> switch
|
|
|
|
( long "ignore-errors"
|
|
|
|
<> help "Continue parsing files, even if there are errors")
|
2018-03-28 06:59:27 +02:00
|
|
|
<*> optional (strOption
|
2018-04-11 02:42:35 +02:00
|
|
|
( short 'E'
|
2018-03-28 06:59:27 +02:00
|
|
|
<> long "expr"
|
|
|
|
<> help "Expression to parse or evaluate"))
|
2018-04-11 06:29:48 +02:00
|
|
|
<*> multiString
|
|
|
|
(\s -> case parseNixText (pack s) of
|
|
|
|
Success x -> pure x
|
|
|
|
Failure err -> errorWithoutStackTrace (show err))
|
|
|
|
( long "arg"
|
|
|
|
<> help "Argument to pass to an evaluated lambda")
|
|
|
|
<*> multiString (pure . pack)
|
|
|
|
( long "argstr"
|
|
|
|
<> help "Argument string to pass to an evaluated lambda")
|
2018-04-06 05:43:13 +02:00
|
|
|
<*> optional (strOption
|
|
|
|
( short 'f'
|
|
|
|
<> long "file"
|
|
|
|
<> help "Parse all of the files given in FILE; - means stdin"))
|
2018-04-06 00:17:45 +02:00
|
|
|
<*> many (strArgument (metavar "FILE" <> help "Path of file to parse"))
|
2018-04-11 06:29:48 +02:00
|
|
|
where
|
|
|
|
multiString f desc = many (option (str >>= f) desc)
|
2015-06-24 02:18:43 +02:00
|
|
|
|
2015-06-23 23:53:07 +02:00
|
|
|
main :: IO ()
|
|
|
|
main = do
|
2018-03-28 06:59:27 +02:00
|
|
|
opts <- execParser optsDef
|
2018-04-06 05:43:13 +02:00
|
|
|
case expression opts of
|
2018-04-10 02:12:16 +02:00
|
|
|
Just s -> handleResult opts Nothing (parseNixTextLoc s)
|
2018-04-06 05:43:13 +02:00
|
|
|
Nothing -> case fromFile opts of
|
|
|
|
Just "-" ->
|
|
|
|
mapM_ (processFile opts) =<< (lines <$> getContents)
|
|
|
|
Just path ->
|
|
|
|
mapM_ (processFile opts) =<< (lines <$> readFile path)
|
|
|
|
Nothing -> case filePaths opts of
|
|
|
|
[] ->
|
2018-04-10 02:12:16 +02:00
|
|
|
handleResult opts Nothing . parseNixTextLoc
|
|
|
|
=<< Text.getContents
|
2018-04-06 05:43:13 +02:00
|
|
|
["-"] ->
|
2018-04-10 02:12:16 +02:00
|
|
|
handleResult opts Nothing . parseNixTextLoc
|
|
|
|
=<< Text.getContents
|
2018-04-06 05:43:13 +02:00
|
|
|
paths ->
|
|
|
|
mapM_ (processFile opts) paths
|
|
|
|
where
|
|
|
|
optsDef :: ParserInfo Options
|
|
|
|
optsDef = info (helper <*> mainOptions)
|
|
|
|
(fullDesc <> progDesc "" <> header "hnix")
|
|
|
|
|
|
|
|
processFile opts path = do
|
2018-04-09 11:07:40 +02:00
|
|
|
-- putStrLn "Parsing file..."
|
2018-04-06 05:43:13 +02:00
|
|
|
eres <- parseNixFileLoc path
|
|
|
|
handleResult opts (Just path) eres
|
2018-03-28 06:59:27 +02:00
|
|
|
|
2018-04-03 23:24:16 +02:00
|
|
|
-- print . printNix =<< Nix.eval [nix|1 + 3|]
|
2018-04-03 23:21:33 +02:00
|
|
|
|
2018-04-06 05:43:13 +02:00
|
|
|
handleResult opts mpath = \case
|
2018-04-10 06:35:46 +02:00
|
|
|
Failure err ->
|
2018-04-10 18:02:02 +02:00
|
|
|
(if ignoreErrors opts
|
|
|
|
then hPutStrLn stderr
|
|
|
|
else errorWithoutStackTrace) $ "Parse failed: " ++ show err
|
2018-04-10 06:35:46 +02:00
|
|
|
|
2018-04-10 21:25:34 +02:00
|
|
|
Success expr -> Exc.catch (process expr) $ \case
|
|
|
|
NixEvalException msg -> errorWithoutStackTrace msg
|
|
|
|
where
|
|
|
|
process expr = do
|
2018-04-09 11:07:40 +02:00
|
|
|
-- expr <- Exc.evaluate $ force expr
|
|
|
|
-- putStrLn "Parsing file...done"
|
|
|
|
|
2018-04-03 23:21:33 +02:00
|
|
|
when (check opts) $
|
2018-04-04 22:33:53 +02:00
|
|
|
putStrLn $ runST $ Nix.runLintM . renderSymbolic
|
2018-04-11 06:01:48 +02:00
|
|
|
=<< Nix.lint expr
|
2018-04-04 22:33:53 +02:00
|
|
|
|
2018-04-11 06:29:48 +02:00
|
|
|
let _args = arg opts ++ map mkStr (argstr opts)
|
|
|
|
|
2018-03-30 10:11:27 +02:00
|
|
|
if | evaluate opts, debug opts ->
|
2018-04-03 23:21:33 +02:00
|
|
|
print =<< Nix.tracingEvalLoc mpath expr
|
2018-03-30 00:35:12 +02:00
|
|
|
| evaluate opts ->
|
2018-04-03 23:21:33 +02:00
|
|
|
putStrLn . printNix =<< Nix.evalLoc mpath expr
|
2018-03-30 00:35:12 +02:00
|
|
|
| debug opts ->
|
2018-04-10 02:12:16 +02:00
|
|
|
print $ stripAnnotation expr
|
2018-04-10 17:34:21 +02:00
|
|
|
| parseOnly opts ->
|
|
|
|
void $ Exc.evaluate $ force expr
|
2018-03-30 00:35:12 +02:00
|
|
|
| otherwise ->
|
2018-03-31 08:00:06 +02:00
|
|
|
displayIO stdout
|
|
|
|
. renderPretty 0.4 80
|
|
|
|
. prettyNix
|
|
|
|
. stripAnnotation $ expr
|