Correctly escape double quotes. Update nix files. Use quotes with reserved variable bindings

This commit is contained in:
Allen Nelson 2015-10-15 18:33:26 -05:00
parent c8173f0abd
commit 892cad2b36
7 changed files with 39 additions and 26 deletions

View File

@ -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

View File

@ -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

View File

@ -130,6 +130,7 @@ escapeCodes =
, ('\t', 't' )
, ('\\', '\\')
, ('$' , '$' )
, ('"', '"')
]
fromEscapeCode :: Char -> Maybe Char

View File

@ -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 {}

View File

@ -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.

21
project.nix Normal file
View File

@ -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;
}

View File

@ -1 +1,2 @@
{ pkgs ? import <nixpkgs> {} }: pkgs.haskellPackages.callPackage ./default.nix {}
{ nixpkgs ? import <nixpkgs> {}, compiler ? "ghc7102" }:
(import ./default.nix { inherit nixpkgs compiler; }).env