iregex_syntax/
display.rs

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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use core::fmt;
use iregex::automata::AnyRange;
use std::fmt::Write;

use crate::{Ast, Atom, Charset, Disjunction, Repeat, Sequence};

impl fmt::Display for Ast {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		if self.start_anchor {
			f.write_char('^')?;
		}

		self.disjunction.fmt(f)?;

		if self.end_anchor {
			f.write_char('$')
		} else {
			Ok(())
		}
	}
}

impl fmt::Display for Disjunction {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		for (i, sequence) in self.iter().enumerate() {
			if i > 0 {
				f.write_char('|')?;
			}

			sequence.fmt(f)?;
		}

		Ok(())
	}
}

impl fmt::Display for Sequence {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		for atom in self {
			atom.fmt(f)?;
		}

		Ok(())
	}
}

impl fmt::Display for Atom {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		match self {
			Self::Any => f.write_char('.'),
			Self::Char(c) => fmt_char(*c, f),
			Self::Set(charset) => charset.fmt(f),
			Self::Repeat(atom, repeat) => {
				atom.fmt(f)?;
				repeat.fmt(f)
			}
			Self::Group(g) => {
				f.write_char('(')?;
				g.fmt(f)?;
				f.write_char(')')
			}
		}
	}
}

impl fmt::Display for Charset {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		if self.negative {
			f.write_char('^')?;
		}

		for &range in &self.set {
			fmt_range(range, f)?
		}

		Ok(())
	}
}

impl fmt::Display for Repeat {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		if self.min == 0 && self.max == Some(1) {
			f.write_char('?')
		} else if self.min == 0 && self.max.is_none() {
			f.write_char('*')
		} else if self.min == 1 && self.max.is_some() {
			f.write_char('+')
		} else {
			match self.max {
				Some(max) => {
					if self.min == max {
						write!(f, "{{{}}}", self.min)
					} else {
						write!(f, "{{{},{}}}", self.min, max)
					}
				}
				None => write!(f, "{{{},}}", self.min),
			}
		}
	}
}

pub fn fmt_range(range: AnyRange<char>, f: &mut fmt::Formatter) -> fmt::Result {
	if range.len() == 1 {
		fmt_char(range.first().unwrap(), f)
	} else {
		let a = range.first().unwrap();
		let b = range.last().unwrap();

		fmt_char(a, f)?;
		if a as u32 + 1 < b as u32 {
			write!(f, "-")?;
		}
		fmt_char(b, f)
	}
}

pub fn fmt_char(c: char, f: &mut fmt::Formatter) -> fmt::Result {
	match c {
		'(' => write!(f, "\\("),
		')' => write!(f, "\\)"),
		'[' => write!(f, "\\["),
		']' => write!(f, "\\]"),
		'{' => write!(f, "\\{{"),
		'}' => write!(f, "\\}}"),
		'?' => write!(f, "\\?"),
		'*' => write!(f, "\\*"),
		'+' => write!(f, "\\+"),
		'-' => write!(f, "\\-"),
		'^' => write!(f, "\\^"),
		'|' => write!(f, "\\|"),
		'\\' => write!(f, "\\\\"),
		'\0' => write!(f, "\\0"),
		'\x07' => write!(f, "\\a"),
		'\x08' => write!(f, "\\b"),
		'\t' => write!(f, "\\t"),
		'\n' => write!(f, "\\n"),
		'\x0b' => write!(f, "\\v"),
		'\x0c' => write!(f, "\\f"),
		'\r' => write!(f, "\\r"),
		'\x1b' => write!(f, "\\e"),
		_ => fmt::Display::fmt(&c, f),
	}
}

// #[cfg(test)]
// mod tests {
// 	// Each pair is of the form `(regexp, formatted)`.
// 	// We check that the regexp is correctly parsed by formatting it and
// 	// checking that it matches the expected `formatted` string.
// 	const TESTS: &[(&str, &str)] = &[
// 		("a*", "a*"),
// 		("a\\*", "a\\*"),
// 		("[cab]", "[a-c]"),
// 		("[^cab]", "[^a-c]"),
// 		("(abc)|de", "abc|de"),
// 		("(a|b)?", "(a|b)?"),
// 		("[A-Za-z0-89]", "[0-9A-Za-z]"),
// 		("[a|b]", "[ab\\|]"),
// 	];

// 	#[test]
// 	fn test() {
// 		for &(regexp, formatted) in TESTS {
// 			assert_eq!(
// 				super::Ast::parse(regexp.chars()).unwrap().to_string(),
// 				*formatted
// 			)
// 		}
// 	}
// }