From 9ee5a6317d0f6b7086c16fbaa9626b2903c19c44 Mon Sep 17 00:00:00 2001 From: Igor Ranieri Date: Sun, 28 Sep 2025 22:15:45 +0200 Subject: [PATCH] Trying something to get rid of so much try/choice. --- src/Lexer.hs | 42 +++++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/src/Lexer.hs b/src/Lexer.hs index 3b04ecc..c2da642 100644 --- a/src/Lexer.hs +++ b/src/Lexer.hs @@ -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)