Look ahead before testing for EOF. (#12)

Look ahead before testing for EOF.

This fixes panics in saphyr's test_multiline_trailing_newline and
test_multiline_leading_newline tests, in which `self.input.next_is_z`
would be called on an empty buffer and panic in `peek`
This commit is contained in:
jneem 2024-09-26 23:32:28 +07:00 committed by Ethiraric
parent 434f4521dd
commit d82866555a
3 changed files with 47 additions and 3 deletions

View file

@ -1709,11 +1709,11 @@ impl<T: Input> Scanner<T> {
self.scan_block_scalar_content_line(&mut string, &mut line_buffer);
// break on EOF
self.input.lookahead(2);
if self.input.next_is_z() {
break;
}
self.input.lookahead(2);
self.read_break(&mut leading_break);
// Eat the following indentation spaces and line breaks.

View file

@ -1,9 +1,9 @@
use saphyr_parser::{Event, Parser, ScanError, TScalarStyle};
use saphyr_parser::{BufferedInput, Event, Parser, ScanError, TScalarStyle};
/// Run the parser through the string.
///
/// # Returns
/// This functions returns the events if parsing succeeds, the error the parser returned otherwise.
/// This function returns the events if parsing succeeds, the error the parser returned otherwise.
fn run_parser(input: &str) -> Result<Vec<Event>, ScanError> {
let mut events = vec![];
for x in Parser::new_from_str(input) {
@ -12,6 +12,18 @@ fn run_parser(input: &str) -> Result<Vec<Event>, ScanError> {
Ok(events)
}
/// Run the parser through the string, using a `BufferedInput`
///
/// # Returns
/// This function returns the events if parsing succeeds, the error the parser returned otherwise.
fn run_parser_buffered(input: &str) -> Result<Vec<Event>, ScanError> {
let mut events = vec![];
for x in Parser::new(BufferedInput::new(input.chars())) {
events.push(x?.0);
}
Ok(events)
}
#[test]
#[allow(clippy::too_many_lines)]
fn test_issue1() {
@ -166,3 +178,19 @@ fn test_issue1() {
]
);
}
#[test]
fn test_pr12() {
assert_eq!(
run_parser_buffered("---\n- |\n a").unwrap(),
[
Event::StreamStart,
Event::DocumentStart(true),
Event::SequenceStart(0, None),
Event::Scalar("a\n".to_string(), TScalarStyle::Literal, 0, None),
Event::SequenceEnd,
Event::DocumentEnd,
Event::StreamEnd,
]
);
}

View file

@ -67,3 +67,19 @@ macro_rules! assert_next {
// auto generated from handler_spec_test.cpp
include!("specexamples.rs.inc");
include!("spec_test.rs.inc");
mod with_buffered_input {
use super::{Parser, TestEvent, YamlChecker};
fn str_to_test_events(docs: &str) -> Vec<TestEvent> {
use saphyr_parser::BufferedInput;
let mut p = YamlChecker { evs: Vec::new() };
let input = BufferedInput::new(docs.chars());
let mut parser = Parser::new(input);
parser.load(&mut p, true).unwrap();
p.evs
}
include!("specexamples.rs.inc");
include!("spec_test.rs.inc");
}