Changeset 8603 for src/Pugs/Prim/Yaml.hs
- Timestamp:
- 01/07/06 10:33:51 (3 years ago)
- Files:
-
- 1 modified
-
src/Pugs/Prim/Yaml.hs (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Pugs/Prim/Yaml.hs
r8602 r8603 40 40 41 41 dumpYaml :: Int -> Val -> Eval Val 42 dumpYaml limit v = do43 obj <- toYaml limitv42 dumpYaml limit v = let ?d = limit in do 43 obj <- toYaml v 44 44 rv <- liftIO (emitYaml obj) 45 45 either (fail . ("YAML Emit Error: "++)) 46 46 (return . VStr) rv 47 47 48 toYaml :: Int -> Val -> Eval YamlNode49 toYaml 0 _= return $ YamlStr "<deep recursion>" -- fail? make this configurable?50 toYaml _ VUndef= return YamlNil51 toYaml _ (VStr str)= return $ YamlStr (encodeUTF8 str)52 toYaml (d+1) v@(VRef r) =do48 toYaml :: (?d :: Int) => Val -> Eval YamlNode 49 toYaml _ | ?d == 0 = return $ YamlStr "<deep recursion>" -- fail? make this configurable? 50 toYaml VUndef = return YamlNil 51 toYaml (VStr str) = return $ YamlStr (encodeUTF8 str) 52 toYaml v@(VRef r) = let ?d = pred ?d in do 53 53 t <- evalValType v 54 ifValTypeIsa v "Hash" (hashToYaml dr) $ do54 ifValTypeIsa v "Hash" (hashToYaml r) $ do 55 55 v' <- readRef r 56 nodes <- toYaml dv'56 nodes <- toYaml v' 57 57 ifValTypeIsa v "Array" (return nodes) $ case v' of 58 58 VObject _ -> return nodes 59 59 _ -> return (YamlMap Nothing [(YamlStr "<ref>", nodes)]) 60 toYaml ( d+1) (VList nodes) =do61 fmap YamlSeq $ mapM (toYaml d)nodes62 toYaml (d+1) v@(VObject obj) =do60 toYaml (VList nodes) = let ?d = pred ?d in do 61 fmap YamlSeq $ mapM toYaml nodes 62 toYaml v@(VObject obj) = let ?d = pred ?d in do 63 63 -- ... dump the objAttrs 64 64 -- XXX this needs fixing WRT demagicalized pairs: … … 66 66 -- parens, which is, of course, wrong. 67 67 hash <- fromVal v :: Eval VHash 68 attrs <- toYaml d$ VRef (hashRef hash)68 attrs <- toYaml $ VRef (hashRef hash) 69 69 return $ addTag (Just $ "!pugs:object/" ++ showType (objType obj)) attrs 70 70 where 71 71 addTag _ (YamlMap (Just x) _) = error ("can't add tag: already tagged with" ++ x) 72 72 addTag tag (YamlMap _ m) = YamlMap tag m 73 toYaml _v = (return . YamlStr . encodeUTF8 . pretty) v73 toYaml v = (return . YamlStr . encodeUTF8 . pretty) v 74 74 75 hashToYaml :: Int -> VRef -> Eval YamlNode76 hashToYaml d(MkRef (IHash hv)) = do75 hashToYaml :: (?d :: Int) => VRef -> Eval YamlNode 76 hashToYaml (MkRef (IHash hv)) = do 77 77 h <- hash_fetch hv 78 78 let assocs = Map.toList h 79 79 yamlmap <- flip mapM assocs $ \(ka, va) -> do 80 ka' <- toYaml d$ VStr ka81 va' <- toYaml dva80 ka' <- toYaml $ VStr ka 81 va' <- toYaml va 82 82 return (ka', va') 83 83 return $ YamlMap Nothing yamlmap 84 hashToYaml _r = error ("unexpected node: " ++ show r)84 hashToYaml r = error ("unexpected node: " ++ show r) 85 85 86 86 {-
