css-syntax 0.0.2 → 0.0.3
raw patch · 3 files changed
+51/−33 lines, 3 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- Data.CSS.Syntax.Tokens: instance Eq HashFlag
- Data.CSS.Syntax.Tokens: instance Eq NumericValue
- Data.CSS.Syntax.Tokens: instance Eq Token
- Data.CSS.Syntax.Tokens: instance Show HashFlag
- Data.CSS.Syntax.Tokens: instance Show NumericValue
- Data.CSS.Syntax.Tokens: instance Show Token
+ Data.CSS.Syntax.Tokens: instance GHC.Classes.Eq Data.CSS.Syntax.Tokens.HashFlag
+ Data.CSS.Syntax.Tokens: instance GHC.Classes.Eq Data.CSS.Syntax.Tokens.NumericValue
+ Data.CSS.Syntax.Tokens: instance GHC.Classes.Eq Data.CSS.Syntax.Tokens.Token
+ Data.CSS.Syntax.Tokens: instance GHC.Show.Show Data.CSS.Syntax.Tokens.HashFlag
+ Data.CSS.Syntax.Tokens: instance GHC.Show.Show Data.CSS.Syntax.Tokens.NumericValue
+ Data.CSS.Syntax.Tokens: instance GHC.Show.Show Data.CSS.Syntax.Tokens.Token
- Data.CSS.Syntax.Tokens: BadString :: !Text -> Token
+ Data.CSS.Syntax.Tokens: BadString :: !Char -> !Text -> Token
- Data.CSS.Syntax.Tokens: String :: !Text -> Token
+ Data.CSS.Syntax.Tokens: String :: !Char -> !Text -> Token
Files
- css-syntax.cabal +1/−1
- src/Data/CSS/Syntax/Tokens.hs +25/−7
- test/Test.hs +25/−25
css-syntax.cabal view
@@ -1,5 +1,5 @@ name: css-syntax-version: 0.0.2+version: 0.0.3 synopsis: This package implments a parser for the CSS syntax description:
src/Data/CSS/Syntax/Tokens.hs view
@@ -20,6 +20,7 @@ import Data.Monoid import Data.Char import Data.Scientific+import Numeric import Prelude @@ -50,8 +51,8 @@ | Column - | String !Text- | BadString !Text+ | String !Char !Text+ | BadString !Char !Text | Number !Text !NumericValue | Percentage !Text !NumericValue@@ -158,8 +159,8 @@ renderToken (Column) = "||" -renderToken (String x) = "'" <> x <> "'"-renderToken (BadString x) = "'" <> x <> "'"+renderToken (String d x) = T.singleton d <> renderString x <> T.singleton d+renderToken (BadString d x) = T.singleton d <> renderString x <> T.singleton d renderToken (Number x _) = x renderToken (Percentage x _) = x <> "%"@@ -174,12 +175,29 @@ renderToken (Function x) = x <> "(" -renderToken (Hash _ x) = x+renderToken (Hash _ x) = "#" <> x renderToken (Delim x) = T.singleton x +renderString :: Text -> Text+renderString = T.pack . concatMap f . T.unpack+ where+ nonPrintableCodePoint c+ | c >= '\x0000' && c <= '\x0008' = True -- NULL through BACKSPACE+ | c == '\x000B' = True -- LINE TABULATION+ | c >= '\x000E' && c <= '\x001F' = True -- SHIFT OUT through INFORMATION SEPARATOR ONE+ | c == '\x007F' = True -- DELETE+ | otherwise = False++ nonASCIICodePoint c = c >= '\x0080' -- control++ f c = if nonPrintableCodePoint c || nonASCIICodePoint c+ then "\\" <> showHex (ord c) ""+ else [c]++ parseComment :: Parser () parseComment = do void $ AP.string "/*"@@ -248,9 +266,9 @@ where go acc = choice- [ (AP.endOfInput <|> void (AP.char endingCodePoint)) *> return (String acc)+ [ (AP.endOfInput <|> void (AP.char endingCodePoint)) *> return (String endingCodePoint acc) , AP.string "\\\n" *> go acc- , whenNext '\n' (BadString acc)+ , whenNext '\n' (BadString endingCodePoint acc) , nextInputCodePoint >>= \ch -> go (acc <> T.singleton ch) ]
test/Test.hs view
@@ -50,8 +50,8 @@ it "Escapes" $ do tokenize "hel\\6Co" `shouldBe` Right [Ident "hello"] tokenize "\\26 B" `shouldBe` Right [Ident "&B"]- tokenize "'hel\\6c o'" `shouldBe` Right [String "hello"]- tokenize "'spac\\65\r\ns'" `shouldBe` Right [String "spaces"]+ tokenize "'hel\\6c o'" `shouldBe` Right [String '\'' "hello"]+ tokenize "'spac\\65\r\ns'" `shouldBe` Right [String '\'' "spaces"] tokenize "spac\\65\r\ns" `shouldBe` Right [Ident "spaces"] tokenize "spac\\65\n\rs" `shouldBe` Right [Ident "space", Whitespace, Ident "s"] tokenize "sp\\61\tc\\65\fs" `shouldBe` Right [Ident "spaces"]@@ -105,10 +105,10 @@ tokenize "foo-bar\\ baz(" `shouldBe` Right [Function "foo-bar baz"] tokenize "fun\\(ction(" `shouldBe` Right [Function "fun(ction"] tokenize "-foo(" `shouldBe` Right [Function "-foo"]- tokenize "url(\"foo.gif\"" `shouldBe` Right [Function "url", String "foo.gif"]- tokenize "foo( \'bar.gif\'" `shouldBe` Right [Function "foo", Whitespace, String "bar.gif"]+ tokenize "url(\"foo.gif\"" `shouldBe` Right [Function "url", String '"' "foo.gif"]+ tokenize "foo( \'bar.gif\'" `shouldBe` Right [Function "foo", Whitespace, String '\'' "bar.gif"] -- // To simplify implementation we drop the whitespace in function(url),whitespace,string()- tokenize "url( \'bar.gif\'" `shouldBe` Right [Function "url", String "bar.gif"]+ tokenize "url( \'bar.gif\'" `shouldBe` Right [Function "url", String '\'' "bar.gif"] it "AtKeyword" $ do tokenize "@at-keyword" `shouldBe` Right [AtKeyword "at-keyword"]@@ -147,26 +147,26 @@ tokenize "url(ba'd\\\\))" `shouldBe` Right [BadUrl "ba'd\\", RightParen] it "String" $ do- tokenize "'text'" `shouldBe` Right [String "text"]- tokenize "\"text\"" `shouldBe` Right [String "text"]- tokenize "'testing, 123!'" `shouldBe` Right [String "testing, 123!"]- tokenize "'es\\'ca\\\"pe'" `shouldBe` Right [String "es'ca\"pe"]- tokenize "'\"quotes\"'" `shouldBe` Right [String "\"quotes\""]- tokenize "\"'quotes'\"" `shouldBe` Right [String "'quotes'"]- tokenize "\"mismatch'" `shouldBe` Right [String "mismatch'"]- tokenize "'text\5\t\xb'" `shouldBe` Right [String "text\5\t\xb"]- tokenize "\"end on eof" `shouldBe` Right [String "end on eof"]- tokenize "'esca\\\nped'" `shouldBe` Right [String "escaped"]- tokenize "\"esc\\\faped\"" `shouldBe` Right [String "escaped"]- tokenize "'new\\\rline'" `shouldBe` Right [String "newline"]- tokenize "\"new\\\r\nline\"" `shouldBe` Right [String "newline"]- tokenize "'bad\nstring" `shouldBe` Right [BadString "bad", Whitespace, Ident "string"]- tokenize "'bad\rstring" `shouldBe` Right [BadString "bad", Whitespace, Ident "string"]- tokenize "'bad\r\nstring" `shouldBe` Right [BadString "bad", Whitespace, Ident "string"]- tokenize "'bad\fstring" `shouldBe` Right [BadString "bad", Whitespace, Ident "string"]- tokenize "'\0'" `shouldBe` Right [String "\xFFFD"]- tokenize "'hel\0lo'" `shouldBe` Right [String "hel\xfffdlo"]- tokenize "'h\\65l\0lo'" `shouldBe` Right [String "hel\xfffdlo"]+ tokenize "'text'" `shouldBe` Right [String '\'' "text"]+ tokenize "\"text\"" `shouldBe` Right [String '"' "text"]+ tokenize "'testing, 123!'" `shouldBe` Right [String '\'' "testing, 123!"]+ tokenize "'es\\'ca\\\"pe'" `shouldBe` Right [String '\'' "es'ca\"pe"]+ tokenize "'\"quotes\"'" `shouldBe` Right [String '\'' "\"quotes\""]+ tokenize "\"'quotes'\"" `shouldBe` Right [String '"' "'quotes'"]+ tokenize "\"mismatch'" `shouldBe` Right [String '"' "mismatch'"]+ tokenize "'text\5\t\xb'" `shouldBe` Right [String '\'' "text\5\t\xb"]+ tokenize "\"end on eof" `shouldBe` Right [String '"' "end on eof"]+ tokenize "'esca\\\nped'" `shouldBe` Right [String '\'' "escaped"]+ tokenize "\"esc\\\faped\"" `shouldBe` Right [String '"' "escaped"]+ tokenize "'new\\\rline'" `shouldBe` Right [String '\'' "newline"]+ tokenize "\"new\\\r\nline\"" `shouldBe` Right [String '"' "newline"]+ tokenize "'bad\nstring" `shouldBe` Right [BadString '\'' "bad", Whitespace, Ident "string"]+ tokenize "'bad\rstring" `shouldBe` Right [BadString '\'' "bad", Whitespace, Ident "string"]+ tokenize "'bad\r\nstring" `shouldBe` Right [BadString '\'' "bad", Whitespace, Ident "string"]+ tokenize "'bad\fstring" `shouldBe` Right [BadString '\'' "bad", Whitespace, Ident "string"]+ tokenize "'\0'" `shouldBe` Right [String '\'' "\xFFFD"]+ tokenize "'hel\0lo'" `shouldBe` Right [String '\'' "hel\xfffdlo"]+ tokenize "'h\\65l\0lo'" `shouldBe` Right [String '\'' "hel\xfffdlo"] it "Hash" $ do tokenize "#id-selector" `shouldBe` Right [Hash HId "id-selector"]