Add an internal "dhall haskell-syntax-tree" command (#1553)

… for debugging.
This commit is contained in:
Simon Jakobi 2019-11-22 04:57:14 +01:00 committed by mergify[bot]
parent 3b222cda91
commit feb378b8da
4 changed files with 53 additions and 4 deletions

View File

@ -431,6 +431,7 @@ Library
parsers >= 0.12.4 && < 0.13, parsers >= 0.12.4 && < 0.13,
prettyprinter >= 1.5.1 && < 1.6 , prettyprinter >= 1.5.1 && < 1.6 ,
prettyprinter-ansi-terminal >= 1.1.1 && < 1.2 , prettyprinter-ansi-terminal >= 1.1.1 && < 1.2 ,
pretty-simple < 4 ,
profunctors >= 3.1.2 && < 5.6 , profunctors >= 3.1.2 && < 5.6 ,
repline >= 0.2.1.0 && < 0.3 , repline >= 0.2.1.0 && < 0.3 ,
serialise >= 0.2.0.0 && < 0.3 , serialise >= 0.2.0.0 && < 0.3 ,

View File

@ -50,6 +50,7 @@ import qualified Data.Aeson
import qualified Data.Aeson.Encode.Pretty import qualified Data.Aeson.Encode.Pretty
import qualified Data.ByteString.Lazy import qualified Data.ByteString.Lazy
import qualified Data.ByteString.Lazy.Char8 import qualified Data.ByteString.Lazy.Char8
import qualified Data.Map
import qualified Data.Text import qualified Data.Text
import qualified Data.Text.IO import qualified Data.Text.IO
import qualified Data.Text.Prettyprint.Doc as Pretty import qualified Data.Text.Prettyprint.Doc as Pretty
@ -76,7 +77,7 @@ import qualified System.Exit as Exit
import qualified System.IO import qualified System.IO
import qualified System.FilePath import qualified System.FilePath
import qualified Text.Dot import qualified Text.Dot
import qualified Data.Map import qualified Text.Pretty.Simple
-- | Top-level program options -- | Top-level program options
data Options = Options data Options = Options
@ -130,6 +131,7 @@ data Mode
| Encode { file :: Input, json :: Bool } | Encode { file :: Input, json :: Bool }
| Decode { file :: Input, json :: Bool } | Decode { file :: Input, json :: Bool }
| Text { file :: Input } | Text { file :: Input }
| SyntaxTree { file :: Input }
data ResolveMode data ResolveMode
= Dot = Dot
@ -158,11 +160,12 @@ parseOptions =
f True = Censor f True = Censor
f False = NoCensor f False = NoCensor
subcommand :: String -> String -> Parser a -> Parser a subcommand' :: Bool -> String -> String -> Parser a -> Parser a
subcommand name description parser = subcommand' internal name description parser =
Options.Applicative.hsubparser Options.Applicative.hsubparser
( Options.Applicative.command name parserInfo ( Options.Applicative.command name parserInfo
<> Options.Applicative.metavar name <> Options.Applicative.metavar name
<> if internal then Options.Applicative.internal else mempty
) )
where where
parserInfo = parserInfo =
@ -171,6 +174,12 @@ subcommand name description parser =
<> Options.Applicative.progDesc description <> Options.Applicative.progDesc description
) )
subcommand :: String -> String -> Parser a -> Parser a
subcommand = subcommand' False
internalSubcommand :: String -> String -> Parser a -> Parser a
internalSubcommand = subcommand' True
parseMode :: Parser Mode parseMode :: Parser Mode
parseMode = parseMode =
subcommand subcommand
@ -229,6 +238,10 @@ parseMode =
"text" "text"
"Render a Dhall expression that evaluates to a Text literal" "Render a Dhall expression that evaluates to a Text literal"
(Text <$> parseFile) (Text <$> parseFile)
<|> internalSubcommand
"haskell-syntax-tree"
"Output the parsed syntax tree (for debugging)"
(SyntaxTree <$> parseFile)
<|> ( Default <|> ( Default
<$> parseFile <$> parseFile
<*> parseOutput <*> parseOutput
@ -745,7 +758,14 @@ command (Options {..}) = do
System.IO.withFile file System.IO.WriteMode (`Data.Text.IO.hPutStr` tags) System.IO.withFile file System.IO.WriteMode (`Data.Text.IO.hPutStr` tags)
StandardOutput -> Data.Text.IO.putStrLn tags StandardOutput -> Data.Text.IO.putStrLn tags
SyntaxTree {..} -> do
expression <- getExpression file
let denoted :: Expr Void Import
denoted = Dhall.Core.denote expression
Text.Pretty.Simple.pPrintNoColor denoted
-- | Entry point for the @dhall@ executable -- | Entry point for the @dhall@ executable
main :: IO () main :: IO ()

27
nix/pretty-simple.nix Normal file
View File

@ -0,0 +1,27 @@
{ mkDerivation, aeson, ansi-terminal, base, bytestring, Cabal
, cabal-doctest, containers, criterion, doctest, Glob, mtl
, optparse-applicative, QuickCheck, stdenv, template-haskell, text
, transformers
}:
mkDerivation {
pname = "pretty-simple";
version = "3.1.0.0";
sha256 = "ea8a062fe7a535a0778ea0f85442551b9a67fad95979fc3e4c617399452c775c";
configureFlags = [ "-fbuildexample" "-fbuildexe" ];
isLibrary = true;
isExecutable = true;
setupHaskellDepends = [ base Cabal cabal-doctest ];
libraryHaskellDepends = [
ansi-terminal base containers mtl text transformers
];
executableHaskellDepends = [
aeson base bytestring optparse-applicative text
];
testHaskellDepends = [
base doctest Glob QuickCheck template-haskell
];
benchmarkHaskellDepends = [ base criterion text ];
homepage = "https://github.com/cdepillabout/pretty-simple";
description = "pretty printer for data types with a 'Show' instance";
license = stdenv.lib.licenses.bsd3;
}

View File

@ -48,6 +48,7 @@ extra-deps:
- parser-combinators-1.0.0 - parser-combinators-1.0.0
- prettyprinter-1.5.1@sha256:fca87c3e2611d3499a0341a59857e9b424a23f31646e4737d535a18582284f96,5375 - prettyprinter-1.5.1@sha256:fca87c3e2611d3499a0341a59857e9b424a23f31646e4737d535a18582284f96,5375
- prettyprinter-ansi-terminal-1.1.1.2 - prettyprinter-ansi-terminal-1.1.1.2
- pretty-simple-3.1.0.0@sha256:d9d045de35be6a2d78a9b3b38d39b13caba96c0ef5ba9d4b44b9b7c2c50e7fa1
- primitive-0.6.3.0 - primitive-0.6.3.0
- process-1.6.2.0 - process-1.6.2.0
- profunctors-5.4 - profunctors-5.4