Kirill Simonov avatar Kirill Simonov committed 567d807

Emit an explicit document end indicator when there is a possibility of ambiguous parsing.

Comments (0)

Files changed (1)

lib/yaml/emitter.py

         self.whitespace = True
         self.indention = True
 
+        # Whether the document requires an explicit document indicator
+        self.open_ended = False
+
         # Formatting details.
         self.canonical = canonical
         self.allow_unicode = allow_unicode
 
     def expect_document_start(self, first=False):
         if isinstance(self.event, DocumentStartEvent):
+            if (self.event.version or self.event.tags) and self.open_ended:
+                self.write_indicator(u'...', True)
+                self.write_indent()
             if self.event.version:
                 version_text = self.prepare_version(self.event.version)
                 self.write_version_directive(version_text)
                     self.write_indent()
             self.state = self.expect_document_root
         elif isinstance(self.event, StreamEndEvent):
+            if self.open_ended:
+                self.write_indicator(u'...', True)
+                self.write_indent()
             self.write_stream_end()
             self.state = self.expect_nothing
         else:
         self.whitespace = whitespace
         self.indention = self.indention and indention
         self.column += len(data)
+        self.open_ended = False
         if self.encoding:
             data = data.encode(self.encoding)
         self.stream.write(data)
     def write_folded(self, text):
         hints = self.determine_block_hints(text)
         self.write_indicator(u'>'+hints, True)
+        if hints[-1:] == u'+':
+            self.open_ended = True
         self.write_line_break()
         leading_space = True
         spaces = False
             end += 1
 
     def write_literal(self, text):
-        chomp = self.determine_block_hints(text)
-        self.write_indicator(u'|'+chomp, True)
+        hints = self.determine_block_hints(text)
+        self.write_indicator(u'|'+hints, True)
+        if hints[-1:] == u'+':
+            self.open_ended = True
         self.write_line_break()
         breaks = True
         start = end = 0
             end += 1
 
     def write_plain(self, text, split=True):
+        if self.root_context:
+            self.open_ended = True
         if not text:
             return
         if not self.whitespace:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.