Changeset 9021
- Timestamp:
- 02/15/06 21:26:43 (3 years ago)
- svk:copy_cache_prev:
- 11571
- Location:
- src
- Files:
-
- 5 modified
-
Data/Yaml/Syck.hsc (modified) (8 diffs)
-
DrIFT/YAML.hs (modified) (4 diffs)
-
Pugs/AST/Internals.hs (modified) (8 diffs)
-
Pugs/AST/Internals.hs-drift (modified) (6 diffs)
-
Pugs/Prim/Yaml.hs (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Data/Yaml/Syck.hsc
r9018 r9021 23 23 import Data.Generics 24 24 import qualified Data.HashTable as Hash 25 26 type YamlTag = Maybe String 25 import qualified Data.FastPackedString as Str 26 27 type Str = Str.FastString 28 type YamlTag = Maybe Str 27 29 data YamlAnchor 28 30 = MkYamlAnchor Int … … 49 51 = YamlMap [(YamlNode, YamlNode)] 50 52 | YamlSeq [YamlNode] 51 | YamlStr Str ing53 | YamlStr Str 52 54 | YamlNil 53 55 deriving (Show, Ord, Eq, Typeable, Data) … … 69 71 70 72 tagNode :: YamlTag -> YamlNode -> YamlNode 71 tagNode _ MkYamlNode{tag=Just x} = error ("can't add tag: already tagged with" ++ x)73 tagNode _ MkYamlNode{tag=Just x} = error ("can't add tag: already tagged with" ++ (Str.unpack x)) 72 74 tagNode tag node = node{tag = tag} 73 75 … … 76 78 77 79 mkTagNode :: String -> YamlElem -> YamlNode 78 mkTagNode s x = MkYamlNode 0 x (Just s) Nothing80 mkTagNode s x = MkYamlNode 0 x (Just $ Str.pack s) Nothing 79 81 80 82 -- the extra commas here are not a bug … … 165 167 syck_emit_scalar e tag scalarNone 0 0 0 cs 1 166 168 167 emitNode _ e n@(MkYamlNode{el = YamlStr "~"})= do168 withTag n "string" $ \tag -> 169 withCString "~" $ \cs -> 169 emitNode _ e n@(MkYamlNode{el = YamlStr s}) | s == Str.pack "~" = do 170 withTag n "string" $ \tag -> 171 withCString "~" $ \cs -> 170 172 syck_emit_scalar e tag scalar1quote 0 0 0 cs 1 171 173 172 174 emitNode _ e n@(MkYamlNode{el = YamlStr str}) = do 173 175 withTag n "string" $ \tag -> 174 withCString str $ \cs->175 syck_emit_scalar e tag scalarNone 0 0 0 cs (toEnum $ length str)176 Str.unsafeUseAsCStringLen str $ \(cs, l) -> 177 syck_emit_scalar e tag scalarNone 0 0 0 cs (toEnum l) 176 178 177 179 emitNode freeze e n@(MkYamlNode{el = YamlSeq seq}) = do … … 190 192 191 193 withTag :: YamlNode -> String -> (CString -> IO a) -> IO a 192 withTag node def f = withCString (maybe defid (tag node)) f194 withTag node def f = Str.useAsCString (maybe (Str.pack def) id (tag node)) f 193 195 194 196 parseYaml :: String -> IO (Either String (Maybe YamlNode)) … … 260 262 deRefStablePtr (castPtrToStablePtr ptr) 261 263 262 syckNodeTag :: SyckNode -> IO (Maybe Str ing)264 syckNodeTag :: SyckNode -> IO (Maybe Str) 263 265 syckNodeTag syckNode = do 264 266 tag <- #{peek SyckNode, type_id} syckNode 265 267 if (tag == nullPtr) then (return Nothing) else do 266 tag' <- peekCString tag 267 return $ Just tag' 268 return $ Just $ Str.packMallocCString tag 268 269 269 270 syckNodeKind :: SyckNode -> IO SyckKind … … 296 297 tag <- syckNodeTag syckNode 297 298 cstr <- syck_str_read syckNode 298 str <- peekCStringLen (cstr, fromEnum len)299 let str = Str.packCStringLen (cstr, fromEnum len) 299 300 return $ nilNode{ el = YamlStr str, tag = tag } 300 301 -
src/DrIFT/YAML.hs
r9008 r9021 17 17 import System.IO.Unsafe 18 18 import Control.Monad.Reader 19 import qualified Data.FastPackedString as Str 20 21 type Str = Str.FastString 19 22 20 23 type YAMLClass = String … … 66 69 fromYAMLpair ~(MkYamlNode{el=YamlStr k}, v) = do 67 70 v' <- fromYAML v 68 return ( k, v')71 return (Str.unpack k, v') 69 72 70 73 71 74 asYAMLcls :: YAMLClass -> EmitAs YamlNode 72 asYAMLcls c = return $ mkTagNode (tagHs c) (YamlStr c)75 asYAMLcls c = return $ mkTagNode (tagHs c) (YamlStr $ Str.pack c) 73 76 74 77 tagHs :: YAMLClass -> String 75 78 tagHs = ("tag:hs:" ++) 79 80 deTag :: YamlNode -> YAMLClass 81 deTag MkYamlNode{tag=Just s} = 82 let 't':'a':'g':':':'h':'s':':':tag = s' in tag 83 where s' = Str.unpack s 84 deTag _ = error "not a Haskell tag" 76 85 77 86 instance YAML () where … … 80 89 81 90 instance YAML Int where 82 asYAML x = return $ mkTagNode "int" (YamlStr $ show x)83 fromYAMLElem ~(YamlStr x) = return $ read x91 asYAML x = return $ mkTagNode "int" (YamlStr $ Str.pack $ show x) 92 fromYAMLElem ~(YamlStr x) = return $ read $ Str.unpack x 84 93 85 94 instance YAML String where 86 asYAML str = return $ mkTagNode "str" (YamlStr str)87 fromYAMLElem ~(YamlStr str) = return $ read str95 asYAML str = return $ mkTagNode "str" (YamlStr $ Str.pack str) 96 fromYAMLElem ~(YamlStr str) = return $ read $ Str.unpack str 88 97 89 98 instance YAML Bool where 90 asYAML True = return $ mkTagNode "bool#yes" (YamlStr "1")91 asYAML False = return $ mkTagNode "bool#no" (YamlStr "0")92 fromYAML MkYamlNode{tag=Just "bool#yes"}= return True93 fromYAML MkYamlNode{tag=Just "bool#no"}= return False99 asYAML True = return $ mkTagNode "bool#yes" (YamlStr $ Str.pack "1") 100 asYAML False = return $ mkTagNode "bool#no" (YamlStr $ Str.pack "0") 101 fromYAML MkYamlNode{tag=Just s} | s == Str.pack "bool#yes" = return True 102 fromYAML MkYamlNode{tag=Just s} | s == Str.pack "bool#no" = return False 94 103 95 104 instance YAML Integer where 96 asYAML x = return $ mkTagNode "int" (YamlStr $ show x)97 fromYAMLElem ~(YamlStr x) = return $ read x105 asYAML x = return $ mkTagNode "int" (YamlStr $ Str.pack $ show x) 106 fromYAMLElem ~(YamlStr x) = return $ read $ Str.unpack x 98 107 99 108 instance YAML Rational where … … 104 113 fromYAMLElem ~(YamlStr str) = return $ (read x) / (read y) 105 114 where 106 (x,y) = break (== '/') str115 (x,y) = break (== '/') (Str.unpack str) 107 116 108 117 instance YAML Double where 109 asYAML num | show num == "Infinity" = return $ mkTagNode "float#inf" (YamlStr ".Inf")110 | show num == "-Infinity" = return $ mkTagNode "float#neginf" (YamlStr "-.Inf")111 | show num == "NaN" = return $ mkTagNode "float#nan" (YamlStr "-.NaN")112 | otherwise = return $ mkTagNode "float" (YamlStr $ show num)113 fromYAML MkYamlNode{tag=Just "float#inf"}= return $ 1/0 -- "Infinity"114 fromYAML MkYamlNode{tag=Just "float#neginf"}= return $ -1/0 -- "-Infinity"115 fromYAML MkYamlNode{tag=Just "float#nan"}= return $ 0/0 -- "NaN"116 fromYAML ~MkYamlNode{el=YamlStr x} = return $ readx118 asYAML num | show num == "Infinity" = return $ mkTagNode "float#inf" (YamlStr $ Str.pack ".Inf") 119 | show num == "-Infinity" = return $ mkTagNode "float#neginf" (YamlStr $ Str.pack "-.Inf") 120 | show num == "NaN" = return $ mkTagNode "float#nan" (YamlStr $ Str.pack "-.NaN") 121 | otherwise = return $ mkTagNode "float" (YamlStr $ Str.pack $ show num) 122 fromYAML MkYamlNode{tag=Just s} | s == Str.pack "float#inf" = return $ 1/0 -- "Infinity" 123 fromYAML MkYamlNode{tag=Just s} | s == Str.pack "float#neginf" = return $ -1/0 -- "-Infinity" 124 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 117 126 118 127 instance (YAML a) => YAML (Maybe a) where -
src/Pugs/AST/Internals.hs
r9016 r9021 106 106 import qualified Data.Map as Map 107 107 import qualified Data.IntMap as IntMap 108 import qualified Data.FastPackedString as Str 108 109 109 110 import Data.Yaml.Syck … … 128 129 #include "../Types/Pair.hs" 129 130 #include "../Types/Object.hs" 131 132 type Str = Str.FastString 130 133 131 134 errIndex :: Show a => Maybe b -> a -> Eval b … … 768 771 -- junctions, contains the set of values that appear exactly 769 772 -- /once/. 770 } deriving (Eq, Ord, Typeable) {-!derive: YAML!-} 773 } deriving (Eq, Ord, Typeable) 774 775 instance YAML VJunc where 776 asYAML (MkJunc aa ab ac) = asYAMLmap "MkJunc" 777 [("juncType", asYAML aa) , ("juncDup", asYAML ab) , 778 ("juncSet", asYAML ac)] 779 fromYAML node = do 780 let YamlMap assocs = el node 781 let [aa, ab, ac] = map snd assocs 782 liftM3 MkJunc (fromYAML aa) (fromYAML ab) (fromYAML ac) 783 771 784 772 785 -- | The combining semantics of a junction. See 'VJunc' for more info. … … 929 942 | Pos !Pos -- ^ Position 930 943 | Prag ![Pragma] -- ^ Lexical pragmas 931 deriving (Show, Eq, Ord, Typeable) {-!derive: YAML!-} 944 deriving (Show, Eq, Ord, Typeable) 945 946 instance YAML Ann where 947 asYAML (Cxt aa) = asYAMLseq "Cxt" [asYAML aa] 948 asYAML (Pos aa) = asYAMLseq "Pos" [asYAML aa] 949 asYAML (Prag aa) = asYAMLseq "Prag" [asYAML aa] 950 fromYAML node = case deTag node of 951 "Cxt" -> do 952 let YamlSeq [aa] = el node 953 fmap Cxt $ fromYAML aa 954 "Pos" -> do 955 let YamlSeq [aa] = el node 956 fmap Pos $ fromYAML aa 957 "Prag" -> do 958 let YamlSeq [aa] = el node 959 fmap Prag $ fromYAML aa 932 960 933 961 {- Expressions … … 1839 1867 instance YAML a => YAML (Map String a) where 1840 1868 asYAML x = asYAMLmap "Map" $ Map.toList (Map.map asYAML x) 1841 fromYAML node @MkYamlNode{tag=Just "tag:hs:Map"}= fmap Map.fromList (fromYAMLmap node)1869 fromYAML node = fmap Map.fromList (fromYAMLmap node) 1842 1870 instance Typeable a => YAML (IVar a) where 1843 1871 asYAML x = asYAML (MkRef x) … … 1871 1899 liftIO $ print svC 1872 1900 fail ("not implemented: asYAML \"" ++ showType (refType ref) ++ "\"") 1873 fromYAML node@MkYamlNode{tag=Just "tag:hs:VCode"}=1901 fromYAML node@MkYamlNode{tag=Just s} | s == Str.pack "tag:hs:VCode" = 1874 1902 fmap (MkRef . ICode) (fromYAML node :: IO VCode) 1875 fromYAML node@MkYamlNode{tag=Just "tag:hs:VScalar"}=1903 fromYAML node@MkYamlNode{tag=Just s} | s == Str.pack "tag:hs:VScalar" = 1876 1904 fmap (MkRef . IScalar) (fromYAML node :: IO VScalar) 1877 fromYAML node@MkYamlNode{tag=Just "tag:hs:IScalar"}=1905 fromYAML node@MkYamlNode{tag=Just s} | s == Str.pack "tag:hs:IScalar" = 1878 1906 fmap MkRef (newScalar =<< fromYAML node) 1879 fromYAML node@MkYamlNode{tag=Just "tag:hs:Array"}=1907 fromYAML node@MkYamlNode{tag=Just s} | s == Str.pack "tag:hs:Array" = 1880 1908 fmap MkRef (newArray =<< fromYAML node) 1881 fromYAML node@MkYamlNode{tag=Just "tag:hs:Hash"}= do1909 fromYAML node@MkYamlNode{tag=Just s} | s == Str.pack "tag:hs:Hash" = do 1882 1910 fmap MkRef (newHash =<< fromYAML node) 1883 fromYAML node@MkYamlNode{tag=Just "tag:hs:Pair"}= do1911 fromYAML node@MkYamlNode{tag=Just s} | s == Str.pack "tag:hs:Pair" = do 1884 1912 fmap pairRef (fromYAML node :: IO VPair) 1885 1913 … … 2061 2089 asYAML (PerlSV aa) = asYAMLseq "PerlSV" [asYAML aa] 2062 2090 2063 instance YAML VJunc where2064 asYAML (MkJunc aa ab ac) = asYAMLmap "MkJunc"2065 [("juncType", asYAML aa) , ("juncDup", asYAML ab) ,2066 ("juncSet", asYAML ac)]2067 2068 2091 instance YAML JuncType where 2069 2092 asYAML (JAny) = asYAMLcls "JAny" … … 2134 2157 ("subReturns", asYAML ai) , ("subLValue", asYAML aj) , 2135 2158 ("subBody", asYAML ak) , ("subCont", asYAML al)] 2136 2137 instance YAML Ann where2138 asYAML (Cxt aa) = asYAMLseq "Cxt" [asYAML aa]2139 asYAML (Pos aa) = asYAMLseq "Pos" [asYAML aa]2140 asYAML (Prag aa) = asYAMLseq "Prag" [asYAML aa]2141 2159 2142 2160 instance YAML Exp where -
src/Pugs/AST/Internals.hs-drift
r9016 r9021 78 78 import qualified Data.Map as Map 79 79 import qualified Data.IntMap as IntMap 80 import qualified Data.FastPackedString as Str 80 81 81 82 import Data.Yaml.Syck … … 100 101 #include "../Types/Pair.hs" 101 102 #include "../Types/Object.hs" 103 104 type Str = Str.FastString 102 105 103 106 errIndex :: Show a => Maybe b -> a -> Eval b … … 774 777 -- junctions, contains the set of values that appear exactly 775 778 -- /once/. 776 } deriving (Eq, Ord, Typeable) {-!derive: YAML!-} 779 } deriving (Eq, Ord, Typeable) 780 781 instance YAML VJunc where 782 asYAML (MkJunc aa ab ac) = asYAMLmap "MkJunc" 783 [("juncType", asYAML aa) , ("juncDup", asYAML ab) , 784 ("juncSet", asYAML ac)] 785 fromYAML node = do 786 let YamlMap assocs = el node 787 let [aa, ab, ac] = map snd assocs 788 liftM3 MkJunc (fromYAML aa) (fromYAML ab) (fromYAML ac) 789 777 790 778 791 -- | The combining semantics of a junction. See 'VJunc' for more info. … … 935 948 | Pos !Pos -- ^ Position 936 949 | Prag ![Pragma] -- ^ Lexical pragmas 937 deriving (Show, Eq, Ord, Typeable) {-!derive: YAML!-} 950 deriving (Show, Eq, Ord, Typeable) 951 952 instance YAML Ann where 953 asYAML (Cxt aa) = asYAMLseq "Cxt" [asYAML aa] 954 asYAML (Pos aa) = asYAMLseq "Pos" [asYAML aa] 955 asYAML (Prag aa) = asYAMLseq "Prag" [asYAML aa] 956 fromYAML node = case deTag node of 957 "Cxt" -> do 958 let YamlSeq [aa] = el node 959 fmap Cxt $ fromYAML aa 960 "Pos" -> do 961 let YamlSeq [aa] = el node 962 fmap Pos $ fromYAML aa 963 "Prag" -> do 964 let YamlSeq [aa] = el node 965 fmap Prag $ fromYAML aa 938 966 939 967 {- Expressions … … 1845 1873 instance YAML a => YAML (Map String a) where 1846 1874 asYAML x = asYAMLmap "Map" $ Map.toList (Map.map asYAML x) 1847 fromYAML node @MkYamlNode{tag=Just "tag:hs:Map"}= fmap Map.fromList (fromYAMLmap node)1875 fromYAML node = fmap Map.fromList (fromYAMLmap node) 1848 1876 instance Typeable a => YAML (IVar a) where 1849 1877 asYAML x = asYAML (MkRef x) … … 1877 1905 liftIO $ print svC 1878 1906 fail ("not implemented: asYAML \"" ++ showType (refType ref) ++ "\"") 1879 fromYAML node@MkYamlNode{tag=Just "tag:hs:VCode"}=1907 fromYAML node@MkYamlNode{tag=Just s} | s == Str.pack "tag:hs:VCode" = 1880 1908 fmap (MkRef . ICode) (fromYAML node :: IO VCode) 1881 fromYAML node@MkYamlNode{tag=Just "tag:hs:VScalar"}=1909 fromYAML node@MkYamlNode{tag=Just s} | s == Str.pack "tag:hs:VScalar" = 1882 1910 fmap (MkRef . IScalar) (fromYAML node :: IO VScalar) 1883 fromYAML node@MkYamlNode{tag=Just "tag:hs:IScalar"}=1911 fromYAML node@MkYamlNode{tag=Just s} | s == Str.pack "tag:hs:IScalar" = 1884 1912 fmap MkRef (newScalar =<< fromYAML node) 1885 fromYAML node@MkYamlNode{tag=Just "tag:hs:Array"}=1913 fromYAML node@MkYamlNode{tag=Just s} | s == Str.pack "tag:hs:Array" = 1886 1914 fmap MkRef (newArray =<< fromYAML node) 1887 fromYAML node@MkYamlNode{tag=Just "tag:hs:Hash"}= do1915 fromYAML node@MkYamlNode{tag=Just s} | s == Str.pack "tag:hs:Hash" = do 1888 1916 fmap MkRef (newHash =<< fromYAML node) 1889 fromYAML node@MkYamlNode{tag=Just "tag:hs:Pair"}= do1917 fromYAML node@MkYamlNode{tag=Just s} | s == Str.pack "tag:hs:Pair" = do 1890 1918 fmap pairRef (fromYAML node :: IO VPair) 1891 1919 -
src/Pugs/Prim/Yaml.hs
r9005 r9021 13 13 import qualified Data.IntSet as IntSet 14 14 import qualified Data.IntMap as IntMap 15 import qualified Data.FastPackedString as Str 15 16 import Foreign.StablePtr 16 17 import Foreign.Ptr 17 18 import Data.Generics 19 20 type Str = Str.FastString 18 21 19 22 evalYaml :: Val -> Eval Val … … 28 31 fromYaml :: YamlNode -> Eval Val 29 32 fromYaml MkYamlNode{el=YamlNil} = return VUndef 30 fromYaml MkYamlNode{el=YamlStr str} = return $ VStr (decodeUTF8 str)33 fromYaml MkYamlNode{el=YamlStr str} = return $ VStr $ decodeUTF8 $ Str.unpack str 31 34 fromYaml MkYamlNode{el=YamlSeq nodes} = do 32 35 vals <- mapM fromYaml nodes … … 43 46 hv <- liftSTM $ (newTVar (Map.fromList vals) :: STM IHash) 44 47 return $ VRef (hashRef hv) 45 Just ('p':'u':'g':'s':'/':'o':'b':'j':'e':'c':'t':':':typ) -> do 48 Just s | Just (pre, post) <- Str.breakFirst ':' s 49 , pre == Str.pack "pugs/Object" -> do 50 let typ = Str.unpack post 46 51 vals <- forM nodes $ \(keyNode, valNode) -> do 47 52 key <- fromVal =<< fromYaml keyNode … … 49 54 return (key, val) 50 55 return . VObject =<< createObject (mkType typ) vals 51 Just "pugs/Rule" -> do56 Just s | s == Str.pack "pugs/Rule" -> do 52 57 vals <- forM nodes $ \(keyNode, valNode) -> do 53 58 key <- fromVal =<< fromYaml keyNode … … 63 68 adverbs <- Map.lookup "adverbs" spec 64 69 return $ VRule MkRulePGE{rxRule=rule, rxGlobal=global, rxStringify=stringify, rxAdverbs=adverbs} 65 Just x -> error ("can't deserialize: " ++ x)70 Just x -> error ("can't deserialize: " ++ (Str.unpack x)) 66 71 67 72 dumpYaml :: Int -> Val -> Eval Val … … 74 79 75 80 strNode :: String -> YamlNode 76 strNode = mkNode . YamlStr 81 strNode = mkNode . YamlStr . Str.pack 77 82 78 83 addressOf :: a -> IO Int … … 107 112 hash <- fromVal v :: Eval VHash 108 113 attrs <- toYaml $ VRef (hashRef hash) 109 return $ tagNode (Just $ "tag:pugs:object:" ++ showType (objType obj)) attrs114 return $ tagNode (Just $ Str.pack $ "tag:pugs:object:" ++ showType (objType obj)) attrs 110 115 toYaml (VRule MkRulePGE{rxRule=rule, rxGlobal=global, rxStringify=stringify, rxAdverbs=adverbs}) =do 111 116 adverbs' <- toYaml adverbs
