@@ -26,7 +26,9 @@ module Text.Libyaml
26 , emitStream
26 , emitStream
27 , emitDocument
27 , emitDocument
28 , emitSequence
28 , emitSequence
29+ , emitSequenceWithAnchor
29 , emitMapping
30 , emitMapping
31+ , emitMappingWithAnchor
30 -- * Higher level functions
32 -- * Higher level functions
@@ -57,11 +59,11 @@ data Event =
57 | EventStreamEnd
59 | EventStreamEnd
58 | EventDocumentStart
60 | EventDocumentStart
59 | EventDocumentEnd
61 | EventDocumentEnd
60− | EventAlias
62+ | EventAlias !AnchorName
61− | EventScalar !ByteString !Tag !Style
63+ | EventScalar !ByteString !Tag !Style !Anchor
62− | EventSequenceStart
64+ | EventSequenceStart !Anchor
63 | EventSequenceEnd
65 | EventSequenceEnd
64− | EventMappingStart
66+ | EventMappingStart !Anchor
65 | EventMappingEnd
67 | EventMappingEnd
66 deriving (Show, Eq)
68 deriving (Show, Eq)
@@ -85,6 +87,9 @@ data Tag = StrTag
86 deriving (Show, Eq)
88 deriving (Show, Eq)
90+type AnchorName = String
91+type Anchor = Maybe AnchorName
88tagToString :: Tag -> String
93tagToString :: Tag -> String
89tagToString StrTag = "tag:yaml.org,2002:str"
94tagToString StrTag = "tag:yaml.org,2002:str"
90tagToString FloatTag = "tag:yaml.org,2002:float"
95tagToString FloatTag = "tag:yaml.org,2002:float"
@@ -296,6 +301,18 @@ foreign import ccall unsafe "get_scalar_tag_len"
296foreign import ccall unsafe "get_scalar_style"
301foreign import ccall unsafe "get_scalar_style"
297 c_get_scalar_style :: EventRaw -> IO CInt
302 c_get_scalar_style :: EventRaw -> IO CInt
304+foreign import ccall unsafe "get_scalar_anchor"
305+ c_get_scalar_anchor :: EventRaw -> IO CString
307+foreign import ccall unsafe "get_sequence_start_anchor"
308+ c_get_sequence_start_anchor :: EventRaw -> IO CString
310+foreign import ccall unsafe "get_mapping_start_anchor"
311+ c_get_mapping_start_anchor :: EventRaw -> IO CString
313+foreign import ccall unsafe "get_alias_anchor"
314+ c_get_alias_anchor :: EventRaw -> IO CString
299getEvent :: EventRaw -> IO Event
316getEvent :: EventRaw -> IO Event
300getEvent er = do
317getEvent er = do
301 et <- c_get_event_type er
318 et <- c_get_event_type er
@@ -305,7 +322,12 @@ getEvent er = do
305 YamlStreamEndEvent -> return EventStreamEnd
322 YamlStreamEndEvent -> return EventStreamEnd
306 YamlDocumentStartEvent -> return EventDocumentStart
323 YamlDocumentStartEvent -> return EventDocumentStart
307 YamlDocumentEndEvent -> return EventDocumentEnd
324 YamlDocumentEndEvent -> return EventDocumentEnd
325+ YamlAliasEvent -> do
326+ yanchor <- c_get_alias_anchor er
327+ anchor <- if yanchor == nullPtr
328+ then error "got YamlAliasEvent with empty anchor"
329+ else peekCString yanchor
308− YamlAliasEvent -> return EventAlias
330+ return $ EventAlias anchor
309 YamlScalarEvent -> do
331 YamlScalarEvent -> do
310 yvalue <- c_get_scalar_value er
332 yvalue <- c_get_scalar_value er
311 ylen <- c_get_scalar_length er
333 ylen <- c_get_scalar_length er
@@ -324,10 +346,24 @@ getEvent er = do
324 else B.create ytag_len'
346 else B.create ytag_len'
325 $ \dest -> B.memcpy dest ytag' (toEnum ytag_len')
347 $ \dest -> B.memcpy dest ytag' (toEnum ytag_len')
326 let style = toEnum $ fromEnum ystyle
348 let style = toEnum $ fromEnum ystyle
349+ yanchor <- c_get_scalar_anchor er
350+ anchor <- if yanchor == nullPtr
352+ else fmap Just $ peekCString yanchor
327− return $ EventScalar bs (bsToTag tagbs) style
353+ return $ EventScalar bs (bsToTag tagbs) style anchor
328− YamlSequenceStartEvent -> return EventSequenceStart
354+ YamlSequenceStartEvent -> do
355+ yanchor <- c_get_sequence_start_anchor er
356+ anchor <- if yanchor == nullPtr
358+ else fmap Just $ peekCString yanchor
359+ return $ EventSequenceStart anchor
329 YamlSequenceEndEvent -> return EventSequenceEnd
360 YamlSequenceEndEvent -> return EventSequenceEnd
330− YamlMappingStartEvent -> return EventMappingStart
361+ YamlMappingStartEvent -> do
362+ yanchor <- c_get_mapping_start_anchor er
363+ anchor <- if yanchor == nullPtr
365+ else fmap Just $ peekCString yanchor
366+ return $ EventMappingStart anchor
331 YamlMappingEndEvent -> return EventMappingEnd
367 YamlMappingEndEvent -> return EventMappingEnd
333-- Emitter
369-- Emitter
@@ -429,10 +465,19 @@ emitStream, emitDocument, emitSequence, emitMapping
429emitStream e = emitEvent EventStreamStart >> e >> emitEvent EventStreamEnd
465emitStream e = emitEvent EventStreamStart >> e >> emitEvent EventStreamEnd
430emitDocument e = emitEvent EventDocumentStart >> e
466emitDocument e = emitEvent EventDocumentStart >> e
431 >> emitEvent EventDocumentEnd
467 >> emitEvent EventDocumentEnd
468+emitSequence e = emitSequenceWithAnchor e Nothing
469+emitMapping e = emitMappingWithAnchor e Nothing
471+emitSequenceWithAnchor, emitMappingWithAnchor
472+ :: (MonadIO m, MonadFailure YamlException m)
432−emitSequence e = emitEvent EventSequenceStart >> e
476+emitSequenceWithAnchor e a = emitEvent (EventSequenceStart a) >> e
433− >> emitEvent EventSequenceEnd
477+ >> emitEvent EventSequenceEnd
434−emitMapping e = emitEvent EventMappingStart >> e
478+emitMappingWithAnchor e a = emitEvent (EventMappingStart a) >> e
435− >> emitEvent EventMappingEnd
479+ >> emitEvent EventMappingEnd
437parseEvent :: (With m, MonadFailure YamlException m)
482parseEvent :: (With m, MonadFailure YamlException m)
438 => YamlDecoder m Event
483 => YamlDecoder m Event
@@ -489,6 +534,12 @@ foreign import ccall unsafe "yaml_mapping_start_event_initialize"
489foreign import ccall unsafe "yaml_mapping_end_event_initialize"
534foreign import ccall unsafe "yaml_mapping_end_event_initialize"
490 c_yaml_mapping_end_event_initialize :: EventRaw -> IO CInt
535 c_yaml_mapping_end_event_initialize :: EventRaw -> IO CInt
537+foreign import ccall unsafe "yaml_alias_event_initialize"
538+ c_yaml_alias_event_initialize
492toEventRaw :: Event -> (EventRaw -> IO a) -> IO a
543toEventRaw :: Event -> (EventRaw -> IO a) -> IO a
493toEventRaw e f = allocaBytesR eventSize $ \er -> do
544toEventRaw e f = allocaBytesR eventSize $ \er -> do
494 ret <- case e of
545 ret <- case e of
@@ -502,7 +553,7 @@ toEventRaw e f = allocaBytesR eventSize $ \er -> do
502 c_simple_document_start er
553 c_simple_document_start er
503 EventDocumentEnd ->
554 EventDocumentEnd ->
504 c_yaml_document_end_event_initialize er 1
555 c_yaml_document_end_event_initialize er 1
505− EventScalar bs thetag style -> do
556+ EventScalar bs thetag style anchor -> do
506 let (fvalue, offset, len) = B.toForeignPtr bs
557 let (fvalue, offset, len) = B.toForeignPtr bs
507 withForeignPtr fvalue $ \value -> do
558 withForeignPtr fvalue $ \value -> do
508 let value' = value `plusPtr` offset
559 let value' = value `plusPtr` offset
@@ -515,34 +566,71 @@ toEventRaw e f = allocaBytesR eventSize $ \er -> do
515 let style' = toEnum $ fromEnum style
566 let style' = toEnum $ fromEnum style
516 tagP = castPtr tag'
567 tagP = castPtr tag'
517 qi = if null thetag' then 1 else 0
568 qi = if null thetag' then 1 else 0
518− c_yaml_scalar_event_initialize
571+ c_yaml_scalar_event_initialize
520− nullPtr -- anchor
573+ nullPtr -- anchor
521− tagP -- tag
574+ tagP -- tag
522− value'' -- value
575+ value'' -- value
523− len' -- length
576+ len' -- length
524− 0 -- plain_implicit
577+ 0 -- plain_implicit
525− qi -- quoted_implicit
578+ qi -- quoted_implicit
581+ withCString anchor' $ \anchorP' -> do
582+ let anchorP = castPtr anchorP'
583+ c_yaml_scalar_event_initialize
590+ qi -- quoted_implicit
526− style' -- style
591+ style' -- style
527− EventSequenceStart ->
592+ EventSequenceStart Nothing ->
528 c_yaml_sequence_start_event_initialize
593 c_yaml_sequence_start_event_initialize
533 0 -- YAML_ANY_SEQUENCE_STYLE
598 0 -- YAML_ANY_SEQUENCE_STYLE
599+ EventSequenceStart (Just anchor) ->
600+ withCString anchor $ \anchor' -> do
601+ let anchorP = castPtr anchor'
602+ c_yaml_sequence_start_event_initialize
607+ 0 -- YAML_ANY_SEQUENCE_STYLE
534 EventSequenceEnd ->
608 EventSequenceEnd ->
535 c_yaml_sequence_end_event_initialize er
609 c_yaml_sequence_end_event_initialize er
536− EventMappingStart ->
610+ EventMappingStart Nothing ->
537 c_yaml_mapping_start_event_initialize
611 c_yaml_mapping_start_event_initialize
542 0 -- YAML_ANY_SEQUENCE_STYLE
616 0 -- YAML_ANY_SEQUENCE_STYLE
617+ EventMappingStart (Just anchor) ->
618+ withCString anchor $ \anchor' -> do
619+ let anchorP = castPtr anchor'
620+ c_yaml_mapping_start_event_initialize
625+ 0 -- YAML_ANY_SEQUENCE_STYLE
543 EventMappingEnd ->
626 EventMappingEnd ->
544 c_yaml_mapping_end_event_initialize er
627 c_yaml_mapping_end_event_initialize er
545− EventAlias -> error "toEventRaw: EventAlias not supported"
628+ EventAlias anchor ->
629+ withCString anchor $ \anchorP' -> do
630+ let anchorP = castPtr anchorP'
631+ c_yaml_alias_event_initialize
546 EventNone -> error "toEventRaw: EventNone not supported"
634 EventNone -> error "toEventRaw: EventNone not supported"
547 unless (ret == 1) $ throwIO $ ToEventRawException ret
635 unless (ret == 1) $ throwIO $ ToEventRawException ret