Permalink
Browse files

start work on AST parser

  • Loading branch information...
1 parent 6bc063c commit cc723b8363e26de28ef0f73b24a8d1819721af6e Sam Stokes committed Apr 5, 2009
Showing with 60 additions and 12 deletions.
  1. +49 −0 Grammar/AST.hs
  2. +6 −1 Grammar/Token.hs
  3. +5 −11 main.hs
View
@@ -0,0 +1,49 @@
+module Grammar.AST
+( Stylesheet
+, stylesheet
+) where
+
+import Text.ParserCombinators.Parsec
+import Text.ParserCombinators.Parsec.Pos (initialPos)
+import Grammar.Token
+
+data Stylesheet = Stylesheet [RuleSet] deriving (Show)
+
+data RuleSet = RuleSet [Selector] [Declaration] deriving (Show)
+
+type Selector = [Char] -- TODO
+type Declaration = [Char] -- TODO
+
+stylesheet :: CharParser st Stylesheet
+stylesheet = do
+ many whitespace
+ -- TODO other things than rulesets
+ rulesets <- endBy ruleset (many whitespace)
+ eof
+ return $ Stylesheet rulesets
+
+ruleset = do
+ selectors <- sepBy1 selector $ do { char ','; many whitespace }
+ many whitespace
+ char '{'
+ -- TODO actually read declarations
+ many whitespace
+ char '}'
+ return $ RuleSet selectors []
+
+selector = simple_selector -- TODO parse compound selectors
+
+-- this stuff is horrible and breaks
+{-selector = do-}
+ {-parts <- try (sepBy1 (try simple_selector) (try combinator))-}
+ {-return $ concat parts-}
+
+{-combinator = do { char '+'; many whitespace; return '+' }-}
+ {-<|> do { char '>'; many whitespace; return '>' }-}
+ {-<|> do { whitespace; return ' ' }-}
+ {-[><?> "combinator"<]-}
+
+simple_selector = do
+ (Ident i) <- ident
+ return i
+ <?> "simple selector"
View
@@ -1,6 +1,8 @@
module Grammar.Token
-( CssToken
+( CssToken(Ident)
, css_token
+, ident
+, whitespace
) where
import Text.ParserCombinators.Parsec
@@ -39,19 +41,22 @@ css_token = (try ident)
<|> delim
nmchar = char '_' <|> letter <|> digit <|> char '-'
+ <?> "name character"
ident = do
hyphen <- option "" (string "-")
first <- char '_' <|> letter
rest <- many nmchar
return (Ident (hyphen ++ (first : rest)))
+ <?> "identifier"
hash = do
char '#'
name <- many nmchar
return $ Hash name
whitespace = many1 space >> return S
+ <?> "whitespace"
comment = do
-- FIXME doesn't return contents of comment correctly
View
16 main.hs
@@ -1,13 +1,7 @@
-import Text.ParserCombinators.Parsec (CharParser, many, parse, eof)
+import Text.ParserCombinators.Parsec (parseTest)
-import Grammar.Token
+import Grammar.AST
-css_tokens :: CharParser st [CssToken]
-css_tokens = do
- them <- many css_token
- eof
- return them
-
-main = interact $ debug . (parse css_tokens "")
- where debug (Left error) = show error
- debug (Right tokens) = unlines $ map show tokens
+main = do
+ contents <- getContents
+ parseTest stylesheet contents

0 comments on commit cc723b8

Please sign in to comment.