Changeset 8606
- Timestamp:
- 01/07/06 13:49:41 (3 years ago)
- Location:
- src
- Files:
-
- 2 modified
-
Data/Yaml/Syck.hsc (modified) (8 diffs)
-
Pugs/Prim/Yaml.hs (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Data/Yaml/Syck.hsc
r8604 r8606 5 5 module Data.Yaml.Syck ( 6 6 parseYaml, emitYaml, 7 YamlNode(..), 7 YamlNode(..), YamlElem(..), emptyYamlNode, tagNode 8 8 ) where 9 9 … … 26 26 import Control.Monad.Trans 27 27 28 type YamlTag = Maybe String 29 30 -- XXX: add tags for other types except maps? 31 data YamlNode 32 = YamlMap YamlTag [(YamlNode, YamlNode)] 28 type YamlTag = Maybe String 29 type YamlAnchor = Maybe String 30 type SYMID = CULong 31 32 data YamlNode = MkYamlNode 33 { nid :: SYMID 34 , tag :: YamlTag 35 , anchor :: YamlAnchor 36 , el :: YamlElem 37 } 38 deriving (Show, Ord, Eq) 39 40 data YamlElem 41 = YamlMap [(YamlNode, YamlNode)] 33 42 | YamlSeq [YamlNode] 34 43 | YamlStr String … … 36 45 deriving (Show, Ord, Eq) 37 46 38 type SYMID = CULong39 47 type SyckNode = Ptr () 40 48 type SyckParser = Ptr () … … 48 56 data SyckKind = SyckMap | SyckSeq | SyckStr | SyckNil 49 57 deriving (Show, Ord, Eq, Enum) 58 59 emptyYamlNode :: YamlNode 60 emptyYamlNode = MkYamlNode 0 Nothing Nothing YamlNil 61 62 tagNode :: YamlTag -> YamlNode -> YamlNode 63 tagNode _ MkYamlNode{tag=Just x} = error ("can't add tag: already tagged with" ++ x) 64 tagNode tag node = node{tag = tag} 50 65 51 66 -- the extra comma here is not a bug … … 93 108 94 109 emitNode :: (?e :: SyckEmitter) -> YamlNode -> IO () 95 emitNode YamlNil= do96 with CString "string" $ \stringLiteral ->110 emitNode n@(MkYamlNode{el = YamlNil}) = do 111 withTag n "string" $ \tag -> 97 112 withCString "~" $ \cs -> 98 syck_emit_scalar ?e stringLiteralscalarNone 0 0 0 cs 199 100 emitNode (YamlStr str) = do101 with CString "string" $ \stringLiteral->113 syck_emit_scalar ?e tag scalarNone 0 0 0 cs 1 114 115 emitNode n@(MkYamlNode{el = YamlStr str}) = do 116 withTag n "string" $ \tag -> 102 117 withCString str $ \cs -> 103 syck_emit_scalar ?e stringLiteralscalarNone 0 0 0 cs (toEnum $ length str)104 105 emitNode (YamlSeq seq) = do106 with CString "array" $ \arrayLiteral->107 syck_emit_seq ?e arrayLiteralseqNone118 syck_emit_scalar ?e tag scalarNone 0 0 0 cs (toEnum $ length str) 119 120 emitNode n@(MkYamlNode{el = YamlSeq seq}) = do 121 withTag n "array" $ \tag -> 122 syck_emit_seq ?e tag seqNone 108 123 -- TODO: fix pesky warning about "integer from pointer without a cast" here 109 124 mapM_ (syck_emit_item ?e) =<< (mapM freezeNode seq) 110 125 syck_emit_end ?e 111 126 112 emitNode (YamlMap tag m) = do127 emitNode n@(MkYamlNode{el = YamlMap m}) = do 113 128 --trace ("hash<" ++ maybe "" id tag ++">: " ++ (show m)) $ return () 114 with CString (maybe "hash" id tag) $ \hashLiteral -> do115 syck_emit_map ?e hashLiteralmapNone129 withTag n "hash" $ \tag -> 130 syck_emit_map ?e tag mapNone 116 131 flip mapM_ m (\(k,v) -> do 117 132 syck_emit_item ?e =<< freezeNode k … … 119 134 syck_emit_end ?e 120 135 136 withTag :: YamlNode -> String -> (CString -> IO a) -> IO a 137 withTag node def f = withCString (maybe def id (tag node)) f 121 138 122 139 parseYaml :: String -> IO (Either String (Maybe YamlNode)) … … 192 209 val <- readNode parser valId 193 210 return (key, val) 194 return $ YamlMap Nothing pairs211 return $ emptyYamlNode{ el = YamlMap pairs } 195 212 196 213 parseNode SyckSeq parser syckNode len = do … … 198 215 symId <- syck_seq_read syckNode idx 199 216 readNode parser symId 200 return $ YamlSeq nodes217 return $ emptyYamlNode{ el = YamlSeq nodes } 201 218 202 219 parseNode SyckStr _ syckNode len = do 203 220 cstr <- syck_str_read syckNode 204 221 str <- peekCStringLen (cstr, fromEnum len) 205 return $ YamlStr str222 return $ emptyYamlNode{ el = YamlStr str } 206 223 207 224 foreign import ccall "wrapper" -
src/Pugs/Prim/Yaml.hs
r8604 r8606 23 23 24 24 fromYaml :: YamlNode -> Eval Val 25 fromYaml YamlNil= return VUndef26 fromYaml (YamlStr str)= return $ VStr (decodeUTF8 str)27 fromYaml (YamlSeq nodes)= do25 fromYaml MkYamlNode{el=YamlNil} = return VUndef 26 fromYaml MkYamlNode{el=YamlStr str} = return $ VStr (decodeUTF8 str) 27 fromYaml MkYamlNode{el=YamlSeq nodes} = do 28 28 vals <- mapM fromYaml nodes 29 29 av <- liftSTM $ newTVar $ 30 30 IntMap.fromAscList ([0..] `zip` map lazyScalar vals) 31 31 return $ VRef (arrayRef av) 32 fromYaml (YamlMap _ nodes)= do32 fromYaml MkYamlNode{el=YamlMap nodes} = do 33 33 vals <- forM nodes $ \(keyNode, valNode) -> do 34 34 key <- fromVal =<< fromYaml keyNode … … 47 47 48 48 toYaml :: (?d :: Int) => Val -> Eval YamlNode 49 toYaml _ | ?d == 0 = return $ YamlStr "<deep recursion>"-- fail? make this configurable?50 toYaml VUndef = return YamlNil51 toYaml (VStr str) = return $ YamlStr (encodeUTF8 str)49 toYaml _ | ?d == 0 = return $ emptyYamlNode{el = YamlStr "<deep recursion>"} -- fail? make this configurable? 50 toYaml VUndef = return emptyYamlNode 51 toYaml (VStr str) = return $ emptyYamlNode{el = YamlStr (encodeUTF8 str)} 52 52 toYaml v@(VRef r) = let ?d = pred ?d in do 53 53 t <- evalValType v … … 57 57 ifValTypeIsa v "Array" (return nodes) $ case v' of 58 58 VObject _ -> return nodes 59 _ -> return (YamlMap Nothing [(YamlStr "<ref>", nodes)])59 _ -> return emptyYamlNode{el = YamlMap [(emptyYamlNode{el=YamlStr "<ref>"}, nodes)]} 60 60 toYaml (VList nodes) = let ?d = pred ?d in do 61 fmap YamlSeq $ mapM toYaml nodes 61 n <- mapM toYaml nodes 62 return $ emptyYamlNode{el=YamlSeq n} -- golfme! 63 -- fmap YamlSeq$ mapM toYaml nodes 62 64 toYaml v@(VObject obj) = let ?d = pred ?d in do 63 65 -- ... dump the objAttrs … … 67 69 hash <- fromVal v :: Eval VHash 68 70 attrs <- toYaml $ VRef (hashRef hash) 69 return $ addTag (Just $ "tag:pugs:object:" ++ showType (objType obj)) attrs 70 where 71 addTag _ (YamlMap (Just x) _) = error ("can't add tag: already tagged with" ++ x) 72 addTag tag (YamlMap _ m) = YamlMap tag m 73 toYaml v = (return . YamlStr . encodeUTF8 . pretty) v 71 return $ tagNode (Just $ "tag:pugs:object:" ++ showType (objType obj)) attrs 72 toYaml v = return $ emptyYamlNode{el=YamlStr p} 73 where p = (encodeUTF8 . pretty) v 74 74 75 75 76 hashToYaml :: (?d :: Int) => VRef -> Eval YamlNode … … 81 82 va' <- toYaml va 82 83 return (ka', va') 83 return $ YamlMap Nothing yamlmap84 return $ emptyYamlNode{el=YamlMap yamlmap} 84 85 hashToYaml r = error ("unexpected node: " ++ show r) 85 86 {- 87 ifValTypeIsa v "Pair" 88 (case v' of 89 VList [ks, vs] -> do 90 kStr <- toYaml d ks 91 vStr <- toYaml d vs 92 return $ YamlMap [(kStr, vStr)] -- assume a pair is a one-element hash 93 _ -> toYaml d v' -- XXX: probably broken to blithingly ignore ref levels here 94 ) 95 (ifValTypeIsa v "Hash" 96 --fmap YamlMap $ mapM (\(k, v) -> do {k' <- toYaml k; v' <- toYaml v; return (k', v')}) Map.toList =<< hash_fetch v') 97 (do 98 case r of 99 MkRef (IHash hv) -> do 100 h <- hash_fetch hv 101 let assocs = Map.toList h 102 yamlmap <- mapM (\(k, v) -> do 103 k' <- toYaml d (VStr k) 104 v' <- toYaml d v 105 return (k', v')) assocs 106 return $ YamlMap yamlmap 107 _ -> error ("can't process hash: " ++ show v') -- XXX 108 ) 109 (do nodes <- toYaml d v' 110 ifValTypeIsa v "Array" 111 (return $ nodes) 112 (return $ YamlMap [(YamlStr "<ref>", nodes)])) -- XXX 113 ) 114 -} 86
