Types.hs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. {-# LANGUAGE FlexibleInstances #-}
  2. module Types where
  3. data OutputStyle = LispStyle | PlainStyle
  4. data Options = Options {
  5. outputStyle :: OutputStyle
  6. , hlintOpts :: [String]
  7. , ghcOpts :: [String]
  8. , operators :: Bool
  9. , expandSplice :: Bool
  10. , sandbox :: Maybe String
  11. }
  12. defaultOptions :: Options
  13. defaultOptions = Options {
  14. outputStyle = PlainStyle
  15. , hlintOpts = []
  16. , ghcOpts = []
  17. , operators = False
  18. , expandSplice = False
  19. , sandbox = Nothing
  20. }
  21. ----------------------------------------------------------------
  22. convert :: ToString a => Options -> a -> String
  23. convert Options{ outputStyle = LispStyle } = toLisp
  24. convert Options{ outputStyle = PlainStyle } = toPlain
  25. class ToString a where
  26. toLisp :: a -> String
  27. toPlain :: a -> String
  28. instance ToString [String] where
  29. toLisp = addNewLine . toSexp True
  30. toPlain = unlines
  31. instance ToString [((Int,Int,Int,Int),String)] where
  32. toLisp = addNewLine . toSexp False . map toS
  33. where
  34. toS x = "(" ++ tupToString x ++ ")"
  35. toPlain = unlines . map tupToString
  36. toSexp :: Bool -> [String] -> String
  37. toSexp False ss = "(" ++ unwords ss ++ ")"
  38. toSexp True ss = "(" ++ unwords (map quote ss) ++ ")"
  39. tupToString :: ((Int,Int,Int,Int),String) -> String
  40. tupToString ((a,b,c,d),s) = show a ++ " "
  41. ++ show b ++ " "
  42. ++ show c ++ " "
  43. ++ show d ++ " "
  44. ++ quote s
  45. quote :: String -> String
  46. quote x = "\"" ++ x ++ "\""
  47. addNewLine :: String -> String
  48. addNewLine = (++ "\n")