diff --git a/.envrc b/.envrc deleted file mode 100644 index 1d953f4..0000000 --- a/.envrc +++ /dev/null @@ -1 +0,0 @@ -use nix diff --git a/.forgejo/workflows/ci.yml b/.forgejo/workflows/ci.yml deleted file mode 100644 index 3f7362e..0000000 --- a/.forgejo/workflows/ci.yml +++ /dev/null @@ -1,2 +0,0 @@ -runs-on: self-hosted - diff --git a/.forgejo/workflows/test.yaml b/.forgejo/workflows/test.yaml deleted file mode 100644 index ee81782..0000000 --- a/.forgejo/workflows/test.yaml +++ /dev/null @@ -1,99 +0,0 @@ -name: Haskell CI -on: - pull_request: - branches: - - dev - - main - push: - branches: - - main - -jobs: - build: - runs-on: docker - container: - image: elland/haddock2:latest - steps: - - name: Checkout code - uses: actions/checkout@v4 - - name: Check versions - run: | - ghc --version - cabal --version - node --version - - name: Cache Cabal packages - uses: actions/cache@v4 - with: - path: | - ~/.cabal/packages - ~/.cabal/store - dist-newstyle - key: ${{ runner.os }}-haskell-9.10-cabal-${{ hashFiles('**/*.cabal', '**/cabal.project') }} - restore-keys: | - ${{ runner.os }}-haskell-9.10-cabal- - - name: Update Cabal package index - run: cabal update - - name: Configure project - run: cabal configure --enable-tests --enable-benchmarks - - name: Build dependencies - run: cabal build --only-dependencies --enable-tests --enable-benchmarks - - name: Build project - run: cabal build --enable-tests --enable-benchmarks - - name: Run documentation build - run: cabal haddock - - test: - runs-on: docker - container: - image: elland/haddock2:latest - needs: build - steps: - - name: Checkout code - uses: actions/checkout@v4 - - name: Cache Cabal packages - uses: actions/cache@v4 - with: - path: | - ~/.cabal/packages - ~/.cabal/store - dist-newstyle - key: ${{ runner.os }}-haskell-9.10-cabal-${{ hashFiles('**/*.cabal', '**/cabal.project') }} - restore-keys: | - ${{ runner.os }}-haskell-9.10-cabal- - - name: Update Cabal package index - run: cabal update - - name: Configure project - run: cabal configure --enable-tests --enable-benchmarks - - name: Build dependencies - run: cabal build --only-dependencies --enable-tests --enable-benchmarks - - name: Build project - run: cabal build --enable-tests --enable-benchmarks - - name: Run tests - run: cabal test --test-show-details=direct - - fourmolu: - runs-on: docker - container: - image: elland/haddock2:latest - needs: build - steps: - - name: Checkout code - uses: actions/checkout@v4 - - name: Run fourmolu - run: | - find src test app -name "*.hs" -exec fourmolu --check-idempotence {} \; 2>/dev/null || true - find src test app -name "*.hs" -exec fourmolu --mode check {} \; - - hlint: - runs-on: docker - container: - image: elland/haddock2:latest - needs: build - steps: - - name: Checkout code - uses: actions/checkout@v4 - - name: Run hlint - run: | - if [ -d src ]; then hlint src/; fi - if [ -d test ]; then hlint test/; fi - if [ -d app ]; then hlint app/; fi diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index c2919c5..0000000 --- a/Dockerfile +++ /dev/null @@ -1,36 +0,0 @@ -FROM haskell:9.10.2-bullseye AS builder - -RUN apt-get update && apt-get install -y curl git && rm -rf /var/lib/apt/lists/* - -RUN curl -fsSL https://deb.nodesource.com/setup_22.x | bash - && \ - apt-get install -y nodejs && \ - rm -rf /var/lib/apt/lists/* - -RUN cabal update && \ - cabal install --install-method=copy --installdir=/usr/local/bin \ - fourmolu hlint cabal-gild - -WORKDIR /workspace - -FROM haskell:9.10.2-bullseye - -RUN apt-get update && apt-get install -y \ - libgmp10 \ - curl \ - && rm -rf /var/lib/apt/lists/* - -RUN curl -fsSL https://deb.nodesource.com/setup_22.x | bash - && \ - apt-get install -y nodejs && \ - rm -rf /var/lib/apt/lists/* - -RUN cabal update - -COPY --from=builder /usr/local/bin/cabal /usr/local/bin/ -COPY --from=builder /usr/local/bin/fourmolu /usr/local/bin/ -COPY --from=builder /usr/local/bin/hlint /usr/local/bin/ -COPY --from=builder /usr/local/bin/cabal-gild /usr/local/bin/ - -WORKDIR /workspace - -CMD [ "bash" ] - diff --git a/Grammar.ebnf b/Grammar.ebnf index 2404a67..bff331d 100644 --- a/Grammar.ebnf +++ b/Grammar.ebnf @@ -12,7 +12,7 @@ bold ::= '__' text_no_newline '__' monospace ::= '@' text_content '@' link ::= module_link | hyperlink | markdown_link -module_link ::= '"' module_name ( ('#' | '\#') anchor_name )? '"' +module_link ::= '"' module_name ( '#' anchor_name )? '"' hyperlink ::= '<' url ( ' ' link_text )? '>' markdown_link ::= '[' link_text '](' ( url | module_link ) ')' diff --git a/Makefile b/Makefile deleted file mode 100644 index 6bc9ca2..0000000 --- a/Makefile +++ /dev/null @@ -1,44 +0,0 @@ -.PHONY: help -help: ## Show this help - @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' - -.PHONY: build -build: ## Build the project - cabal build - -.PHONY: test -test: ## Run tests - cabal test --test-show-details=direct - -.PHONY: clean -clean: ## Clean build artifacts - cabal clean - -.PHONY: fourmolu -fourmolu: ## Format Haskell code - find . -type f -name "*.hs" ! -path "./dist-newstyle/*" -exec fourmolu -i {} + - -.PHONY: fourmolu-check -fourmolu-check: ## Check if code is formatted - find . -type f -name "*.hs" ! -path "./dist-newstyle/*" -exec fourmolu --mode check {} \; - -.PHONY: lint -lint: ## Run hlint - hlint src test app - -.PHONY: cabal-gild -cabal-gild: ## Format cabal file - cabal-gild --io=haddock2.cabal - -.PHONY: format -format: fourmolu cabal-gild ## Run all formatters - -.PHONY: check -check: fourmolu-check lint ## Run all checks (CI-style) - -.PHONY: ci -ci: build test check ## Run full CI pipeline locally - -.PHONY: docs -docs: ## Generate documentation - cabal haddock --haddock-hyperlink-source diff --git a/haddock2.cabal b/haddock2.cabal index 9ae122d..7fe124c 100644 --- a/haddock2.cabal +++ b/haddock2.cabal @@ -46,10 +46,10 @@ test-suite haddock2-test type: exitcode-stdio-1.0 main-is: Spec.hs build-depends: + parsec ^>=3.1.18.0, base >=4.20.1.0, haddock2:{haddock2-lib}, hspec ^>=2.11.0, - parsec ^>=3.1.18.0, text ^>=2.1.2, hs-source-dirs: test diff --git a/npins/default.nix b/npins/default.nix deleted file mode 100644 index 6592476..0000000 --- a/npins/default.nix +++ /dev/null @@ -1,146 +0,0 @@ -/* - This file is provided under the MIT licence: - - Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -*/ -# Generated by npins. Do not modify; will be overwritten regularly -let - data = builtins.fromJSON (builtins.readFile ./sources.json); - version = data.version; - - # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/lists.nix#L295 - range = - first: last: if first > last then [ ] else builtins.genList (n: first + n) (last - first + 1); - - # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L257 - stringToCharacters = s: map (p: builtins.substring p 1 s) (range 0 (builtins.stringLength s - 1)); - - # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L269 - stringAsChars = f: s: concatStrings (map f (stringToCharacters s)); - concatMapStrings = f: list: concatStrings (map f list); - concatStrings = builtins.concatStringsSep ""; - - # If the environment variable NPINS_OVERRIDE_${name} is set, then use - # the path directly as opposed to the fetched source. - # (Taken from Niv for compatibility) - mayOverride = - name: path: - let - envVarName = "NPINS_OVERRIDE_${saneName}"; - saneName = stringAsChars (c: if (builtins.match "[a-zA-Z0-9]" c) == null then "_" else c) name; - ersatz = builtins.getEnv envVarName; - in - if ersatz == "" then - path - else - # this turns the string into an actual Nix path (for both absolute and - # relative paths) - builtins.trace "Overriding path of \"${name}\" with \"${ersatz}\" due to set \"${envVarName}\"" ( - if builtins.substring 0 1 ersatz == "/" then - /. + ersatz - else - /. + builtins.getEnv "PWD" + "/${ersatz}" - ); - - mkSource = - name: spec: - assert spec ? type; - let - path = - if spec.type == "Git" then - mkGitSource spec - else if spec.type == "GitRelease" then - mkGitSource spec - else if spec.type == "PyPi" then - mkPyPiSource spec - else if spec.type == "Channel" then - mkChannelSource spec - else if spec.type == "Tarball" then - mkTarballSource spec - else - builtins.throw "Unknown source type ${spec.type}"; - in - spec // { outPath = mayOverride name path; }; - - mkGitSource = - { - repository, - revision, - url ? null, - submodules, - hash, - branch ? null, - ... - }: - assert repository ? type; - # At the moment, either it is a plain git repository (which has an url), or it is a GitHub/GitLab repository - # In the latter case, there we will always be an url to the tarball - if url != null && !submodules then - builtins.fetchTarball { - inherit url; - sha256 = hash; # FIXME: check nix version & use SRI hashes - } - else - let - url = - if repository.type == "Git" then - repository.url - else if repository.type == "GitHub" then - "https://github.com/${repository.owner}/${repository.repo}.git" - else if repository.type == "GitLab" then - "${repository.server}/${repository.repo_path}.git" - else - throw "Unrecognized repository type ${repository.type}"; - urlToName = - url: rev: - let - matched = builtins.match "^.*/([^/]*)(\\.git)?$" url; - - short = builtins.substring 0 7 rev; - - appendShort = if (builtins.match "[a-f0-9]*" rev) != null then "-${short}" else ""; - in - "${if matched == null then "source" else builtins.head matched}${appendShort}"; - name = urlToName url revision; - in - builtins.fetchGit { - rev = revision; - inherit name; - # hash = hash; - inherit url submodules; - }; - - mkPyPiSource = - { url, hash, ... }: - builtins.fetchurl { - inherit url; - sha256 = hash; - }; - - mkChannelSource = - { url, hash, ... }: - builtins.fetchTarball { - inherit url; - sha256 = hash; - }; - - mkTarballSource = - { - url, - locked_url ? url, - hash, - ... - }: - builtins.fetchTarball { - url = locked_url; - sha256 = hash; - }; -in -if version == 5 then - builtins.mapAttrs mkSource data.pins -else - throw "Unsupported format version ${toString version} in sources.json. Try running `npins upgrade`" diff --git a/npins/sources.json b/npins/sources.json deleted file mode 100644 index 5317047..0000000 --- a/npins/sources.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "pins": { - "nixpkgs": { - "type": "Channel", - "name": "nixpkgs-unstable", - "url": "https://releases.nixos.org/nixpkgs/nixpkgs-25.11pre868532.647e5c14cbd5/nixexprs.tar.xz", - "hash": "0i6mgl7pm7y4ydrrll7szmv8hhxb3cyny8x1g1a8sp3g5wl3yd9g" - } - }, - "version": 5 -} diff --git a/shell.nix b/shell.nix deleted file mode 100644 index 61a931d..0000000 --- a/shell.nix +++ /dev/null @@ -1,25 +0,0 @@ -let - sources = import ./npins; -in -{ - pkgs ? import sources.nixpkgs { }, -}: - -pkgs.mkShell rec { - name = "haddock2"; - - packages = - with pkgs; - [ - haskell.packages.ghc912.ghc - haskell.packages.ghc912.haskell-language-server - zlib - ] - ++ map haskell.lib.justStaticExecutables [ - haskellPackages.cabal-gild - haskellPackages.fourmolu - cabal-install - ]; - - env.LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath packages; -} diff --git a/src/Lexer.hs b/src/Lexer.hs index 77fc84a..c299d41 100644 --- a/src/Lexer.hs +++ b/src/Lexer.hs @@ -4,22 +4,18 @@ module Lexer ( Token (..), lexer, emphasis, -) -where +) where import Control.Monad (mfilter, void) -import Data.Char (ord, toLower) import Data.Functor (($>)) import Data.Text (Text, intercalate) import Data.Text qualified as Text -import GHC.Unicode (isAlphaNum, isControl, isDigit, isPrint, isSpace, isUpper) +import GHC.Unicode (isAlpha, isAlphaNum, isControl, isPrint, isSpace, isUpper) import ParserMonad (Parser, initialParserState) import Text.Parsec import Text.Parsec qualified as Parsec import Text.Parsec.Pos (updatePosChar) -type Located a = (SourcePos, a) - type LocatedToken = (SourcePos, Token) type Lexer = Parser [LocatedToken] @@ -35,7 +31,7 @@ data Level data Token = Token Text - | Anchor Text + | Anchor | BirdTrack | BoldOpen | BoldClose @@ -54,24 +50,17 @@ data Token | ParenClose | BracketOpen | BracketClose - | MathInlineOpen - | MathInlineClose - | MathMultilineOpen - | MathMultilineClose - | NumericEntity Int - | Module Text + | MathsParenOpen + | MathsParenClose + | MathsBracketOpen + | MathsBracketClose + | Module | QuoteOpen | QuoteClose | Space | EOF deriving (Eq, Show) -located :: Parser a -> Parser (SourcePos, a) -located p = (,) <$> getPosition <*> p - -tokenise :: [Parser a] -> Parser [(SourcePos, a)] -tokenise = mapM located - lexer :: String -> Either ParseError [LocatedToken] lexer = Parsec.runParser lexText initialParserState "input" . Text.pack @@ -85,17 +74,16 @@ lexText = go toks <- choice $ Parsec.try - <$> [ mathMultiline - , mathInline + <$> [ mathsBracket + , mathsParens , escape -- maths go before escape to avoid mismatch , headers , newlineToken , spaceToken , link , labeledLink - , module_ - , anchor - , numericEntity + , modules + , anchors , textElement , quotes , birdTrack @@ -127,123 +115,161 @@ headers = , header6 ] +delimitedMaybe :: Parser a -> Parser a -> Token -> Maybe Token -> Parser [LocatedToken] +delimitedMaybe openMark closeMark openToken closeToken = do + openPos <- getPosition + void openMark + tokenPos <- getPosition + content <- anyUntil closeMark + closePos <- getPosition + void closeMark + + let openTok :: LocatedToken = (openPos, openToken) + res :: LocatedToken = (tokenPos, Token content) + closeToks :: [LocatedToken] = case closeToken of + Just close -> [(closePos, close)] + Nothing -> [] + + pure $ [openTok, res] <> closeToks + anyUntil :: Parser a -> Parser Text anyUntil p = Text.pack <$> manyTill anyChar (lookAhead p) -delimitedAsTuple :: Parser open -> Parser close -> Parser (Located open, LocatedToken, Located close) -delimitedAsTuple openP closeP = - (,,) - <$> located openP - <*> located (Token <$> anyUntil closeP) - <*> located closeP - -delimited :: Parser open -> Parser close -> Token -> Token -> Parser [LocatedToken] -delimited openP closeP openTok closeTok = asList <$> delimitedAsTuple (openTok <$ openP) (closeTok <$ closeP) - where - asList (a, tok, b) = [a, tok, b] - -delimitedNoTrailing :: Parser open -> Parser close -> Token -> Parser [LocatedToken] -delimitedNoTrailing openP closeP openTok = asList <$> delimitedAsTuple (openTok <$ openP) (void closeP) - where - asList (a, tok, _) = [a, tok] +delimited :: Parser a -> Parser a -> Token -> Token -> Parser [LocatedToken] +delimited a b c d = delimitedMaybe a b c (Just d) delimitedSymmetric :: Parser a -> Token -> Token -> Parser [LocatedToken] -delimitedSymmetric s = delimited s s +delimitedSymmetric s t1 t2 = delimited s s t1 t2 eol :: Parser () -eol = void "\n" <|> void "\r\n" <|> Parsec.eof +eol = void "\n" <|> Parsec.eof header1 :: Lexer -header1 = delimitedNoTrailing "= " eol (Header One) +header1 = delimitedMaybe (void $ "= ") eol (Header One) Nothing header2 :: Lexer -header2 = delimitedNoTrailing "== " eol (Header Two) +header2 = delimitedMaybe (void $ "== ") eol (Header Two) Nothing header3 :: Lexer -header3 = delimitedNoTrailing "=== " eol (Header Three) +header3 = delimitedMaybe (void $ "=== ") eol (Header Three) Nothing header4 :: Lexer -header4 = delimitedNoTrailing "==== " eol (Header Four) +header4 = delimitedMaybe (void $ "==== ") eol (Header Four) Nothing header5 :: Lexer -header5 = delimitedNoTrailing "===== " eol (Header Five) +header5 = delimitedMaybe (void $ "===== ") eol (Header Five) Nothing header6 :: Lexer -header6 = delimitedNoTrailing "====== " eol (Header Six) +header6 = delimitedMaybe (void $ "====== ") eol (Header Six) Nothing -- #anchors# -anchor :: Lexer -anchor = do - x <- located $ between "#" "#" (Anchor <$> anyUntil "#") - pure [x] +anchors :: Lexer +anchors = do + pos <- getPosition + void $ try anchor' + pos' <- getPosition + txt <- anyUntil anchor' + pos'' <- getPosition + void $ try anchor' -moduleNames :: Parser Text -moduleNames = intercalate "." . fmap Text.pack <$> upperId `sepBy1` char '.' - -upperId :: Parser String -upperId = (:) <$> satisfy isUpper <*> many1 identifierChar - -identifierChar :: Parser Char -identifierChar = satisfy (\c -> isAlphaNum c || c == '_') + pure + [ (pos, Anchor) + , (pos', Token txt) + , (pos'', Anchor) + ] + where + anchor' = (string "#" <|> string "\\#") -- "Module.Name" -- "Module.Name#anchor" --- "Module.Name\#anchor" -- known as "old anchor". this has been deprecated for 9 years, thanks Ben -module_ :: Lexer -module_ = between (char '"') (char '"') inner +-- "Module.Name#anchor" +modules :: Lexer +modules = do + pos <- getPosition + void $ char '"' + pos' <- getPosition + modName <- modId + anch <- option [] do + pos'' <- getPosition + void $ try (string "#" <|> string "\\#") + pos''' <- getPosition + a <- Text.pack <$> many (satisfy (\c -> c /= '"' && not (isSpace c))) + pure [(pos'', Anchor), (pos''', Token a)] + + void $ char '"' + pure $ [(pos, Module), (pos', Token modName)] <> anch where - inner = do - m <- located $ Module <$> moduleNames - mAnchor <- optionMaybe (located $ anchorHash *> (Anchor <$> anchorText)) - pure $ case mAnchor of - Just anc -> [m, anc] - Nothing -> [m] + modId = intercalate "." <$> (fmap Text.pack <$> (conId `sepBy1` (char '.'))) - anchorHash :: Parser Text - anchorHash = "#" <|> try "\\#" + conId :: Parser String + conId = + (:) + <$> satisfy (\c -> isAlpha c && isUpper c) + <*> many1 conChar - anchorText :: Parser Text - anchorText = Text.pack <$> many (satisfy (\c -> c /= '"' && not (isSpace c))) + conChar :: Parser Char + conChar = satisfy (\c -> isAlphaNum c || c == '_') linkRaw :: Lexer -linkRaw = - tokenise - [ BracketOpen <$ "[" - , Token <$> anyUntil "]" - , BracketClose <$ "]" - , ParenOpen <$ "(" - , Token <$> anyUntil ")" - , ParenClose <$ ")" +linkRaw = do + pos1 <- getPosition + void $ string "[" + pos2 <- getPosition + text <- anyUntil $ Text.pack <$> string "]" + pos3 <- getPosition + void $ "]" + pos4 <- getPosition + void $ "(" + pos5 <- getPosition + link' <- anyUntil $ Text.pack <$> string ")" + pos6 <- getPosition + void $ ")" + + pure $ + [ (pos1, BracketOpen) + , (pos2, Token text) + , (pos3, BracketClose) + , (pos4, ParenOpen) + , (pos5, Token link') + , (pos6, ParenClose) ] link :: Lexer link = do pos <- getPosition l <- linkRaw - -- register the position of the last token + -- "unconsume" the last token pos' <- flip incSourceColumn (-1) <$> getPosition pure $ (pos, LinkOpen) : l <> [(pos', LinkClose)] labeledLink :: Lexer labeledLink = do - open <- located $ LabeledLinkOpen <$ "<" - linkRes <- linkRaw - labelRes <- located $ Token <$> anyUntil ">" - close <- located $ LabeledLinkClose <$ ">" + pos <- getPosition + void $ string "<" + link' <- linkRaw + pos7 <- getPosition + label' <- anyUntil $ string ">" + pos8 <- getPosition + void $ ">" + pure $ - open : linkRes <> [labelRes, close] + (pos, LabeledLinkOpen) + : link' + <> [ (pos7, Token label') + , (pos8, LabeledLinkClose) + ] -mathMultiline :: Lexer -mathMultiline = delimited "\\[" "\\]" MathMultilineOpen MathMultilineClose +mathsBracket :: Lexer +mathsBracket = delimited (void $ "\\[") (void "\\]") MathsBracketOpen MathsBracketClose -mathInline :: Lexer -mathInline = delimited "\\(" "\\)" MathInlineOpen MathInlineClose +mathsParens :: Lexer +mathsParens = delimited (void $ "\\(") (void "\\)") MathsParenOpen MathsParenClose birdTrack :: Lexer -birdTrack = delimitedNoTrailing ">> " eol BirdTrack +birdTrack = delimitedMaybe (void ">> ") eol BirdTrack Nothing escape :: Lexer -escape = delimitedNoTrailing "\\" eol Escape +escape = delimitedMaybe (void "\\") eol Escape Nothing quotes :: Lexer quotes = delimitedSymmetric "\"" QuoteOpen QuoteClose @@ -257,30 +283,6 @@ bold = delimitedSymmetric "__" BoldOpen BoldClose monospace :: Lexer monospace = delimitedSymmetric "@" MonospaceOpen MonospaceClose -decimal :: Parser Int -decimal = read . Text.unpack <$> takeWhile1_ isDigit - -hexadecimal :: Parser Int -hexadecimal = "x" *> (convert 0 . fmap (normalise . toLower) <$> many1 hexDigit) - where - normalise :: Char -> Int - normalise c - | ord '0' <= n && n <= ord '9' = n - ord '0' - | ord 'A' <= n && n <= ord 'F' = n - ord 'A' + 10 - | ord 'a' <= n && n <= ord 'f' = n - ord 'a' + 10 - | otherwise = error "unexpected: invalid hex number" - where - n = ord c - - convert :: Int -> [Int] -> Int - convert acc [] = acc - convert acc (x : xs) = convert (acc * 16 + x) xs - -numericEntity :: Lexer -numericEntity = do - x <- located $ between "&#" ";" (NumericEntity <$> (hexadecimal <|> decimal)) - pure [x] - other :: Lexer other = do pos <- getPosition @@ -329,7 +331,6 @@ scan :: Parser Text scan f initState = do parserState@State{stateInput = input, statePos = pos} <- Parsec.getParserState - (remaining, finalPos, ct) <- go input initState pos 0 let newState = parserState{stateInput = remaining, statePos = finalPos} Parsec.setParserState newState $> Text.take ct input diff --git a/src/Parser/Util.hs b/src/Parser/Util.hs index a75fcef..4cf96cb 100644 --- a/src/Parser/Util.hs +++ b/src/Parser/Util.hs @@ -13,9 +13,7 @@ import Text.Parsec.Pos (updatePosChar) Return everything consumed except for the end pattern itself. -} takeUntil :: Text -> Parser Text -takeUntil end_ = - requireEnd (scan p (False, end)) - >>= gotSome . Text.dropEnd (Text.length end_) +takeUntil end_ = Text.dropEnd (Text.length end_) <$> requireEnd (scan p (False, end)) >>= gotSome where end = Text.unpack end_ diff --git a/src/Types.hs b/src/Types.hs index ec7a4e4..a41e38b 100644 --- a/src/Types.hs +++ b/src/Types.hs @@ -9,8 +9,6 @@ module Types ( ) where -import Data.Foldable (fold) - newtype Document = Document { meta :: Meta } @@ -30,7 +28,6 @@ data Since = Since -- Could have a better type? type Version = [Int] - type Package = String data DocMarkup mod id @@ -54,78 +51,78 @@ data DocMarkup mod id | -- | Bold __bold text__ DocBold (DocMarkup mod id) | {- | Unordered lists - * this - or - - this + * this + or + - this -} DocUnorderedList [DocMarkup mod id] | {- | Ordered lists - 1. this - or - (1) this + 1. this + or + (1) this -} DocOrderedList [(Int, DocMarkup mod id)] | {- | Definition lists - [term] a term - [another term] another definition + [term] a term + [another term] another definition -} DocDefinitionList [(DocMarkup mod id, DocMarkup mod id)] | {- | Code blocks - @ - a code block in here - with multiple lines - @ + @ + a code block in here + with multiple lines + @ - Or with bird tracks: - > some code - > goes here + Or with bird tracks: + > some code + > goes here -} DocCodeBlock (DocMarkup mod id) | {- | Hyperlinks - __marked__: - - - __Auto-detected URLs__: - http://example.com - https://example.com - ftp://example.com - __Markdown style__ - [link text](http://example.com) - [link text]("Module.Name") + __marked__: + + + __Auto-detected URLs__: + http://example.com + https://example.com + ftp://example.com + __Markdown style__ + [link text](http://example.com) + [link text]("Module.Name") -} DocHyperlink (Hyperlink (DocMarkup mod id)) | {- | Pictures - <> - <> + <> + <> - __Markdown Images__ + __Markdown Images__ - ![alt text](image.png) + ![alt text](image.png) -} DocPicture Picture | {- | Inline math expressions - \(mathematical expression\) + \(mathematical expression\) -} DocMathInline String | {- | Math multiline display - \[ - mathematical expression - in multiple lines - \] + \[ + mathematical expression + in multiple lines + \] -} DocMathDisplay String | {- | Anchors, no spaces allowed - #anchor-name# + #anchor-name# -} DocAnchor String | {- | Property descriptions - prop> property description + prop> property description -} DocProperty String | {- | Examples - >>> expression - result line 1 - result line 2 + >>> expression + result line 1 + result line 2 -} DocExamples [Example] | -- | Header @@ -139,7 +136,7 @@ instance Semigroup (DocMarkup mod id) where instance Monoid (DocMarkup mod id) where mempty = DocEmpty - mconcat = fold + mconcat = foldr (<>) mempty data ModuleLink id = ModuleLink { name :: String diff --git a/test/Spec.hs b/test/Spec.hs index 2040e2f..0b61570 100644 --- a/test/Spec.hs +++ b/test/Spec.hs @@ -1,15 +1,15 @@ {-# LANGUAGE OverloadedStrings #-} {-# OPTIONS_GHC -Wno-orphans #-} -import Data.String (IsString (..)) -import Data.Text (Text) -import GHC.Stack +import Test.Hspec + import Identifier (Identifier) import Lexer import Parser import Types -import Test.Hspec +import Data.String (IsString (..)) +import Data.Text (Text) import Text.Parsec.Pos main :: IO () @@ -18,8 +18,8 @@ main = hspec $ do describe "minimal" do it "handles unicode" unicode it "escapes" escaping - it "maths" math - it "anchors" anchor + it "maths" maths + it "anchors" anchors it "space chars" space it "bare string" someString it "emphasis" emphatic @@ -29,14 +29,13 @@ main = hspec $ do it "bird tracks" birdTracks it "module names" modules it "quotes" quotes - it "numeric entity" numericEntity it "ignores nesting" ignoreNesting describe "Parser" do it "Bold" do - "__bold__" `shouldParseTo` DocBold (DocString "bold") + "__bold__" `shouldParseTo` (DocBold (DocString "bold")) it "Emphasis" do - "/emphasis/" `shouldParseTo` DocEmphasis (DocString "emphasis") + "/emphasis/" `shouldParseTo` (DocEmphasis (DocString "emphasis")) ------------ -- Tests @@ -45,17 +44,15 @@ main = hspec $ do modules :: Expectation modules = do "\"MyModule.Name\"" - `shouldLexTo` [ (1, 2, Module "MyModule.Name") + `shouldLexTo` [ (1, 1, Module) + , (1, 2, Token "MyModule.Name") ] "\"OtherModule.Name#myAnchor\"" - `shouldLexTo` [ (1, 2, Module "OtherModule.Name") - , (1, 18, Anchor "myAnchor") - ] - - "\"OtherModule.Name\\#myAnchor\"" - `shouldLexTo` [ (1, 2, Module "OtherModule.Name") - , (1, 18, Anchor "myAnchor") + `shouldLexTo` [ (1, 1, Module) + , (1, 2, Token "OtherModule.Name") + , (1, 18, Anchor) + , (1, 19, Token "myAnchor") ] link :: Expectation @@ -85,35 +82,33 @@ labeledLink = , (1, 35, LabeledLinkClose) ] -anchor :: Expectation -anchor = +anchors :: Expectation +anchors = "#myAnchor#" - `shouldLexTo` [ (1, 1, Anchor "myAnchor") + `shouldLexTo` [ (1, 1, Anchor) + , (1, 2, Token "myAnchor") + , (1, 10, Anchor) ] -math :: IO () -math = do +maths :: IO () +maths = do "\\[some math\\]" - `shouldLexTo` [ (1, 1, MathMultilineOpen) + `shouldLexTo` [ (1, 1, MathsBracketOpen) , (1, 3, Token "some math") - , (1, 12, MathMultilineClose) + , (1, 12, MathsBracketClose) ] "\\(other maths\\)" - `shouldLexTo` [ (1, 1, MathInlineOpen) + `shouldLexTo` [ (1, 1, MathsParenOpen) , (1, 3, Token "other maths") - , (1, 14, MathInlineClose) + , (1, 14, MathsParenClose) ] escaping :: Expectation -escaping = do +escaping = "\\(" `shouldLexTo` [ (1, 1, Escape) , (1, 2, Token "(") ] - "\\(\r\n" - `shouldLexTo` [ (1, 1, Escape) - , (1, 2, Token "(") - ] unicode :: Expectation unicode = @@ -153,18 +148,6 @@ space = do , (1, 2, Newline) ] -numericEntity :: Expectation -numericEntity = do - "A λ" - `shouldLexTo` [ (1, 1, NumericEntity 65) - , (1, 6, Space) - , (1, 7, NumericEntity 955) -- lambda - ] - -- Hex - "e" - `shouldLexTo` [ (1, 1, NumericEntity 101) - ] - monospace :: Expectation monospace = "@mono@" @@ -200,12 +183,11 @@ instance IsString (Doc String) where shouldLexTo :: String -> [(Int, Int, Token)] -> Expectation shouldLexTo input expected = - withFrozenCallStack $ - case lexer input of - Right tokens -> do - let actual = map (\(pos, tok) -> (sourceLine pos, sourceColumn pos, tok)) tokens - actual `shouldBe` expected - Left err -> expectationFailure $ "Parse error: " <> show err + case lexer input of + Right tokens -> do + let actual = map (\(pos, tok) -> (sourceLine pos, sourceColumn pos, tok)) tokens + actual `shouldBe` expected + Left err -> expectationFailure $ "Parse error: " <> show err shouldParseTo :: Text -> DocMarkup mod Identifier -> Expectation shouldParseTo input ast = parseText input `shouldBe` ast diff --git a/test/markup.md b/test/markup.md index 187a525..befd3ed 100644 --- a/test/markup.md +++ b/test/markup.md @@ -27,16 +27,8 @@ ftp\://example.com ![alt text](image.png) -\(mathematical 1+3 expression\) - -\[mathematical - expression - accross lines with + addition and such -\] - -{ -e -¥ +\(mathematical expression\) +\[mathematical expression\] @ code block content