diff --git a/dhall/dhall.cabal b/dhall/dhall.cabal index cd150cb..f2757e5 100644 --- a/dhall/dhall.cabal +++ b/dhall/dhall.cabal @@ -431,6 +431,7 @@ Library parsers >= 0.12.4 && < 0.13, prettyprinter >= 1.5.1 && < 1.6 , prettyprinter-ansi-terminal >= 1.1.1 && < 1.2 , + pretty-simple < 4 , profunctors >= 3.1.2 && < 5.6 , repline >= 0.2.1.0 && < 0.3 , serialise >= 0.2.0.0 && < 0.3 , diff --git a/dhall/src/Dhall/Main.hs b/dhall/src/Dhall/Main.hs index 32fb98c..6d8ce0f 100644 --- a/dhall/src/Dhall/Main.hs +++ b/dhall/src/Dhall/Main.hs @@ -50,6 +50,7 @@ import qualified Data.Aeson import qualified Data.Aeson.Encode.Pretty import qualified Data.ByteString.Lazy import qualified Data.ByteString.Lazy.Char8 +import qualified Data.Map import qualified Data.Text import qualified Data.Text.IO 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.FilePath import qualified Text.Dot -import qualified Data.Map +import qualified Text.Pretty.Simple -- | Top-level program options data Options = Options @@ -130,6 +131,7 @@ data Mode | Encode { file :: Input, json :: Bool } | Decode { file :: Input, json :: Bool } | Text { file :: Input } + | SyntaxTree { file :: Input } data ResolveMode = Dot @@ -158,11 +160,12 @@ parseOptions = f True = Censor f False = NoCensor -subcommand :: String -> String -> Parser a -> Parser a -subcommand name description parser = +subcommand' :: Bool -> String -> String -> Parser a -> Parser a +subcommand' internal name description parser = Options.Applicative.hsubparser ( Options.Applicative.command name parserInfo <> Options.Applicative.metavar name + <> if internal then Options.Applicative.internal else mempty ) where parserInfo = @@ -171,6 +174,12 @@ subcommand name description parser = <> 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 = subcommand @@ -229,6 +238,10 @@ parseMode = "text" "Render a Dhall expression that evaluates to a Text literal" (Text <$> parseFile) + <|> internalSubcommand + "haskell-syntax-tree" + "Output the parsed syntax tree (for debugging)" + (SyntaxTree <$> parseFile) <|> ( Default <$> parseFile <*> parseOutput @@ -745,7 +758,14 @@ command (Options {..}) = do System.IO.withFile file System.IO.WriteMode (`Data.Text.IO.hPutStr` 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 main :: IO () diff --git a/nix/pretty-simple.nix b/nix/pretty-simple.nix new file mode 100644 index 0000000..df6235b --- /dev/null +++ b/nix/pretty-simple.nix @@ -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; +} diff --git a/stack-lts-6.yaml b/stack-lts-6.yaml index 9353c74..e87d31b 100644 --- a/stack-lts-6.yaml +++ b/stack-lts-6.yaml @@ -48,6 +48,7 @@ extra-deps: - parser-combinators-1.0.0 - prettyprinter-1.5.1@sha256:fca87c3e2611d3499a0341a59857e9b424a23f31646e4737d535a18582284f96,5375 - prettyprinter-ansi-terminal-1.1.1.2 +- pretty-simple-3.1.0.0@sha256:d9d045de35be6a2d78a9b3b38d39b13caba96c0ef5ba9d4b44b9b7c2c50e7fa1 - primitive-0.6.3.0 - process-1.6.2.0 - profunctors-5.4