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
44 p_Attrs :: Parser e s [(Text,Attr)]
45 p_Attrs = P.many $ P.try $ (,) <$> p_Spaces <*> p_Attr
46 p_Attr :: Parser e s Attr
47 p_Attr = P.try p_Attr_Eq <|> p_Attr_Word
48 p_Spaces :: Parser e s Text
49 p_Spaces = Text.pack <$> P.some (P.satisfy Char.isSpace)
50 p_Attr_Eq :: Parser e s Attr
52 (\n (o,v,c) -> Attr n ("="<>o) v c)
56 p_Attr_Word :: Parser e s Attr
58 (\(o,v,c) -> Attr "" o v c)
60 p_Attr_Value :: Parser e s (Text,Text,Text)
62 p_Attr_Value_Quote '\'' <|>
63 p_Attr_Value_Quote '"' <|>
65 p_Attr_Value_Quote :: Char -> Parser e s (Text,Text,Text)
66 p_Attr_Value_Quote q =
67 (\o v c -> (Text.singleton o, Text.pack v, Text.singleton c))
69 <*> P.many (P.satisfy $ \c ->
70 Char.isPrint c && c/='/' && c/='>' && c/=q)
72 p_Attr_Value_Word :: Parser e s (Text,Text,Text)
74 (\v -> ("", Text.pack v, ""))
75 <$> P.many (P.satisfy Char.isAlphaNum)
77 p_Word :: Parser e s Text
78 p_Word = pdbg "Word" $
81 <*> P.option "" (p_plain <|> p_link)
85 <$> (Text.pack <$> P.some (P.satisfy (\c -> c=='_' || c=='-')))
89 <$> P.some (P.satisfy $ \c ->