Changeset 8994 for src/Pugs/Prim/Yaml.hs
- Timestamp:
- 02/14/06 19:26:38 (3 years ago)
- Files:
-
- 1 modified
-
src/Pugs/Prim/Yaml.hs (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Pugs/Prim/Yaml.hs
r8676 r8994 12 12 import qualified Data.Map as Map 13 13 import qualified Data.IntMap as IntMap 14 import Foreign.StablePtr 15 import Foreign.Ptr 14 16 15 17 evalYaml :: Val -> Eval Val … … 62 64 63 65 dumpYaml :: Int -> Val -> Eval Val 64 dumpYaml limit v = let ?d = limit indo65 obj <- toYaml v66 dumpYaml limit v = do 67 obj <- toYaml IntMap.empty v 66 68 rv <- liftIO (emitYaml obj) 67 69 either (fail . ("YAML Emit Error: "++)) … … 71 73 strNode = mkNode . YamlStr 72 74 73 toYaml :: (?d :: Int) => Val -> Eval YamlNode 74 toYaml _ | ?d == 0 = return $ strNode "<deep recursion>" -- fail? make this configurable? 75 toYaml VUndef = return $ mkNode YamlNil 76 toYaml (VBool x) = return $ boolToYaml x 77 toYaml (VStr str) = return $ strNode (encodeUTF8 str) 78 toYaml v@(VRef r) = let ?d = pred ?d in do 79 t <- evalValType v 80 ifValTypeIsa v "Hash" (hashToYaml r) $ do 81 v' <- readRef r 82 nodes <- toYaml v' 83 ifValTypeIsa v "Array" (return nodes) . return $ case v' of 84 VObject _ -> nodes 85 _ -> mkNode $ YamlMap [(strNode "<ref>", nodes)] 86 toYaml (VList nodes) = let ?d = pred ?d in do 87 n <- mapM toYaml nodes 75 addressOf :: a -> IO Int 76 addressOf x = do 77 ptr <- newStablePtr x 78 return (castStablePtrToPtr ptr `minusPtr` (nullPtr :: Ptr ())) 79 80 toYaml :: IntMap YamlNode -> Val -> Eval YamlNode 81 toYaml _ VUndef = return $ mkNode YamlNil 82 toYaml _ (VBool x) = return $ boolToYaml x 83 toYaml _ (VStr str) = return $ strNode (encodeUTF8 str) 84 toYaml seen v@(VRef r) = do 85 ptr <- liftIO $ addressOf r 86 case IntMap.lookup ptr seen of 87 Just node -> return node 88 Nothing -> do 89 rv <- ifValTypeIsa v "Hash" (hashToYaml seen r) $ do 90 v' <- readRef r 91 nodes <- toYaml seen v' -- XXX -- (IntMap.insert ptr rv seen) v' 92 ifValTypeIsa v "Array" (return nodes) . return $ case v' of 93 VObject _ -> nodes 94 _ -> mkNode $ YamlMap [(strNode "<ref>", nodes)] 95 return rv 96 toYaml seen (VList nodes) = do 97 n <- mapM (toYaml seen) nodes 88 98 return $ mkNode (YamlSeq n) 89 99 -- fmap YamlSeq$ mapM toYaml nodes 90 toYaml v@(VObject obj) = let ?d = pred ?d indo100 toYaml seen v@(VObject obj) = do 91 101 -- ... dump the objAttrs 92 102 -- XXX this needs fixing WRT demagicalized pairs: … … 94 104 -- parens, which is, of course, wrong. 95 105 hash <- fromVal v :: Eval VHash 96 attrs <- toYaml $ VRef (hashRef hash)106 attrs <- toYaml seen $ VRef (hashRef hash) 97 107 return $ tagNode (Just $ "tag:pugs:object:" ++ showType (objType obj)) attrs 98 toYaml (VRule MkRulePGE{rxRule=rule, rxGlobal=global, rxStringify=stringify, rxAdverbs=adverbs}) = let ?d = pred ?d indo99 adverbs' <- toYaml adverbs108 toYaml seen (VRule MkRulePGE{rxRule=rule, rxGlobal=global, rxStringify=stringify, rxAdverbs=adverbs}) =do 109 adverbs' <- toYaml seen adverbs 100 110 return . mkTagNode "tag:pugs:Rule" $ YamlMap 101 111 [ (strNode "rule", strNode rule) … … 104 114 , (strNode "adverbs", adverbs') 105 115 ] 106 toYaml v = return $ strNode $ (encodeUTF8 . pretty) v116 toYaml _ v = return $ strNode $ (encodeUTF8 . pretty) v 107 117 108 hashToYaml :: (?d :: Int) => VRef -> Eval YamlNode109 hashToYaml (MkRef (IHash hv)) = do118 hashToYaml :: IntMap YamlNode -> VRef -> Eval YamlNode 119 hashToYaml seen (MkRef (IHash hv)) = do 110 120 h <- hash_fetch hv 111 121 let assocs = Map.toList h 112 122 yamlmap <- forM assocs $ \(ka, va) -> do 113 ka' <- toYaml $ VStr ka114 va' <- toYaml va123 ka' <- toYaml seen $ VStr ka 124 va' <- toYaml seen va 115 125 return (ka', va') 116 126 return $ mkNode (YamlMap yamlmap) 117 hashToYaml r = error ("unexpected node: " ++ show r)127 hashToYaml _ r = error ("unexpected node: " ++ show r) 118 128 119 129 boolToYaml :: VBool -> YamlNode
