Improved bare link parser
This commit is contained in:
parent
82dcb6913e
commit
7c4603528d
2 changed files with 38 additions and 26 deletions
60
src/Lexer.hs
60
src/Lexer.hs
|
|
@ -42,6 +42,8 @@ data Token
|
||||||
| MonospaceOpen
|
| MonospaceOpen
|
||||||
| MonospaceClose
|
| MonospaceClose
|
||||||
| Newline
|
| Newline
|
||||||
|
| LinkOpen
|
||||||
|
| LinkClose
|
||||||
| LabeledLinkOpen
|
| LabeledLinkOpen
|
||||||
| LabeledLinkClose
|
| LabeledLinkClose
|
||||||
| ParenOpen
|
| ParenOpen
|
||||||
|
|
@ -159,30 +161,6 @@ header5 = delimitedMaybe (void $ "===== ") eol (Header Five) Nothing
|
||||||
header6 :: Lexer
|
header6 :: Lexer
|
||||||
header6 = delimitedMaybe (void $ "====== ") eol (Header Six) Nothing
|
header6 = delimitedMaybe (void $ "====== ") eol (Header Six) Nothing
|
||||||
|
|
||||||
link :: Lexer
|
|
||||||
link = do
|
|
||||||
pos1 <- getPosition
|
|
||||||
void $ string "["
|
|
||||||
pos2 <- getPosition
|
|
||||||
text <- anyUntil $ Text.pack <$> string "]"
|
|
||||||
pos3 <- getPosition
|
|
||||||
void $ "]"
|
|
||||||
pos4 <- getPosition
|
|
||||||
void $ "("
|
|
||||||
pos5 <- getPosition
|
|
||||||
link' <- anyUntil $ Text.pack <$> string ")"
|
|
||||||
pos6 <- getPosition
|
|
||||||
void $ ")"
|
|
||||||
|
|
||||||
pure $
|
|
||||||
[ (pos1, BracketOpen)
|
|
||||||
, (pos2, Token text)
|
|
||||||
, (pos3, BracketClose)
|
|
||||||
, (pos4, ParenOpen)
|
|
||||||
, (pos5, Token link')
|
|
||||||
, (pos6, ParenClose)
|
|
||||||
]
|
|
||||||
|
|
||||||
-- "Module.Name"
|
-- "Module.Name"
|
||||||
-- "Module.Name#anchor"
|
-- "Module.Name#anchor"
|
||||||
-- "Module.Name#anchor"
|
-- "Module.Name#anchor"
|
||||||
|
|
@ -213,11 +191,43 @@ modules = do
|
||||||
conChar :: Parser Char
|
conChar :: Parser Char
|
||||||
conChar = satisfy (\c -> isAlphaNum c || c == '_')
|
conChar = satisfy (\c -> isAlphaNum c || c == '_')
|
||||||
|
|
||||||
|
linkRaw :: Lexer
|
||||||
|
linkRaw = do
|
||||||
|
pos1 <- getPosition
|
||||||
|
void $ string "["
|
||||||
|
pos2 <- getPosition
|
||||||
|
text <- anyUntil $ Text.pack <$> string "]"
|
||||||
|
pos3 <- getPosition
|
||||||
|
void $ "]"
|
||||||
|
pos4 <- getPosition
|
||||||
|
void $ "("
|
||||||
|
pos5 <- getPosition
|
||||||
|
link' <- anyUntil $ Text.pack <$> string ")"
|
||||||
|
pos6 <- getPosition
|
||||||
|
void $ ")"
|
||||||
|
|
||||||
|
pure $
|
||||||
|
[ (pos1, BracketOpen)
|
||||||
|
, (pos2, Token text)
|
||||||
|
, (pos3, BracketClose)
|
||||||
|
, (pos4, ParenOpen)
|
||||||
|
, (pos5, Token link')
|
||||||
|
, (pos6, ParenClose)
|
||||||
|
]
|
||||||
|
|
||||||
|
link :: Lexer
|
||||||
|
link = do
|
||||||
|
pos <- getPosition
|
||||||
|
l <- linkRaw
|
||||||
|
-- "unconsume" the last token
|
||||||
|
pos' <- flip incSourceColumn (-1) <$> getPosition
|
||||||
|
pure $ (pos, LinkOpen) : l <> [(pos', LinkClose)]
|
||||||
|
|
||||||
labeledLink :: Lexer
|
labeledLink :: Lexer
|
||||||
labeledLink = do
|
labeledLink = do
|
||||||
pos <- getPosition
|
pos <- getPosition
|
||||||
void $ string "<"
|
void $ string "<"
|
||||||
link' <- link
|
link' <- linkRaw
|
||||||
pos7 <- getPosition
|
pos7 <- getPosition
|
||||||
label' <- anyUntil $ string ">"
|
label' <- anyUntil $ string ">"
|
||||||
pos8 <- getPosition
|
pos8 <- getPosition
|
||||||
|
|
|
||||||
|
|
@ -57,12 +57,14 @@ modules = do
|
||||||
link :: Expectation
|
link :: Expectation
|
||||||
link =
|
link =
|
||||||
"[link to](http://some.website)"
|
"[link to](http://some.website)"
|
||||||
`shouldLexTo` [ (1, 1, BracketOpen)
|
`shouldLexTo` [ (1, 1, LinkOpen)
|
||||||
|
, (1, 1, BracketOpen)
|
||||||
, (1, 2, Token "link to")
|
, (1, 2, Token "link to")
|
||||||
, (1, 9, BracketClose)
|
, (1, 9, BracketClose)
|
||||||
, (1, 10, ParenOpen)
|
, (1, 10, ParenOpen)
|
||||||
, (1, 11, Token "http://some.website")
|
, (1, 11, Token "http://some.website")
|
||||||
, (1, 30, ParenClose)
|
, (1, 30, ParenClose)
|
||||||
|
, (1, 30, LinkClose)
|
||||||
]
|
]
|
||||||
|
|
||||||
labeledLink :: Expectation
|
labeledLink :: Expectation
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue