Fix error with comments after tags.

This commit is contained in:
Ethiraric 2024-04-16 12:03:42 +02:00
parent cbba46fa72
commit 9ee2d113bc
2 changed files with 32 additions and 17 deletions

View file

@ -506,7 +506,7 @@ impl<T: Iterator<Item = char>> Scanner<T> {
/// Consume a linebreak (either CR, LF or CRLF), if any. Do nothing if there's none. /// Consume a linebreak (either CR, LF or CRLF), if any. Do nothing if there's none.
#[inline] #[inline]
fn skip_line(&mut self) { fn skip_linebreak(&mut self) {
if self.buffer[0] == '\r' && self.buffer[1] == '\n' { if self.buffer[0] == '\r' && self.buffer[1] == '\n' {
// While technically not a blank, this does not matter as `self.leading_whitespace` // While technically not a blank, this does not matter as `self.leading_whitespace`
// will be reset by `skip_nl`. // will be reset by `skip_nl`.
@ -843,7 +843,7 @@ impl<T: Iterator<Item = char>> Scanner<T> {
'\t' | ' ' => self.skip_blank(), '\t' | ' ' => self.skip_blank(),
'\n' | '\r' => { '\n' | '\r' => {
self.lookahead(2); self.lookahead(2);
self.skip_line(); self.skip_linebreak();
if self.flow_level == 0 { if self.flow_level == 0 {
self.allow_simple_key(); self.allow_simple_key();
} }
@ -874,7 +874,7 @@ impl<T: Iterator<Item = char>> Scanner<T> {
} }
'\n' | '\r' => { '\n' | '\r' => {
self.lookahead(2); self.lookahead(2);
self.skip_line(); self.skip_linebreak();
if self.flow_level == 0 { if self.flow_level == 0 {
self.allow_simple_key(); self.allow_simple_key();
} }
@ -971,8 +971,6 @@ impl<T: Iterator<Item = char>> Scanner<T> {
self.disallow_simple_key(); self.disallow_simple_key();
let tok = self.scan_directive()?; let tok = self.scan_directive()?;
self.skip_ws_to_eol(SkipTabs::Yes)?;
self.tokens.push_back(tok); self.tokens.push_back(tok);
Ok(()) Ok(())
@ -1004,20 +1002,16 @@ impl<T: Iterator<Item = char>> Scanner<T> {
self.skip_ws_to_eol(SkipTabs::Yes)?; self.skip_ws_to_eol(SkipTabs::Yes)?;
if !is_breakz(self.ch()) { if is_breakz(self.ch()) {
return Err(ScanError::new_str( self.lookahead(2);
self.skip_linebreak();
Ok(tok)
} else {
Err(ScanError::new_str(
start_mark, start_mark,
"while scanning a directive, did not find expected comment or line break", "while scanning a directive, did not find expected comment or line break",
)); ))
} }
// Eat a line break
if is_break(self.ch()) {
self.lookahead(2);
self.skip_line();
}
Ok(tok)
} }
fn scan_version_directive_value(&mut self, mark: &Marker) -> Result<Token, ScanError> { fn scan_version_directive_value(&mut self, mark: &Marker) -> Result<Token, ScanError> {
@ -2046,7 +2040,7 @@ impl<T: Iterator<Item = char>> Scanner<T> {
'\\' if !single && is_break(self.buffer[1]) => { '\\' if !single && is_break(self.buffer[1]) => {
self.lookahead(3); self.lookahead(3);
self.skip_non_blank(); self.skip_non_blank();
self.skip_line(); self.skip_linebreak();
*leading_blanks = true; *leading_blanks = true;
break; break;
} }

View file

@ -181,6 +181,27 @@ fn test_issue_65_mwe() {
assert!(run_parser(b).is_err()); assert!(run_parser(b).is_err());
} }
#[test]
fn test_comment_after_tag() {
// https://github.com/Ethiraric/yaml-rust2/issues/21#issuecomment-2053513507
let s = "
%YAML 1.2
# This is a comment
--- #-------
foobar";
assert_eq!(
run_parser(s).unwrap(),
[
Event::StreamStart,
Event::DocumentStart,
Event::Scalar("foobar".to_string(), TScalarStyle::Plain, 0, None),
Event::DocumentEnd,
Event::StreamEnd,
]
);
}
#[test] #[test]
fn test_bad_docstart() { fn test_bad_docstart() {
assert!(run_parser("---This used to cause an infinite loop").is_ok()); assert!(run_parser("---This used to cause an infinite loop").is_ok());