Foo.hs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. {-# LANGUAGE OverloadedStrings #-}
  2. module Data.SCargot.Foo where
  3. import Control.Applicative hiding ((<|>), many)
  4. import Data.Char
  5. import Data.Monoid ((<>))
  6. import Data.Text (Text, concatMap, pack, singleton)
  7. import Numeric (readDec, readFloat, readHex, readSigned)
  8. import Text.Parsec
  9. import Text.Parsec.Text
  10. import Text.Parsec.Token (float, integer, stringLiteral)
  11. import Text.Parsec.Language (haskell)
  12. import Prelude hiding (concatMap)
  13. import Data.SCargot.Repr.Basic (SExpr)
  14. import Data.SCargot.General
  15. data Atom
  16. = AToken Text
  17. | AString Text
  18. | AInt Integer
  19. | AFloat Double
  20. deriving (Eq, Show)
  21. atomChar :: Parser Char
  22. atomChar = satisfy go
  23. where go c = isAlphaNum c
  24. || c == '-' || c == '*' || c == '/'
  25. || c == '+' || c == '<' || c == '>'
  26. || c == '=' || c == '!' || c == '?'
  27. pToken :: Parser Text
  28. pToken = pack <$> ((:) <$> letter <*> many atomChar)
  29. pString :: Parser Text
  30. pString = pack <$> between (char '"') (char '"') (many (val <|> esc))
  31. where val = Just <$> satisfy (\ c -> c /= '"' && c /= '\\' && c > '\026')
  32. esc = do char '\\'
  33. Nothing <$ (gap <|> char '&') <|>
  34. Just <$> cod
  35. gap = many1 space >> char '\\'
  36. cod = undefined
  37. pFloat :: Parser Double
  38. pFloat = undefined
  39. pInt :: Parser Integer
  40. pInt = do
  41. s <- (negate <$ char '-' <|> id <$ char '+' <|> pure id)
  42. n <- read <$> many1 digit
  43. return (s n)
  44. pAtom :: Parser Atom
  45. pAtom = AInt <$> pInt
  46. <|> AFloat <$> pFloat
  47. <|> AToken <$> pToken
  48. <|> AString <$> pString
  49. escape :: Char -> Text
  50. escape '\n' = "\\n"
  51. escape '\t' = "\\t"
  52. escape '\r' = "\\r"
  53. escape '\b' = "\\b"
  54. escape '\f' = "\\f"
  55. escape '\\' = "\\\\"
  56. escape '"' = "\\\""
  57. escape c = singleton c
  58. sAtom :: Atom -> Text
  59. sAtom (AToken t) = t
  60. sAtom (AString s) = "\"" <> concatMap escape s <> "\""
  61. sAtom (AInt i) = pack (show i)
  62. sAtom (AFloat f) = pack (show f)
  63. fooSpec :: SExprSpec Atom (SExpr Atom)
  64. fooSpec = mkSpec pAtom sAtom