Changeset 8608

Show
Ignore:
Timestamp:
01/08/06 20:40:46 (3 years ago)
Author:
gaal
Message:

YAML serialization -
* emit PGE Rules (rx:g/a/.yaml works)
* normalize emitted booleans
* src/Data/Yaml/Syck.hsc cleanups

  • YamlNode? structure shouldn't change much henceforth
  • remove implicit vars
Location:
src
Files:
2 modified

Legend:

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

    r8607 r8608  
    3131 
    3232data YamlNode = MkYamlNode 
    33     { nid    :: SYMID 
    34     , tag    :: YamlTag 
    35     , anchor :: YamlAnchor 
    36     , el     :: YamlElem 
     33    { nid      :: SYMID 
     34    , el       :: YamlElem 
     35    , tag      :: YamlTag 
     36    , anchor   :: YamlAnchor 
     37    , shortcut :: (Maybe YamlNode) 
    3738    } 
    3839    deriving (Show, Ord, Eq) 
     
    5859 
    5960emptyYamlNode :: YamlNode 
    60 emptyYamlNode = MkYamlNode 0 Nothing Nothing YamlNil 
     61emptyYamlNode = MkYamlNode 0 YamlNil Nothing Nothing Nothing 
    6162 
    6263tagNode :: YamlTag -> YamlNode -> YamlNode 
     
    6465tagNode tag node                   = node{tag = tag} 
    6566 
    66 -- the extra comma here is not a bug 
     67-- the extra commas here are not a bug 
    6768#enum CInt, , scalar_none, scalar_1quote, scalar_2quote, scalar_fold, scalar_literal, scalar_plain 
    6869#enum CInt, , seq_none, seq_inline 
     
    105106 
    106107emitterCallback :: SyckEmitter -> Ptr () -> IO () 
    107 emitterCallback e vp = let ?e = e in emitNode =<< thawNode vp 
     108emitterCallback e vp = emitNode e =<< thawNode vp 
    108109     
    109 emitNode :: (?e :: SyckEmitter) -> YamlNode -> IO () 
    110 emitNode n@(MkYamlNode{el = YamlNil}) = do 
     110emitNode :: SyckEmitter -> YamlNode -> IO () 
     111emitNode e n@(MkYamlNode{el = YamlNil}) = do 
    111112    withTag n "string" $ \tag -> 
    112113        withCString "~" $ \cs ->        
    113             syck_emit_scalar ?e tag scalarNone 0 0 0 cs 1 
    114  
    115 emitNode n@(MkYamlNode{el = YamlStr str}) = do 
     114            syck_emit_scalar e tag scalarNone 0 0 0 cs 1 
     115 
     116emitNode e n@(MkYamlNode{el = YamlStr str}) = do 
    116117    withTag n "string" $ \tag ->        
    117118        withCString str $ \cs ->        
    118             syck_emit_scalar ?e tag scalarNone 0 0 0 cs (toEnum $ length str) 
    119  
    120 emitNode n@(MkYamlNode{el = YamlSeq seq}) = do 
     119            syck_emit_scalar e tag scalarNone 0 0 0 cs (toEnum $ length str) 
     120 
     121emitNode e n@(MkYamlNode{el = YamlSeq seq}) = do 
    121122    withTag n "array" $ \tag -> 
    122         syck_emit_seq ?e tag seqNone 
     123        syck_emit_seq e tag seqNone 
    123124    -- TODO: fix pesky warning about "integer from pointer without a cast" here 
    124     mapM_ (syck_emit_item ?e) =<< (mapM freezeNode seq) 
    125     syck_emit_end ?e 
    126  
    127 emitNode n@(MkYamlNode{el = YamlMap m}) = do 
    128     --trace ("hash<" ++ maybe "" id tag ++">: " ++ (show m)) $ return () 
     125    mapM_ (syck_emit_item e) =<< (mapM freezeNode seq) 
     126    syck_emit_end e 
     127 
     128emitNode e n@(MkYamlNode{el = YamlMap m}) = do 
    129129    withTag n "hash" $ \tag ->  
    130         syck_emit_map ?e tag mapNone 
     130        syck_emit_map e tag mapNone 
    131131    flip mapM_ m (\(k,v) -> do 
    132         syck_emit_item ?e =<< freezeNode k 
    133         syck_emit_item ?e =<< freezeNode v) 
    134     syck_emit_end ?e 
     132        syck_emit_item e =<< freezeNode k 
     133        syck_emit_item e =<< freezeNode v) 
     134    syck_emit_end e 
    135135 
    136136withTag :: YamlNode -> String -> (CString -> IO a) -> IO a 
     
    146146        syck_parser_implicit_typing parser 1 
    147147        syck_parser_taguri_expansion parser 0 
    148         symId   <- syck_parse parser 
     148        symId <- syck_parse parser 
    149149        if symId /= 0 then fmap (Right . Just) (readNode parser symId) else do 
    150150        rv <- readIORef err 
  • src/Pugs/Prim/Yaml.hs

    r8607 r8608  
    5454           (return . VStr) rv 
    5555 
     56strNode :: String -> YamlNode 
     57strNode str = emptyYamlNode{el = YamlStr str } 
     58 
    5659toYaml :: (?d :: Int) => Val -> Eval YamlNode 
    57 toYaml _ | ?d == 0  = return $ emptyYamlNode{el = YamlStr "<deep recursion>"} -- fail? make this configurable? 
     60toYaml _ | ?d == 0  = return $ strNode "<deep recursion>" -- fail? make this configurable? 
    5861toYaml VUndef       = return emptyYamlNode 
    59 toYaml (VStr str)   = return $ emptyYamlNode{el = YamlStr (encodeUTF8 str)} 
     62toYaml (VBool x)    = return $ boolToYaml x 
     63toYaml (VStr str)   = return $ strNode (encodeUTF8 str) 
    6064toYaml v@(VRef r)   = let ?d = pred ?d in do 
    6165    t  <- evalValType v 
     
    6569        ifValTypeIsa v "Array" (return nodes) $ case v' of 
    6670            VObject _   -> return nodes 
    67             _           -> return emptyYamlNode{el = YamlMap [(emptyYamlNode{el=YamlStr "<ref>"}, nodes)]} 
     71            _           -> return emptyYamlNode{el = YamlMap [(strNode "<ref>", nodes)]} 
    6872toYaml (VList nodes) = let ?d = pred ?d in do 
    6973    n <- mapM toYaml nodes 
     
    7882    attrs   <- toYaml $ VRef (hashRef hash) 
    7983    return $ tagNode (Just $ "tag:pugs:object:" ++ showType (objType obj)) attrs 
    80 toYaml v = return $ emptyYamlNode{el=YamlStr p} 
    81     where p = (encodeUTF8 . pretty) v 
     84toYaml (VRule MkRulePGE{rxRule=rule, rxGlobal=global, rxStringify=stringify, rxAdverbs=adverbs}) = let ?d = pred ?d in do 
     85    adverbs' <- toYaml adverbs 
     86    return emptyYamlNode{el = YamlMap 
     87                            [ (strNode "rule", strNode rule) 
     88                            , (strNode "global", boolToYaml global) 
     89                            , (strNode "stringify", boolToYaml stringify) 
     90                            , (strNode "adverbs", adverbs') 
     91                            ] , tag = Just "tag:pugs:Rule"} 
     92toYaml v = return $ strNode $ (encodeUTF8 . pretty) v 
    8293 
    8394 
     
    93104hashToYaml r = error ("unexpected node: " ++ show r) 
    94105 
     106boolToYaml :: VBool -> YamlNode 
     107boolToYaml True  = strNode "true" 
     108boolToYaml False = strNode "false"