[Git][ghc/ghc][master] 5 commits: haddock-library: fix the build of the spec and fixtures test suites
Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC Commits: 51f38f3a by Sylvain Henry at 2026-11-11T13:58:14-05:00 haddock-library: fix the build of the spec and fixtures test suites The documentation pipeline moved from String to Text (commit 202ed264141) but the two test suites of haddock-library were left behind and no longer compiled. They are not run by GHC's CI (see #16206), so this went unnoticed. The fixtures suite still has four pre-existing failures (list-blocks1, table1, table4, table5) that are not addressed here. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> - - - - - 394d9d6d by Sylvain Henry at 2026-11-11T13:58:14-05:00 haddock: parse backtick spans as monospaced text A backtick span whose content is not a valid Haskell identifier, such as `foo bar`, was rendered as plain text with literal backticks. It is now parsed as monospaced text, like @foo bar@. A closed backtick span whose whole content is one identifier keeps its meaning as a hyperlinked identifier. A span such as `foldl' f z xs` is code: it is no longer read as the identifier foldl between a backtick and a single quote. The TeX-style form `foo' stays an identifier when no backtick closes the span on the same line. Code spans follow the CommonMark rules: a span opened by a run of N backticks ends at the next run of exactly N backticks on the same line, so ``x `plus` y`` is code "x `plus` y". One space is removed at each end when both are present, and an unclosed opening run is plain text. The content is literal: no markup is parsed and backslashes are kept, so `\x -> x` renders as written. See Note [Backtick code spans] in Documentation.Haddock.Parser. Fixes #28090 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> - - - - - 7ae41c37 by Sylvain Henry at 2026-11-11T13:58:14-05:00 Use 'T' instead of `T' in Haddock comments next to backtick spans With backtick code spans, a TeX-style quote `T' followed by a backtick on the same line opens a code span up to that backtick. Use the 'T' form in the Haddock comments where this happens. See Note [Backtick code spans] in Documentation.Haddock.Parser. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> - - - - - ce96f0b6 by Sylvain Henry at 2026-11-11T13:58:14-05:00 haddock: do not parse backtick spans inside @...@ Text between @...@ is already code, so backtick code spans are no longer parsed there. Thus in @x <- f -- of type `M Bool`@ the backticks stay as text. Inline markup inside @...@, such as /.../, passes this context down. See Note [Backticks in code] in Documentation.Haddock.Parser. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> - - - - - 744b3036 by Sylvain Henry at 2026-11-11T13:58:15-05:00 haddock: keep the backticks of infix identifiers inside @...@ A backticked identifier inside @...@ was always rendered without its backticks, so @f `elem` xs@ rendered as "f elem xs". Now an identifier in an infix application or an operator section, such as @f `elem` xs@ or @(`elem` xs)@, is parsed in infix form, as if written ``elem``, and renders with its backticks and with elem linked. Elsewhere, as in @`foldlM` :: ...@, the backticks still only mark the link. See Note [Backticks in code] in Documentation.Haddock.Parser. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> - - - - - 15 changed files: - compiler/Language/Haskell/Syntax/Decls/Foreign.hs - libraries/base/src/Data/Foldable.hs - utils/haddock/CHANGES.md - utils/haddock/doc/markup.rst - utils/haddock/haddock-api/haddock-api.cabal - utils/haddock/haddock-library/CHANGES.md - utils/haddock/haddock-library/fixtures/Fixtures.hs - + utils/haddock/haddock-library/fixtures/examples/codeBackticks.input - + utils/haddock/haddock-library/fixtures/examples/codeBackticks.parsed - + utils/haddock/haddock-library/fixtures/examples/codeDoubleBackticks.input - + utils/haddock/haddock-library/fixtures/examples/codeDoubleBackticks.parsed - utils/haddock/haddock-library/haddock-library.cabal - utils/haddock/haddock-library/src/Documentation/Haddock/Parser.hs - utils/haddock/haddock-library/src/Documentation/Haddock/Parser/Identifier.hs - utils/haddock/haddock-library/test/Documentation/Haddock/ParserSpec.hs Changes: ===================================== compiler/Language/Haskell/Syntax/Decls/Foreign.hs ===================================== @@ -130,14 +130,14 @@ data ForeignImport pass -- * the two strings specifying a header file or library -- may be empty, which indicates the absence of a -- header or object specification (both are not used - -- in the case of `CWrapper' and when `CFunction' + -- in the case of 'CWrapper' and when 'CFunction' -- has a dynamic target) -- -- * the calling convention is irrelevant for code - -- generation in the case of `CLabel', but is needed + -- generation in the case of 'CLabel', but is needed -- for pretty printing -- - -- * `Safety' is irrelevant for `CLabel' and `CWrapper' + -- * 'Safety' is irrelevant for 'CLabel' and 'CWrapper' -- CImport (XCImport pass) ===================================== libraries/base/src/Data/Foldable.hs ===================================== @@ -1075,7 +1075,7 @@ import GHC.Internal.Data.Foldable -- abstract potentially faster than linear search over general container -- structures. This can be achieved by defining an additional type class (e.g. -- @HasMember@ below). Instances of such a type class (that are also --- `Foldable') can employ the `elem` linear search as a last resort, when +-- 'Foldable') can employ the `elem` linear search as a last resort, when -- faster search is not supported. -- -- > {-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-} ===================================== utils/haddock/CHANGES.md ===================================== @@ -1,5 +1,18 @@ ## Changes in next + * Backtick spans that are not valid identifiers, e.g. `` `foo bar` ``, are now + rendered as monospaced text, like `@foo bar@`, instead of plain text. The + content of such a span is taken literally. As in Markdown, a span opened + with N backticks closes with N backticks, e.g. ``` ``x `plus` y`` ```. + A closed span is a link only when its whole content is one identifier, so + `` `foldl' f z xs` `` is code, not a link to `foldl`. (#28090) + + * Backtick spans are not parsed inside `@...@`: their backticks stay as + text. An identifier between backticks inside `@...@` in an infix + application or an operator section, as in `` @f `elem` xs@ `` or + `` @(`elem` xs)@ ``, now keeps its backticks in the + output instead of rendering as `f elem xs`. + * Allow the `--incremental` flag for modules in other units than main. In the initial implementation, the argument to incremental could only be the name of a module in the main unit. Now this can be changed by ===================================== utils/haddock/doc/markup.rst ===================================== @@ -906,6 +906,8 @@ Haddock provides markup for properties: :: This allows third-party applications to extract and verify them. +.. _hyperlinked-identifiers: + Hyperlinked Identifiers ~~~~~~~~~~~~~~~~~~~~~~~ @@ -981,6 +983,51 @@ Frequent special cases: * ``@__FILE__@`` will render ``FILE`` in bold with no underscores, which may not be what you had in mind. +Monospaced text can also be written between backticks, as in Markdown: :: + + -- | Run `ghc -O2 Main.hs` to build it. + +A backtick span is interpreted as a hyperlinked identifier when its +content is a valid Haskell identifier (see :ref:`hyperlinked-identifiers`), +and as monospaced text otherwise. Both render in a typewriter font, so +```foo``` and ```foo bar``` look alike; the first is also a link when +``foo`` is in scope. + +Unlike ``@...@``, no markup is interpreted inside a backtick span: the +content is taken literally, backslashes included: :: + + -- | The span `/not emphasized/` is literal, and so is `\x -> x`. + +As in Markdown, a span can start with more than one backtick. It ends +at the next run of the same number of backticks, so a span with +backticks inside, such as an infix application, uses double backticks. +When the content both starts and ends with a space, one space is removed +at each end: :: + + -- | Write ``x `plus` y`` or `` `plus` x y ``. + +The span must be non-empty and closed on the same line; otherwise the +opening backticks are plain text. + +A closed backtick span is a hyperlinked identifier only when the whole +content is one identifier. Thus ```foldl' f z xs``` is monospaced text, +not a link to ``foldl`` closed by the single quote. The mixed form +```T'`` is still a link when no backtick closes the span on the same +line. Where it does, use ``'T'`` instead: :: + + -- | 'Foldable' and `elem`. + +Inside ``@...@``, the text is already code, so backtick spans are not +parsed and their backticks stay as text: :: + + -- | @x <- f -- of type `M Bool`@ + +A backticked identifier inside ``@...@`` is still a hyperlinked +identifier. When it is Haskell infix syntax, in an infix application or +an operator section, the backticks are shown: ``@f `elem` xs@`` renders +as ``f `elem` xs`` with ``elem`` linked, and so does ``@(`elem` xs)@``. Elsewhere the backticks only mark +the link, as in ``@`foldlM` :: ...@``. + Linking to Modules ~~~~~~~~~~~~~~~~~~ ===================================== utils/haddock/haddock-api/haddock-api.cabal ===================================== @@ -81,7 +81,7 @@ library -- this package typically supports only single major versions build-depends: base >= 4.16 && < 4.24 , ghc ^>= 10.1 - , haddock-library ^>= 1.11 + , haddock-library ^>= 1.12 , xhtml ^>= 3000.4.0.0 , parsec ^>= 3.1.13.0 , text >= 2.0 && < 2.2 @@ -214,7 +214,7 @@ test-suite spec build-depends: ghc ^>= 9.7 , ghc-paths ^>= 0.1.0.12 - , haddock-library ^>= 1.11 + , haddock-library ^>= 1.12 , xhtml ^>= 3000.4.0.0 , hspec ^>= 2.9 , parsec ^>= 3.1.13.0 ===================================== utils/haddock/haddock-library/CHANGES.md ===================================== @@ -1,3 +1,18 @@ +## Changes in version 1.12.0 + + * Backtick spans that are not valid identifiers, e.g. `` `foo bar` ``, are + now parsed as monospaced text (like `@foo bar@`) instead of plain text. + The content of such a span is taken literally. As in Markdown, a span + opened with N backticks closes with N backticks, e.g. + ``` ``x `plus` y`` ```. A closed span is an identifier only when its + whole content is one identifier, so `` `foldl' f z xs` `` is code, not + the identifier `foldl`. (#28090) + + * Backtick spans are not parsed inside `@...@`. An identifier between + backticks inside `@...@` in an infix application or an operator section, + as in `` @f `elem` xs@ `` or `` @(`elem` xs)@ ``, is now parsed in infix form (like ``` ``elem`` ```), + so its backticks are rendered. + ## Changes in version 1.11.0 * Add support for linking identifiers with a quote between backticks (#1408) ===================================== utils/haddock/haddock-library/fixtures/Fixtures.hs ===================================== @@ -9,6 +9,7 @@ import Control.Exception (IOException, catch) import Control.Monad (when) import Data.Foldable (traverse_) import qualified Data.List as List +import qualified Data.Text as T import Data.Traversable (for) import Data.TreeDiff import Data.TreeDiff.Golden @@ -102,7 +103,7 @@ acceptFixtures = traverse_ $ \(Fixture i o) -> do writeFile o actual parseString :: String -> Doc String -parseString = Parse.toRegular . _doc . Parse.parseParas Nothing +parseString = Parse.toRegular . _doc . Parse.parseParas Nothing . T.pack data Cmd = CmdRun | CmdAccept | CmdList ===================================== utils/haddock/haddock-library/fixtures/examples/codeBackticks.input ===================================== @@ -0,0 +1 @@ +Run `ghc -O2 Main.hs` and then `main`. ===================================== utils/haddock/haddock-library/fixtures/examples/codeBackticks.parsed ===================================== @@ -0,0 +1,8 @@ +DocParagraph + (DocAppend + (DocString "Run ") + (DocAppend + (DocMonospaced (DocString "ghc -O2 Main.hs")) + (DocAppend + (DocString " and then ") + (DocAppend (DocIdentifier "main") (DocString "."))))) ===================================== utils/haddock/haddock-library/fixtures/examples/codeDoubleBackticks.input ===================================== @@ -0,0 +1 @@ +Use ``x `plus` y`` or `\x -> x`. ===================================== utils/haddock/haddock-library/fixtures/examples/codeDoubleBackticks.parsed ===================================== @@ -0,0 +1,12 @@ +DocParagraph + (DocAppend + (DocString "Use ") + (DocAppend + (DocMonospaced + (DocString "x `plus` y")) + (DocAppend + (DocString " or ") + (DocAppend + (DocMonospaced + (DocString "\\x -> x")) + (DocString "."))))) ===================================== utils/haddock/haddock-library/haddock-library.cabal ===================================== @@ -1,6 +1,6 @@ cabal-version: 3.0 name: haddock-library -version: 1.11.0 +version: 1.12.0 synopsis: Library exposing some functionality of Haddock. description: Haddock is a documentation-generation tool for Haskell libraries. These modules expose some ===================================== utils/haddock/haddock-library/src/Documentation/Haddock/Parser.hs ===================================== @@ -148,32 +148,54 @@ parseString = parseText . T.pack parseText :: Text -> DocH mod Identifier parseText = parseParagraph . T.dropWhile isSpace . T.filter (/= '\r') +-- | Where inline markup is parsed. See Note [Backticks in code]. +data SpanContext + = -- | Normal text + Prose + | -- | Text between \@...\@ + Code + parseParagraph :: Text -> DocH mod Identifier -parseParagraph = snd . parse p +parseParagraph = parse_paragraph Prose + +parse_paragraph :: SpanContext -> Text -> DocH mod Identifier +parse_paragraph ctx = snd . parse p where p :: Parser (DocH mod Identifier) p = - docConcat + docConcat . mark_infix <$> many - ( choice' + ( choice' $ [ monospace , anchor - , identifier - , moduleName - , picture - , mathDisplay - , mathInline - , markdownImage - , markdownLink - , hyperlink - , bold - , emphasis + ] + ++ backtick_parsers + ++ [ backtickRun + , moduleName + , picture + , mathDisplay + , mathInline + , markdownImage + , markdownLink ctx + , hyperlink + , bold ctx + , emphasis ctx , encodedChar , string' - , skipSpecialChar - ] + , skipSpecialChar + ] ) + backtick_parsers :: [Parser (DocH mod Identifier)] + backtick_parsers = case ctx of + Prose -> [backtickSpan, identifier] + Code -> [identifier] + + mark_infix :: [DocH mod Identifier] -> [DocH mod Identifier] + mark_infix = case ctx of + Prose -> id + Code -> infixInCode + -- | Parses and processes -- <https://en.wikipedia.org/wiki/Numeric_character_reference Numeric character references> -- @@ -230,17 +252,17 @@ skipSpecialChar = DocString . T.singleton <$> Parsec.oneOf specialChar -- -- >>> parseString "/Hello world/" -- DocEmphasis (DocString "Hello world") -emphasis :: Parser (DocH mod Identifier) -emphasis = - DocEmphasis . parseParagraph +emphasis :: SpanContext -> Parser (DocH mod Identifier) +emphasis ctx = + DocEmphasis . parse_paragraph ctx <$> disallowNewline ("/" *> takeWhile1_ (/= '/') <* "/") -- | Bold parser. -- -- >>> parseString "__Hello world__" -- DocBold (DocString "Hello world") -bold :: Parser (DocH mod Identifier) -bold = DocBold . parseParagraph <$> disallowNewline ("__" *> takeUntil "__") +bold :: SpanContext -> Parser (DocH mod Identifier) +bold ctx = DocBold . parse_paragraph ctx <$> disallowNewline ("__" *> takeUntil "__") disallowNewline :: Parser Text -> Parser Text disallowNewline = mfilter (T.all (/= '\n')) @@ -273,9 +295,110 @@ anchor = -- DocMonospaced (DocString "cruel") monospace :: Parser (DocH mod Identifier) monospace = - DocMonospaced . parseParagraph + DocMonospaced . parse_paragraph Code <$> ("@" *> takeWhile1_ (/= '@') <* "@") +-- Note [Backtick code spans] +-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ +-- A backtick span has two readings: +-- +-- `foo` a hyperlinked identifier, same as 'foo' +-- `foo bar` a monospaced code span, same as @foo bar@ +-- +-- In prose, 'backtickSpan' is tried before 'identifier'. Text between @...@ +-- does not use it: see Note [Backticks in code]. 'backtickSpan' parses a +-- closed code span (rules below) and then decides: +-- +-- * If 'takeIdentifier' parses the whole span, the result is the +-- identifier. Thus `foo`, `Foo.bar`, `(++)` and `foldl'` are links, and +-- ``foo`` is the identifier foo in infix form, not code "foo". +-- +-- * Else the result is code. This includes spans in which 'takeIdentifier' +-- reads a shorter identifier closed by a single quote: +-- +-- `foldl' f z xs` code "foldl' f z xs", not identifier foldl +-- `don't panic` code "don't panic", not identifier don +-- +-- If no closing run exists, 'backtickSpan' fails and 'identifier' gets the +-- input. Thus the TeX-style form `foo' stays the identifier foo. Then +-- 'backtickRun' makes the opening run plain text. +-- +-- Code spans follow the CommonMark rules: +-- +-- * A span opens with a run of N backticks and closes with the next run of +-- exactly N backticks on the same line. A run of a different length is +-- part of the content. Thus a code span that contains a backtick, such as +-- an infix application, uses more backticks: +-- +-- ``x `plus` y`` code "x `plus` y" +-- +-- * If the content both starts and ends with a space, and is not only +-- spaces, one space is removed at each end: +-- +-- `` `foo` `` code "`foo`" +-- +-- * The content is literal: no markup is parsed and backslash escapes are +-- not removed, so `\x -> x` keeps its backslash. +-- +-- * If no closing run exists, the whole opening run is plain text. Thus +-- "``" and a lone backtick keep their old meaning, and in "`` `a b`" only +-- "`a b`" is a code span. +-- +-- A closed code span wins over the mixed form `foo'. Thus a line such as +-- "`Foldable') or `elem`" gives code "Foldable') or ". Write 'Foldable' +-- there instead. + +-- | Backtick span parser: an identifier or a code span. Fails if the span +-- is not closed. See Note [Backtick code spans]. +-- +-- >>> parseString "`foo bar`" +-- DocMonospaced (DocString "foo bar") +-- +-- >>> parseString "``x `plus` y``" +-- DocMonospaced (DocString "x `plus` y") +-- +-- >>> parseString "`foldl' f z xs`" +-- DocMonospaced (DocString "foldl' f z xs") +backtickSpan :: Parser (DocH mod Identifier) +backtickSpan = do + (n, content) <- code_span + let fence = T.replicate n "`" + pure $ case takeIdentifier (fence <> content <> fence) of + Just (ns, op, ident, cl, rest) + | T.null rest -> DocIdentifier (Identifier ns op (T.unpack ident) cl) + _ -> DocMonospaced (DocString (strip_space content)) + where + -- The length of the opening run and the content up to the closing run + -- of exactly as many backticks. + code_span :: Parser (Int, Text) + code_span = do + n <- T.length <$> takeWhile1 (== '`') + content <- T.concat <$> many (try (content_chunk n)) + _ <- backtick_run n + if T.null content then empty else pure (n, content) + + content_chunk :: Int -> Parser Text + content_chunk n = + takeWhile1 (\c -> c /= '`' && c /= '\n') + <|> mfilter ((/= n) . T.length) (takeWhile1 (== '`')) + + backtick_run :: Int -> Parser Text + backtick_run n = mfilter ((== n) . T.length) (takeWhile1 (== '`')) + + strip_space :: Text -> Text + strip_space t + | T.length t >= 2 + , T.head t == ' ' + , T.last t == ' ' + , not (T.all (== ' ') t) = + T.init (T.tail t) + | otherwise = t + +-- | A run of backticks that opens neither a code span nor an identifier is +-- plain text. See Note [Backtick code spans]. +backtickRun :: Parser (DocH mod a) +backtickRun = DocString <$> takeWhile1 (== '`') + -- | Module names. -- -- Note that we allow '#' and '\' to support anchors (old style anchors are of @@ -304,9 +427,9 @@ moduleNameString = modid `maybeFollowedBy` anchor_ -- | A labeled link to an indentifier, module or url using markdown -- syntax. -markdownLink :: Parser (DocH mod Identifier) -markdownLink = do - lbl <- markdownLinkText +markdownLink :: SpanContext -> Parser (DocH mod Identifier) +markdownLink ctx = do + lbl <- markdownLinkText ctx choice' [markdownModuleName lbl, markdownURL lbl] where markdownModuleName lbl = do @@ -361,7 +484,7 @@ mathDisplay = -- DocPic (Picture "www.site.com" (Just "some emphasis in a description")) markdownImage :: Parser (DocH mod Identifier) markdownImage = do - text <- markup stringMarkup <$> ("!" *> markdownLinkText) + text <- markup stringMarkup <$> ("!" *> markdownLinkText Prose) url <- markdownLinkTarget pure $ DocPic (Picture url (Just text)) where @@ -629,7 +752,7 @@ textParagraph :: Parser (DocH mod Identifier) textParagraph = parseText . T.intercalate "\n" <$> some nonEmptyLine textParagraphThatStartsWithMarkdownLink :: Parser (DocH mod Identifier) -textParagraphThatStartsWithMarkdownLink = docParagraph <$> (docAppend <$> markdownLink <*> optionalTextParagraph) +textParagraphThatStartsWithMarkdownLink = docParagraph <$> (docAppend <$> markdownLink Prose <*> optionalTextParagraph) where optionalTextParagraph :: Parser (DocH mod Identifier) optionalTextParagraph = @@ -874,7 +997,7 @@ property = DocProperty . T.strip <$> ("prop>" *> takeWhile1 (/= '\n')) -- for markup. codeblock :: Parser (DocH mod Identifier) codeblock = - DocCodeBlock . parseParagraph . dropSpaces + DocCodeBlock . parse_paragraph Code . dropSpaces <$> ("@" *> skipHorizontalSpace *> "\n" *> block' <* "@") where dropSpaces xs = let ys = splitByNl xs in @@ -919,8 +1042,8 @@ angleBracketLink = <$> disallowNewline ("<" *> takeUntil ">") -- | The text for a markdown link, enclosed in square brackets. -markdownLinkText :: Parser (DocH mod Identifier) -markdownLinkText = parseParagraph . T.strip <$> ("[" *> takeUntil "]") +markdownLinkText :: SpanContext -> Parser (DocH mod Identifier) +markdownLinkText ctx = parse_paragraph ctx . T.strip <$> ("[" *> takeUntil "]") -- | The target for a markdown link, enclosed in parenthesis. markdownLinkTarget :: Parser Text @@ -956,3 +1079,134 @@ autoUrl = mkLink <$> url -- | Parses identifiers with help of 'parseValid'. identifier :: Parser (DocH mod Identifier) identifier = DocIdentifier <$> parseValid + +-- Note [Backticks in code] +-- ~~~~~~~~~~~~~~~~~~~~~~~~ +-- Text between @...@ is already code, so 'parse_paragraph' does not use +-- 'backtickSpan' there. The backticks of a span stay as text: +-- +-- @x <- f -- of type `M Bool`@ code "x <- f -- of type `M Bool`" +-- +-- 'identifier' still parses `x`, and its backticks have two uses: +-- +-- * A link marker, as in prose: @`foldlM` :: ...@ or @`seq`@. The +-- backticks are not shown. +-- +-- * The infix form of Haskell: @f `elem` xs@. The backticks must be shown, +-- else the code reads "f elem xs". +-- +-- 'infixInCode' uses the Haskell grammar to choose: `x` is infix only in +-- an infix application or in an operator section. Thus, spaces excluded, +-- one of these must be true: +-- +-- * An operand ends before `x` (a name, a literal, a closing bracket or +-- other markup) and an operand starts after it. +-- +-- * A section: '(' is before `x` and an operand starts after it, or an +-- operand ends before `x` and ')' is after it. +-- +-- @f `elem` xs@ infix: link "`elem`", as if written ``elem`` +-- @(`elem` xs)@ infix (right section) +-- @(x `elem`)@ infix (left section) +-- @`foldlM` :: t@ link marker: link "foldlM" +-- @(`elem`)@ link marker: no operand +-- @`map` `show` xs@ link markers: `map` is not an operand +-- @(op \``on`\` f)@ link marker: '`' does not end an operand +-- +-- Only an alphanumeric identifier can be infix, so @`(<*>)`@ is always a +-- link marker. +-- +-- If GHC cannot parse an infix identifier (for example the keyword +-- `case`), it becomes text with both pairs of backticks: "``case``". + +-- | Mark the infix identifiers of a code span. See Note [Backticks in code]. +-- +-- >>> parseString "@f `elem` xs@" +-- DocMonospaced (DocAppend (DocString "f ") (DocAppend (DocIdentifier (Identifier None '`' "`elem`" '`')) (DocString " xs"))) +infixInCode :: [DocH mod Identifier] -> [DocH mod Identifier] +infixInCode = go [] + where + -- The first argument holds the previous pieces, nearest first. + go :: [DocH mod Identifier] -> [DocH mod Identifier] -> [DocH mod Identifier] + go _ [] = [] + go before (d : after) = d' : go (d : before) after + where + d' = case d of + DocIdentifier (Identifier ns '`' x '`') + | is_alpha_ident x + , is_infix (side_before before) (side_after after) -> + DocIdentifier (Identifier ns '`' ("`" ++ x ++ "`") '`') + _ -> d + + -- An infix application, or a left or right operator section. + is_infix :: InfixSide -> InfixSide -> Bool + is_infix l r = case (l, r) of + (Operand, Operand) -> True + (SectionParen, Operand) -> True + (Operand, SectionParen) -> True + _ -> False + + -- Only an alphanumeric identifier, possibly qualified, can be infix. + is_alpha_ident :: String -> Bool + is_alpha_ident x = case x of + c : _ -> isAlpha c || c == '_' + [] -> False + + side_before :: [DocH mod Identifier] -> InfixSide + side_before = infix_side (fmap snd . T.unsnoc . T.stripEnd) classify + where + classify c + | isAlphaNum c || c `elem` (")]}_'\"#" :: String) = Operand + | c == '(' = SectionParen + | otherwise = NoOperand + + side_after :: [DocH mod Identifier] -> InfixSide + side_after = infix_side (fmap fst . T.uncons . T.stripStart) classify + where + classify c + | isAlphaNum c || c `elem` ("([_'\"\\" :: String) = Operand + | c == ')' = SectionParen + | otherwise = NoOperand + +-- | Classify the side of a backticked identifier in code. See +-- 'infixInCode'. +-- +-- The pieces are the ones on that side, nearest first. The first function +-- gives the character of a text that is nearest to the identifier, +-- spaces excluded; the second classifies it. +-- +-- * A text of only spaces (or newlines, in a code block) says nothing, +-- so the next piece decides. Thus @f `elem` xs@ is infix. +-- +-- * An identifier between backticks is not an operand: next to it, the +-- backticks are link markers. Thus in @`map` `show` xs@ both are links, +-- and the code reads "map show xs". +-- +-- * Any other piece comes from markup inside the code: an identifier, a +-- module name, a link, emphasis, and so on. Such a piece stands for a +-- name or an expression, so it is an operand. Thus @'f' `elem` xs@ is +-- infix. +-- +-- * No piece means the edge of the code span: there is no operand. +infix_side + :: (Text -> Maybe Char) + -> (Char -> InfixSide) + -> [DocH mod Identifier] + -> InfixSide +infix_side nearest_char classify = go + where + go ds = case ds of + DocString t : rest -> maybe (go rest) classify (nearest_char t) + DocIdentifier (Identifier _ '`' _ '`') : _ -> NoOperand + _ : _ -> Operand + [] -> NoOperand + +-- | What is on one side of a backticked identifier in code. See +-- 'infixInCode'. +data InfixSide + = -- | The end of an operand before, or the start of an operand after + Operand + | -- | The parenthesis of an operator section: '(' before or ')' after + SectionParen + | -- | Anything else, or the edge of the code span + NoOperand ===================================== utils/haddock/haddock-library/src/Documentation/Haddock/Parser/Identifier.hs ===================================== @@ -13,6 +13,7 @@ module Documentation.Haddock.Parser.Identifier ( Identifier (..) , parseValid + , takeIdentifier ) where import Control.Monad (guard) ===================================== utils/haddock/haddock-library/test/Documentation/Haddock/ParserSpec.hs ===================================== @@ -22,19 +22,19 @@ infixr 6 <> type Doc id = DocH () id instance IsString (Doc String) where - fromString = DocString + fromString = DocString . T.pack instance IsString a => IsString (Maybe a) where fromString = Just . fromString -parseParas :: T.Text -> MetaDoc () String -parseParas = overDoc Parse.toRegular . Parse.parseParas Nothing +parseParas :: String -> MetaDoc () String +parseParas = overDoc Parse.toRegular . Parse.parseParas Nothing . T.pack -parseString :: T.Text -> Doc String +parseString :: String -> Doc String parseString = Parse.toRegular . Parse.parseString hyperlink :: String -> Maybe (Doc String) -> Doc String -hyperlink url = DocHyperlink . Hyperlink url +hyperlink url = DocHyperlink . Hyperlink (T.pack url) main :: IO () main = hspec spec @@ -63,7 +63,7 @@ spec = do it "allows to backslash-escape characters except \\r" $ do property $ \y -> case y of '\r' -> "\\\r" `shouldParseTo` DocString "\\" - x -> ['\\', x] `shouldParseTo` DocString [x] + x -> ['\\', x] `shouldParseTo` DocString (T.singleton x) context "when parsing strings contaning numeric character references" $ do it "will implicitly convert digits to characters" $ do @@ -271,7 +271,7 @@ spec = do context "when parsing images" $ do let image :: String -> Maybe String -> Doc String - image uri = DocPic . Picture uri + image uri = DocPic . Picture (T.pack uri) . fmap T.pack it "accepts markdown syntax for images" $ do "" `shouldParseTo` image "url" "label" @@ -377,6 +377,105 @@ spec = do it "requires the closing @" $ do "@foo /bar/ baz" `shouldParseTo` "@foo " <> DocEmphasis "bar" <> " baz" + context "when parsing backtick code spans" $ do + it "parses a backtick span that is not an identifier as monospaced text" $ do + "`foo bar`" `shouldParseTo` DocMonospaced "foo bar" + + it "parses inline backtick code spans" $ do + "foo `bar baz` qux" `shouldParseTo` "foo " <> DocMonospaced "bar baz" <> " qux" + + it "prefers the identifier reading for a valid identifier" $ do + "`foo`" `shouldParseTo` DocIdentifier "foo" + "`Foo.bar`" `shouldParseTo` DocIdentifier "Foo.bar" + "`(++)`" `shouldParseTo` DocIdentifier "(++)" + + it "does not parse markup inside a backtick code span" $ do + "`/foo/ @bar@ 'baz'`" `shouldParseTo` DocMonospaced "/foo/ @bar@ 'baz'" + + it "does not remove backslashes inside a backtick code span" $ do + "`\\x -> x`" `shouldParseTo` DocMonospaced "\\x -> x" + + it "closes a span with a run of the same number of backticks" $ do + "``x `plus` y``" `shouldParseTo` DocMonospaced "x `plus` y" + "```a `` b```" `shouldParseTo` DocMonospaced "a `` b" + + it "keeps the identifier reading of a backticked infix identifier" $ do + "``foo``" `shouldParseTo` DocIdentifier "`foo`" + + it "keeps the identifier reading of an identifier with a prime" $ do + "`foldl'`" `shouldParseTo` DocIdentifier "foldl'" + + it "does not close a span with a single quote inside it" $ do + "`foldl' f z xs`" `shouldParseTo` DocMonospaced "foldl' f z xs" + "`don't panic`" `shouldParseTo` DocMonospaced "don't panic" + "`a' b'`" `shouldParseTo` DocMonospaced "a' b'" + + it "keeps the identifier reading of `foo' when no span closes" $ do + "`foo' and 'bar'" `shouldParseTo` DocIdentifier "foo" <> " and " <> DocIdentifier "bar" + + it "prefers a closed span to `foo'" $ do + "`foo' or `bar`" `shouldParseTo` DocMonospaced "foo' or " <> "bar`" + + it "strips one space at each end when both are present" $ do + "`` `foo` ``" `shouldParseTo` DocMonospaced "`foo`" + "`` a ``" `shouldParseTo` DocMonospaced " a " + "`` a``" `shouldParseTo` DocMonospaced " a" + "`` ``" `shouldParseTo` DocMonospaced " " + + it "treats an unclosed run of backticks as plain text" $ do + "``foo`" `shouldParseTo` "``foo`" + "`` and `x y`" `shouldParseTo` "`` and " <> DocMonospaced "x y" + + it "accepts unicode" $ do + "`foo 灼眼のシャナ bar`" `shouldParseTo` DocMonospaced "foo 灼眼のシャナ bar" + + it "requires the closing backtick" $ do + "`foo /bar/ baz" `shouldParseTo` "`foo " <> DocEmphasis "bar" <> " baz" + + it "requires the span to be on a single line" $ do + "`foo\nbar`" `shouldParseTo` "`foo\nbar`" + + it "does not treat an empty span as code" $ do + "``" `shouldParseTo` "``" + + it "allows backtick code spans inside other markup" $ do + "/foo `bar baz`/" `shouldParseTo` DocEmphasis ("foo " <> DocMonospaced "bar baz") + + it "does not parse backtick code spans inside @...@" $ do + "@x `M Bool` y@" `shouldParseTo` DocMonospaced "x `M Bool` y" + "@/a `b c`/@" `shouldParseTo` DocMonospaced (DocEmphasis "a `b c`") + + it "keeps the backticks of an infix identifier inside @...@" $ do + "@f `elem` xs@" `shouldParseTo` DocMonospaced ("f " <> DocIdentifier "`elem`" <> " xs") + "@x `M.member` m@" `shouldParseTo` DocMonospaced ("x " <> DocIdentifier "`M.member`" <> " m") + "@x ``elem`` y@" `shouldParseTo` DocMonospaced ("x " <> DocIdentifier "`elem`" <> " y") + "@'elem' x xs@" `shouldParseTo` DocMonospaced (DocIdentifier "elem" <> " x xs") + "@'f' `elem` xs@" `shouldParseTo` DocMonospaced (DocIdentifier "f" <> " " <> DocIdentifier "`elem`" <> " xs") + "@x `seq` \\\\y -> y@" `shouldParseTo` DocMonospaced ("x " <> DocIdentifier "`seq`" <> " \\y -> y") + "@r{a = 1} `seq` x@" `shouldParseTo` DocMonospaced ("r{a = 1} " <> DocIdentifier "`seq`" <> " x") + "@x# `seq` y@" `shouldParseTo` DocMonospaced ("x# " <> DocIdentifier "`seq`" <> " y") + + it "keeps the backticks of an operator section inside @...@" $ do + "@(`elem` xs)@" `shouldParseTo` DocMonospaced ("(" <> DocIdentifier "`elem`" <> " xs)") + "@(x `elem`)@" `shouldParseTo` DocMonospaced ("(x " <> DocIdentifier "`elem`" <> ")") + "@filter (`elem` xs) ys@" `shouldParseTo` DocMonospaced ("filter (" <> DocIdentifier "`elem`" <> " xs) ys") + + it "keeps backticks as link markers inside @...@ where infix is not possible" $ do + "@`seq`@" `shouldParseTo` DocMonospaced (DocIdentifier "seq") + "@`foldlM` :: t@" `shouldParseTo` DocMonospaced (DocIdentifier "foldlM" <> " :: t") + "@(`elem`)@" `shouldParseTo` DocMonospaced ("(" <> DocIdentifier "elem" <> ")") + "@x `seq`@" `shouldParseTo` DocMonospaced ("x " <> DocIdentifier "seq") + "@`flip` `(<*>)`@" `shouldParseTo` DocMonospaced (DocIdentifier "flip" <> " " <> DocIdentifier "(<*>)") + "@`map` `show` xs@" `shouldParseTo` DocMonospaced (DocIdentifier "map" <> " " <> DocIdentifier "show" <> " xs") + "@`x` `f` y@" `shouldParseTo` DocMonospaced (DocIdentifier "x" <> " " <> DocIdentifier "f" <> " y") + + it "handles several infix identifiers inside @...@" $ do + "@a `f` b `g` c@" `shouldParseTo` DocMonospaced ("a " <> DocIdentifier "`f`" <> " b " <> DocIdentifier "`g`" <> " c") + "@a`f`b`g`c@" `shouldParseTo` DocMonospaced ("a" <> DocIdentifier "`f`" <> "b" <> DocIdentifier "`g`" <> "c") + "@(a `f` b) `g` c@" `shouldParseTo` DocMonospaced ("(a " <> DocIdentifier "`f`" <> " b) " <> DocIdentifier "`g`" <> " c") + "@x `f` 'y' `g` z@" `shouldParseTo` DocMonospaced ("x " <> DocIdentifier "`f`" <> " " <> DocIdentifier "y" <> " " <> DocIdentifier "`g`" <> " z") + "@((`f` a) . (`g` b))@" `shouldParseTo` DocMonospaced ("((" <> DocIdentifier "`f`" <> " a) . (" <> DocIdentifier "`g`" <> " b))") + context "when parsing bold strings" $ do it "allows for a bold string on its own" $ do "__bold string__" @@ -616,7 +715,7 @@ spec = do property $ \xs -> let input = filterSpecial xs in (not . null) input ==> - input `shouldParseTo` DocParagraph (DocString input) + input `shouldParseTo` DocParagraph (DocString (T.pack input)) it "separates paragraphs by empty lines" $ do unlines @@ -631,6 +730,34 @@ spec = do it "turns it into a code block" $ do "@foo@" `shouldParseTo` DocCodeBlock "foo" + context "when a paragraph only contains a backtick code span" $ do + it "turns it into a code block" $ do + "`foo bar`" `shouldParseTo` DocCodeBlock "foo bar" + + context "when a code block contains backticks" $ do + it "keeps them" $ do + unlines + [ "@" + , "myExample opt1 opt2 =" + , " Code.do" + , " x <- someSideEffect -- This one is of type `M Bool`" + , " if x then opt1 else opt2" + , "@" + ] + `shouldParseTo` DocCodeBlock + ( DocString + ( T.unlines + [ "myExample opt1 opt2 =" + , " Code.do" + , " x <- someSideEffect -- This one is of type `M Bool`" + , " if x then opt1 else opt2" + ] + ) + ) + + it "keeps the backticks of an infix identifier" $ do + "@\nf `elem` xs\n@" `shouldParseTo` DocCodeBlock ("f " <> DocIdentifier "`elem`" <> " xs\n") + context "when a paragraph contains a markdown link" $ do it "correctly parses the link" $ do "Blah [label](url)" @@ -843,7 +970,7 @@ spec = do it "parses bird-tracks inside of paragraphs as plain strings" $ do let xs = "foo\n>>> bar" - xs `shouldParseTo` DocParagraph (DocString xs) + xs `shouldParseTo` DocParagraph (DocString (T.pack xs)) it "skips empty lines in front of an example" $ do "\n \n\n>>> foo" `shouldParseTo` DocExamples [Example "foo" []] View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/ff0a5d32f459ae94bb8269be95a77d0... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/ff0a5d32f459ae94bb8269be95a77d0... You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
participants (1)
-
Marge Bot (@marge-bot)