diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2016-01-12 05:07:27 +0000 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2016-01-12 05:07:27 +0000 |
commit | a5d285a8b74d2278e8549909d29c01b62dc84424 (patch) | |
tree | 4a6e016dc930ff5fa4b5d8ca965a3162518a1541 /bbcode/src/Text/BBCode | |
parent | ed629414fbc3af1700f7ed6829744f0fb30417c9 (diff) | |
download | thermoprint-a5d285a8b74d2278e8549909d29c01b62dc84424.tar thermoprint-a5d285a8b74d2278e8549909d29c01b62dc84424.tar.gz thermoprint-a5d285a8b74d2278e8549909d29c01b62dc84424.tar.bz2 thermoprint-a5d285a8b74d2278e8549909d29c01b62dc84424.tar.xz thermoprint-a5d285a8b74d2278e8549909d29c01b62dc84424.zip |
Lexing tag attributes
Diffstat (limited to 'bbcode/src/Text/BBCode')
-rw-r--r-- | bbcode/src/Text/BBCode/Lexer.hs | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/bbcode/src/Text/BBCode/Lexer.hs b/bbcode/src/Text/BBCode/Lexer.hs index a7294fe..ad26113 100644 --- a/bbcode/src/Text/BBCode/Lexer.hs +++ b/bbcode/src/Text/BBCode/Lexer.hs | |||
@@ -14,20 +14,31 @@ import Data.Attoparsec.Text | |||
14 | import Data.Text (Text) | 14 | import Data.Text (Text) |
15 | import qualified Data.Text as T (singleton) | 15 | import qualified Data.Text as T (singleton) |
16 | 16 | ||
17 | import Data.Char (isSpace) | ||
18 | |||
17 | import Control.Applicative | 19 | import Control.Applicative |
18 | 20 | ||
21 | import Prelude hiding (takeWhile) | ||
22 | |||
19 | -- | Our lexicographical unit | 23 | -- | Our lexicographical unit |
20 | data BBToken = BBOpen Text -- ^ Tag open | 24 | data BBToken = BBOpen Text [(Text, Maybe Text)] -- ^ Tag open with attributes |
21 | | BBClose Text -- ^ Tag close | 25 | | BBClose Text -- ^ Tag close |
22 | | BBStr Text -- ^ Content of a tag | 26 | | BBStr Text -- ^ Content of a tag |
23 | deriving (Eq, Show) | 27 | deriving (Eq, Show) |
24 | 28 | ||
25 | token :: Parser BBToken | 29 | token :: Parser BBToken |
26 | -- ^ Tokenizer | 30 | -- ^ Tokenizer |
27 | token = BBClose <$> (string "[/" *> escapedText' [']'] <* string "]") | 31 | token = BBClose <$> ("[/" *> escapedText' [']'] <* "]") |
28 | <|> BBOpen <$> (string "[" *> escapedText' [']'] <* string "]") | 32 | <|> BBOpen <$> ("[" *> escapedText' [']', ' ']) <*> (option [] attrs <* "]") |
29 | <|> BBStr <$> escapedText ['['] | 33 | <|> BBStr <$> escapedText ['['] |
30 | 34 | ||
35 | attrs :: Parser [(Text, Maybe Text)] | ||
36 | attrs = (:) <$> (attrs' <* takeWhile isSpace) <*> (option [] $ attrs) | ||
37 | where | ||
38 | attrs' = (,) <$> escapedText ['=', ']', ' '] <*> optional ("=" *> attrArg) | ||
39 | attrArg = "\"" *> escapedText ['"'] <* "\"" | ||
40 | <|> escapedText [']', ' '] | ||
41 | |||
31 | escapedText :: [Char] -> Parser Text | 42 | escapedText :: [Char] -> Parser Text |
32 | -- ^ @escapedText cs@ consumes 'Text' up to (not including) the first occurence of a character from @cs@ that is not escaped using @\\@ | 43 | -- ^ @escapedText cs@ consumes 'Text' up to (not including) the first occurence of a character from @cs@ that is not escaped using @\\@ |
33 | -- | 44 | -- |