M src/Stylish/Parse.hs => src/Stylish/Parse.hs +7 -3
@@ 20,7 20,11 @@ class StyleSheet s where
addAtRule :: s -> Text -> [Token] -> (s, [Token])
addAtRule self _ tokens = (self, skipAtRule tokens)
-data StyleRule = StyleRule [Selector] [(Text, [Token])] deriving (Show, Eq)
+addRules self (selector:selectors, properties) = addRules self' (selectors, properties)
+ where self' = addRule self $ StyleRule selector properties
+addRules self ([], _) = self
+
+data StyleRule = StyleRule Selector [(Text, [Token])] deriving (Show, Eq)
data TrivialStyleSheet = TrivialStyleSheet [StyleRule] deriving (Show, Eq)
instance StyleSheet TrivialStyleSheet where
@@ 42,8 46,8 @@ parse' stylesheet [] = stylesheet
parse' stylesheet (AtKeyword kind:tokens) = parse' stylesheet' tokens'
where (stylesheet', tokens') = addAtRule stylesheet kind tokens
-parse' stylesheet tokens = parse' (addRule stylesheet rule) tokens'
- where (rule, tokens') = concatP StyleRule parseSelectors parseProperties tokens
+parse' stylesheet tokens = parse' (addRules stylesheet rule) tokens'
+ where (rule, tokens') = concatP (,) parseSelectors parseProperties tokens
--------
---- Property parsing
M src/Stylish/Parse/Selector.hs => src/Stylish/Parse/Selector.hs +29 -17
@@ 8,23 8,27 @@ import Stylish.Parse.Utils
import Data.Text.Internal (Text(..))
-type Selector = [SimpleSelector]
-data SimpleSelector = Tag Text | Id Text | Class Text | Property Text PropertyTest |
- Child | Descendant | Adjacent | Sibling
+-- type Selector = [SimpleSelector]
+data Selector = Element [SimpleSelector] |
+ Child Selector [SimpleSelector] | Descendant Selector [SimpleSelector] |
+ Adjacent Selector [SimpleSelector] | Sibling Selector [SimpleSelector]
+ deriving (Show, Eq)
+data SimpleSelector = Tag Text | Id Text | Class Text | Property Text PropertyTest
deriving (Show, Eq)
data PropertyTest = Exists | Equals Text | Suffix Text | Prefix Text | Substring Text |
Include Text | Dash Text
deriving (Show, Eq)
parseSelectors :: [Token] -> ([Selector], [Token])
-parseSelectors tokens = concatP (:) parseSelector parseSelectorsTail $ skipSpace tokens
+parseSelectors tokens = concatP (:) parseCompound parseSelectorsTail $ skipSpace tokens
parseSelectorsTail (Comma:tokens) = parseSelectors tokens
parseSelectorsTail tokens = ([], tokens)
+parseCompound tokens = parseCombinators (Element selector) tokens'
+ where (selector, tokens') = parseSelector tokens
parseSelector' op tokens = (op:selector, tokens')
where (selector, tokens') = parseSelector tokens
-parseSelector :: [Token] -> (Selector, [Token])
parseSelector (Delim '*':tokens) = parseSelector tokens
parseSelector (Ident tag:tokens) = parseSelector' (Tag tag) tokens
parseSelector (Hash _ id:tokens) = parseSelector' (Id id) tokens
@@ 32,23 36,31 @@ parseSelector (Delim '.':Ident class_:tokens) = parseSelector' (Class class_) to
parseSelector (LeftSquareBracket:Ident prop:tokens) =
concatP appendPropertySel parsePropertySel parseSelector tokens
where appendPropertySel test selector = Property prop test : selector
-
-parseSelector (Whitespace:tokens) = parseCombinator $ skipSpace tokens
-parseSelector (Delim c:tokens) | c `elem` ">~+" = parseCombinator $ Delim c:tokens
parseSelector tokens = ([], tokens)
-parseCombinator (Delim '>':tokens) = parseSelector' Child $ skipSpace tokens
-parseCombinator (Delim '~':tokens) = parseSelector' Sibling $ skipSpace tokens
-parseCombinator (Delim '+':tokens) = parseSelector' Adjacent $ skipSpace tokens
+parseCombinators' selector tokens = parseCombinators selector' tokens'
+ where (selector', tokens') = parseCombinator selector tokens
+parseCombinators selector (Whitespace:tokens) = parseCombinators' selector tokens
+parseCombinators selector tokens@(Delim c:_) = parseCombinators' selector tokens
+parseCombinators selector tokens = (selector, tokens)
+
+parseCombinator' cb selector tokens = (cb selector selector', tokens')
+ where (selector', tokens') = parseSelector $ skipSpace tokens
+parseCombinator :: Selector -> [Token] -> (Selector, [Token])
+parseCombinator selector (Whitespace:tokens) = parseCombinator selector tokens
+parseCombinator selector (Delim '>':tokens) = parseCombinator' Child selector tokens
+parseCombinator selector (Delim '~':tokens) = parseCombinator' Sibling selector tokens
+parseCombinator selector (Delim '+':tokens) = parseCombinator' Adjacent selector tokens
-- Take special care to avoid adding a trailing Descendant when not needed.
-parseCombinator tokens@(LeftCurlyBracket:_) = ([], tokens)
-parseCombinator tokens@(RightCurlyBracket:_) = ([], tokens)
-parseCombinator tokens@(RightSquareBracket:_) = ([], tokens)
-parseCombinator tokens@(Comma:_) = ([], tokens)
+parseCombinator selector tokens@(LeftCurlyBracket:_) = (selector, tokens)
+parseCombinator selector tokens@(RightCurlyBracket:_) = (selector, tokens)
+parseCombinator selector tokens@(RightSquareBracket:_) = (selector, tokens)
+parseCombinator selector tokens@(Comma:_) = (selector, tokens)
-parseCombinator tokens@(RightParen:_) = ([], tokens)
+parseCombinator selector tokens@(RightParen:_) = (selector, tokens)
+parseCombinator selector [] = (selector, [])
-parseCombinator tokens = parseSelector' Descendant tokens
+parseCombinator selector tokens = parseCombinator' Descendant selector tokens
parsePropertySel (RightSquareBracket:tokens) = (Exists, tokens)
parsePropertySel (Delim '=':tokens) = parsePropertyVal (Equals) tokens
M test/Test.hs => test/Test.hs +16 -13
@@ 3,9 3,11 @@ module Main where
import Test.Hspec
import Test.Hspec.QuickCheck
-import Stylish.Parse
import Data.CSS.Syntax.Tokens
+import Stylish.Parse
+import Stylish.Style.Index
+
main = hspec spec
spec = do
@@ 28,45 30,46 @@ spec = do
it "Parses style rules" $ do
-- Syntax examples from "Head First HTML & CSS with XHTML"
parse emptyStyle "bedroom { drapes: blue; carpet: wool shag; }" `shouldBe` TrivialStyleSheet [
- StyleRule [[Tag "bedroom"]] [
+ StyleRule (Element [Tag "bedroom"]) [
("drapes", [Ident "blue"]),
("carpet", [Ident "wool", Ident "shag"])
]]
parse emptyStyle " bathroom{tile :1in white;drapes :pink}" `shouldBe` TrivialStyleSheet [
- StyleRule [[Tag "bathroom"]] [
+ StyleRule (Element [Tag "bathroom"]) [
("tile", [Dimension "1" (NVInteger 1) "in", Ident "white"]),
("drapes", [Ident "pink"])
]]
it "Parses selectors" $ do
parse emptyStyle ".class {}" `shouldBe` TrivialStyleSheet [
- StyleRule [[Class "class"]] []
+ StyleRule (Element [Class "class"]) []
]
parse emptyStyle "*.class {}" `shouldBe` TrivialStyleSheet [
- StyleRule [[Class "class"]] []
+ StyleRule (Element [Class "class"]) []
]
parse emptyStyle "#id {}" `shouldBe` TrivialStyleSheet [
- StyleRule [[Id "id"]] []
+ StyleRule (Element [Id "id"]) []
]
parse emptyStyle "[attr] {}" `shouldBe` TrivialStyleSheet [
- StyleRule [[Property "attr" Exists]] []
+ StyleRule (Element [Property "attr" Exists]) []
]
parse emptyStyle "a , b {}" `shouldBe` TrivialStyleSheet [
- StyleRule [[Tag "a"], [Tag "b"]] []
+ StyleRule (Element [Tag "b"]) [],
+ StyleRule (Element [Tag "a"]) []
]
parse emptyStyle "a b {}" `shouldBe` TrivialStyleSheet [
- StyleRule [[Tag "a", Descendant, Tag "b"]] []
+ StyleRule (Descendant (Element [Tag "a"]) [Tag "b"]) []
]
parse emptyStyle "a > b {}" `shouldBe` TrivialStyleSheet [
- StyleRule [[Tag "a", Child, Tag "b"]] []
+ StyleRule (Child (Element [Tag "a"]) [Tag "b"]) []
]
parse emptyStyle "a ~ b {}" `shouldBe` TrivialStyleSheet [
- StyleRule [[Tag "a", Sibling, Tag "b"]] []
+ StyleRule (Sibling (Element [Tag "a"]) [Tag "b"]) []
]
parse emptyStyle "a + b {}" `shouldBe` TrivialStyleSheet [
- StyleRule [[Tag "a", Adjacent, Tag "b"]] []
+ StyleRule (Adjacent (Element [Tag "a"]) [Tag "b"]) []
]
emptyStyle = TrivialStyleSheet []
linkStyle = TrivialStyleSheet [
- StyleRule [[Tag "a"]] [("color", [Ident "green"])]
+ StyleRule (Element [Tag "a"]) [("color", [Ident "green"])]
]