From 2430451e6950780f3a52ddf36fa3347687d8663a Mon Sep 17 00:00:00 2001 From: mochi-neko Date: Tue, 20 Feb 2024 16:00:40 +0900 Subject: [PATCH] Ignore a line that has only whitespaces --- src/str_parser.rs | 2 +- src/vtt_parser.rs | 2 +- tests/general.rs | 114 +++++----------------------------------------- 3 files changed, 13 insertions(+), 105 deletions(-) diff --git a/src/str_parser.rs b/src/str_parser.rs index 5c7d204..503fb11 100644 --- a/src/str_parser.rs +++ b/src/str_parser.rs @@ -38,7 +38,7 @@ peg::parser! { /// Multiple lines block of text. rule multiline() -> Vec - = !(whitespace() / newline()) lines:$((!newline() [_])+ newline()) ++ () + = !(whitespace() / newline()) lines:$(!(whitespace()+ newline()) (!newline() [_])+ newline()) ++ () { lines .iter() diff --git a/src/vtt_parser.rs b/src/vtt_parser.rs index f48d8ed..7a88b58 100644 --- a/src/vtt_parser.rs +++ b/src/vtt_parser.rs @@ -120,7 +120,7 @@ peg::parser! { /// Multiple lines. rule multiline() -> Vec - = !(whitespace() / newline()) lines:$((!newline() [_])+ newline()) ++ () + = !(whitespace() / newline()) lines:$(!(whitespace()+ newline()) (!newline() [_])+ newline()) ++ () { lines .iter() diff --git a/tests/general.rs b/tests/general.rs index e4e413c..4b83cd8 100644 --- a/tests/general.rs +++ b/tests/general.rs @@ -3,33 +3,9 @@ peg::parser! { /// Whitespace. pub(crate) rule whitespace() = [' ' | '\t'] - /// Zero or more whitespaces. - pub(crate) rule whitespaces() = quiet!{ whitespace()* } - - /// One or more whitespaces. - pub(crate) rule some_whitespaces() = whitespace()+ - /// Newline. pub(crate) rule newline() = "\r\n" / "\n" / "\r" - /// Zero or more newlines. - pub(crate) rule newlines() = quiet!{ newline()* } - - /// One or more newlines. - pub(crate) rule some_newlines() = newline()+ - - /// Whitespace or newline. - pub(crate) rule whitespace_or_newline() = [' ' | '\t' | '\r' | '\n'] - - /// Zero or more whitespaces or newlines. - pub(crate) rule whitespaces_or_newlines() = quiet!{ whitespace_or_newline()* } - - /// One or more whitespaces or one newline. - pub(crate) rule some_whitespaces_or_newline() = some_whitespaces() / newline() - - /// One or more whitespaces or newlines. - pub(crate) rule some_whitespaces_or_newlines() = whitespace_or_newline()+ - /// Any-digit number. pub(crate) rule number() -> u32 = n:$(['0'..='9']+) {? @@ -94,7 +70,7 @@ peg::parser! { /// Sequential text without space of newline. pub(crate) rule sequence() -> String - = t:$((!whitespace_or_newline() [_])+) + = t:$((!(whitespace() / newline()) [_])+) { t.to_string() } @@ -108,25 +84,19 @@ peg::parser! { /// Single text with newline. pub(crate) rule line() -> String - = !whitespace_or_newline() t:$((!newline() [_])+) newline() + = !(whitespace() / newline()) t:$((!newline() [_])+) newline() { t.to_string().trim().to_string() } /// Multiple lines block of text. pub(crate) rule multiline() -> Vec - = !whitespace_or_newline() lines:$((!newline() [_])+ newline()) ** () - {? - let lines = lines + = !(whitespace() / newline()) lines:$(!(whitespace()+ newline()) (!newline() [_])+ newline()) ++ () + { + lines .iter() .map(|l| l.to_string().trim().to_string()) - .collect::>(); - - if !lines.is_empty() { - Ok(lines) - } else { - Err("Empty multiline") - } + .collect() } } } @@ -140,24 +110,6 @@ mod test { assert!(super::rules::whitespace("a").is_err()); } - #[test] - fn whitespaces() { - assert!(super::rules::whitespaces("").is_ok()); - assert!(super::rules::whitespaces(" ").is_ok()); - assert!(super::rules::whitespaces(" ").is_ok()); - assert!(super::rules::whitespaces(" ").is_ok()); - assert!(super::rules::whitespaces("a").is_err()); - } - - #[test] - fn some_whitespaces() { - assert!(super::rules::some_whitespaces("").is_err()); - assert!(super::rules::some_whitespaces(" ").is_ok()); - assert!(super::rules::some_whitespaces(" ").is_ok()); - assert!(super::rules::some_whitespaces(" ").is_ok()); - assert!(super::rules::some_whitespaces("a").is_err()); - } - #[test] fn newline() { assert!(super::rules::newline("\n").is_ok()); @@ -169,55 +121,6 @@ mod test { assert!(super::rules::newline("a").is_err()); } - #[test] - fn newlines() { - assert!(super::rules::newlines("").is_ok()); - assert!(super::rules::newlines("\n").is_ok()); - assert!(super::rules::newlines("\n\n").is_ok()); - assert!(super::rules::newlines("a").is_err()); - } - - #[test] - fn some_newlines() { - assert!(super::rules::some_newlines("").is_err()); - assert!(super::rules::some_newlines("\n").is_ok()); - assert!(super::rules::some_newlines("\n\n").is_ok()); - assert!(super::rules::some_newlines("a").is_err()); - } - - #[test] - fn some_whitespaces_or_newline() { - assert!(super::rules::some_whitespaces_or_newline(" ").is_ok()); - assert!(super::rules::some_whitespaces_or_newline(" ").is_ok()); - assert!(super::rules::some_whitespaces_or_newline("\n").is_ok()); - assert!(super::rules::some_whitespaces_or_newline(" \n").is_err()); - assert!(super::rules::some_whitespaces_or_newline("\n ").is_err()); - assert!(super::rules::some_whitespaces_or_newline("\n\n").is_err()); - assert!(super::rules::some_whitespaces_or_newline("a").is_err()); - } - - #[test] - fn whitespaces_or_newlines() { - assert!(super::rules::whitespaces_or_newlines("").is_ok()); - assert!(super::rules::whitespaces_or_newlines(" ").is_ok()); - assert!(super::rules::whitespaces_or_newlines("\n").is_ok()); - assert!(super::rules::whitespaces_or_newlines("\n ").is_ok()); - assert!(super::rules::whitespaces_or_newlines(" ").is_ok()); - assert!(super::rules::whitespaces_or_newlines("\n\n").is_ok()); - assert!(super::rules::whitespaces_or_newlines("a").is_err()); - } - - #[test] - fn some_whitespaces_or_newlines() { - assert!(super::rules::some_whitespaces_or_newlines("").is_err()); - assert!(super::rules::some_whitespaces_or_newlines(" ").is_ok()); - assert!(super::rules::some_whitespaces_or_newlines("\n").is_ok()); - assert!(super::rules::some_whitespaces_or_newlines("\n ").is_ok()); - assert!(super::rules::some_whitespaces_or_newlines(" ").is_ok()); - assert!(super::rules::some_whitespaces_or_newlines("\n\n").is_ok()); - assert!(super::rules::some_whitespaces_or_newlines("a").is_err()); - } - #[test] fn number() { assert_eq!(super::rules::number("0").unwrap(), 0); @@ -596,11 +499,16 @@ mod test { assert!(super::rules::multiline("").is_err()); assert!(super::rules::multiline("Hello, world!").is_err()); + assert!(super::rules::multiline(" Hello, world!\n").is_err()); assert!(super::rules::multiline("\nHello, world!\n").is_err()); assert!( super::rules::multiline("Hello, world!\nThis is a test.\n\n") .is_err() ); + assert!( + super::rules::multiline("Hello, world!\nThis is a test.\n \n") + .is_err() + ); assert!(super::rules::multiline("some\ntext\n\nover\nline").is_err()); } }