@@ -57,6 +57,7 @@ module Dhall.Core (
5757 , reservedIdentifiers
5858 , escapeText
5959 , subExpressions
60+ , pathCharacter
6061 ) where
6162
6263#if MIN_VERSION_base(4,8,0)
@@ -75,7 +76,7 @@ import Data.String (IsString(..))
7576import Data.Semigroup (Semigroup (.. ))
7677import Data.Sequence (Seq , ViewL (.. ), ViewR (.. ))
7778import Data.Text (Text )
78- import Data.Text.Prettyprint.Doc (Pretty )
79+ import Data.Text.Prettyprint.Doc (Doc , Pretty )
7980import Data.Traversable
8081import Dhall.Map (Map )
8182import Dhall.Set (Set )
@@ -131,10 +132,7 @@ instance Semigroup Directory where
131132 Directory (components₁ <> components₀)
132133
133134instance Pretty Directory where
134- pretty (Directory {.. }) =
135- foldMap prettyComponent (reverse components)
136- where
137- prettyComponent text = " /" <> Pretty. pretty text
135+ pretty (Directory {.. }) = foldMap prettyPathComponent (reverse components)
138136
139137{-| A `File` is a `directory` followed by one additional path component
140138 representing the `file` name
@@ -145,7 +143,9 @@ data File = File
145143 } deriving (Eq , Generic , Ord , Show )
146144
147145instance Pretty File where
148- pretty (File {.. }) = Pretty. pretty directory <> " /" <> Pretty. pretty file
146+ pretty (File {.. }) =
147+ Pretty. pretty directory
148+ <> prettyPathComponent file
149149
150150instance Semigroup File where
151151 File directory₀ _ <> File directory₁ file =
@@ -2331,3 +2331,28 @@ subExpressions f (Project a b) = Project <$> f a <*> pure b
23312331subExpressions f (Note a b) = Note a <$> f b
23322332subExpressions f (ImportAlt l r) = ImportAlt <$> f l <*> f r
23332333subExpressions _ (Embed a) = pure (Embed a)
2334+
2335+ {-| Returns `True` if the given `Char` is valid within an unquoted path
2336+ component
2337+
2338+ This is exported for reuse within the @"Dhall.Parser.Token"@ module
2339+ -}
2340+ pathCharacter :: Char -> Bool
2341+ pathCharacter c =
2342+ ' \x21 ' == c
2343+ || (' \x24 ' <= c && c <= ' \x27 ' )
2344+ || (' \x2A ' <= c && c <= ' \x2B ' )
2345+ || (' \x2D ' <= c && c <= ' \x2E ' )
2346+ || (' \x30 ' <= c && c <= ' \x3B ' )
2347+ || c == ' \x3D '
2348+ || (' \x40 ' <= c && c <= ' \x5A ' )
2349+ || (' \x5E ' <= c && c <= ' \x7A ' )
2350+ || c == ' \x7C '
2351+ || c == ' \x7E '
2352+
2353+ prettyPathComponent :: Text -> Doc ann
2354+ prettyPathComponent text
2355+ | Data.Text. all pathCharacter text =
2356+ " /" <> Pretty. pretty text
2357+ | otherwise =
2358+ " /\" " <> Pretty. pretty text <> " \" "
0 commit comments