From f5a9dec3d602ce37a2c4b9eac7b3df46a2276677 Mon Sep 17 00:00:00 2001 From: Igor Ranieri Date: Sun, 28 Sep 2025 21:55:02 +0200 Subject: [PATCH] Added expression eval; adjusted birdtrack, added sol combinator. --- sp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ src/Lexer.hs | 22 ++++++++++++++++++++-- test/Spec.hs | 38 ++++++++++++++++++++++++++++++++------ 3 files changed, 100 insertions(+), 8 deletions(-) create mode 100644 sp diff --git a/sp b/sp new file mode 100644 index 0000000..d8db58f --- /dev/null +++ b/sp @@ -0,0 +1,48 @@ +2025-09-28T19:31:53.102437Z | Info | haskell-language-server version: 2.11.0.0 (GHC: 9.10.2) (PATH: /Users/igor/.ghcup/hls/2.11.0.0/lib/haskell-language-server-2.11.0.0/bin/haskell-language-server-9.10.2) +2025-09-28T19:31:53.103002Z | Info | Directory: /Users/igor/dev/haddock2 +2025-09-28T19:31:53.103169Z | Info | Logging heap statistics every 60.00s +2025-09-28T19:31:53.107534Z | Info | Cradle path: src/Parser.hs +2025-09-28T19:31:53.107609Z | Warning | No [cradle](https://github.com/mpickering/hie-bios#hie-bios) found for src/Parser.hs. +Proceeding with [implicit cradle](https://hackage.haskell.org/package/implicit-hie). +You should ignore this message, unless you see a 'Multi Cradle: No prefixes matched' error. +2025-09-28T19:31:53.107865Z | Info | invoking build tool to determine build flags (this may take some time depending on the cache) +2025-09-28T19:31:53.217226Z | Info | Load cabal cradle using single file +2025-09-28T19:31:53.306920Z | Info | cabal --builddir=/Users/igor/.cache/hie-bios/dist-haddock2-46ec6b456aa9d4df12c3c8dec8c82e6e v2-repl --with-compiler /Users/igor/.cache/hie-bios/wrapper-b54f81dea4c0e6d1626911c526bc4e36 /Users/igor/dev/haddock2/src/Parser.hs + Environment Variables + HIE_BIOS_OUTPUT: /private/var/folders/9h/3j3xz6k52s95nrz7fkscvxph0000gs/T/HIE_BIOS_OUTPUT74985-0 + HIE_BIOS_GHC: /Users/igor/.ghcup/bin/ghc + HIE_BIOS_GHC_ARGS: -B/Users/igor/.ghcup/ghc/9.10.2/lib/ghc-9.10.2/lib +2025-09-28T19:31:54.614435Z | Info | Interface files cache directory: /Users/igor/.cache/ghcide/haddock2-0.1.0.0-inplace-haddock2-lib-94626af6e87fa2a1c5fe808fee1ff90470339b17 +2025-09-28T19:31:54.616421Z | Info | Making new HscEnv. In-place unit ids: [haddock2-0.1.0.0-inplace-haddock2-lib] +2025-09-28T19:31:54.624755Z | Info | Cradle path: app/Main.hs +2025-09-28T19:31:54.625170Z | Warning | No [cradle](https://github.com/mpickering/hie-bios#hie-bios) found for app/Main.hs. +Proceeding with [implicit cradle](https://hackage.haskell.org/package/implicit-hie). +You should ignore this message, unless you see a 'Multi Cradle: No prefixes matched' error. +2025-09-28T19:31:54.625483Z | Info | invoking build tool to determine build flags (this may take some time depending on the cache) +2025-09-28T19:31:54.738862Z | Info | Load cabal cradle using single file +2025-09-28T19:31:54.831553Z | Info | cabal --builddir=/Users/igor/.cache/hie-bios/dist-haddock2-46ec6b456aa9d4df12c3c8dec8c82e6e v2-repl --with-compiler /Users/igor/.cache/hie-bios/wrapper-b54f81dea4c0e6d1626911c526bc4e36 /Users/igor/dev/haddock2/app/Main.hs + Environment Variables + HIE_BIOS_OUTPUT: /private/var/folders/9h/3j3xz6k52s95nrz7fkscvxph0000gs/T/HIE_BIOS_OUTPUT74985-1 + HIE_BIOS_GHC: /Users/igor/.ghcup/bin/ghc + HIE_BIOS_GHC_ARGS: -B/Users/igor/.ghcup/ghc/9.10.2/lib/ghc-9.10.2/lib +2025-09-28T19:31:55.143536Z | Info | Interface files cache directory: /Users/igor/.cache/ghcide/haddock2-0.1.0.0-inplace-haddock2-9922c59f82c27147a754c9396574cd2314aa3d19 +2025-09-28T19:31:55.143714Z | Info | Interface files cache directory: /Users/igor/.cache/ghcide/haddock2-0.1.0.0-inplace-haddock2-lib-9922c59f82c27147a754c9396574cd2314aa3d19 +2025-09-28T19:31:55.145930Z | Info | Making new HscEnv. In-place unit ids: [ haddock2-0.1.0.0-inplace-haddock2 + , haddock2-0.1.0.0-inplace-haddock2-lib ] +2025-09-28T19:31:55.158809Z | Info | Cradle path: test/Spec.hs +2025-09-28T19:31:55.158933Z | Warning | No [cradle](https://github.com/mpickering/hie-bios#hie-bios) found for test/Spec.hs. +Proceeding with [implicit cradle](https://hackage.haskell.org/package/implicit-hie). +You should ignore this message, unless you see a 'Multi Cradle: No prefixes matched' error. +2025-09-28T19:31:55.159193Z | Info | invoking build tool to determine build flags (this may take some time depending on the cache) +2025-09-28T19:31:55.275847Z | Info | Load cabal cradle using single file +2025-09-28T19:31:55.368454Z | Info | cabal --builddir=/Users/igor/.cache/hie-bios/dist-haddock2-46ec6b456aa9d4df12c3c8dec8c82e6e v2-repl --with-compiler /Users/igor/.cache/hie-bios/wrapper-b54f81dea4c0e6d1626911c526bc4e36 /Users/igor/dev/haddock2/test/Spec.hs + Environment Variables + HIE_BIOS_OUTPUT: /private/var/folders/9h/3j3xz6k52s95nrz7fkscvxph0000gs/T/HIE_BIOS_OUTPUT74985-2 + HIE_BIOS_GHC: /Users/igor/.ghcup/bin/ghc + HIE_BIOS_GHC_ARGS: -B/Users/igor/.ghcup/ghc/9.10.2/lib/ghc-9.10.2/lib +2025-09-28T19:31:57.159162Z | Info | Interface files cache directory: /Users/igor/.cache/ghcide/haddock2-0.1.0.0-inplace-haddock2-test-c33b0758a00798820f56b5dde448009132dfd22d +2025-09-28T19:31:57.159344Z | Info | Interface files cache directory: /Users/igor/.cache/ghcide/haddock2-0.1.0.0-inplace-haddock2-c33b0758a00798820f56b5dde448009132dfd22d +2025-09-28T19:31:57.159444Z | Info | Interface files cache directory: /Users/igor/.cache/ghcide/haddock2-0.1.0.0-inplace-haddock2-lib-c33b0758a00798820f56b5dde448009132dfd22d +2025-09-28T19:31:57.171150Z | Info | Making new HscEnv. In-place unit ids: [ haddock2-0.1.0.0-inplace-haddock2 + , haddock2-0.1.0.0-inplace-haddock2-lib + , haddock2-0.1.0.0-inplace-haddock2-test ] diff --git a/src/Lexer.hs b/src/Lexer.hs index 84e38cc..6b5474c 100644 --- a/src/Lexer.hs +++ b/src/Lexer.hs @@ -42,6 +42,7 @@ data Token | Escape | EmphasisOpen | EmphasisClose + | Expression | Header Level | MonospaceOpen | MonospaceClose @@ -101,6 +102,7 @@ lexText = go , try module_ , quotes + , try expression , birdTrack -- starts with "\" @@ -158,16 +160,26 @@ delimited openP closeP openTok closeTok = asList <$> delimitedAsTuple (openTok < 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) +delimitedNoTrailing openP closeP openTok = + asList <$> delimitedAsTuple (openTok <$ openP) (void closeP) where asList (a, tok, _) = [a, tok] delimitedSymmetric :: Parser a -> Token -> Token -> Parser [LocatedToken] delimitedSymmetric s = delimited s s +--- End of line // end of file eol :: Parser () eol = void "\n" <|> void "\r\n" <|> Parsec.eof +-- Start of line // start of file +sol :: Parser () +sol = do + p <- getPosition + if sourceLine p == 1 && sourceColumn p == 1 + then pure () + else fail "Not at start of line/document" + header1 :: Lexer header1 = delimitedNoTrailing "= " eol (Header One) @@ -256,7 +268,13 @@ mathInline = delimited "\\(" "\\)" MathInlineOpen MathInlineClose -- TODO: make sure this starts at column 0? birdTrack :: Lexer -birdTrack = delimitedNoTrailing ">> " eol BirdTrack +birdTrack = delimitedNoTrailing (sol <* "> ") eol BirdTrack + +-- TODO: also match following lines iff: +-- they start with alphanum +-- they're not empty +expression :: Lexer +expression = delimitedNoTrailing (sol <* ">>> ") eol Expression escape :: Lexer escape = delimitedNoTrailing "\\" eol Escape diff --git a/test/Spec.hs b/test/Spec.hs index 2040e2f..3db44f1 100644 --- a/test/Spec.hs +++ b/test/Spec.hs @@ -18,7 +18,7 @@ main = hspec $ do describe "minimal" do it "handles unicode" unicode it "escapes" escaping - it "maths" math + it "maths" maths it "anchors" anchor it "space chars" space it "bare string" someString @@ -29,6 +29,7 @@ main = hspec $ do it "bird tracks" birdTracks it "module names" modules it "quotes" quotes + it "expressions" expressions it "numeric entity" numericEntity it "ignores nesting" ignoreNesting @@ -91,8 +92,8 @@ anchor = `shouldLexTo` [ (1, 1, Anchor "myAnchor") ] -math :: IO () -math = do +maths :: IO () +maths = do "\\[some math\\]" `shouldLexTo` [ (1, 1, MathMultilineOpen) , (1, 3, Token "some math") @@ -128,10 +129,35 @@ ignoreNesting = ] birdTracks :: Expectation -birdTracks = - ">> code" +birdTracks = do + "> code line" `shouldLexTo` [ (1, 1, BirdTrack) - , (1, 4, Token "code") + , (1, 3, Token "code line") + ] + " > not code" + `shouldLexTo` [ (1, 1, Space) + , (1, 2, Token ">") + , (1, 3, Space) + , (1, 4, Token "not") + , (1, 7, Space) + , (1, 8, Token "code") + ] + +expressions :: Expectation +expressions = do + ">>> eval this" + `shouldLexTo` [ (1, 1, Expression) + , (1, 5, Token "eval this") + ] + " >>> not eval this" + `shouldLexTo` [ (1, 1, Space) + , (1, 2, Token ">>>") + , (1, 5, Space) + , (1, 6, Token "not") + , (1, 9, Space) + , (1, 10, Token "eval") + , (1, 14, Space) + , (1, 15, Token "this") ] quotes :: Expectation