@@ -31,6 +31,7 @@ module Dhall.Pretty.Internal (
3131 , prettyEnvironmentVariable
3232
3333 , prettyConst
34+ , UnescapedLabel (.. )
3435 , escapeLabel
3536 , prettyLabel
3637 , prettyAnyLabel
@@ -518,26 +519,44 @@ headCharacter c = alpha c || c == '_'
518519tailCharacter :: Char -> Bool
519520tailCharacter c = alphaNum c || c == ' _' || c == ' -' || c == ' /'
520521
522+ -- | The set of labels which do not need to be escaped
523+ data UnescapedLabel
524+ = NonReservedLabel
525+ -- ^ This corresponds to the `nonreserved-label` rule in the grammar
526+ | AnyLabel
527+ -- ^ This corresponds to the `any-label` rule in the grammar
528+ | AnyLabelOrSome
529+ -- ^ This corresponds to the `any-label-or-some` rule in the grammar
530+
521531-- | Escape a label if it is not valid when unquoted
522- escapeLabel :: Bool -> Text -> Text
523- escapeLabel allowReserved l =
532+ escapeLabel :: UnescapedLabel -> Text -> Text
533+ escapeLabel allowedLabel l =
524534 case Text. uncons l of
525535 Just (h, t)
526- | headCharacter h && Text. all tailCharacter t && (notReservedIdentifier || (allowReserved && someOrNotLanguageKeyword)) && l /= " ?"
536+ | headCharacter h && Text. all tailCharacter t && allowed && l /= " ?"
527537 -> l
528538 _ -> " `" <> l <> " `"
529- where
530- notReservedIdentifier = not (Data.HashSet. member l reservedIdentifiers)
531- someOrNotLanguageKeyword = l == " Some" || not (Data.HashSet. member l reservedKeywords)
539+ where
540+ allowed = case allowedLabel of
541+ NonReservedLabel -> notReservedIdentifier
542+ AnyLabel -> notReservedKeyword
543+ AnyLabelOrSome -> notReservedKeyword || l == " Some"
544+
545+ notReservedIdentifier = not (Data.HashSet. member l reservedIdentifiers)
532546
533- prettyLabelShared :: Bool -> Text -> Doc Ann
547+ notReservedKeyword = not (Data.HashSet. member l reservedKeywords)
548+
549+ prettyLabelShared :: UnescapedLabel -> Text -> Doc Ann
534550prettyLabelShared b l = label (Pretty. pretty (escapeLabel b l))
535551
536552prettyLabel :: Text -> Doc Ann
537- prettyLabel = prettyLabelShared False
553+ prettyLabel = prettyLabelShared NonReservedLabel
538554
539555prettyAnyLabel :: Text -> Doc Ann
540- prettyAnyLabel = prettyLabelShared True
556+ prettyAnyLabel = prettyLabelShared AnyLabel
557+
558+ prettyAnyLabelOrSome :: Text -> Doc Ann
559+ prettyAnyLabelOrSome = prettyLabelShared AnyLabelOrSome
541560
542561prettyKeys
543562 :: Foldable list
@@ -571,7 +590,7 @@ prettyKeys prettyK keys = Pretty.group (Pretty.flatAlt long short)
571590prettyLabels :: [Text ] -> Doc Ann
572591prettyLabels a
573592 | null a = lbrace <> rbrace
574- | otherwise = braces (map (duplicate . prettyAnyLabel ) a)
593+ | otherwise = braces (map (duplicate . prettyAnyLabelOrSome ) a)
575594
576595prettyNumber :: Integer -> Doc Ann
577596prettyNumber = literal . Pretty. pretty
@@ -846,7 +865,7 @@ prettyPrinters characterSet =
846865 prettyKeyValue prettyKey prettyOperatorExpression equals
847866 (makeKeyValue b c)
848867
849- prettyKey (WithLabel text) = prettyAnyLabel text
868+ prettyKey (WithLabel text) = prettyAnyLabelOrSome text
850869 prettyKey WithQuestion = syntax " ?"
851870 prettyExpression (Assert a) =
852871 Pretty. group (Pretty. flatAlt long short)
@@ -1558,7 +1577,7 @@ prettyPrinters characterSet =
15581577 prettyRecord :: Pretty a => Map Text (RecordField Src a ) -> Doc Ann
15591578 prettyRecord =
15601579 ( braces
1561- . map (prettyKeyValue prettyAnyLabel prettyExpression colon . adapt)
1580+ . map (prettyKeyValue prettyAnyLabelOrSome prettyExpression colon . adapt)
15621581 . Map. toList
15631582 )
15641583 where
@@ -1615,14 +1634,14 @@ prettyPrinters characterSet =
16151634 | Var (V key' 0 ) <- Dhall.Syntax. shallowDenote val
16161635 , key == key'
16171636 , not (containsComment mSrc2) ->
1618- duplicate (prettyKeys prettyAnyLabel [(mSrc0, key, mSrc1)])
1637+ duplicate (prettyKeys prettyAnyLabelOrSome [(mSrc0, key, mSrc1)])
16191638 _ ->
1620- prettyKeyValue prettyAnyLabel prettyExpression equals kv
1639+ prettyKeyValue prettyAnyLabelOrSome prettyExpression equals kv
16211640
16221641 prettyAlternative (key, Just val) =
1623- prettyKeyValue prettyAnyLabel prettyExpression colon (makeKeyValue (pure key) val)
1642+ prettyKeyValue prettyAnyLabelOrSome prettyExpression colon (makeKeyValue (pure key) val)
16241643 prettyAlternative (key, Nothing ) =
1625- duplicate (prettyAnyLabel key)
1644+ duplicate (prettyAnyLabelOrSome key)
16261645
16271646 prettyUnion :: Pretty a => Map Text (Maybe (Expr Src a )) -> Doc Ann
16281647 prettyUnion =
0 commit comments