# HG changeset patch
# User Tina Müller <cpan2@tinita.de>
# Date 1586973402 -7200
#      Wed Apr 15 19:56:42 2020 +0200
# Node ID 64758cef676f33b7dc3fae5f4830f63789b488a0
# Parent  6c0f5c49ef59f3b436e7890fbf61ca3f7db3b677
Fix logic for document end before directive

open_ended can have three states now:
0: The previous document was ended explicitly with '...'
1: The previous document wasn't ended with '...'
2: The last scalar event was a block scalar with trailing empty lines |+, and
   last document wasn't ended with '...'.
   Important at stream end.

This was broken in the past, and fixed in fa1293a.
With my last PR #162 I added the faulty behaviour again.

The problematic behaviour showed only when all of the following conditions were
true:
* writing YAML directives
* writing unquoted top level scalars
* writing more than one document

================== BROKEN ==============================

The first example shows that the document end marker is not emitted before
the next document. This would be valid in YAML 1.1 if the scalar was quoted,
but not if it is plain.

This commit fixes this.

echo '--- foo
--- bar
' | ./tests/run-parser-test-suite  | ./tests/run-emitter-test-suite  --directive 1.1

%YAML 1.1
--- foo
%YAML 1.1
--- bar

================== FIXED ==============================

echo '--- foo
--- bar
' | ./tests/run-parser-test-suite  | ./tests/run-emitter-test-suite  --directive 1.1
%YAML 1.1
--- foo
...
%YAML 1.1
--- bar

=======================================================

Other examples which should look like this (and were correct already before
this fix):

Open ended scalars like |+ need '...' at the end of the stream:

echo '--- |+
  a

--- |+
  a

' | ./tests/run-parser-test-suite  | ./tests/run-emitter-test-suite
--- |+
  a

--- |+
  a

...

=======================================================

If a document is ended with an explicit '...', the code should not
print '...' twice:

echo '--- foo
...
--- bar
' | ./tests/run-parser-test-suite  | ./tests/run-emitter-test-suite --directive 1.1
%YAML 1.1
--- foo
...
%YAML 1.1
--- bar

==========================================================

diff --git a/src/emitter.c b/src/emitter.c
--- a/src/emitter.c
+++ b/src/emitter.c
@@ -495,6 +495,7 @@
 yaml_emitter_emit_stream_start(yaml_emitter_t *emitter,
         yaml_event_t *event)
 {
+    emitter->open_ended = 0;
     if (event->type == YAML_STREAM_START_EVENT)
     {
         if (!emitter->encoding) {
@@ -594,10 +595,10 @@
         {
             if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0))
                 return 0;
-            emitter->open_ended = 0;
             if (!yaml_emitter_write_indent(emitter))
                 return 0;
         }
+        emitter->open_ended = 0;
 
         if (event->data.document_start.version_directive) {
             implicit = 0;
@@ -662,7 +663,7 @@
          * This can happen if a block scalar with trailing empty lines
          * is at the end of the stream
          */
-        if (emitter->open_ended)
+        if (emitter->open_ended == 2)
         {
             if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0))
                 return 0;
@@ -715,6 +716,8 @@
             if (!yaml_emitter_write_indent(emitter))
                 return 0;
         }
+        else if (!emitter->open_ended)
+            emitter->open_ended = 1;
         if (!yaml_emitter_flush(emitter))
             return 0;
 
@@ -2222,7 +2225,7 @@
         else if (string.start == string.pointer)
         {
             chomp_hint = "+";
-            emitter->open_ended = 1;
+            emitter->open_ended = 2;
         }
         else
         {
@@ -2232,7 +2235,7 @@
             if (IS_BREAK(string))
             {
                 chomp_hint = "+";
-                emitter->open_ended = 1;
+                emitter->open_ended = 2;
             }
         }
     }