~alcinnz/amphiarao

ref: 70de37cee9613aaeceedea91f0ac89cfd83bd8aa amphiarao/src/XML/Selectors/CSS/Tokens.hs -rw-r--r-- 2.0 KiB
70de37ce — Adrian Cochrane Show syntax-highlighted start tags for elements in HTML UI. 3 years ago
                                                                                
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
{-# LANGUAGE OverloadedStrings #-}
module XML.Selectors.CSS.Tokens where

import XML.Selectors.CSS.Types
import Data.CSS.Syntax.Tokens as T
import Data.Text (pack, unpack)

lexer str = let ret = lexer' $ T.tokenize $ pack str in if TokenEOF `elem` ret
    then Left "invalid token!" else Right ret

lexer' (T.Whitespace:ts) = TokenSpace:lexer' ts
lexer' (T.Ident n:ts) = TokenName (unpack n):lexer' ts
lexer' (T.Delim n:T.Ident ns:ts) | n /= ':' = TokenName (n:unpack ns):lexer' ts
lexer' (T.Hash _ n:ts) = TokenName (unpack n):lexer' ts
lexer' (T.String txt:ts) = TokenString (unpack txt):lexer' ts
lexer' (T.Delim '+':ts) = TokenPlus:lexer' ts
lexer' (T.Delim '-':ts) = TokenMinus:lexer' ts
lexer' (T.Delim '/':ts) = TokenSlash:lexer' ts
lexer' (T.Delim '*':ts) = TokenAster:lexer' ts
lexer' (T.Delim '>':ts) = TokenChild:lexer' ts
lexer' (T.Delim ':':T.Function "nth-child":ts) = TokenNthChild:TokenOP:lexer' ts
lexer' (T.Delim ':':T.Ident "first-child":ts) = TokenFirstChild:lexer' ts
lexer' (T.Delim ':':T.Ident "last-child":ts) = TokenLastChild:lexer' ts
lexer' (T.Delim ':':T.Function "nth-last-child":ts) = TokenNthLastChild:TokenOP:lexer' ts
lexer' (T.Delim '~':ts) = TokenAnySibling:lexer' ts
lexer' (T.LeftSquareBracket:ts) = TokenOB:lexer' ts
lexer' (T.RightSquareBracket:ts) = TokenCB:lexer' ts
lexer' (T.LeftParen:ts) = TokenOP:lexer' ts
lexer' (T.RightParen:ts) = TokenCP:lexer' ts
lexer' (T.Number txt _:ts) = TokenDigits (unpack txt):lexer' ts
lexer' (T.Dimension txt _ unit:ts) = TokenDigits (unpack txt):TokenName (unpack unit):lexer' ts
lexer' (T.Delim '#':ts) = TokenHash:lexer' ts
lexer' (T.Delim '.':ts) = TokenDot:lexer' ts
lexer' (T.Delim '=':ts) = TokenEquals:lexer' ts
lexer' (T.IncludeMatch:ts) = TokenIncludes:lexer' ts
lexer' (T.DashMatch:ts) = TokenDashMatch:lexer' ts
lexer' (T.PrefixMatch:ts) = TokenBeginsWith:lexer' ts
lexer' (T.SuffixMatch:ts) = TokenEndsWith:lexer' ts
lexer' (T.BadString:ts) = TokenQuote:lexer' ts
lexer' (T.Delim ':':ts) = TokenPseudo:lexer' ts
lexer' (_:ts) = TokenEOF:lexer' ts
lexer' [] = []