iregex_syntax/
build.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
use iregex::automata::{any_char, AnyRange, RangeSet};

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

impl Ast {
	pub fn build(&self) -> iregex::IRegEx {
		let root = self.disjunction.build();

		iregex::IRegEx {
			root,
			prefix: if self.start_anchor {
				iregex::Affix::Anchor
			} else {
				iregex::Affix::Any
			},
			suffix: if self.end_anchor {
				iregex::Affix::Anchor
			} else {
				iregex::Affix::Any
			},
		}
	}
}

impl Disjunction {
	pub fn build(&self) -> iregex::Alternation {
		self.iter().map(Sequence::build).collect()
	}
}

impl Sequence {
	pub fn build(&self) -> iregex::Concatenation {
		self.iter().map(Atom::build).collect()
	}
}

impl Atom {
	pub fn build(&self) -> iregex::Atom {
		match self {
			Self::Any => iregex::Atom::Token(any_char()),
			Self::Char(c) => iregex::Atom::Token(RangeSet::from_iter([*c])),
			Self::Set(set) => iregex::Atom::Token(set.build()),
			Self::Group(g) => iregex::Atom::alternation(g.build()),
			Self::Repeat(atom, repeat) => iregex::Atom::Repeat(atom.build().into(), repeat.build()),
		}
	}
}

impl Classes {
	pub fn build(&self) -> iregex::automata::RangeSet<char> {
		let mut result = iregex::automata::RangeSet::new();

		for c in self {
			result.extend(c.build());
		}

		result
	}
}

impl Class {
	pub fn build(&self) -> iregex::automata::RangeSet<char> {
		todo!()
	}
}

impl Charset {
	pub fn build(&self) -> iregex::automata::RangeSet<char> {
		let mut result = self.set.clone();
		result.extend(self.classes.build());

		if self.negative {
			return result.gaps().map(AnyRange::cloned).collect();
		} else {
			result
		}
	}
}

impl Repeat {
	pub fn build(&self) -> iregex::Repeat {
		iregex::Repeat {
			min: self.min,
			max: self.max,
		}
	}
}