1 {-# LANGUAGE FlexibleContexts #-}
2 {-# LANGUAGE OverloadedStrings #-}
3 {-# LANGUAGE Rank2Types #-}
4 {-# LANGUAGE TypeFamilies #-}
5 module Language.TCT.Read.Elem where
7 import Control.Applicative (Applicative(..), Alternative(..))
9 import Data.Char (Char)
10 import Data.Eq (Eq(..))
11 import Data.Function (($))
12 import Data.Functor ((<$>))
13 import Data.Semigroup (Semigroup(..))
14 import Data.String (String)
15 import Data.Text (Text)
16 import Text.Show (Show(..))
17 import qualified Data.Char as Char
18 import qualified Data.Text as Text
19 import qualified Text.Megaparsec as P
21 import Language.TCT.Elem
24 -- | Convenient alias.
27 , P.ShowErrorComponent e
34 , P.ShowErrorComponent e
37 , P.ShowToken (P.Token s)
39 ) => String -> P.ParsecT e s m a -> P.ParsecT e s m a
40 -- pdbg m p = P.dbg m p
43 p_Attrs :: Parser e s [(Text,Attr)]
44 p_Attrs = P.many $ P.try $ (,) <$> p_Spaces <*> p_Attr
45 p_Attr :: Parser e s Attr
46 p_Attr = P.try p_Attr_Eq <|> p_Attr_Word
47 p_Spaces :: Parser e s Text
48 p_Spaces = Text.pack <$> P.some (P.satisfy Char.isSpace)
49 p_Attr_Eq :: Parser e s Attr
51 (\n (o,v,c) -> Attr n ("="<>o) v c)
55 p_Attr_Word :: Parser e s Attr
57 (\(o,v,c) -> Attr "" o v c)
59 p_Attr_Value :: Parser e s (Text,Text,Text)
61 p_Attr_Value_Quote '\'' <|>
62 p_Attr_Value_Quote '"' <|>
64 p_Attr_Value_Quote :: Char -> Parser e s (Text,Text,Text)
65 p_Attr_Value_Quote q =
66 (\o v c -> (Text.singleton o, Text.pack v, Text.singleton c))
68 <*> P.many (P.satisfy $ \c ->
69 Char.isPrint c && c/='/' && c/='>' && c/=q)
71 p_Attr_Value_Word :: Parser e s (Text,Text,Text)
73 (\v -> ("", Text.pack v, ""))
74 <$> P.many (P.satisfy Char.isAlphaNum)
76 p_Word :: Parser e s Text
77 p_Word = pdbg "Word" $
80 <*> P.option "" (p_plain <|> p_link)
84 <$> (Text.pack <$> P.some (P.satisfy (\c -> c=='_' || c=='-')))
88 <$> P.some (P.satisfy $ \c ->