diff --git a/dhall/src/Dhall/Pretty/Internal.hs b/dhall/src/Dhall/Pretty/Internal.hs index 8ae5347..9b07f47 100644 --- a/dhall/src/Dhall/Pretty/Internal.hs +++ b/dhall/src/Dhall/Pretty/Internal.hs @@ -1147,13 +1147,16 @@ prettyCharacterSet characterSet expression = -- >>> multilineChunks (Chunks [] "\n\NUL\b\f\t") -- Chunks [("\n",TextLit (Chunks [] "\NUL\b\f"))] "\t" multilineChunks :: Chunks s a -> Chunks s a -multilineChunks = escapeControlCharacters . escapeLastLineLeadingWhitespace +multilineChunks = + escapeTrailingSingleQuote + . escapeControlCharacters + . escapeLastLineLeadingWhitespace -- | Escape leading whitespace on the last line by moving it into a string --- string interpolation +-- interpolation -- --- Unescaped leading whitespace on the last line would otherwise be removed --- by the parser's dedentation logic. +-- This ensures that the parser can find the correct indentation level, no matter +-- what the other lines contain.- -- -- >>> escapeLastLineLeadingWhitespace (Chunks [] "\n \tx") -- Chunks [("\n",TextLit (Chunks [] " \t"))] "x" @@ -1231,6 +1234,19 @@ splitOnPredicate p t = case Text.break p t of (c, d) -> case splitOnPredicate p d of (e, f) -> ((a, c) : e, f) +-- | Escape a trailing single quote by moving it into a string interpolation +-- +-- Otherwise the multiline-string would end with @'''@, which would be parsed +-- as an escaped @''@. +-- +-- >>> escapeTrailingSingleQuote (Chunks [] "\n'") +-- Chunks [("\n",TextLit (Chunks [] "'"))] "" +escapeTrailingSingleQuote :: Chunks s a -> Chunks s a +escapeTrailingSingleQuote chunks@(Chunks as b) = + case Text.unsnoc b of + Just (b', '\'') -> Chunks (as ++ [(b', TextLit (Chunks [] "'"))]) "" + _ -> chunks + -- | Pretty-print a value pretty_ :: Pretty a => a -> Text pretty_ = prettyToStrictText diff --git a/dhall/tests/format/multilineTrailingSingleQuoteA.dhall b/dhall/tests/format/multilineTrailingSingleQuoteA.dhall new file mode 100644 index 0000000..51189da --- /dev/null +++ b/dhall/tests/format/multilineTrailingSingleQuoteA.dhall @@ -0,0 +1 @@ +"\n'" diff --git a/dhall/tests/format/multilineTrailingSingleQuoteB.dhall b/dhall/tests/format/multilineTrailingSingleQuoteB.dhall new file mode 100644 index 0000000..c0a7256 --- /dev/null +++ b/dhall/tests/format/multilineTrailingSingleQuoteB.dhall @@ -0,0 +1,3 @@ +'' + +${"'"}''