[<<][meta][>>][..]
Wed Jan 18 10:39:13 EST 2012

Prettyprinting

This[1] is simply fantastic.

  -- (useful subset of) Lua data syntax.
  data LuaData = LuaStr String
               | LuaNum String
               | LuaTable [(LuaData, LuaData)] deriving (Eq)

  -- Prettyprinting with Text.PrettyPrint.HughesPJ
  instance Show LuaData where
    show = (++"\n") . show . docLua

  docLua (LuaNum s) = text s
  docLua (LuaStr s) = quotes $ text s
  docLua (LuaTable xs) = doc where
    doc = nest 2 $ braces $ cat $ punctuate comma $ list xs
    list xs = zipWith pair [1..] xs
    pair i (k,v) = docPair i k $ docLua v

  -- If i is in sequence, don't print index.
  docPair i (LuaNum n)   sv | show i == n = sv
  docPair _ n@(LuaNum _) sv = cat [ brackets $ docLua n, sv ]
  docPair _ (LuaStr s) sv = cat [cat [text s, equals], sv]
  docPair _ (LuaTable t) sv = error $ "Table as key."


[1] http://www.haskell.org/ghc/docs/6.2.2/html/libraries/base/Text.PrettyPrint.HughesPJ.html



[Reply][About]
[<<][meta][>>][..]