Changeset 8603
- Timestamp:
- 01/07/06 10:33:51 (3 years ago)
- Location:
- src
- Files:
-
- 2 modified
-
Data/Yaml/Syck.hsc (modified) (1 diff)
-
Pugs/Prim/Yaml.hs (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Data/Yaml/Syck.hsc
r8600 r8603 90 90 91 91 emitterCallback :: SyckEmitter -> Ptr () -> IO () 92 emitterCallback e vp = emitNodee =<< thawNode vp92 emitterCallback e vp = let ?e = e in emitNode =<< thawNode vp 93 93 94 emitNode :: SyckEmitter -> YamlNode -> IO () 95 emitNode e YamlNil = do 96 -- syck_emit_scalar(e, "string", scalar_none, 0, 0, 0, "~", 1); 94 emitNode :: (?e :: SyckEmitter) -> YamlNode -> IO () 95 emitNode YamlNil = do 97 96 withCString "string" $ \string_literal -> 98 97 withCString "~" $ \cs -> 99 syck_emit_scalar e string_literal scalarNone 0 0 0 cs 1 100 101 emitNode e (YamlStr str) = do 102 -- return syck_emit_scalar(e, "string", scalar_none, 0, 0, 0, SvPVX(sv), SvCUR(sv)); 98 syck_emit_scalar ?e string_literal scalarNone 0 0 0 cs 1 99 100 emitNode (YamlStr str) = do 103 101 withCString "string" $ \string_literal -> 104 102 withCString str $ \cs -> 105 syck_emit_scalar e string_literal scalarNone 0 0 0 cs (toEnum $ length str) 106 107 emitNode e (YamlSeq seq) = do 108 -- syck_emit_seq(e, "array", seq_none); 103 syck_emit_scalar ?e string_literal scalarNone 0 0 0 cs (toEnum $ length str) 104 105 emitNode (YamlSeq seq) = do 109 106 withCString "array" $ \array_literal -> 110 syck_emit_seq e array_literal seqNone107 syck_emit_seq ?e array_literal seqNone 111 108 -- TODO: fix pesky warning about "integer from pointer without a cast" here 112 mapM_ (syck_emit_item e) =<< (mapM freezeNode seq) 113 syck_emit_end e 114 115 emitNode e (YamlMap tag m) = do 116 -- syck_emit_map(e, "hash", map_none); 117 trace ("hash<" ++ maybe "" id tag ++">: " ++ (show m)) $ return () 109 mapM_ (syck_emit_item ?e) =<< (mapM freezeNode seq) 110 syck_emit_end ?e 111 112 emitNode (YamlMap tag m) = do 113 -- trace ("hash<" ++ maybe "" id tag ++">: " ++ (show m)) $ return () 118 114 withCString (maybe "hash" id tag) $ \hash_literal -> do 119 syck_emit_map e hash_literal mapNone120 when (isJust tag) (do {syck_emit_tag e hash_literal nullPtr ; return ()})115 syck_emit_map ?e hash_literal mapNone 116 when (isJust tag) (do {syck_emit_tag ?e hash_literal nullPtr ; return ()}) 121 117 flip mapM_ m (\(k,v) -> do 122 syck_emit_item e =<< freezeNode k123 syck_emit_item e =<< freezeNode v)124 syck_emit_end e118 syck_emit_item ?e =<< freezeNode k 119 syck_emit_item ?e =<< freezeNode v) 120 syck_emit_end ?e 125 121 126 122 -
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 {-
