Changeset 9039
- Timestamp:
- 02/16/06 19:06:21 (3 years ago)
- Location:
- src
- Files:
-
- 3 modified
-
Data/Yaml/Syck.hsc (modified) (17 diffs)
-
DrIFT/YAML.hs (modified) (11 diffs)
-
Pugs/Prim/Yaml.hs (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Data/Yaml/Syck.hsc
r9035 r9039 1 {-# OPTIONS_GHC -fglasgow-exts -fvia-C -optc-w -f no-warn-unused-binds #-}1 {-# OPTIONS_GHC -fglasgow-exts -fvia-C -optc-w -funbox-strict-fields #-} 2 2 #include "../../syck/syck.h" 3 3 #include "../../cbits/fpstring.h" … … 24 24 import qualified Data.HashTable as Hash 25 25 import qualified Data.FastPackedString as Str 26 import GHC.Ptr (Ptr(..)) 26 27 27 28 type Str = Str.FastString 28 29 type YamlTag = Maybe Str 29 30 data YamlAnchor 30 = MkYamlAnchor Int 31 | MkYamlReference Int 31 = MkYamlAnchor !Int 32 | MkYamlReference !Int 33 | MkYamlSingleton 32 34 deriving (Show, Ord, Eq, Typeable, Data) 33 35 type SYMID = CULong … … 41 43 42 44 data YamlNode = MkYamlNode 43 { nid :: SYMID44 , el :: YamlElem45 , tag :: YamlTag46 , anchor :: MaybeYamlAnchor45 { nid :: !SYMID 46 , el :: !YamlElem 47 , tag :: !YamlTag 48 , anchor :: !YamlAnchor 47 49 } 48 50 deriving (Show, Ord, Eq, Typeable, Data) … … 51 53 = YamlMap [(YamlNode, YamlNode)] 52 54 | YamlSeq [YamlNode] 53 | YamlStr Str55 | YamlStr !Str 54 56 | YamlNil 55 57 deriving (Show, Ord, Eq, Typeable, Data) … … 68 70 69 71 nilNode :: YamlNode 70 nilNode = MkYamlNode 0 YamlNil Nothing Nothing72 nilNode = MkYamlNode 0 YamlNil Nothing MkYamlSingleton 71 73 72 74 tagNode :: YamlTag -> YamlNode -> YamlNode … … 75 77 76 78 mkNode :: YamlElem -> YamlNode 77 mkNode x = MkYamlNode 0 x Nothing Nothing79 mkNode x = MkYamlNode 0 x Nothing MkYamlSingleton 78 80 79 81 mkTagNode :: String -> YamlElem -> YamlNode 80 mkTagNode s x = MkYamlNode 0 x (Just $ Str.pack s) Nothing82 mkTagNode s x = MkYamlNode 0 x (Just $ Str.pack s) MkYamlSingleton 81 83 82 84 -- the extra commas here are not a bug … … 89 91 type EmitterExtras = Ptr () 90 92 -} 91 92 {-# NOINLINE _decLiteralFS #-}93 _decLiteralFS = Str.unsafePackAddress 3 "%d"##94 93 95 94 emitYamlFS :: YamlNode -> IO (Either Str.FastString Str.FastString) … … 100 99 #{poke SyckEmitter, style} emitter scalarFold 101 100 -- #{poke SyckEmitter, sort_keys} emitter (1 :: CInt) 102 Str.useAsCString _decLiteralFS $ #{poke SyckEmitter, anchor_format} emitter101 #{poke SyckEmitter, anchor_format} emitter (Ptr "%d"## :: CString) 103 102 104 103 marks <- Hash.new (==) (Hash.hashInt) … … 108 107 syck_output_handler emitter =<< mkOutputCallback (outputCallbackPS out) 109 108 110 markYamlNode marksemitter node109 markYamlNode freeze emitter node 111 110 112 111 nodePtr <- freeze node … … 119 118 emitYaml node = fmap (either (Left . Str.unpack) (Right . Str.unpack)) (emitYamlFS node) 120 119 121 markYamlNode :: Hash.HashTable Int SyckNodePtr -> SyckEmitter -> YamlNode -> IO () 122 markYamlNode marks emitter MkYamlNode{ anchor = Just (MkYamlReference n) } = do 120 markYamlNode :: (YamlNode -> IO SyckNodePtr) -> SyckEmitter -> YamlNode -> IO () 121 {- 122 markYamlNode marks emitter MkYamlNode{ anchor = MkYamlReference n } = do 123 123 Just nodePtr <- Hash.lookup marks n 124 124 syck_emitter_mark_node emitter nodePtr 125 125 return () 126 markYamlNode marks emitter node = do 127 nodePtr <- freezeNode marks node 126 -} 127 markYamlNode freeze emitter node = do 128 nodePtr <- freeze node 128 129 rv <- syck_emitter_mark_node emitter nodePtr 129 130 if rv == 0 then return () else do 130 case anchor node of131 Just (MkYamlAnchor n) -> Hash.insert marks n nodePtr132 _ -> return ()133 131 case el node of 134 132 YamlMap xs -> sequence_ [ mark x >> mark y | (x, y) <- xs ] … … 136 134 _ -> return () 137 135 where 138 mark = markYamlNode marksemitter136 mark = markYamlNode freeze emitter 139 137 140 138 outputCallbackPS :: IORef [Str.FastString] -> SyckEmitter -> CString -> CLong -> IO () … … 150 148 modifyIORef out (++ str) 151 149 152 freezeFS :: ForeignPtr Word8 -> IO FSPtr153 freezeFS ps = do154 ptr <- newStablePtr ps155 return (castPtr $ castStablePtrToPtr ptr)156 157 readFS :: FSPtr -> IO (ForeignPtr Word8)158 readFS fs = do159 ptr <- peek . castPtr =<< peek fs160 deRefStablePtr (castPtrToStablePtr ptr)161 162 {-# NOINLINE _stringLiteralFS #-}163 {-# NOINLINE _tildeLiteralFS #-}164 {-# NOINLINE _arrayLiteralFS #-}165 {-# NOINLINE _hashLiteralFS #-}166 _stringLiteralFS = Str.unsafePackAddress 7 "string"##167 _tildeLiteralFS = Str.unsafePackAddress 2 "~"##168 _arrayLiteralFS = Str.unsafePackAddress 6 "array"##169 _hashLiteralFS = Str.unsafePackAddress 5 "hash"##170 171 150 emitterCallback :: (YamlNode -> IO SyckNodePtr) -> SyckEmitter -> Ptr () -> IO () 172 151 emitterCallback f e vp = emitNode f e =<< thawNode vp 173 152 153 {-# NOINLINE _tildeLiteralFS #-} 154 _tildeLiteralFS = Str.pack "~" 155 174 156 emitNode :: (YamlNode -> IO SyckNodePtr) -> SyckEmitter -> YamlNode -> IO () 175 157 emitNode _ e n@(MkYamlNode{el = YamlNil}) = do 176 withTag n _stringLiteralFS $ \tag -> 177 Str.useAsCString _tildeLiteralFS $ \cs -> 178 syck_emit_scalar e tag scalarNone 0 0 0 cs 1 179 180 emitNode _ e n@(MkYamlNode{el = YamlStr s}) | s == _tildeLiteralFS = do 181 withTag n _stringLiteralFS $ \tag -> 182 Str.useAsCString _tildeLiteralFS $ \cs -> 183 syck_emit_scalar e tag scalar1quote 0 0 0 cs 1 158 withTag n (Ptr "string"##) $ \tag -> 159 syck_emit_scalar e tag scalarNone 0 0 0 (Ptr "~"##) 1 160 161 emitNode _ e n@(MkYamlNode{el = YamlStr s}) | Str.length s == 1, Str.head s == '~' = do 162 withTag n (Ptr "string"##) $ \tag -> 163 syck_emit_scalar e tag scalar1quote 0 0 0 (Ptr "~"##) 1 184 164 185 165 emitNode _ e n@(MkYamlNode{el = YamlStr str}) = do 186 withTag n _stringLiteralFS $ \tag ->166 withTag n (Ptr "string"##) $ \tag -> 187 167 Str.unsafeUseAsCStringLen str $ \(cs, l) -> 188 syck_emit_scalar e tag scalarNone 0 0 0 cs (toEnum l)168 syck_emit_scalar e tag scalarNone 0 0 0 cs (toEnum l) 189 169 190 170 emitNode freeze e n@(MkYamlNode{el = YamlSeq seq}) = do 191 withTag n _arrayLiteralFS$ \tag ->171 withTag n (Ptr "array"##) $ \tag -> 192 172 syck_emit_seq e tag seqNone 193 173 mapM_ (syck_emit_item e) =<< mapM freeze seq … … 195 175 196 176 emitNode freeze e n@(MkYamlNode{el = YamlMap m}) = do 197 withTag n _hashLiteralFS $ \tag ->177 withTag n (Ptr "map"##) $ \tag -> 198 178 syck_emit_map e tag mapNone 199 179 flip mapM_ m (\(k,v) -> do … … 202 182 syck_emit_end e 203 183 204 withTag :: YamlNode -> Str-> (CString -> IO a) -> IO a205 withTag node def f = Str.useAsCString (maybe def id (tag node)) f184 withTag :: YamlNode -> CString -> (CString -> IO a) -> IO a 185 withTag node def f = maybe (f def) (`Str.useAsCString` f) (tag node) 206 186 207 187 parseYaml :: String -> IO (Either String (Maybe YamlNode)) … … 249 229 250 230 freezeNode :: Hash.HashTable Int (Ptr a) -> YamlNode -> IO (Ptr a) 251 freezeNode nodes node@MkYamlNode{ anchor = Just (MkYamlReference n)} = do231 freezeNode nodes node@MkYamlNode{ anchor = MkYamlReference n } = do 252 232 Just ptr <- Hash.lookup nodes n 253 233 return ptr … … 256 236 let ptr' = castPtr $ castStablePtrToPtr ptr 257 237 case anchor node of 258 Just (MkYamlAnchor n) -> Hash.insert nodes n ptr' >> return ptr' 259 _ -> return ptr' 238 MkYamlAnchor n -> do 239 Hash.insert nodes n ptr' 240 return ptr' 241 _ -> return ptr' 260 242 261 243 thawNode :: Ptr () -> IO YamlNode … … 275 257 {-# NOINLINE _tagLiteralFS #-} 276 258 {-# NOINLINE _colonLiteralFS #-} 277 _tagLiteralFS = Str. unsafePackAddress 4 "tag:"##278 _colonLiteralFS = Str. unsafePackAddress 1 ":"##259 _tagLiteralFS = Str.pack "tag:" 260 _colonLiteralFS = Str.pack ":" 279 261 280 262 syckNodeTag :: SyckNode -> IO (Maybe Str) -
src/DrIFT/YAML.hs
r9035 r9039 12 12 import Control.Concurrent.STM 13 13 import Data.IORef 14 import qualified Data.Int Map as IntMap14 import qualified Data.IntSet as IntSet 15 15 import Foreign.StablePtr 16 16 import Foreign.Ptr … … 24 24 type YAMLKey = String 25 25 type YAMLVal = YamlNode 26 type SeenCache = Int Map.IntMap YamlNode26 type SeenCache = IntSet.IntSet 27 27 28 28 showYaml :: YAML a => a -> IO String 29 29 showYaml x = do 30 node <- (`runReaderT` Int Map.empty) (asYAML x)30 node <- (`runReaderT` IntSet.empty) (asYAML x) 31 31 rv <- emitYaml node 32 32 case rv of … … 86 86 instance YAML () where 87 87 asYAML _ = return nilNode 88 fromYAML _ = return ()88 fromYAMLElem _ = return () 89 89 90 90 instance YAML Int where … … 101 101 fromYAML MkYamlNode{tag=Just s} | s == Str.pack "bool#yes" = return True 102 102 fromYAML MkYamlNode{tag=Just s} | s == Str.pack "bool#no" = return False 103 fromYAML MkYamlNode{el=x} = fromYAMLElem x 104 fromYAMLElem ~(YamlStr x) = return (x == Str.pack "0") 103 105 104 106 instance YAML Integer where … … 123 125 fromYAML MkYamlNode{tag=Just s} | s == Str.pack "float#neginf" = return $ -1/0 -- "-Infinity" 124 126 fromYAML MkYamlNode{tag=Just s} | s == Str.pack "float#nan" = return $ 0/0 -- "NaN" 125 fromYAML ~MkYamlNode{el=YamlStr x} = return $ read $ Str.unpack x 127 fromYAML MkYamlNode{el=x} = fromYAMLElem x 128 fromYAMLElem ~(YamlStr x) = return $ read $ Str.unpack x 126 129 127 130 instance (YAML a) => YAML (Maybe a) where … … 130 133 fromYAML MkYamlNode{el=YamlNil} = return Nothing 131 134 fromYAML x = return . Just =<< fromYAML x 135 fromYAMLElem YamlNil = return Nothing 136 fromYAMLElem x = return . Just =<< fromYAMLElem x 132 137 133 138 instance (YAML a) => YAML [a] where … … 135 140 xs' <- mapM asYAML xs 136 141 (return . mkNode . YamlSeq) xs' 137 fromYAML ~MkYamlNode{el=YamlSeq s}= mapM fromYAML s142 fromYAMLElem ~(YamlSeq s) = mapM fromYAML s 138 143 139 144 instance (YAML a, YAML b) => YAML (a, b) where … … 142 147 y' <- asYAML y 143 148 return $ mkNode (YamlSeq [x', y']) 144 fromYAML ~MkYamlNode{el=YamlSeq [x, y]}= do149 fromYAMLElem ~(YamlSeq [x, y]) = do 145 150 x' <- fromYAML x 146 151 y' <- fromYAML y … … 153 158 z' <- asYAML z 154 159 return $ mkNode (YamlSeq [x', y', z']) 155 fromYAML ~MkYamlNode{el=YamlSeq [x, y, z]}= do160 fromYAMLElem ~(YamlSeq [x, y, z]) = do 156 161 x' <- fromYAML x 157 162 y' <- fromYAML y … … 161 166 instance (Typeable a, YAML a) => YAML (TVar a) where 162 167 asYAML = asYAMLwith (lift . atomically . readTVar) 168 fromYAML = (atomically . newTVar =<<) . fromYAML 169 fromYAMLElem = (atomically . newTVar =<<) . fromYAMLElem 163 170 164 171 asYAMLwith :: (YAML a, YAML b) => (a -> EmitAs b) -> a -> EmitAs YamlNode … … 166 173 ptr <- liftIO $ addressOf x 167 174 seen <- ask 168 case IntMap.lookup ptr seen of169 Just node -> return node170 _ -> mdo171 rv <- local (Int Map.insert ptr rv) (asYAML =<< f x)172 return rv 175 if IntSet.member ptr seen 176 then return nilNode{ anchor = MkYamlReference ptr } 177 else do 178 rv <- local (IntSet.insert ptr) (asYAML =<< f x) 179 return rv{ anchor = MkYamlAnchor ptr } 173 180 174 181 addressOf :: a -> IO Int -
src/Pugs/Prim/Yaml.hs
r9021 r9039 92 92 toYaml v@(VRef r) = do 93 93 ptr <- liftIO $ addressOf r 94 if IntSet.member ptr ?seen then return nilNode{ anchor = Just (MkYamlReference ptr)} else do94 if IntSet.member ptr ?seen then return nilNode{ anchor = MkYamlReference ptr } else do 95 95 let ?seen = IntSet.insert ptr ?seen 96 96 node <- ifValTypeIsa v "Hash" (hashToYaml r) $ do … … 100 100 VObject _ -> nodes 101 101 _ -> mkNode $ YamlMap [(strNode "<ref>", nodes)] 102 return node{ anchor = Just (MkYamlAnchor ptr)}102 return node{ anchor = MkYamlAnchor ptr } 103 103 toYaml (VList nodes) = do 104 104 n <- mapM toYaml nodes
