1use nom::{
2 IResult,
3 branch::alt,
4 bytes::complete::{take_while, take_while1, is_not},
5 character::complete::{char, line_ending},
6 combinator::eof,
7 multi::{many0, many1},
8 sequence::{preceded, terminated, delimited},
9 Parser,
10};
11use std::cell::RefCell;
12
13#[derive(Debug, Clone, PartialEq)]
14pub struct Link {
15 pub id: Option<String>,
16 pub values: Vec<Link>,
17 pub children: Vec<Link>,
18}
19
20impl Link {
21 pub fn new_point(id: String) -> Self {
22 Link {
23 id: Some(id),
24 values: vec![],
25 children: vec![],
26 }
27 }
28
29 pub fn new_value(values: Vec<Link>) -> Self {
30 Link {
31 id: None,
32 values,
33 children: vec![],
34 }
35 }
36
37 pub fn new_link(id: Option<String>, values: Vec<Link>) -> Self {
38 Link {
39 id,
40 values,
41 children: vec![],
42 }
43 }
44
45 pub fn with_children(mut self, children: Vec<Link>) -> Self {
46 self.children = children;
47 self
48 }
49}
50
51pub struct ParserState {
52 indentation_stack: RefCell<Vec<usize>>,
53}
54
55impl ParserState {
56 pub fn new() -> Self {
57 ParserState {
58 indentation_stack: RefCell::new(vec![0]),
59 }
60 }
61
62 pub fn push_indentation(&self, indent: usize) {
63 self.indentation_stack.borrow_mut().push(indent);
64 }
65
66 pub fn pop_indentation(&self) {
67 let mut stack = self.indentation_stack.borrow_mut();
68 if stack.len() > 1 {
69 stack.pop();
70 }
71 }
72
73 pub fn current_indentation(&self) -> usize {
74 *self.indentation_stack.borrow().last().unwrap_or(&0)
75 }
76
77 pub fn check_indentation(&self, indent: usize) -> bool {
78 indent == self.current_indentation()
79 }
80}
81
82fn is_whitespace_char(c: char) -> bool {
83 c == ' ' || c == '\t' || c == '\n' || c == '\r'
84}
85
86fn is_horizontal_whitespace(c: char) -> bool {
87 c == ' ' || c == '\t'
88}
89
90fn is_reference_char(c: char) -> bool {
91 !is_whitespace_char(c) && c != '(' && c != ':' && c != ')'
92}
93
94fn horizontal_whitespace(input: &str) -> IResult<&str, &str> {
95 take_while(is_horizontal_whitespace)(input)
96}
97
98fn whitespace(input: &str) -> IResult<&str, &str> {
99 take_while(is_whitespace_char)(input)
100}
101
102fn simple_reference(input: &str) -> IResult<&str, String> {
103 take_while1(is_reference_char)
104 .map(|s: &str| s.to_string())
105 .parse(input)
106}
107
108fn double_quoted_reference(input: &str) -> IResult<&str, String> {
109 delimited(
110 char('"'),
111 is_not("\""),
112 char('"')
113 )
114 .map(|s: &str| s.to_string())
115 .parse(input)
116}
117
118fn single_quoted_reference(input: &str) -> IResult<&str, String> {
119 delimited(
120 char('\''),
121 is_not("'"),
122 char('\'')
123 )
124 .map(|s: &str| s.to_string())
125 .parse(input)
126}
127
128fn reference(input: &str) -> IResult<&str, String> {
129 alt((
130 double_quoted_reference,
131 single_quoted_reference,
132 simple_reference,
133 )).parse(input)
134}
135
136fn eol(input: &str) -> IResult<&str, &str> {
137 alt((
138 preceded(horizontal_whitespace, line_ending),
139 preceded(horizontal_whitespace, eof),
140 )).parse(input)
141}
142
143fn point_link(input: &str) -> IResult<&str, Link> {
144 reference.map(Link::new_point).parse(input)
145}
146
147fn single_line_point_link(input: &str) -> IResult<&str, Link> {
148 preceded(horizontal_whitespace, point_link).parse(input)
149}
150
151fn multi_line_point_link(input: &str) -> IResult<&str, Link> {
152 delimited(
153 (char('('), whitespace),
154 point_link,
155 (whitespace, char(')'))
156 ).parse(input)
157}
158
159fn reference_or_link<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, Link> {
160 alt((
161 |i| multi_line_any_link(i, state),
162 reference.map(Link::new_point),
163 )).parse(input)
164}
165
166fn multi_line_value_and_whitespace<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, Link> {
167 terminated(
168 |i| reference_or_link(i, state),
169 whitespace
170 ).parse(input)
171}
172
173fn multi_line_values<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, Vec<Link>> {
174 preceded(
175 whitespace,
176 many0(|i| multi_line_value_and_whitespace(i, state))
177 ).parse(input)
178}
179
180fn single_line_value_and_whitespace<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, Link> {
181 preceded(
182 horizontal_whitespace,
183 |i| reference_or_link(i, state)
184 ).parse(input)
185}
186
187fn single_line_values<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, Vec<Link>> {
188 many1(|i| single_line_value_and_whitespace(i, state)).parse(input)
189}
190
191fn single_line_link<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, Link> {
192 (
193 horizontal_whitespace,
194 reference,
195 horizontal_whitespace,
196 char(':'),
197 |i| single_line_values(i, state)
198 ).map(|(_, id, _, _, values)| Link::new_link(Some(id), values))
199 .parse(input)
200}
201
202fn multi_line_link<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, Link> {
203 (
204 char('('),
205 whitespace,
206 reference,
207 whitespace,
208 char(':'),
209 |i| multi_line_values(i, state),
210 whitespace,
211 char(')')
212 ).map(|(_, _, id, _, _, values, _, _)| Link::new_link(Some(id), values))
213 .parse(input)
214}
215
216fn single_line_value_link<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, Link> {
217 (|i| single_line_values(i, state))
218 .map(Link::new_value)
219 .parse(input)
220}
221
222fn multi_line_value_link<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, Link> {
223 (
224 char('('),
225 |i| multi_line_values(i, state),
226 whitespace,
227 char(')')
228 ).map(|(_, values, _, _)| Link::new_value(values))
229 .parse(input)
230}
231
232fn multi_line_any_link<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, Link> {
233 alt((
234 multi_line_point_link,
235 |i| multi_line_value_link(i, state),
236 |i| multi_line_link(i, state),
237 )).parse(input)
238}
239
240fn single_line_any_link<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, Link> {
241 alt((
242 terminated(|i| single_line_link(i, state), eol),
243 terminated(single_line_point_link, eol),
244 terminated(|i| single_line_value_link(i, state), eol),
245 )).parse(input)
246}
247
248fn any_link<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, Link> {
249 alt((
250 terminated(|i| multi_line_any_link(i, state), eol),
251 |i| single_line_any_link(i, state),
252 )).parse(input)
253}
254
255fn count_indentation(input: &str) -> IResult<&str, usize> {
256 take_while(|c| c == ' ')
257 .map(|s: &str| s.len())
258 .parse(input)
259}
260
261fn push_indentation<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, ()> {
262 let (input, spaces) = count_indentation(input)?;
263 let current = state.current_indentation();
264
265 if spaces > current {
266 state.push_indentation(spaces);
267 Ok((input, ()))
268 } else {
269 Err(nom::Err::Error(nom::error::Error::new(input, nom::error::ErrorKind::Verify)))
270 }
271}
272
273fn check_indentation<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, ()> {
274 let (input, spaces) = count_indentation(input)?;
275
276 if state.check_indentation(spaces) {
277 Ok((input, ()))
278 } else {
279 Err(nom::Err::Error(nom::error::Error::new(input, nom::error::ErrorKind::Verify)))
280 }
281}
282
283fn element<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, Link> {
284 let (input, link) = any_link(input, state)?;
285
286 if let Ok((input, _)) = push_indentation(input, state) {
287 let (input, children) = links(input, state)?;
288 Ok((input, link.with_children(children)))
289 } else {
290 Ok((input, link))
291 }
292}
293
294fn first_line<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, Link> {
295 element(input, state)
296}
297
298fn line<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, Link> {
299 preceded(
300 |i| check_indentation(i, state),
301 |i| element(i, state)
302 ).parse(input)
303}
304
305fn links<'a>(input: &'a str, state: &ParserState) -> IResult<&'a str, Vec<Link>> {
306 let (input, first) = first_line(input, state)?;
307 let (input, rest) = many0(|i| line(i, state)).parse(input)?;
308
309 state.pop_indentation();
310
311 let mut result = vec![first];
312 result.extend(rest);
313 Ok((input, result))
314}
315
316pub fn parse_document(input: &str) -> IResult<&str, Vec<Link>> {
317 let state = ParserState::new();
318
319 let input = input.trim_start_matches(|c: char| c == '\n' || c == '\r');
321
322 if input.trim().is_empty() {
324 return Ok(("", vec![]));
325 }
326
327 let (input, result) = links(input, &state)?;
328 let (input, _) = whitespace(input)?;
329 let (input, _) = eof(input)?;
330
331 Ok((input, result))
332}
333