1 {-# LANGUAGE OverloadedStrings #-}
2 {-# LANGUAGE RecordWildCards #-}
3 {-# LANGUAGE TypeFamilies #-}
4 module Language.TCT.Token where
6 import Data.Char (Char)
7 import Data.Eq (Eq(..))
8 import Data.Foldable (foldMap, foldr)
9 import Data.Function ((.))
10 import Data.Monoid (Monoid(..))
11 import Data.Semigroup (Semigroup(..))
12 import Data.Sequence (Seq, ViewL(..), ViewR(..), (<|))
13 import Data.Text (Text)
14 import Data.Text.Buildable (Buildable(..))
15 import GHC.Exts (IsList(..))
16 import Text.Show (Show(..))
17 import qualified Data.Sequence as Seq
19 import Language.TCT.Elem
24 | TokenPair !Pair !Tokens
30 instance Buildable Token where
31 build (TokenPlain t) = build t
32 build (TokenTag t) = "#"<>build t
33 build (TokenLink lnk) = build lnk
34 build (TokenEscape c) = "\\"<>build c
35 build (TokenPair p ts) = build c<>build ts<>build o
36 where (o,c) = pairBorders p ts
39 newtype Tokens = Tokens (Seq Token)
42 instance Semigroup Tokens where
43 Tokens (Seq.viewr -> xs:>TokenPlain x) <>
44 Tokens (Seq.viewl -> TokenPlain y:<ys) =
45 Tokens (xs<>(TokenPlain (x<>y)<|ys))
46 Tokens x <> Tokens y = Tokens (x<>y)
47 instance Monoid Tokens where
48 mempty = Tokens mempty
50 instance Buildable Tokens where
51 build (Tokens ts) = foldr (\a -> (<> build a)) "" ts
52 instance IsList Tokens where
53 type Item Tokens = Token
54 fromList = Tokens . fromList
55 toList (Tokens ts) = toList ts
57 unTokens :: Tokens -> Seq Token
58 unTokens (Tokens ts) = ts
60 -- | Build 'Tokens' from many 'Token's.
61 tokens :: [Token] -> Tokens
62 tokens = Tokens . Seq.fromList
64 -- | Build 'Tokens' from one 'Token'.
65 tokens1 :: Token -> Tokens
66 tokens1 = Tokens . Seq.singleton
68 tokensPlainEmpty :: Tokens
69 tokensPlainEmpty = Tokens (Seq.singleton (TokenPlain ""))
76 = PairHash -- ^ @#value#@
77 | PairElem !Elem !Attrs -- ^ @<elem n0=v0 n1=v1>value</elem>@
78 | PairStar -- ^ @*value*@
79 | PairSlash -- ^ @/value/@
80 | PairUnderscore -- ^ @_value_@
81 | PairDash -- ^ @-value-@
82 | PairBackquote -- ^ @`value`@
83 | PairSinglequote -- ^ @'value'@
84 | PairDoublequote -- ^ @"value"@
85 | PairFrenchquote -- ^ @«value»@
86 | PairParen -- ^ @(value)@
87 | PairBrace -- ^ @{value}@
88 | PairBracket -- ^ @[value]@
91 pairBorders :: Pair -> Tokens -> (Text,Text)
96 Tokens s | Seq.null s -> ("<"<>e<>foldMap f attrs<>"/>","")
97 _ -> ("<"<>e<>foldMap f attrs<>">","</"<>e<>">")
98 where f (attr_white,Attr{..}) =
104 PairHash -> ("#","#")
105 PairStar -> ("*","*")
106 PairSlash -> ("/","/")
107 PairUnderscore -> ("_","_")
108 PairDash -> ("-","-")
109 PairBackquote -> ("`","`")
110 PairSinglequote -> ("'","'")
111 PairDoublequote -> ("\"","\"")
112 PairFrenchquote -> ("«","»")
113 PairParen -> ("(",")")
114 PairBrace -> ("{","}")
115 PairBracket -> ("[","]")