Fix pretty printing of escape codes in strings
A line break in a string should not be outputed as a literal linebreak, but instead must be outputed as an escape code.
This commit is contained in:
parent
ba744bf166
commit
52a97733d2
|
@ -154,15 +154,7 @@ nixString = NString . merge <$> (char '"' *> manyTill stringChar (symbolic '"'))
|
|||
<|> Plain . singleton <$> char '$'
|
||||
<|> Plain . pack <$> some (noneOf "\"\\$")
|
||||
|
||||
escapeCode = choice $ map (\(x,y) -> x <$ char y)
|
||||
[ ('\n', 'n')
|
||||
, ('\r', 'r')
|
||||
, ('\t', 't')
|
||||
, ('\\', '\\')
|
||||
, ('$' , '$')
|
||||
, ('"' , '"')
|
||||
, ('\'', '\'')
|
||||
]
|
||||
escapeCode = choice [ c <$ char e | (c,e) <- escapeCodes ]
|
||||
|
||||
argExpr :: Parser (Formals NExpr)
|
||||
argExpr = choice
|
||||
|
|
|
@ -47,8 +47,9 @@ wrapParens op sub
|
|||
|
||||
prettyString :: NString NixDoc -> Doc
|
||||
prettyString (NString parts) = dquotes . hcat . map prettyPart $ parts
|
||||
where prettyPart (Plain t) = text . unpack $ t
|
||||
where prettyPart (Plain t) = text . concatMap escape . unpack $ t
|
||||
prettyPart (Antiquoted r) = text "$" <> braces (withoutParens r)
|
||||
escape x = maybe [x] (('\\':) . (:[])) $ toEscapeCode x
|
||||
|
||||
prettyFormals :: Formals NixDoc -> Doc
|
||||
prettyFormals (FormalName n) = text $ unpack n
|
||||
|
|
18
Nix/Types.hs
18
Nix/Types.hs
|
@ -17,6 +17,7 @@ import qualified Data.Map as Map
|
|||
import Data.Maybe (catMaybes)
|
||||
import Data.Text hiding (concat, concatMap, head, map, zipWith, reverse, intercalate)
|
||||
import Data.Traversable
|
||||
import Data.Tuple (swap)
|
||||
import GHC.Exts
|
||||
import GHC.Generics
|
||||
import Prelude hiding (readFile, concat, concatMap, elem, mapM,
|
||||
|
@ -60,6 +61,23 @@ runAntiquoted _ f (Antiquoted r) = f r
|
|||
newtype NString r = NString [Antiquoted Text r]
|
||||
deriving (Eq, Ord, Generic, Typeable, Data, Functor, Show)
|
||||
|
||||
escapeCodes :: [(Char, Char)]
|
||||
escapeCodes =
|
||||
[ ('\n', 'n' )
|
||||
, ('\r', 'r' )
|
||||
, ('\t', 't' )
|
||||
, ('\\', '\\')
|
||||
, ('$' , '$' )
|
||||
, ('"' , '"' )
|
||||
, ('\'', '\'')
|
||||
]
|
||||
|
||||
fromEscapeCode :: Char -> Maybe Char
|
||||
fromEscapeCode = (`lookup` map swap escapeCodes)
|
||||
|
||||
toEscapeCode :: Char -> Maybe Char
|
||||
toEscapeCode = (`lookup` escapeCodes)
|
||||
|
||||
instance IsString (NString r) where
|
||||
fromString = NString . (:[]) . Plain . pack
|
||||
|
||||
|
|
Loading…
Reference in a new issue