Added picture support #11
1 changed files with 36 additions and 3 deletions
39
src/Lexer.hs
39
src/Lexer.hs
|
|
@ -1,4 +1,5 @@
|
||||||
{-# LANGUAGE OverloadedStrings #-}
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
{-# LANGUAGE RecordWildCards #-}
|
||||||
|
|
||||||
module Lexer (
|
module Lexer (
|
||||||
Token (..),
|
Token (..),
|
||||||
|
|
@ -7,7 +8,7 @@ module Lexer (
|
||||||
)
|
)
|
||||||
where
|
where
|
||||||
|
|
||||||
import Control.Monad (mfilter, void)
|
import Control.Monad (guard, mfilter, void)
|
||||||
import Data.Char (ord, toLower)
|
import Data.Char (ord, toLower)
|
||||||
import Data.Functor (($>))
|
import Data.Functor (($>))
|
||||||
import Data.Text (Text, intercalate)
|
import Data.Text (Text, intercalate)
|
||||||
|
|
@ -62,6 +63,7 @@ data Token
|
||||||
| Module Text
|
| Module Text
|
||||||
| QuoteOpen
|
| QuoteOpen
|
||||||
| QuoteClose
|
| QuoteClose
|
||||||
|
| Picture {href :: Text, altText :: Maybe Text}
|
||||||
| Space
|
| Space
|
||||||
| EOF
|
| EOF
|
||||||
deriving (Eq, Show)
|
deriving (Eq, Show)
|
||||||
|
|
@ -90,6 +92,7 @@ lexText = go
|
||||||
, escape -- maths go before escape to avoid mismatch
|
, escape -- maths go before escape to avoid mismatch
|
||||||
, headers
|
, headers
|
||||||
, newlineToken
|
, newlineToken
|
||||||
|
, pictures
|
||||||
, spaceToken
|
, spaceToken
|
||||||
, link
|
, link
|
||||||
, labeledLink
|
, labeledLink
|
||||||
|
|
@ -127,6 +130,14 @@ headers =
|
||||||
, header6
|
, header6
|
||||||
]
|
]
|
||||||
|
|
||||||
|
pictures :: Parser [LocatedToken]
|
||||||
|
pictures =
|
||||||
|
choice $
|
||||||
|
Parsec.try
|
||||||
|
<$> [ picture
|
||||||
|
, markdownPicture
|
||||||
|
]
|
||||||
|
|
||||||
anyUntil :: Parser a -> Parser Text
|
anyUntil :: Parser a -> Parser Text
|
||||||
anyUntil p = Text.pack <$> manyTill anyChar (lookAhead p)
|
anyUntil p = Text.pack <$> manyTill anyChar (lookAhead p)
|
||||||
|
|
||||||
|
|
@ -177,6 +188,27 @@ anchor = do
|
||||||
x <- located $ between "#" "#" (Anchor <$> anyUntil "#")
|
x <- located $ between "#" "#" (Anchor <$> anyUntil "#")
|
||||||
pure [x]
|
pure [x]
|
||||||
|
|
||||||
|
picture :: Lexer
|
||||||
|
picture = do
|
||||||
|
void $ string "<<"
|
||||||
|
pos <- getPosition
|
||||||
|
href <- anyUntil (string " " <|> string ">>")
|
||||||
|
altText <- optionMaybe do
|
||||||
|
void $ many1 space
|
||||||
|
anyUntil ">>"
|
||||||
|
void (string ">>")
|
||||||
|
pure [(pos, Picture{..})]
|
||||||
|
|
||||||
|
markdownPicture :: Lexer
|
||||||
|
markdownPicture = do
|
||||||
|
void $ string ""
|
||||||
|
void $ string ")"
|
||||||
|
pure [(pos, Picture{..})]
|
||||||
|
|
||||||
moduleNames :: Parser Text
|
moduleNames :: Parser Text
|
||||||
moduleNames = intercalate "." . fmap Text.pack <$> upperId `sepBy1` char '.'
|
moduleNames = intercalate "." . fmap Text.pack <$> upperId `sepBy1` char '.'
|
||||||
|
|
||||||
|
|
@ -286,8 +318,9 @@ other = do
|
||||||
pos <- getPosition
|
pos <- getPosition
|
||||||
c <- takeWhile1_ isUnicodeAlphaNum
|
c <- takeWhile1_ isUnicodeAlphaNum
|
||||||
pure . pure $ (pos, Token c)
|
pure . pure $ (pos, Token c)
|
||||||
where
|
|
||||||
isUnicodeAlphaNum c = isPrint c && not (isControl c) && not (isSpace c)
|
isUnicodeAlphaNum :: Char -> Bool
|
||||||
|
isUnicodeAlphaNum c = isPrint c && not (isControl c) && not (isSpace c)
|
||||||
|
|
||||||
spaceToken :: Lexer
|
spaceToken :: Lexer
|
||||||
spaceToken = do
|
spaceToken = do
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue