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
use crate::parser::Parser;
use crate::syntax::sigil::Comma;
use crate::syntax::Syntax;
use lark_collections::Seq;
use lark_debug_derive::DebugWith;
use lark_error::ErrorReported;
#[derive(DebugWith)]
pub struct CommaList<T>(pub T);
impl<T> CommaList<T> {
fn element(&mut self) -> &mut T {
&mut self.0
}
}
impl<T> Syntax<'parse> for CommaList<T>
where
T: Syntax<'parse>,
{
type Data = Seq<T::Data>;
fn test(&mut self, parser: &Parser<'parse>) -> bool {
SeparatedList(self.element(), Comma).test(parser)
}
fn expect(&mut self, parser: &mut Parser<'parse>) -> Result<Seq<T::Data>, ErrorReported> {
SeparatedList(self.element(), Comma).expect(parser)
}
}
#[derive(DebugWith)]
pub struct SeparatedList<T, S>(pub T, pub S);
impl<T, S> SeparatedList<T, S> {
fn element(&self) -> &T {
&self.0
}
fn separator(&self) -> &S {
&self.1
}
}
impl<T, S> Syntax<'parse> for SeparatedList<T, S>
where
T: Syntax<'parse>,
S: Syntax<'parse>,
{
type Data = Seq<T::Data>;
fn test(&mut self, _parser: &Parser<'parse>) -> bool {
true
}
fn expect(&mut self, parser: &mut Parser<'parse>) -> Result<Seq<T::Data>, ErrorReported> {
let SeparatedList(element, delimiter) = self;
let mut result = vec![];
parser.skip_newlines();
loop {
if let Some(element) = parser.parse_if_present(&mut *element) {
result.push(element?);
if let Some(_) = parser.parse_if_present(&mut *delimiter) {
parser.skip_newlines();
continue;
} else if parser.skip_newlines() {
continue;
} else {
break;
}
} else {
break;
}
}
Ok(Seq::from(result))
}
}