ref(lexer): attempt to not try on every token for a better error message

This commit is contained in:
Primrose 2025-09-28 12:35:56 +08:00
parent 82eb8435ab
commit 8261866ef2
Signed by: primrose
GPG key ID: 4E887A4CA9714ADA

View file

@ -83,24 +83,32 @@ lexText = go
Just _ -> pure [] Just _ -> pure []
Nothing -> do Nothing -> do
toks <- toks <-
choice $ -- backtracking here so we always have a chance to try "other", the "catch-all-leave-to-parser-to-deal-with" choice
Parsec.try -- TODO: is this desirable? do we throw lexer error at all?
<$> [ mathMultiline try
, mathInline ( choice
, escape -- maths go before escape to avoid mismatch -- Sorted in
, headers -- - longest to shortest parse path
, newlineToken -- - highest frequency to lowest frequency (for performance?)
, spaceToken -- - more exact to more freeform (the latter can be the former but not vice versa)
, link [ spaceToken
, labeledLink , newlineToken
, module_ , -- starts with "\"
, anchor try mathMultiline
, numericEntity , try mathInline
, textElement , escape
, quotes , headers
, birdTrack , labeledLink
, other , link
] , anchor
, numericEntity
, textElement
, try module_
, quotes
, birdTrack
]
)
<|> other
rest <- go rest <- go
pure (toks <> rest) pure (toks <> rest)
@ -239,6 +247,7 @@ mathMultiline = delimited "\\[" "\\]" MathMultilineOpen MathMultilineClose
mathInline :: Lexer mathInline :: Lexer
mathInline = delimited "\\(" "\\)" MathInlineOpen MathInlineClose mathInline = delimited "\\(" "\\)" MathInlineOpen MathInlineClose
-- TODO: make sure this starts at column 0?
birdTrack :: Lexer birdTrack :: Lexer
birdTrack = delimitedNoTrailing ">> " eol BirdTrack birdTrack = delimitedNoTrailing ">> " eol BirdTrack