{-# 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' [] = []