module Data.GI.CodeGen.GtkDoc
( parseGtkDoc
, GtkDoc(..)
, Token(..)
, Language(..)
, Link(..)
, ListItem(..)
, CRef(..)
) where
import Prelude hiding (takeWhile)
#if !MIN_VERSION_base(4,8,0)
import Control.Applicative ((<$>), (<*))
#endif
#if !MIN_VERSION_base(4,13,0)
import Data.Monoid ((<>))
#endif
import Control.Applicative ((<|>))
import Data.Attoparsec.Text
import Data.Char (isAsciiUpper, isAsciiLower, isDigit)
import qualified Data.Text as T
import Data.Text (Text)
data Token = Literal Text
| Text
| Verbatim Text
| CodeBlock (Maybe Language) Text
| ExternalLink Link
| Image Link
| List [ListItem]
| Int GtkDoc
| SymbolRef CRef
deriving (Int -> Token -> ShowS
[Token] -> ShowS
Token -> String
(Int -> Token -> ShowS)
-> (Token -> String) -> ([Token] -> ShowS) -> Show Token
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Token] -> ShowS
$cshowList :: [Token] -> ShowS
show :: Token -> String
$cshow :: Token -> String
showsPrec :: Int -> Token -> ShowS
$cshowsPrec :: Int -> Token -> ShowS
Show, Token -> Token -> Bool
(Token -> Token -> Bool) -> (Token -> Token -> Bool) -> Eq Token
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Token -> Token -> Bool
$c/= :: Token -> Token -> Bool
== :: Token -> Token -> Bool
$c== :: Token -> Token -> Bool
Eq)
data Link = Link { Link -> Text
linkName :: Text
, Link -> Text
linkAddress :: Text }
deriving (Int -> Link -> ShowS
[Link] -> ShowS
Link -> String
(Int -> Link -> ShowS)
-> (Link -> String) -> ([Link] -> ShowS) -> Show Link
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Link] -> ShowS
$cshowList :: [Link] -> ShowS
show :: Link -> String
$cshow :: Link -> String
showsPrec :: Int -> Link -> ShowS
$cshowsPrec :: Int -> Link -> ShowS
Show, Link -> Link -> Bool
(Link -> Link -> Bool) -> (Link -> Link -> Bool) -> Eq Link
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Link -> Link -> Bool
$c/= :: Link -> Link -> Bool
== :: Link -> Link -> Bool
$c== :: Link -> Link -> Bool
Eq)
data ListItem = ListItem GtkDoc [GtkDoc]
deriving (Int -> ListItem -> ShowS
[ListItem] -> ShowS
ListItem -> String
(Int -> ListItem -> ShowS)
-> (ListItem -> String) -> ([ListItem] -> ShowS) -> Show ListItem
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ListItem] -> ShowS
$cshowList :: [ListItem] -> ShowS
show :: ListItem -> String
$cshow :: ListItem -> String
showsPrec :: Int -> ListItem -> ShowS
$cshowsPrec :: Int -> ListItem -> ShowS
Show, ListItem -> ListItem -> Bool
(ListItem -> ListItem -> Bool)
-> (ListItem -> ListItem -> Bool) -> Eq ListItem
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ListItem -> ListItem -> Bool
$c/= :: ListItem -> ListItem -> Bool
== :: ListItem -> ListItem -> Bool
$c== :: ListItem -> ListItem -> Bool
Eq)
newtype Language = Language Text
deriving (Int -> Language -> ShowS
[Language] -> ShowS
Language -> String
(Int -> Language -> ShowS)
-> (Language -> String) -> ([Language] -> ShowS) -> Show Language
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Language] -> ShowS
$cshowList :: [Language] -> ShowS
show :: Language -> String
$cshow :: Language -> String
showsPrec :: Int -> Language -> ShowS
$cshowsPrec :: Int -> Language -> ShowS
Show, Language -> Language -> Bool
(Language -> Language -> Bool)
-> (Language -> Language -> Bool) -> Eq Language
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Language -> Language -> Bool
$c/= :: Language -> Language -> Bool
== :: Language -> Language -> Bool
$c== :: Language -> Language -> Bool
Eq)
data CRef = FunctionRef Text
| ParamRef Text
| ConstantRef Text
| SignalRef Text Text
| LocalSignalRef Text
| PropertyRef Text Text
| VMethodRef Text Text
| StructFieldRef Text Text
| TypeRef Text
deriving (Int -> CRef -> ShowS
[CRef] -> ShowS
CRef -> String
(Int -> CRef -> ShowS)
-> (CRef -> String) -> ([CRef] -> ShowS) -> Show CRef
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CRef] -> ShowS
$cshowList :: [CRef] -> ShowS
show :: CRef -> String
$cshow :: CRef -> String
showsPrec :: Int -> CRef -> ShowS
$cshowsPrec :: Int -> CRef -> ShowS
Show, CRef -> CRef -> Bool
(CRef -> CRef -> Bool) -> (CRef -> CRef -> Bool) -> Eq CRef
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CRef -> CRef -> Bool
$c/= :: CRef -> CRef -> Bool
== :: CRef -> CRef -> Bool
$c== :: CRef -> CRef -> Bool
Eq, Eq CRef
Eq CRef =>
(CRef -> CRef -> Ordering)
-> (CRef -> CRef -> Bool)
-> (CRef -> CRef -> Bool)
-> (CRef -> CRef -> Bool)
-> (CRef -> CRef -> Bool)
-> (CRef -> CRef -> CRef)
-> (CRef -> CRef -> CRef)
-> Ord CRef
CRef -> CRef -> Bool
CRef -> CRef -> Ordering
CRef -> CRef -> CRef
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: CRef -> CRef -> CRef
$cmin :: CRef -> CRef -> CRef
max :: CRef -> CRef -> CRef
$cmax :: CRef -> CRef -> CRef
>= :: CRef -> CRef -> Bool
$c>= :: CRef -> CRef -> Bool
> :: CRef -> CRef -> Bool
$c> :: CRef -> CRef -> Bool
<= :: CRef -> CRef -> Bool
$c<= :: CRef -> CRef -> Bool
< :: CRef -> CRef -> Bool
$c< :: CRef -> CRef -> Bool
compare :: CRef -> CRef -> Ordering
$ccompare :: CRef -> CRef -> Ordering
$cp1Ord :: Eq CRef
Ord)
newtype GtkDoc = GtkDoc [Token]
deriving (Int -> GtkDoc -> ShowS
[GtkDoc] -> ShowS
GtkDoc -> String
(Int -> GtkDoc -> ShowS)
-> (GtkDoc -> String) -> ([GtkDoc] -> ShowS) -> Show GtkDoc
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GtkDoc] -> ShowS
$cshowList :: [GtkDoc] -> ShowS
show :: GtkDoc -> String
$cshow :: GtkDoc -> String
showsPrec :: Int -> GtkDoc -> ShowS
$cshowsPrec :: Int -> GtkDoc -> ShowS
Show, GtkDoc -> GtkDoc -> Bool
(GtkDoc -> GtkDoc -> Bool)
-> (GtkDoc -> GtkDoc -> Bool) -> Eq GtkDoc
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: GtkDoc -> GtkDoc -> Bool
$c/= :: GtkDoc -> GtkDoc -> Bool
== :: GtkDoc -> GtkDoc -> Bool
$c== :: GtkDoc -> GtkDoc -> Bool
Eq)
parseGtkDoc :: Text -> GtkDoc
parseGtkDoc :: Text -> GtkDoc
parseGtkDoc raw :: Text
raw =
case Parser [Token] -> Text -> Either String [Token]
forall a. Parser a -> Text -> Either String a
parseOnly (Parser [Token]
parseTokens Parser [Token] -> Parser Text () -> Parser [Token]
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser Text ()
forall t. Chunk t => Parser t ()
endOfInput) Text
raw of
Left e :: String
e ->
String -> GtkDoc
forall a. HasCallStack => String -> a
error (String -> GtkDoc) -> String -> GtkDoc
forall a b. (a -> b) -> a -> b
$ "gtk-doc parsing failed with error \"" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
e
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> "\" on the input \"" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
T.unpack Text
raw String -> ShowS
forall a. Semigroup a => a -> a -> a
<> "\""
Right tks :: [Token]
tks -> [Token] -> GtkDoc
GtkDoc ([Token] -> GtkDoc) -> ([Token] -> [Token]) -> [Token] -> GtkDoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Token] -> [Token]
coalesceLiterals
([Token] -> [Token]) -> ([Token] -> [Token]) -> [Token] -> [Token]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Token] -> [Token]
restoreSHPreNewlines ([Token] -> [Token]) -> ([Token] -> [Token]) -> [Token] -> [Token]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Token] -> [Token]
restoreListPreNewline ([Token] -> GtkDoc) -> [Token] -> GtkDoc
forall a b. (a -> b) -> a -> b
$ [Token]
tks
restoreSHPreNewlines :: [Token] -> [Token]
restoreSHPreNewlines :: [Token] -> [Token]
restoreSHPreNewlines [] = []
restoreSHPreNewlines (i :: Token
i : rest :: [Token]
rest) = Token
i Token -> [Token] -> [Token]
forall a. a -> [a] -> [a]
: [Token] -> [Token]
restoreNewlines [Token]
rest
where restoreNewlines :: [Token] -> [Token]
restoreNewlines :: [Token] -> [Token]
restoreNewlines [] = []
restoreNewlines (s :: Token
s@(SectionHeader _ _) : rest :: [Token]
rest) =
Text -> Token
Literal "\n" Token -> [Token] -> [Token]
forall a. a -> [a] -> [a]
: Token
s Token -> [Token] -> [Token]
forall a. a -> [a] -> [a]
: [Token] -> [Token]
restoreNewlines [Token]
rest
restoreNewlines (x :: Token
x : rest :: [Token]
rest) = Token
x Token -> [Token] -> [Token]
forall a. a -> [a] -> [a]
: [Token] -> [Token]
restoreNewlines [Token]
rest
restoreListPreNewline :: [Token] -> [Token]
restoreListPreNewline :: [Token] -> [Token]
restoreListPreNewline [] = []
restoreListPreNewline (l :: Token
l@(List _) : rest :: [Token]
rest) =
Text -> Token
Literal "\n" Token -> [Token] -> [Token]
forall a. a -> [a] -> [a]
: Token
l Token -> [Token] -> [Token]
forall a. a -> [a] -> [a]
: [Token] -> [Token]
restoreListPreNewline [Token]
rest
restoreListPreNewline (x :: Token
x : rest :: [Token]
rest) = Token
x Token -> [Token] -> [Token]
forall a. a -> [a] -> [a]
: [Token] -> [Token]
restoreListPreNewline [Token]
rest
coalesceLiterals :: [Token] -> [Token]
coalesceLiterals :: [Token] -> [Token]
coalesceLiterals tks :: [Token]
tks = Maybe Text -> [Token] -> [Token]
go Maybe Text
forall a. Maybe a
Nothing [Token]
tks
where
go :: Maybe Text -> [Token] -> [Token]
go :: Maybe Text -> [Token] -> [Token]
go Nothing [] = []
go (Just l :: Text
l) [] = [Text -> Token
Literal Text
l]
go Nothing (Literal l :: Text
l : rest :: [Token]
rest) = Maybe Text -> [Token] -> [Token]
go (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
l) [Token]
rest
go (Just l :: Text
l) (Literal l' :: Text
l' : rest :: [Token]
rest) = Maybe Text -> [Token] -> [Token]
go (Text -> Maybe Text
forall a. a -> Maybe a
Just (Text
l Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
l')) [Token]
rest
go Nothing (tk :: Token
tk : rest :: [Token]
rest) = Token
tk Token -> [Token] -> [Token]
forall a. a -> [a] -> [a]
: Maybe Text -> [Token] -> [Token]
go Maybe Text
forall a. Maybe a
Nothing [Token]
rest
go (Just l :: Text
l) (tk :: Token
tk : rest :: [Token]
rest) = Text -> Token
Literal Text
l Token -> [Token] -> [Token]
forall a. a -> [a] -> [a]
: Token
tk Token -> [Token] -> [Token]
forall a. a -> [a] -> [a]
: Maybe Text -> [Token] -> [Token]
go Maybe Text
forall a. Maybe a
Nothing [Token]
rest
parseTokens :: Parser [Token]
parseTokens :: Parser [Token]
parseTokens = Parser [Token]
headerAndTokens Parser [Token] -> Parser [Token] -> Parser [Token]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser [Token]
justTokens
where
headerAndTokens :: Parser [Token]
headerAndTokens :: Parser [Token]
headerAndTokens = do
Token
header <- Parser Token
parseInitialSectionHeader
[Token]
tokens <- Parser [Token]
justTokens
[Token] -> Parser [Token]
forall (m :: * -> *) a. Monad m => a -> m a
return (Token
header Token -> [Token] -> [Token]
forall a. a -> [a] -> [a]
: [Token]
tokens)
justTokens :: Parser [Token]
justTokens :: Parser [Token]
justTokens = Parser Token -> Parser [Token]
forall (m :: * -> *) a. MonadPlus m => m a -> m [a]
many' Parser Token
parseToken
parseToken :: Parser Token
parseToken :: Parser Token
parseToken =
Parser Token
parseFunctionRef
Parser Token -> Parser Token -> Parser Token
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser Token
parseSignal
Parser Token -> Parser Token -> Parser Token
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser Token
parseLocalSignal
Parser Token -> Parser Token -> Parser Token
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser Token
parseProperty
Parser Token -> Parser Token -> Parser Token
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser Token
parseVMethod
Parser Token -> Parser Token -> Parser Token
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser Token
parseStructField
Parser Token -> Parser Token -> Parser Token
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser Token
parseType
Parser Token -> Parser Token -> Parser Token
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser Token
parseConstant
Parser Token -> Parser Token -> Parser Token
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser Token
parseParam
Parser Token -> Parser Token -> Parser Token
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser Token
parseEscaped
Parser Token -> Parser Token -> Parser Token
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser Token
parseVerbatim
Parser Token -> Parser Token -> Parser Token
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser Token
parseCodeBlock
Parser Token -> Parser Token -> Parser Token
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser Token
parseUrl
Parser Token -> Parser Token -> Parser Token
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser Token
parseImage
Parser Token -> Parser Token -> Parser Token
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser Token
parseSectionHeader
Parser Token -> Parser Token -> Parser Token
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser Token
parseList
Parser Token -> Parser Token -> Parser Token
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser Token
parseComment
Parser Token -> Parser Token -> Parser Token
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser Token
parseBoringLiteral
parseSignal :: Parser Token
parseSignal :: Parser Token
parseSignal = do
Char
_ <- Char -> Parser Char
char '#'
Text
obj <- Parser Text
parseCIdent
Text
_ <- Text -> Parser Text
string "::"
Text
signal <- Parser Text
signalOrPropName
Token -> Parser Token
forall (m :: * -> *) a. Monad m => a -> m a
return (CRef -> Token
SymbolRef (Text -> Text -> CRef
SignalRef Text
obj Text
signal))
parseLocalSignal :: Parser Token
parseLocalSignal :: Parser Token
parseLocalSignal = do
Text
_ <- Text -> Parser Text
string "::"
Text
signal <- Parser Text
signalOrPropName
Token -> Parser Token
forall (m :: * -> *) a. Monad m => a -> m a
return (CRef -> Token
SymbolRef (Text -> CRef
LocalSignalRef Text
signal))
parseProperty :: Parser Token
parseProperty :: Parser Token
parseProperty = do
Char
_ <- Char -> Parser Char
char '#'
Text
obj <- Parser Text
parseCIdent
Char
_ <- Char -> Parser Char
char ':'
Text
property <- Parser Text
signalOrPropName
Token -> Parser Token
forall (m :: * -> *) a. Monad m => a -> m a
return (CRef -> Token
SymbolRef (Text -> Text -> CRef
PropertyRef Text
obj Text
property))
parseComment :: Parser Token
= do
String
comment <- Text -> Parser Text
string "<!--" Parser Text -> Parser Text String -> Parser Text String
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser Char -> Parser Text -> Parser Text String
forall (f :: * -> *) a b. Alternative f => f a -> f b -> f [a]
manyTill Parser Char
anyChar (Text -> Parser Text
string "-->")
Token -> Parser Token
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> Token
Comment (Text -> Token) -> Text -> Token
forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack String
comment)
parseVMethod :: Parser Token
parseVMethod :: Parser Token
parseVMethod = do
Char
_ <- Char -> Parser Char
char '#'
Text
obj <- Parser Text
parseCIdent
Char
_ <- Char -> Parser Char
char '.'
Text
method <- Parser Text
parseCIdent
Text
_ <- Text -> Parser Text
string "()"
Token -> Parser Token
forall (m :: * -> *) a. Monad m => a -> m a
return (CRef -> Token
SymbolRef (Text -> Text -> CRef
VMethodRef Text
obj Text
method))
parseStructField :: Parser Token
parseStructField :: Parser Token
parseStructField = do
Char
_ <- Char -> Parser Char
char '#'
Text
obj <- Parser Text
parseCIdent
Char
_ <- Char -> Parser Char
char '.'
Text
field <- Parser Text
parseCIdent
Token -> Parser Token
forall (m :: * -> *) a. Monad m => a -> m a
return (CRef -> Token
SymbolRef (Text -> Text -> CRef
StructFieldRef Text
obj Text
field))
parseType :: Parser Token
parseType :: Parser Token
parseType = do
Char
_ <- Char -> Parser Char
char '#'
Text
obj <- Parser Text
parseCIdent
Token -> Parser Token
forall (m :: * -> *) a. Monad m => a -> m a
return (CRef -> Token
SymbolRef (Text -> CRef
TypeRef Text
obj))
parseConstant :: Parser Token
parseConstant :: Parser Token
parseConstant = do
Char
_ <- Char -> Parser Char
char '%'
Text
c <- Parser Text
parseCIdent
Token -> Parser Token
forall (m :: * -> *) a. Monad m => a -> m a
return (CRef -> Token
SymbolRef (Text -> CRef
ConstantRef Text
c))
parseParam :: Parser Token
parseParam :: Parser Token
parseParam = do
Char
_ <- Char -> Parser Char
char '@'
Text
param <- Parser Text
parseCIdent
Token -> Parser Token
forall (m :: * -> *) a. Monad m => a -> m a
return (CRef -> Token
SymbolRef (Text -> CRef
ParamRef Text
param))
isCIdent :: Char -> Bool
isCIdent :: Char -> Bool
isCIdent '_' = Bool
True
isCIdent c :: Char
c = Char -> Bool
isDigit Char
c Bool -> Bool -> Bool
|| Char -> Bool
isAsciiUpper Char
c Bool -> Bool -> Bool
|| Char -> Bool
isAsciiLower Char
c
signalOrPropName :: Parser Text
signalOrPropName :: Parser Text
signalOrPropName = (Char -> Bool) -> Parser Text
takeWhile1 Char -> Bool
isSignalOrPropIdent
where isSignalOrPropIdent :: Char -> Bool
isSignalOrPropIdent :: Char -> Bool
isSignalOrPropIdent '-' = Bool
True
isSignalOrPropIdent c :: Char
c = Char -> Bool
isCIdent Char
c
parseCIdent :: Parser Text
parseCIdent :: Parser Text
parseCIdent = (Char -> Bool) -> Parser Text
takeWhile1 Char -> Bool
isCIdent
parseFunctionRef :: Parser Token
parseFunctionRef :: Parser Token
parseFunctionRef = do
Text
ident <- Parser Text
parseCIdent
Token -> Parser Token -> Parser Token
forall (f :: * -> *) a. Alternative f => a -> f a -> f a
option (Text -> Token
Literal Text
ident) (Text -> Parser Text
string "()" Parser Text -> Parser Token -> Parser Token
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
Token -> Parser Token
forall (m :: * -> *) a. Monad m => a -> m a
return (CRef -> Token
SymbolRef (Text -> CRef
FunctionRef Text
ident)))
parseEscaped :: Parser Token
parseEscaped :: Parser Token
parseEscaped = do
Char
_ <- Char -> Parser Char
char '\\'
Char
c <- (Char -> Bool) -> Parser Char
satisfy (Char -> String -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` ("#@%\\`" :: [Char]))
Token -> Parser Token
forall (m :: * -> *) a. Monad m => a -> m a
return (Token -> Parser Token) -> Token -> Parser Token
forall a b. (a -> b) -> a -> b
$ Text -> Token
Literal (Char -> Text
T.singleton Char
c)
parseBoringLiteral :: Parser Token
parseBoringLiteral :: Parser Token
parseBoringLiteral = do
Char
c <- Parser Char
anyChar
Text
boring <- (Char -> Bool) -> Parser Text
takeWhile (Bool -> Bool
not (Bool -> Bool) -> (Char -> Bool) -> Char -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Bool
special)
Token -> Parser Token
forall (m :: * -> *) a. Monad m => a -> m a
return (Token -> Parser Token) -> Token -> Parser Token
forall a b. (a -> b) -> a -> b
$ Text -> Token
Literal (Char -> Text -> Text
T.cons Char
c Text
boring)
special :: Char -> Bool
special :: Char -> Bool
special '#' = Bool
True
special '@' = Bool
True
special '%' = Bool
True
special '\\' = Bool
True
special '`' = Bool
True
special '|' = Bool
True
special '[' = Bool
True
special '!' = Bool
True
special '\n' = Bool
True
special ':' = Bool
True
special c :: Char
c = Char -> Bool
isCIdent Char
c
parseVerbatim :: Parser Token
parseVerbatim :: Parser Token
parseVerbatim = do
Char
_ <- Char -> Parser Char
char '`'
Text
v <- (Char -> Bool) -> Parser Text
takeWhile1 (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= '`')
Char
_ <- Char -> Parser Char
char '`'
Token -> Parser Token
forall (m :: * -> *) a. Monad m => a -> m a
return (Token -> Parser Token) -> Token -> Parser Token
forall a b. (a -> b) -> a -> b
$ Text -> Token
Verbatim Text
v
parseUrl :: Parser Token
parseUrl :: Parser Token
parseUrl = do
Char
_ <- Char -> Parser Char
char '['
Text
name <- (Char -> Bool) -> Parser Text
takeWhile1 (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= ']')
Text
_ <- Text -> Parser Text
string "]("
Text
address <- (Char -> Bool) -> Parser Text
takeWhile1 (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= ')')
Char
_ <- Char -> Parser Char
char ')'
Token -> Parser Token
forall (m :: * -> *) a. Monad m => a -> m a
return (Token -> Parser Token) -> Token -> Parser Token
forall a b. (a -> b) -> a -> b
$ Link -> Token
ExternalLink (Link -> Token) -> Link -> Token
forall a b. (a -> b) -> a -> b
$ Link :: Text -> Text -> Link
Link {linkName :: Text
linkName = Text
name, linkAddress :: Text
linkAddress = Text
address}
parseImage :: Parser Token
parseImage :: Parser Token
parseImage = do
Text
_ <- Text -> Parser Text
string "!["
Text
name <- (Char -> Bool) -> Parser Text
takeWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= ']')
Text
_ <- Text -> Parser Text
string "]("
Text
address <- (Char -> Bool) -> Parser Text
takeWhile1 (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= ')')
Char
_ <- Char -> Parser Char
char ')'
Token -> Parser Token
forall (m :: * -> *) a. Monad m => a -> m a
return (Token -> Parser Token) -> Token -> Parser Token
forall a b. (a -> b) -> a -> b
$ Link -> Token
Image (Link -> Token) -> Link -> Token
forall a b. (a -> b) -> a -> b
$ Link :: Text -> Text -> Link
Link {linkName :: Text
linkName = Text
name, linkAddress :: Text
linkAddress = Text
address}
parseCodeBlock :: Parser Token
parseCodeBlock :: Parser Token
parseCodeBlock = do
Text
_ <- Text -> Parser Text
string "|["
Maybe Language
lang <- (Language -> Maybe Language
forall a. a -> Maybe a
Just (Language -> Maybe Language)
-> Parser Text Language -> Parser Text (Maybe Language)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Text Language
parseLanguage) Parser Text (Maybe Language)
-> Parser Text (Maybe Language) -> Parser Text (Maybe Language)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Maybe Language -> Parser Text (Maybe Language)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Language
forall a. Maybe a
Nothing
Text
code <- String -> Text
T.pack (String -> Text) -> Parser Text String -> Parser Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Char -> Parser Text -> Parser Text String
forall (f :: * -> *) a b. Alternative f => f a -> f b -> f [a]
manyTill Parser Char
anyChar (Text -> Parser Text
string "]|")
Token -> Parser Token
forall (m :: * -> *) a. Monad m => a -> m a
return (Token -> Parser Token) -> Token -> Parser Token
forall a b. (a -> b) -> a -> b
$ Maybe Language -> Text -> Token
CodeBlock Maybe Language
lang Text
code
parseLanguage :: Parser Language
parseLanguage :: Parser Text Language
parseLanguage = do
Text
_ <- Text -> Parser Text
string "<!--"
Parser Text ()
skipSpace
Text
_ <- Text -> Parser Text
string "language=\""
Text
lang <- (Char -> Bool) -> Parser Text
takeWhile1 (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= '"')
Char
_ <- Char -> Parser Char
char '"'
Parser Text ()
skipSpace
Text
_ <- Text -> Parser Text
string "-->"
Language -> Parser Text Language
forall (m :: * -> *) a. Monad m => a -> m a
return (Language -> Parser Text Language)
-> Language -> Parser Text Language
forall a b. (a -> b) -> a -> b
$ Text -> Language
Language Text
lang
parseSectionHeader :: Parser Token
= Char -> Parser Char
char '\n' Parser Char -> Parser Token -> Parser Token
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Parser Token
parseInitialSectionHeader
parseInitialSectionHeader :: Parser Token
= do
Text
hashes <- (Char -> Bool) -> Parser Text
takeWhile1 (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== '#')
String
_ <- Parser Char -> Parser Text String
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many1 Parser Char
space
Text
heading <- (Char -> Bool) -> Parser Text
takeWhile1 (String -> Char -> Bool
notInClass "#\n")
Char
_ <- (Text -> Parser Text
string Text
hashes Parser Text -> Parser Char -> Parser Char
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Char -> Parser Char
char '\n') Parser Char -> Parser Char -> Parser Char
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Char -> Parser Char
char '\n')
Token -> Parser Token
forall (m :: * -> *) a. Monad m => a -> m a
return (Token -> Parser Token) -> Token -> Parser Token
forall a b. (a -> b) -> a -> b
$ Int -> GtkDoc -> Token
SectionHeader (Text -> Int
T.length Text
hashes) (Text -> GtkDoc
parseGtkDoc Text
heading)
parseList :: Parser Token
parseList :: Parser Token
parseList = do
[ListItem]
items <- Parser Text ListItem -> Parser Text [ListItem]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many1 Parser Text ListItem
parseListItem
Token -> Parser Token
forall (m :: * -> *) a. Monad m => a -> m a
return (Token -> Parser Token) -> Token -> Parser Token
forall a b. (a -> b) -> a -> b
$ [ListItem] -> Token
List [ListItem]
items
where parseListItem :: Parser ListItem
parseListItem :: Parser Text ListItem
parseListItem = do
Char
_ <- Char -> Parser Char
char '\n'
Text
_ <- Text -> Parser Text
string "\n- " Parser Text -> Parser Text -> Parser Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> Parser Text
string "- "
Text
first <- (Char -> Bool) -> Parser Text
takeWhile1 (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= '\n')
[Text]
rest <- Parser Text -> Parser Text [Text]
forall (m :: * -> *) a. MonadPlus m => m a -> m [a]
many' Parser Text
parseLine
ListItem -> Parser Text ListItem
forall (m :: * -> *) a. Monad m => a -> m a
return (ListItem -> Parser Text ListItem)
-> ListItem -> Parser Text ListItem
forall a b. (a -> b) -> a -> b
$ GtkDoc -> [GtkDoc] -> ListItem
ListItem (Text -> GtkDoc
parseGtkDoc Text
first) ((Text -> GtkDoc) -> [Text] -> [GtkDoc]
forall a b. (a -> b) -> [a] -> [b]
map Text -> GtkDoc
parseGtkDoc [Text]
rest)
parseLine :: Parser Text
parseLine :: Parser Text
parseLine = Text -> Parser Text
string "\n " Parser Text -> Parser Text -> Parser Text
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (Char -> Bool) -> Parser Text
takeWhile1 (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= '\n')