1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use crate::not_starting_with;
use chumsky::prelude::*;

/// Parses any non-empty string that does not contain any of the patterns as a substring.
///
/// # Panics
///
/// If any of the patterns is the empty string (`""`).
pub fn not_containing<'a, I, E>(patterns: I) -> impl Parser<char, String, Error = E> + 'a
where
	I: IntoIterator<Item = &'a str>,
	E: chumsky::Error<char> + 'a
{
	not_starting_with(patterns)
		.repeated()
		.at_least(1)
		.map(|vecs| vecs.into_iter().flatten().collect())
}

#[cfg(test)]
mod tests {
	use super::*;

	fn test_lexer() -> impl Parser<char, String, Error = Simple<char>> {
		not_containing(["{%", "{{"]).then_ignore(end())
	}

	#[test]
	fn test_not_containing_other_chars() {
		let parsed = test_lexer().parse("foo");
		assert_eq!(parsed, Ok("foo".to_owned()));
	}

	#[test]
	fn test_not_containing_first_char() {
		let parsed = test_lexer().parse("foo{bar");
		assert_eq!(parsed, Ok("foo{bar".to_owned()));
	}

	#[test]
	fn test_not_containing_first_char_first() {
		let parsed = test_lexer().parse("{bar");
		assert_eq!(parsed, Ok("{bar".to_owned()));
	}

	#[test]
	fn test_not_containing_first_char_last() {
		let parsed = test_lexer().parse("foo{");
		assert_eq!(parsed, Ok("foo{".to_owned()));
	}

	#[test]
	fn test_not_containing_second_char() {
		let parsed = test_lexer().parse("foo%bar");
		assert_eq!(parsed, Ok("foo%bar".to_owned()));
	}

	#[test]
	fn test_not_containing_second_char_first() {
		let parsed = test_lexer().parse("%bar");
		assert_eq!(parsed, Ok("%bar".to_owned()));
	}

	#[test]
	fn test_not_containing_second_char_last() {
		let parsed = test_lexer().parse("foo%");
		assert_eq!(parsed, Ok("foo%".to_owned()));
	}

	#[test]
	fn test_containing_first_pattern() {
		let parsed = test_lexer().parse("foo{%bar");
		assert_matches!(parsed, Err(_));
	}

	#[test]
	fn test_containing_first_pattern_first() {
		let parsed = test_lexer().parse("{%bar");
		assert_matches!(parsed, Err(_));
	}

	#[test]
	fn test_containing_first_pattern_last() {
		let parsed = test_lexer().parse("foo{%");
		assert_matches!(parsed, Err(_));
	}

	#[test]
	fn test_containing_second_pattern() {
		let parsed = test_lexer().parse("foo{{bar");
		assert_matches!(parsed, Err(_));
	}

	#[test]
	fn test_containing_second_pattern_first() {
		let parsed = test_lexer().parse("{{bar");
		assert_matches!(parsed, Err(_));
	}

	#[test]
	fn test_containing_second_pattern_last() {
		let parsed = test_lexer().parse("foo{{");
		assert_matches!(parsed, Err(_));
	}
}