Trying something to get rid of so much try/choice.

This commit is contained in:
Igor Ranieri 2025-09-28 22:15:45 +02:00
parent f9423d4af0
commit 9ee5a6317d

View file

@ -7,7 +7,7 @@ module Lexer (
)
where
import Control.Monad (mfilter, void)
import Control.Monad (guard, mfilter, void)
import Data.Char (ord, toLower)
import Data.Functor (($>))
import Data.Text (Text, intercalate)
@ -87,39 +87,39 @@ lexText = go
rest <- go
pure (toks <> rest)
{- FOURMOLU_DISABLE -}
topLevel =
-- backtracking here so we always have a chance to try "other", the "catch-all-leave-to-parser-to-deal-with" choice
-- TODO: is this desirable? do we throw lexer error at all?
try
( choice
topLevel = do
-- check for start-of-line markup first
lineStart <-
optionMaybe $
choice
[ try expression
, try birdTrack
, headers
]
case lineStart of
Just toks -> pure toks
Nothing ->
choice $
-- Sorted in
-- - longest to shortest parse path
-- - highest frequency to lowest frequency (for performance?)
-- - more exact to more freeform (the latter can be the former but not vice versa)
[ spaceToken
, newlineToken
, try module_
, quotes
, try expression
, birdTrack
-- starts with "\"
, try mathMultiline
, -- starts with "\"
try mathMultiline
, try mathInline
, escape
, headers
, labeledLink
, link
, anchor
, numericEntity
, textElement
, other
]
)
<|> other
{- FOURMOLU_ENABLE -}
-- Tokens
@ -174,11 +174,7 @@ eol = void "\n" <|> void "\r\n" <|> Parsec.eof
-- Start of line // start of file
sol :: Parser ()
sol = do
p <- getPosition
if sourceColumn p == 1
then pure ()
else fail "Not at start of line/document"
sol = getPosition >>= guard . (== 1) . sourceColumn
header1 :: Lexer
header1 = delimitedNoTrailing "= " eol (Header One)