Merge pull request #27 from jwiegley/fix_quote_escaping
Minor fixes in pretty printing.
This commit is contained in:
commit
f90bcc0f46
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -130,6 +130,7 @@ escapeCodes =
|
|||
, ('\t', 't' )
|
||||
, ('\\', '\\')
|
||||
, ('$' , '$' )
|
||||
, ('"', '"')
|
||||
]
|
||||
|
||||
fromEscapeCode :: Char -> Maybe Char
|
||||
|
|
27
default.nix
27
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 <nixpkgs> {}, compiler ? "ghc7102" }:
|
||||
let
|
||||
haskellPackages = nixpkgs.pkgs.haskell.packages.${compiler};
|
||||
in
|
||||
|
||||
haskellPackages.callPackage ./project.nix {}
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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;
|
||||
}
|
Loading…
Reference in New Issue