diff --git a/Nix/Parser/Library.hs b/Nix/Parser/Library.hs index f2bb20a..ede909b 100644 --- a/Nix/Parser/Library.hs +++ b/Nix/Parser/Library.hs @@ -3,6 +3,7 @@ {-# LANGUAGE OverloadedStrings #-} module Nix.Parser.Library ( module Nix.Parser.Library, module X) where +import Prelude import Control.Applicative import Control.Monad import Control.Monad.IO.Class diff --git a/Nix/Pretty.hs b/Nix/Pretty.hs index d8b055a..79c6f39 100644 --- a/Nix/Pretty.hs +++ b/Nix/Pretty.hs @@ -7,9 +7,11 @@ import Data.Map (toList) import Data.Maybe (isJust) import Data.Text (Text, unpack, replace, strip) import Nix.Types +import Nix.Parser.Library (reservedNames) import Text.PrettyPrint.ANSI.Leijen import qualified Data.Text as Text +import qualified Data.HashSet as HashSet -- | This type represents a pretty printed nix expression -- together with some information about the expression. @@ -54,7 +56,7 @@ prettyString :: NString NixDoc -> Doc prettyString (NString DoubleQuoted parts) = dquotes . hcat . map prettyPart $ parts where prettyPart (Plain t) = text . concatMap escape . unpack $ t prettyPart (Antiquoted r) = text "$" <> braces (withoutParens r) - escape '"' = "\"" + escape '"' = "\\\"" escape x = maybe [x] (('\\':) . (:[])) $ toEscapeCode x prettyString (NString Indented parts) = group $ nest 2 (squote <> squote <$$> content) <$$> squote <> squote @@ -80,7 +82,7 @@ prettyParamSet params = lbrace <+> middle <+> rbrace where prettyArgs = case params of FixedParamSet args -> map prettySetArg (toList args) - + VariadicParamSet args -> map prettySetArg (toList args) ++ [text "..."] middle = hcat $ punctuate (comma <> space) prettyArgs @@ -91,6 +93,8 @@ prettyBind (Inherit s ns) where scope = maybe empty ((<> space) . parens . withoutParens) s prettyKeyName :: NKeyName NixDoc -> Doc +prettyKeyName (StaticKey key) + | HashSet.member (unpack key) reservedNames = dquotes $ text $ unpack key prettyKeyName (StaticKey key) = text . unpack $ key prettyKeyName (DynamicKey key) = runAntiquoted prettyString withoutParens key @@ -131,7 +135,7 @@ prettyNix = withoutParens . cata phi where phi (NSet rec xs) = simpleExpr $ group $ nest 2 (vsep $ recPrefix rec <> lbrace : map prettyBind xs) <$> rbrace phi (NAbs args body) = leastPrecedence $ - (prettyFormals args <> colon) withoutParens body + (prettyFormals args <> colon) (nest 2 $ withoutParens body) phi (NOper oper) = prettyOper oper phi (NSelect r attr o) = (if isJust o then leastPrecedence else flip NixDoc selectOp) $ wrapParens selectOp r <> dot <> prettySelector attr <> ordoc diff --git a/Nix/Types.hs b/Nix/Types.hs index 6b8a29e..f886d1b 100644 --- a/Nix/Types.hs +++ b/Nix/Types.hs @@ -130,6 +130,7 @@ escapeCodes = , ('\t', 't' ) , ('\\', '\\') , ('$' , '$' ) + , ('"', '"') ] fromEscapeCode :: Char -> Maybe Char diff --git a/default.nix b/default.nix index 74776dd..45ab74e 100644 --- a/default.nix +++ b/default.nix @@ -1,21 +1,6 @@ -{ mkDerivation, ansi-wl-pprint, base, containers, data-fix, parsers -, stdenv, tasty, tasty-hunit, tasty-th, text, transformers -, trifecta, unordered-containers -}: -mkDerivation { - pname = "hnix"; - version = "0.2.1"; - src = ./.; - isLibrary = true; - isExecutable = true; - buildDepends = [ - ansi-wl-pprint base containers data-fix parsers text transformers - trifecta unordered-containers - ]; - testDepends = [ - base containers data-fix tasty tasty-hunit tasty-th text - ]; - homepage = "http://github.com/jwiegley/hnix"; - description = "Haskell implementation of the Nix language"; - license = stdenv.lib.licenses.bsd3; -} +{ nixpkgs ? import {}, compiler ? "ghc7102" }: +let + haskellPackages = nixpkgs.pkgs.haskell.packages.${compiler}; +in + +haskellPackages.callPackage ./project.nix {} diff --git a/hnix.cabal b/hnix.cabal index a62e80d..3ebff86 100644 --- a/hnix.cabal +++ b/hnix.cabal @@ -1,5 +1,5 @@ Name: hnix -Version: 0.2.1 +Version: 0.2.2 Synopsis: Haskell implementation of the Nix language Description: Haskell implementation of the Nix language. diff --git a/project.nix b/project.nix new file mode 100644 index 0000000..dbe0ed4 --- /dev/null +++ b/project.nix @@ -0,0 +1,21 @@ +{ mkDerivation, ansi-wl-pprint, base, containers, data-fix, parsers +, stdenv, tasty, tasty-hunit, tasty-th, text, transformers +, trifecta, unordered-containers, cabal-install +}: +mkDerivation { + pname = "hnix"; + version = "0.2.2"; + src = ./.; + isLibrary = true; + isExecutable = true; + buildDepends = [ + ansi-wl-pprint base containers data-fix parsers text transformers + trifecta unordered-containers cabal-install + ]; + testDepends = [ + base containers data-fix tasty tasty-hunit tasty-th text + ]; + homepage = "http://github.com/jwiegley/hnix"; + description = "Haskell implementation of the Nix language"; + license = stdenv.lib.licenses.bsd3; +} diff --git a/shell.nix b/shell.nix index 980160a..27b411d 100644 --- a/shell.nix +++ b/shell.nix @@ -1 +1,2 @@ -{ pkgs ? import {} }: pkgs.haskellPackages.callPackage ./default.nix {} +{ nixpkgs ? import {}, compiler ? "ghc7102" }: +(import ./default.nix { inherit nixpkgs compiler; }).env