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
use crate::primitives::*;
use nom::character::complete::multispace0;
use nom::IResult;
use nom::{multi::many_till, sequence::tuple};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", tag = "type")]
pub enum Stanza<'a> {
#[serde(borrow)]
Comment(Line<'a>),
Line { line: Line<'a>, atoms: Vec<Atom> },
Block {
prefix: Line<'a>,
prefix_atoms: Vec<Atom>,
l_brace: Token<'a>,
r_brace: Token<'a>,
nested: Vec<Stanza<'a>>,
},
}
pub fn parse_stanza(i: Span) -> IResult<Span, Stanza> {
let (i, _) = multispace0(i)?;
if let Ok((s, c)) = unary_comment(i) {
return Ok((s, Stanza::Comment(c)));
}
let (s, l) = line(i)?;
let atoms = l
.atoms()
.map_err(|_e| nom::Err::Error(nom::error::Error::new(s, nom::error::ErrorKind::Satisfy)))?
.clone();
if let Ok((s, lb)) = open_brace(s) {
let (s, (set, (_, rb))) = many_till(parse_stanza, tuple((multispace0, close_brace)))(s)?;
Ok((
s,
Stanza::Block {
prefix: l,
prefix_atoms: atoms,
l_brace: lb,
r_brace: rb,
nested: set,
},
))
} else {
Ok((s, Stanza::Line { line: l, atoms }))
}
}