@@ -31,6 +31,7 @@ module Dhall.Pretty.Internal (
31
31
, prettyEnvironmentVariable
32
32
33
33
, prettyConst
34
+ , UnescapedLabel (.. )
34
35
, escapeLabel
35
36
, prettyLabel
36
37
, prettyAnyLabel
@@ -518,26 +519,44 @@ headCharacter c = alpha c || c == '_'
518
519
tailCharacter :: Char -> Bool
519
520
tailCharacter c = alphaNum c || c == ' _' || c == ' -' || c == ' /'
520
521
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
+
521
531
-- | 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 =
524
534
case Text. uncons l of
525
535
Just (h, t)
526
- | headCharacter h && Text. all tailCharacter t && (notReservedIdentifier || (allowReserved && someOrNotLanguageKeyword)) && l /= " ?"
536
+ | headCharacter h && Text. all tailCharacter t && allowed && l /= " ?"
527
537
-> l
528
538
_ -> " `" <> 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)
532
546
533
- prettyLabelShared :: Bool -> Text -> Doc Ann
547
+ notReservedKeyword = not (Data.HashSet. member l reservedKeywords)
548
+
549
+ prettyLabelShared :: UnescapedLabel -> Text -> Doc Ann
534
550
prettyLabelShared b l = label (Pretty. pretty (escapeLabel b l))
535
551
536
552
prettyLabel :: Text -> Doc Ann
537
- prettyLabel = prettyLabelShared False
553
+ prettyLabel = prettyLabelShared NonReservedLabel
538
554
539
555
prettyAnyLabel :: Text -> Doc Ann
540
- prettyAnyLabel = prettyLabelShared True
556
+ prettyAnyLabel = prettyLabelShared AnyLabel
557
+
558
+ prettyAnyLabelOrSome :: Text -> Doc Ann
559
+ prettyAnyLabelOrSome = prettyLabelShared AnyLabelOrSome
541
560
542
561
prettyKeys
543
562
:: Foldable list
@@ -571,7 +590,7 @@ prettyKeys prettyK keys = Pretty.group (Pretty.flatAlt long short)
571
590
prettyLabels :: [Text ] -> Doc Ann
572
591
prettyLabels a
573
592
| null a = lbrace <> rbrace
574
- | otherwise = braces (map (duplicate . prettyAnyLabel ) a)
593
+ | otherwise = braces (map (duplicate . prettyAnyLabelOrSome ) a)
575
594
576
595
prettyNumber :: Integer -> Doc Ann
577
596
prettyNumber = literal . Pretty. pretty
@@ -846,7 +865,7 @@ prettyPrinters characterSet =
846
865
prettyKeyValue prettyKey prettyOperatorExpression equals
847
866
(makeKeyValue b c)
848
867
849
- prettyKey (WithLabel text) = prettyAnyLabel text
868
+ prettyKey (WithLabel text) = prettyAnyLabelOrSome text
850
869
prettyKey WithQuestion = syntax " ?"
851
870
prettyExpression (Assert a) =
852
871
Pretty. group (Pretty. flatAlt long short)
@@ -1558,7 +1577,7 @@ prettyPrinters characterSet =
1558
1577
prettyRecord :: Pretty a => Map Text (RecordField Src a ) -> Doc Ann
1559
1578
prettyRecord =
1560
1579
( braces
1561
- . map (prettyKeyValue prettyAnyLabel prettyExpression colon . adapt)
1580
+ . map (prettyKeyValue prettyAnyLabelOrSome prettyExpression colon . adapt)
1562
1581
. Map. toList
1563
1582
)
1564
1583
where
@@ -1615,14 +1634,14 @@ prettyPrinters characterSet =
1615
1634
| Var (V key' 0 ) <- Dhall.Syntax. shallowDenote val
1616
1635
, key == key'
1617
1636
, not (containsComment mSrc2) ->
1618
- duplicate (prettyKeys prettyAnyLabel [(mSrc0, key, mSrc1)])
1637
+ duplicate (prettyKeys prettyAnyLabelOrSome [(mSrc0, key, mSrc1)])
1619
1638
_ ->
1620
- prettyKeyValue prettyAnyLabel prettyExpression equals kv
1639
+ prettyKeyValue prettyAnyLabelOrSome prettyExpression equals kv
1621
1640
1622
1641
prettyAlternative (key, Just val) =
1623
- prettyKeyValue prettyAnyLabel prettyExpression colon (makeKeyValue (pure key) val)
1642
+ prettyKeyValue prettyAnyLabelOrSome prettyExpression colon (makeKeyValue (pure key) val)
1624
1643
prettyAlternative (key, Nothing ) =
1625
- duplicate (prettyAnyLabel key)
1644
+ duplicate (prettyAnyLabelOrSome key)
1626
1645
1627
1646
prettyUnion :: Pretty a => Map Text (Maybe (Expr Src a )) -> Doc Ann
1628
1647
prettyUnion =
0 commit comments