Fix formatting of POSIX env vars (#1552)

Fixes #1468.
This commit is contained in:
Simon Jakobi 2019-11-17 04:13:16 +01:00 committed by mergify[bot]
parent d4050aef27
commit 416160b29a
5 changed files with 22 additions and 2 deletions

View File

@ -21,6 +21,7 @@ module Dhall.Pretty.Internal (
, prettyVar
, pretty_
, escapeText_
, prettyEnvironmentVariable
, prettyConst
, prettyLabel
@ -409,11 +410,14 @@ alpha c = ('\x41' <= c && c <= '\x5A') || ('\x61' <= c && c <= '\x7A')
digit :: Char -> Bool
digit c = '\x30' <= c && c <= '\x39'
alphaNum :: Char -> Bool
alphaNum c = alpha c || digit c
headCharacter :: Char -> Bool
headCharacter c = alpha c || c == '_'
tailCharacter :: Char -> Bool
tailCharacter c = alpha c || digit c || c == '_' || c == '-' || c == '/'
tailCharacter c = alphaNum c || c == '_' || c == '-' || c == '/'
prettyLabelShared :: Bool -> Text -> Doc Ann
prettyLabelShared allowReserved a = label doc
@ -459,6 +463,17 @@ prettyVar :: Var -> Doc Ann
prettyVar (V x 0) = label (Pretty.unAnnotate (prettyLabel x))
prettyVar (V x n) = label (Pretty.unAnnotate (prettyLabel x <> "@" <> prettyInt n))
prettyEnvironmentVariable :: Text -> Doc ann
prettyEnvironmentVariable t
| validBashEnvVar t = Pretty.pretty t
| otherwise = Pretty.pretty ("\"" <> escapeText_ t <> "\"")
where
validBashEnvVar v = case Text.uncons v of
Nothing -> False
Just (c, v') ->
(alpha c || c == '_')
&& Text.all (\c' -> alphaNum c' || c' == '_') v'
{- There is a close correspondence between the pretty-printers in 'prettyCharacterSet'
and the sub-parsers in 'Dhall.Parser.Expression.parsers'. Most pretty-printers are
named after the corresponding parser and the relationship between pretty-printers

View File

@ -1,5 +1,6 @@
module Dhall.Pretty.Internal where
import Data.Text (Text)
import Data.Text.Prettyprint.Doc (Pretty, Doc)
import {-# SOURCE #-} Dhall.Syntax
@ -11,3 +12,5 @@ prettyVar :: Var -> Doc Ann
prettyConst :: Const -> Doc Ann
prettyExpr :: Pretty a => Expr s a -> Doc Ann
prettyEnvironmentVariable :: Text -> Doc ann

View File

@ -989,7 +989,7 @@ instance Pretty ImportType where
pretty (Remote url) = Pretty.pretty url
pretty (Env env) = "env:" <> Pretty.pretty env
pretty (Env env) = "env:" <> prettyEnvironmentVariable env
pretty Missing = "missing"

View File

@ -0,0 +1 @@
[ env:x, env:"1", env:" ", env:"\\", env:"." ]

View File

@ -0,0 +1 @@
[ env:x, env:"1", env:" ", env:"\\", env:"." ]