 
 DEFER: parse-sequence
 DEFER: parse-mapping
-: next-complex-value ( parser event type -- obj )
-    {
-        { YAML_SEQUENCE_START_EVENT [ parse-sequence ] }
-        { YAML_MAPPING_START_EVENT [ parse-mapping ] }
+: (parse-sequence) ( parser event prev-event -- obj )
+    [ parse-sequence ] [ sequence_start>> ?register-anchor ] bi* ;
+: (parse-mapping) ( parser event prev-event -- obj )
+    [ parse-mapping ] [ mapping_start>> ?register-anchor ] bi* ;
+: next-complex-value ( parser event prev-event -- obj )
+    dup type>> {
+        { YAML_SEQUENCE_START_EVENT [ data>> (parse-sequence) ] }
+        { YAML_MAPPING_START_EVENT [ data>> (parse-mapping) ] }
+        { YAML_ALIAS_EVENT [ 2nip deref-anchor ] }
         [ throw ]
     } case ;
 
 :: next-value ( parser event -- obj )
     parser event [ next-event ?scalar-value ] with-destructors
-    [ [ drop parser event ] dip next-complex-value ] when* ;
+    [ [ parser event ] dip next-complex-value ] unless ;
 
 :: parse-mapping ( parser event -- map )
     [


: yaml-assert-ok ( ? -- ) [ "yaml error" throw ] unless ;

: event>scalar ( event -- obj )
    data>> scalar>> construct-scalar ;

: ?scalar-value ( event -- scalar/f f/type )
    dup type>> YAML_SCALAR_EVENT =
    [ event>scalar f ] [ type>> clone f swap ] if ;

! Must not reuse the event struct before with-destructors scope ends
: next-event ( parser event -- event )

            [
                parser event next-event type>>
                YAML_SEQUENCE_END_EVENT = [
                    t done! f f
                ] [
                    event ?scalar-value
                ] if
            ] with-destructors
            done [ 2drop ] [
              [ [ parser event ] dip next-complex-value ] unless ,
            ] if
        ] until
    ] { } make ;

