ref(moduleName): break into multiple smaller functions

upperId has been changed to only use isUpper because an non alphabetical
character would be false anyway
This commit is contained in:
Primrose 2025-09-24 20:47:55 +08:00
parent 368e5bc9a0
commit 7ceb9b0277
Signed by: primrose
GPG key ID: 4E887A4CA9714ADA
2 changed files with 23 additions and 26 deletions

View file

@ -56,7 +56,7 @@ data Token
| MathsBracketOpen | MathsBracketOpen
| MathsBracketClose | MathsBracketClose
| NumericEntity Int | NumericEntity Int
| Module | Module Text
| QuoteOpen | QuoteOpen
| QuoteClose | QuoteClose
| Space | Space
@ -180,31 +180,31 @@ anchors =
[ between anchorHash anchorHash (Anchor <$> anyUntil anchorHash) [ between anchorHash anchorHash (Anchor <$> anyUntil anchorHash)
] ]
moduleName :: Parser Text
moduleName = intercalate "." . fmap Text.pack <$> upperId `sepBy1` char '.'
upperId :: Parser String
upperId = (:) <$> satisfy isUpper <*> many1 identifierChar
identifierChar :: Parser Char
identifierChar = satisfy (\c -> isAlphaNum c || c == '_')
-- "Module.Name" -- "Module.Name"
-- "Module.Name#anchor" -- "Module.Name#anchor"
-- "Module.Name\#anchor" -- this has been deprecated for 9 years, thanks Ben -- "Module.Name\#anchor" -- this has been deprecated for 9 years, thanks Ben
modules :: Lexer modules :: Lexer
modules = do modules = between (char '"') (char '"') inner
startPos <- startPosition $ char '"'
(modPos, modName) <- located modId
anch <- option [] do
anchPos <- startPosition (string "#" <|> string' "\\#")
txt <- Text.pack <$> many (satisfy (\c -> c /= '"' && not (isSpace c)))
pure [(anchPos, Anchor txt)]
void $ char '"'
pure $ [(startPos, Module), (modPos, Token modName)] <> anch
where where
modId = intercalate "." <$> (fmap Text.pack <$> (conId `sepBy1` (char '.'))) inner = do
module_ <- located $ Module <$> moduleName
mAnchor <- optionMaybe (located $ anchorHash *> (Anchor <$> anchorText))
pure $ case mAnchor of
Just anchor -> [module_, anchor]
Nothing -> [module_]
conId :: Parser String anchorText :: Parser Text
conId = anchorText = Text.pack <$> many (satisfy (\c -> c /= '"' && not (isSpace c)))
(:)
<$> satisfy (\c -> isAlpha c && isUpper c)
<*> many1 conChar
conChar :: Parser Char
conChar = satisfy (\c -> isAlphaNum c || c == '_')
linkRaw :: Lexer linkRaw :: Lexer
linkRaw = linkRaw =

View file

@ -45,19 +45,16 @@ main = hspec $ do
modules :: Expectation modules :: Expectation
modules = do modules = do
"\"MyModule.Name\"" "\"MyModule.Name\""
`shouldLexTo` [ (1, 1, Module) `shouldLexTo` [ (1, 2, Module "MyModule.Name")
, (1, 2, Token "MyModule.Name")
] ]
"\"OtherModule.Name#myAnchor\"" "\"OtherModule.Name#myAnchor\""
`shouldLexTo` [ (1, 1, Module) `shouldLexTo` [ (1, 2, Module "OtherModule.Name")
, (1, 2, Token "OtherModule.Name")
, (1, 18, Anchor "myAnchor") , (1, 18, Anchor "myAnchor")
] ]
"\"OtherModule.Name\\#myAnchor\"" "\"OtherModule.Name\\#myAnchor\""
`shouldLexTo` [ (1, 1, Module) `shouldLexTo` [ (1, 2, Module "OtherModule.Name")
, (1, 2, Token "OtherModule.Name")
, (1, 18, Anchor "myAnchor") , (1, 18, Anchor "myAnchor")
] ]
link :: Expectation link :: Expectation