Skip to content

Commit

Permalink
Fix logic for document end before directive
Browse files Browse the repository at this point in the history
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

==========================================================
  • Loading branch information
perlpunk committed Apr 19, 2020
1 parent 4e5cea6 commit 3694a4a
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/emitter.c
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ static int
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) {
Expand Down Expand Up @@ -594,10 +595,10 @@ yaml_emitter_emit_document_start(yaml_emitter_t *emitter,
{
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;
Expand Down Expand Up @@ -662,7 +663,7 @@ yaml_emitter_emit_document_start(yaml_emitter_t *emitter,
* 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;
Expand Down Expand Up @@ -715,6 +716,8 @@ yaml_emitter_emit_document_end(yaml_emitter_t *emitter,
if (!yaml_emitter_write_indent(emitter))
return 0;
}
else if (!emitter->open_ended)
emitter->open_ended = 1;
if (!yaml_emitter_flush(emitter))
return 0;

Expand Down Expand Up @@ -2222,7 +2225,7 @@ yaml_emitter_write_block_scalar_hints(yaml_emitter_t *emitter,
else if (string.start == string.pointer)
{
chomp_hint = "+";
emitter->open_ended = 1;
emitter->open_ended = 2;
}
else
{
Expand All @@ -2232,7 +2235,7 @@ yaml_emitter_write_block_scalar_hints(yaml_emitter_t *emitter,
if (IS_BREAK(string))
{
chomp_hint = "+";
emitter->open_ended = 1;
emitter->open_ended = 2;
}
}
}
Expand Down

0 comments on commit 3694a4a

Please sign in to comment.