Skip to content

Commit 416160b

Browse files
sjakobimergify[bot]
authored andcommitted
Fix formatting of POSIX env vars (#1552)
Fixes #1468.
1 parent d4050ae commit 416160b

File tree

5 files changed

+22
-2
lines changed

5 files changed

+22
-2
lines changed

dhall/src/Dhall/Pretty/Internal.hs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module Dhall.Pretty.Internal (
2121
, prettyVar
2222
, pretty_
2323
, escapeText_
24+
, prettyEnvironmentVariable
2425

2526
, prettyConst
2627
, prettyLabel
@@ -409,11 +410,14 @@ alpha c = ('\x41' <= c && c <= '\x5A') || ('\x61' <= c && c <= '\x7A')
409410
digit :: Char -> Bool
410411
digit c = '\x30' <= c && c <= '\x39'
411412

413+
alphaNum :: Char -> Bool
414+
alphaNum c = alpha c || digit c
415+
412416
headCharacter :: Char -> Bool
413417
headCharacter c = alpha c || c == '_'
414418

415419
tailCharacter :: Char -> Bool
416-
tailCharacter c = alpha c || digit c || c == '_' || c == '-' || c == '/'
420+
tailCharacter c = alphaNum c || c == '_' || c == '-' || c == '/'
417421

418422
prettyLabelShared :: Bool -> Text -> Doc Ann
419423
prettyLabelShared allowReserved a = label doc
@@ -459,6 +463,17 @@ prettyVar :: Var -> Doc Ann
459463
prettyVar (V x 0) = label (Pretty.unAnnotate (prettyLabel x))
460464
prettyVar (V x n) = label (Pretty.unAnnotate (prettyLabel x <> "@" <> prettyInt n))
461465

466+
prettyEnvironmentVariable :: Text -> Doc ann
467+
prettyEnvironmentVariable t
468+
| validBashEnvVar t = Pretty.pretty t
469+
| otherwise = Pretty.pretty ("\"" <> escapeText_ t <> "\"")
470+
where
471+
validBashEnvVar v = case Text.uncons v of
472+
Nothing -> False
473+
Just (c, v') ->
474+
(alpha c || c == '_')
475+
&& Text.all (\c' -> alphaNum c' || c' == '_') v'
476+
462477
{- There is a close correspondence between the pretty-printers in 'prettyCharacterSet'
463478
and the sub-parsers in 'Dhall.Parser.Expression.parsers'. Most pretty-printers are
464479
named after the corresponding parser and the relationship between pretty-printers

dhall/src/Dhall/Pretty/Internal.hs-boot

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module Dhall.Pretty.Internal where
22

3+
import Data.Text (Text)
34
import Data.Text.Prettyprint.Doc (Pretty, Doc)
45

56
import {-# SOURCE #-} Dhall.Syntax
@@ -11,3 +12,5 @@ prettyVar :: Var -> Doc Ann
1112
prettyConst :: Const -> Doc Ann
1213

1314
prettyExpr :: Pretty a => Expr s a -> Doc Ann
15+
16+
prettyEnvironmentVariable :: Text -> Doc ann

dhall/src/Dhall/Syntax.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,7 @@ instance Pretty ImportType where
989989

990990
pretty (Remote url) = Pretty.pretty url
991991

992-
pretty (Env env) = "env:" <> Pretty.pretty env
992+
pretty (Env env) = "env:" <> prettyEnvironmentVariable env
993993

994994
pretty Missing = "missing"
995995

dhall/tests/format/envVarsA.dhall

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[ env:x, env:"1", env:" ", env:"\\", env:"." ]

dhall/tests/format/envVarsB.dhall

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[ env:x, env:"1", env:" ", env:"\\", env:"." ]

0 commit comments

Comments
 (0)