-
Notifications
You must be signed in to change notification settings - Fork 0
/
chars.rs
121 lines (106 loc) · 3.08 KB
/
chars.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
use std::ops::Deref;
use std::ops::DerefMut;
use crate::core::into_parser::IntoParser;
use crate::core::parser::Parser;
use crate::core::result::ParseResult;
use crate::core::tuple::Tuple;
use crate::leaf::panic::Panic;
pub struct DynBoxChars<Output>
where
Output: Tuple,
{
parser: std::boxed::Box<dyn for<'a> Parser<std::str::Chars<'a>, Output = Output>>,
}
impl<Output> DynBoxChars<Output>
where
Output: Tuple,
{
pub fn new<ParserType: IntoParser>(parser: ParserType) -> Self
where
ParserType::Into: for<'a> Parser<std::str::Chars<'a>, Output = Output> + 'static,
{
Self {
parser: std::boxed::Box::new(parser.into_parser()),
}
}
pub fn assign<ParserType: IntoParser>(&mut self, parser: ParserType)
where
ParserType::Into: for<'a> Parser<std::str::Chars<'a>, Output = Output> + 'static,
{
self.parser = std::boxed::Box::new(parser.into_parser());
}
}
/// default to dummy parser that always panic
impl<Output: Tuple + 'static> Default for DynBoxChars<Output> {
fn default() -> Self {
Self::new(Panic::new())
}
}
impl<'a, Output> Parser<std::str::Chars<'a>> for DynBoxChars<Output>
where
Output: Tuple,
{
type Output = Output;
fn parse(&self, it: std::str::Chars<'a>) -> ParseResult<Self::Output, std::str::Chars<'a>> {
self.parser.parse(it)
}
fn match_pattern(&self, it: std::str::Chars<'a>) -> ParseResult<(), std::str::Chars<'a>> {
self.parser.match_pattern(it)
}
}
impl<Output> Deref for DynBoxChars<Output>
where
Output: Tuple,
{
type Target = std::boxed::Box<dyn for<'a> Parser<std::str::Chars<'a>, Output = Output>>;
fn deref(&self) -> &Self::Target {
&self.parser
}
}
impl<Output> DerefMut for DynBoxChars<Output>
where
Output: Tuple,
{
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.parser
}
}
impl<Output> IntoParser for DynBoxChars<Output>
where
Output: Tuple,
{
type Into = Self;
fn into_parser(self) -> Self::Into {
self
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::leaf::singleeq::SingleEqualParser;
use crate::leaf::singlerange::SingleRangeParser;
#[test]
fn success1() {
let digit_parser = SingleRangeParser::from('0'..='9');
let a_parser = SingleEqualParser::new('a');
let str = "1a2b3c4d5e6f7g8h9i0j";
let mut boxed: DynBoxChars<(char,)> = DynBoxChars::new(digit_parser);
let res = boxed.parse(str.chars());
let rest: String = res.it.clone().collect();
assert_eq!(res.output, Some(('1',)));
assert_eq!(rest, "a2b3c4d5e6f7g8h9i0j");
// set another parser to same variable
boxed.assign(a_parser);
let res = boxed.parse(res.it);
let rest: String = res.it.collect();
assert_eq!(res.output, Some(('a',)));
assert_eq!(rest, "2b3c4d5e6f7g8h9i0j");
}
#[test]
#[should_panic]
fn panic_test() {
let boxed: DynBoxChars<(i32,)> = Default::default();
boxed.parse("123".chars());
boxed.match_pattern("123".chars());
}
}