From 99d82be6a3f3d958014d8efde5d5b56473253009 Mon Sep 17 00:00:00 2001 From: Ethiraric Date: Fri, 19 Jan 2024 14:55:06 +0100 Subject: [PATCH] Fix possible misindent in block scalar. --- parser/src/scanner.rs | 18 ++++++++++++++++-- parser/tests/yaml-test-suite.rs | 5 ----- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/parser/src/scanner.rs b/parser/src/scanner.rs index cd139f4..60a3d5b 100644 --- a/parser/src/scanner.rs +++ b/parser/src/scanner.rs @@ -681,7 +681,10 @@ impl> Scanner { self.fetch_document_indicator(TokenType::DocumentEnd)?; self.skip_ws_to_eol(SkipTabs::Yes); if !is_breakz(self.ch()) { - return Err(ScanError::new(self.mark, "invalid content after document end marker")); + return Err(ScanError::new( + self.mark, + "invalid content after document end marker", + )); } return Ok(()); } @@ -1690,7 +1693,18 @@ impl> Scanner { } } - *indent = max_indent.max((self.indent + 1) as usize).max(1); + // In case a yaml looks like: + // ```yaml + // | + // foo + // bar + // ``` + // We need to set the indent to 0 and not 1. In all other cases, the indent must be at + // least 1. When in the above example, `self.indent` will be set to -1. + *indent = max_indent.max((self.indent + 1) as usize); + if self.indent > 0 { + *indent = (*indent).max(1); + } } fn fetch_flow_scalar(&mut self, single: bool) -> ScanResult { diff --git a/parser/tests/yaml-test-suite.rs b/parser/tests/yaml-test-suite.rs index 2fb6284..63ad2e4 100644 --- a/parser/tests/yaml-test-suite.rs +++ b/parser/tests/yaml-test-suite.rs @@ -297,11 +297,6 @@ fn expected_events(expected_tree: &str) -> Vec { #[rustfmt::skip] static EXPECTED_FAILURES: &[&str] = &[ - // Bare document after end marker - "M7A3", - // Scalar marker on document start line - "DK3J", - "FP8R", // Comments on nonempty lines need leading space "9JBA", "CVW2",