Changeset 8603

Show
Ignore:
Timestamp:
01/07/06 10:33:51 (3 years ago)
Author:
audreyt
Message:

* Refactoring Yaml code to use implicit parameters for gaal++.

Location:
src
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • src/Data/Yaml/Syck.hsc

    r8600 r8603  
    9090 
    9191emitterCallback :: SyckEmitter -> Ptr () -> IO () 
    92 emitterCallback e vp = emitNode e =<< thawNode vp 
     92emitterCallback e vp = let ?e = e in emitNode =<< thawNode vp 
    9393     
    94 emitNode :: SyckEmitter -> YamlNode -> IO () 
    95 emitNode e YamlNil = do 
    96     -- syck_emit_scalar(e, "string", scalar_none, 0, 0, 0, "~", 1); 
     94emitNode :: (?e :: SyckEmitter) -> YamlNode -> IO () 
     95emitNode YamlNil = do 
    9796    withCString "string" $ \string_literal ->        
    9897        withCString "~" $ \cs ->        
    99             syck_emit_scalar e string_literal scalarNone 0 0 0 cs 1 
    100  
    101 emitNode e (YamlStr str) = do 
    102     -- return syck_emit_scalar(e, "string", scalar_none, 0, 0, 0, SvPVX(sv), SvCUR(sv)); 
     98            syck_emit_scalar ?e string_literal scalarNone 0 0 0 cs 1 
     99 
     100emitNode (YamlStr str) = do 
    103101    withCString "string" $ \string_literal ->        
    104102        withCString str $ \cs ->        
    105             syck_emit_scalar e string_literal scalarNone 0 0 0 cs (toEnum $ length str) 
    106  
    107 emitNode e (YamlSeq seq) = do 
    108     -- syck_emit_seq(e, "array", seq_none); 
     103            syck_emit_scalar ?e string_literal scalarNone 0 0 0 cs (toEnum $ length str) 
     104 
     105emitNode (YamlSeq seq) = do 
    109106    withCString "array" $ \array_literal -> 
    110         syck_emit_seq e array_literal seqNone 
     107        syck_emit_seq ?e array_literal seqNone 
    111108    -- TODO: fix pesky warning about "integer from pointer without a cast" here 
    112     mapM_ (syck_emit_item e) =<< (mapM freezeNode seq) 
    113     syck_emit_end e 
    114  
    115 emitNode e (YamlMap tag m) = do 
    116     -- syck_emit_map(e, "hash", map_none); 
    117     trace ("hash<" ++ maybe "" id tag ++">: " ++ (show m)) $ return () 
     109    mapM_ (syck_emit_item ?e) =<< (mapM freezeNode seq) 
     110    syck_emit_end ?e 
     111 
     112emitNode (YamlMap tag m) = do 
     113    -- trace ("hash<" ++ maybe "" id tag ++">: " ++ (show m)) $ return () 
    118114    withCString (maybe "hash" id tag) $ \hash_literal -> do 
    119         syck_emit_map e hash_literal mapNone 
    120         when (isJust tag) (do {syck_emit_tag e hash_literal nullPtr ; return ()}) 
     115        syck_emit_map ?e hash_literal mapNone 
     116        when (isJust tag) (do {syck_emit_tag ?e hash_literal nullPtr ; return ()}) 
    121117    flip mapM_ m (\(k,v) -> do 
    122         syck_emit_item e =<< freezeNode k 
    123         syck_emit_item e =<< freezeNode v) 
    124     syck_emit_end e 
     118        syck_emit_item ?e =<< freezeNode k 
     119        syck_emit_item ?e =<< freezeNode v) 
     120    syck_emit_end ?e 
    125121 
    126122 
  • src/Pugs/Prim/Yaml.hs

    r8602 r8603  
    4040 
    4141dumpYaml :: Int -> Val -> Eval Val 
    42 dumpYaml limit v = do 
    43     obj  <- toYaml limit v 
     42dumpYaml limit v = let ?d = limit in do 
     43    obj  <- toYaml v 
    4444    rv   <- liftIO (emitYaml obj) 
    4545    either (fail . ("YAML Emit Error: "++)) 
    4646           (return . VStr) rv 
    4747 
    48 toYaml :: Int -> Val -> Eval YamlNode 
    49 toYaml 0     _          = return $ YamlStr "<deep recursion>" -- fail? make this configurable? 
    50 toYaml _     VUndef     = return YamlNil 
    51 toYaml _     (VStr str) = return $ YamlStr (encodeUTF8 str) 
    52 toYaml (d+1) v@(VRef r) = do 
     48toYaml :: (?d :: Int) => Val -> Eval YamlNode 
     49toYaml _ | ?d == 0  = return $ YamlStr "<deep recursion>" -- fail? make this configurable? 
     50toYaml VUndef       = return YamlNil 
     51toYaml (VStr str)  = return $ YamlStr (encodeUTF8 str) 
     52toYaml v@(VRef r)   = let ?d = pred ?d in do 
    5353    t  <- evalValType v 
    54     ifValTypeIsa v "Hash" (hashToYaml d r) $ do 
     54    ifValTypeIsa v "Hash" (hashToYaml r) $ do 
    5555        v'      <- readRef r 
    56         nodes   <- toYaml d v' 
     56        nodes   <- toYaml v' 
    5757        ifValTypeIsa v "Array" (return nodes) $ case v' of 
    5858            VObject _   -> return nodes 
    5959            _           -> return (YamlMap Nothing [(YamlStr "<ref>", nodes)]) 
    60 toYaml (d+1) (VList nodes) = do 
    61     fmap YamlSeq $ mapM (toYaml d) nodes 
    62 toYaml (d+1) v@(VObject obj) = do 
     60toYaml (VList nodes) = let ?d = pred ?d in do 
     61    fmap YamlSeq $ mapM toYaml nodes 
     62toYaml v@(VObject obj) = let ?d = pred ?d in do 
    6363    -- ... dump the objAttrs 
    6464    -- XXX this needs fixing WRT demagicalized pairs: 
     
    6666    -- parens, which is, of course, wrong. 
    6767    hash    <- fromVal v :: Eval VHash 
    68     attrs   <- toYaml d $ VRef (hashRef hash) 
     68    attrs   <- toYaml $ VRef (hashRef hash) 
    6969    return $ addTag (Just $ "!pugs:object/" ++ showType (objType obj)) attrs 
    7070    where 
    7171    addTag _   (YamlMap (Just x) _) = error ("can't add tag: already tagged with" ++ x) 
    7272    addTag tag (YamlMap _        m) = YamlMap tag m 
    73 toYaml _ v = (return . YamlStr . encodeUTF8 . pretty) v 
     73toYaml v = (return . YamlStr . encodeUTF8 . pretty) v 
    7474 
    75 hashToYaml :: Int -> VRef -> Eval YamlNode 
    76 hashToYaml d (MkRef (IHash hv)) = do 
     75hashToYaml :: (?d :: Int) => VRef -> Eval YamlNode 
     76hashToYaml (MkRef (IHash hv)) = do 
    7777    h <- hash_fetch hv 
    7878    let assocs = Map.toList h 
    7979    yamlmap <- flip mapM assocs $ \(ka, va) -> do 
    80         ka' <- toYaml d $ VStr ka 
    81         va' <- toYaml d va 
     80        ka' <- toYaml $ VStr ka 
     81        va' <- toYaml va 
    8282        return (ka', va') 
    8383    return $ YamlMap Nothing yamlmap 
    84 hashToYaml _ r = error ("unexpected node: " ++ show r) 
     84hashToYaml r = error ("unexpected node: " ++ show r) 
    8585    
    8686{-