packages feed

repr-tree-syb (empty) → 0.1.0

raw patch · 4 files changed

+155/−0 lines, 4 filesdep +basedep +containersdep +sybsetup-changed

Dependencies added: base, containers, syb, text

Files

+ LICENSE view
@@ -0,0 +1,22 @@+Copyright (c) 2013, Nikita Volkov++Permission is hereby granted, free of charge, to any person+obtaining a copy of this software and associated documentation+files (the "Software"), to deal in the Software without+restriction, including without limitation the rights to use,+copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the+Software is furnished to do so, subject to the following+conditions:++The above copyright notice and this permission notice shall be+included in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR+OTHER DEALINGS IN THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ repr-tree-syb.cabal view
@@ -0,0 +1,29 @@+name:               repr-tree-syb+version:            0.1.0+cabal-version:      >=1.8+build-type:         Simple+license:            MIT+license-file:       LICENSE+copyright:          (c) 2013, Nikita Volkov+author:             Nikita Volkov+maintainer:         Nikita Volkov <nikita.y.volkov@mail.ru>+stability:          experimental+homepage:           https://github.com/nikita-volkov/repr-tree-syb+bug-reports:        https://github.com/nikita-volkov/repr-tree-syb/issues+synopsis:           Tree representation and pretty-printing of data structures based on SYB+description:        This library provides a convenient way to inspect and debug arbitrary data structures.+category:           Tree, Pretty Printer, Debug, Text++library+  hs-source-dirs:   src+  extensions:       PatternGuards+  exposed-modules:  ReprTree+  other-modules:    +  build-depends:    base >= 4.5 && < 5,+                    containers >= 0.5,+                    text,+                    syb == 0.3.*++source-repository head+  type:             git+  location:         git://github.com/nikita-volkov/repr-tree-syb.git
+ src/ReprTree.hs view
@@ -0,0 +1,102 @@+{-# LANGUAGE DeriveDataTypeable #-}+module ReprTree (reprTree, reprTreeString) where++import Data.Tree+import Data.Generics+import Data.List+import Data.Maybe+import Data.Text (Text)+import qualified Data.Text as Text+import Data.Map (Map)+import qualified Data.Map as Map+import Data.Set (Set)+import qualified Data.Set as Set+++-- | A data representation in form of a formatted multiline string, such as+-- the following:+-- +-- @+-- :+-- - A+-- | - :+-- | | - a+-- | | - b+-- | | - c+-- | - 9+-- - C+-- | - 3+-- - B+-- - D+--   - :+--     - :+--     | - asdf+--     | - 123+--     | - ldskfjkl+--     - :+--       - f+-- @+-- +-- Which is a result of running the following code:+-- +-- > import Data.Generics (Data, Typeable)+-- >+-- > data SomeType = +-- >   A [String] Int | +-- >   B | +-- >   C Int | +-- >   D [[String]]+-- >   deriving (Typeable, Data)+-- > +-- > xxx = A ["a", "b", "c"] 9 +-- >     : C 3 +-- >     : B +-- >     : D [["asdf", "123", "ldskfjkl"], ["f"]]+-- >     : []+-- > +-- > main = putStrLn $ reprTreeString xxx+-- +reprTreeString :: (Data a) => a -> String+reprTreeString = unlines . treeLines . reprTree where+  treeLines (Node x ts) = x : subTreesLines ts+  subTreesLines [] = []+  subTreesLines [t] = shift "- " "  " (treeLines t)+  subTreesLines (t:ts) = shift "- " "| " (treeLines t) ++ subTreesLines ts+  shift first other = zipWith (++) (first : repeat other)++-- | Get a representation tree of a generic data structure using SYB. Can be +-- used to implement a custom converter to textual representation.+reprTree :: Data a => a -> Tree String+reprTree = adtReprTree +  `ext2Q` mapReprTree +  `ext2Q` pairReprTree +  `ext1Q` listReprTree +  `ext1Q` setReprTree +  `extQ` textReprTree +  `extQ` stringReprTree++textReprTree :: Text -> Tree String+textReprTree x = Node (Text.unpack x) []++stringReprTree :: String -> Tree String+stringReprTree x = Node x []++adtReprTree :: Data a => a -> Tree String+adtReprTree a = Node (stripBraces $ showConstr $ toConstr a) (gmapQ reprTree a) +  where+    stripBraces :: String -> String+    stripBraces s = +      fromMaybe s $ +        stripPrefix "(" s >>= fmap reverse . stripPrefix ")" . reverse++mapReprTree :: (Data a, Data k) => Map k a -> Tree String+mapReprTree = Node "Map" . map pairReprTree . Map.toList where++pairReprTree :: (Data a, Data b) => (a, b) -> Tree String+pairReprTree (a, b) = Node "," [reprTree a, reprTree b]++listReprTree :: (Data a) => [a] -> Tree String+listReprTree = Node ":" . map reprTree++setReprTree :: (Data a) => Set a -> Tree String+setReprTree = Node "Set" . map reprTree . Set.toList