Changeset 8994
- Timestamp:
- 02/14/06 19:26:38 (3 years ago)
- Location:
- src
- Files:
-
- 4 modified
-
DrIFT/YAML.hs (modified) (4 diffs)
-
Pugs/AST/Internals.hs (modified) (1 diff)
-
Pugs/AST/Internals.hs-drift (modified) (1 diff)
-
Pugs/Prim/Yaml.hs (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/DrIFT/YAML.hs
r8990 r8994 10 10 import Control.Exception 11 11 import Control.Monad 12 import Control.Concurrent.STM 13 import Data.IORef 14 import qualified Data.IntMap as IntMap 15 import Foreign.StablePtr 16 import Foreign.Ptr 17 import System.IO.Unsafe 18 import Control.Monad.Reader 12 19 13 20 type YAMLClass = String 14 21 type YAMLKey = String 15 22 type YAMLVal = YamlNode 23 type SeenCache = IntMap.IntMap YamlNode 24 25 {-# NOINLINE _SeenCache #-} 26 _SeenCache :: IORef (IntMap.IntMap YamlNode) 27 _SeenCache = unsafePerformIO $ newIORef IntMap.empty 16 28 17 29 showYaml :: YAML a => a -> IO String 18 30 showYaml x = do 19 node <- asYAML x31 node <- (`runReaderT` IntMap.empty) (asYAMLrec x) 20 32 rv <- emitYaml node 21 33 case rv of … … 24 36 25 37 class Typeable a => YAML a where 38 asYAMLrec :: a -> ReaderT SeenCache IO YamlNode 39 asYAMLrec = lift . asYAML 26 40 asYAML :: a -> IO YamlNode 27 41 asYAML x = do … … 31 45 "()" -> nilNode 32 46 _ -> mkTagNode (tagHs ty) YamlNil 33 fromYAML :: YamlNode -> IO a47 fromYAML :: YamlNode -> IO a 34 48 fromYAML (MkYamlNode{el=x}) = fromYAMLElem x 35 49 fromYAMLElem :: YamlElem -> IO a … … 125 139 fromYAML ~(MkYamlNode{el=YamlSeq [x, y, z]}) = liftM3 (,,) (fromYAML x) (fromYAML y) (fromYAML z) 126 140 141 instance (Typeable a, YAML a) => YAML (TVar a) where 142 asYAMLrec tv = do 143 ptr <- liftIO $ addressOf tv 144 seen <- liftIO $ readIORef _SeenCache 145 case IntMap.lookup ptr seen of 146 Just node -> return node 147 _ -> mdo 148 rv <- local (IntMap.insert ptr rv) $ mdo 149 v <- lift (atomically (readTVar tv)) 150 asYAMLrec v 151 return rv 152 153 addressOf :: a -> IO Int 154 addressOf x = do 155 ptr <- newStablePtr x 156 return (castStablePtrToPtr ptr `minusPtr` (nullPtr :: Ptr ())) -
src/Pugs/AST/Internals.hs
r8985 r8994 829 829 deriving (Show, Eq, Ord, Typeable) {-!derive: YAML, Perl5, JSON!-} 830 830 831 instance (Typeable a, YAML a) => YAML (TVar a) where832 asYAML tv = do833 v <- liftSTM (readTVar tv)834 asYAML v835 836 831 -- | A list of formal parameters. 837 832 type Params = [Param] -
src/Pugs/AST/Internals.hs-drift
r8775 r8994 835 835 deriving (Show, Eq, Ord, Typeable) {-!derive: YAML, Perl5, JSON!-} 836 836 837 instance (Typeable a, YAML a) => YAML (TVar a) where838 asYAML tv = do839 v <- liftSTM (readTVar tv)840 asYAML v841 842 837 -- | A list of formal parameters. 843 838 type Params = [Param] -
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
