Tokenise helper
This commit is contained in:
parent
7ae868932d
commit
00a6e11f67
1 changed files with 17 additions and 27 deletions
44
src/Lexer.hs
44
src/Lexer.hs
|
|
@ -62,6 +62,15 @@ data Token
|
||||||
| EOF
|
| EOF
|
||||||
deriving (Eq, Show)
|
deriving (Eq, Show)
|
||||||
|
|
||||||
|
located :: Parser a -> Parser (SourcePos, a)
|
||||||
|
located p = (,) <$> getPosition <*> p
|
||||||
|
|
||||||
|
startPosition :: Parser a -> Parser SourcePos
|
||||||
|
startPosition = fmap fst . located
|
||||||
|
|
||||||
|
tokenise :: [Parser a] -> Parser [(SourcePos, a)]
|
||||||
|
tokenise = sequence . map located
|
||||||
|
|
||||||
lexer :: String -> Either ParseError [LocatedToken]
|
lexer :: String -> Either ParseError [LocatedToken]
|
||||||
lexer = Parsec.runParser lexText initialParserState "input" . Text.pack
|
lexer = Parsec.runParser lexText initialParserState "input" . Text.pack
|
||||||
|
|
||||||
|
|
@ -175,12 +184,6 @@ anchors = do
|
||||||
where
|
where
|
||||||
anchor' = (string "#" <|> string "\\#")
|
anchor' = (string "#" <|> string "\\#")
|
||||||
|
|
||||||
located :: Parser a -> Parser (SourcePos, a)
|
|
||||||
located p = (,) <$> getPosition <*> p
|
|
||||||
|
|
||||||
startPosition :: Parser a -> Parser SourcePos
|
|
||||||
startPosition = fmap fst . located
|
|
||||||
|
|
||||||
-- "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
|
||||||
|
|
@ -208,27 +211,14 @@ modules = do
|
||||||
conChar = satisfy (\c -> isAlphaNum c || c == '_')
|
conChar = satisfy (\c -> isAlphaNum c || c == '_')
|
||||||
|
|
||||||
linkRaw :: Lexer
|
linkRaw :: Lexer
|
||||||
linkRaw = do
|
linkRaw =
|
||||||
pos1 <- getPosition
|
tokenise
|
||||||
void $ string "["
|
[ BracketOpen <$ "["
|
||||||
pos2 <- getPosition
|
, Token <$> anyUntil "]"
|
||||||
text <- anyUntil $ Text.pack <$> string "]"
|
, BracketClose <$ "]"
|
||||||
pos3 <- getPosition
|
, ParenOpen <$ "("
|
||||||
void $ "]"
|
, Token <$> anyUntil ")"
|
||||||
pos4 <- getPosition
|
, ParenClose <$ ")"
|
||||||
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 :: Lexer
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue