aboutsummaryrefslogtreecommitdiff
path: root/bbcode/src/Text/BBCode/Lexer.hs
blob: 8bd14463d3f4808bc09b6f3ebc22525c433c81e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric, DeriveAnyClass #-}

-- | A parser to transform 'Text' into a stream of 'BBToken's
module Text.BBCode.Lexer
       ( BBToken(..)
       , token
       -- , escapedText
       -- , escapedText'
       ) where

import Data.Attoparsec.Text

import Data.Text (Text)
import qualified Data.Text as T (singleton)

import Data.Char (isSpace)

import Control.Applicative
import Control.Monad (liftM2)

import Data.Monoid

import Prelude hiding (takeWhile)

-- | Our lexicographical unit
data BBToken = BBOpen Text [(Text, Text)] -- ^ Tag open with attributes
             | BBContained Text [(Text, Text)] -- ^ Tag open & immediate close with attributes
             | BBClose Text -- ^ Tag close
             | BBStr Text -- ^ Content of a tag
             | BBNewPar
             deriving (Eq, Show)

token :: Parser BBToken
-- ^ Tokenizer
token = BBClose <$> ("[/" *> escapedText' [']'] <* "]")
        <|> uncurry BBContained <$ "[" <*> openTag <* "/]"
        <|> uncurry BBOpen <$ "[" <*> openTag <* "]"
        <|> BBNewPar <$ endOfLine <* many1 endOfLine
        <|> BBStr <$> paragraph
  where
    paragraph = (\a b c -> a <> b <> c) <$> option "" endOfLine' <*> escapedText ['['] <*> option "" paragraph
    endOfLine' = "\n" <$ endOfLine

openTag :: Parser (Text, [(Text, Text)])
openTag = (,) <$> escapedText' [']', ' ', '=', '/'] <*> attrs'

attrs :: Parser [(Text, Text)]
attrs = (:) <$> (namedAttr <|> plainValue) <* takeWhile isSpace <*> attrs'
  where
    namedAttr = (,) <$ takeWhile isSpace <*> escapedText ['=', ']', ' ', '/'] <*> option "" ("=" *> attrArg)
    plainValue = (,) <$> pure "" <* "=" <*> attrArg
    attrArg = "\"" *> escapedText ['"'] <* "\""
              <|> escapedText [']', ' ', '/']

attrs' :: Parser [(Text, Text)]
attrs' = option [] attrs

escapedText :: [Char] -> Parser Text
-- ^ @escapedText cs@ consumes 'Text' up to (not including) the first occurence of a character from @cs@ that is not escaped using @\\@
--
-- Does not consume characters used to indicate linebreaks as determined by `isEndOfLine`
--
-- Always consumes at least one character
--
-- @\\@ needs to be escaped (prefixed with @\\@) iff it precedes a character from @cs@
escapedText [] = takeText
escapedText cs = recurse $ choice [ takeWhile1 $ not . liftM2 (||) special isEndOfLine
                                  , escapeSeq
                                  , escapeChar'
                                  ]
  where
    escapeChar = '\\'
    special = inClass $ escapeChar : cs
    escapeChar' = string $ T.singleton escapeChar
    escapeSeq = escapeChar' >> (T.singleton <$> satisfy special) -- s/\\([:cs])/\1/
    recurse p = mappend <$> p <*> escapedText' cs

escapedText' :: [Char] -> Parser Text
-- ^ @'option' "" $ 'escapedText' cs@
escapedText' cs = option "" $ escapedText cs