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
use std::str;
#[allow(unused_imports)]
use nom::{IResult, Err as NomErr, ErrorKind, alpha, alphanumeric, space, multispace};

use container::expression::Mustache;
use container::parsed::ParsedNode;

use parser::statements::statement;
use parser::statements::stmt_edge;
use parser::expressions::full_expression;


named!(pub text<&[u8], Vec<ParsedNode> >, terminated!( nodes, eof!() ) );


pub fn nodes(input: &[u8]) -> IResult<&[u8], Vec<ParsedNode> > {
    let (i, list) = try_parse!(input, many0!(node) );
    IResult::Done(i, list)
}



named!(node<&[u8], ParsedNode>,
    alt!(
        plain_text          |
        comment             |
        mustache            |
        statement
    )
);


fn comment(input: &[u8]) -> IResult<&[u8], ParsedNode> {
    let (i, (_, comment, _)) = try_parse!(input,
        tuple!(
            tag!("{#"),
            map_res!(
                take_until!("#}"),
                str::from_utf8
            ),
            tag!("#}")
        )
    );
    IResult::Done(i, ParsedNode::Comment(comment.to_owned()))
}


fn mustache(input: &[u8]) -> IResult<&[u8], ParsedNode> {
    let (i, (_, _, fe, _, _)) = try_parse!(input,
        tuple!(
            tag!("{{"),
            many0!(multispace),
            full_expression,
            many0!(multispace),
            tag!("}}")
        )
    );
    IResult::Done(i, Mustache::new(fe).into())
}


fn plain_text(input: &[u8]) -> IResult<&[u8], ParsedNode> {
    #[cfg_attr(feature = "clippy", allow(needless_lifetimes))]
    fn try_brace<'a>(input: &'a [u8]) -> IResult<&[u8], &str> {
        let b = || -> IResult<&'a [u8], ()> {
            let _ = try_parse!(input, alt!( mustache | comment | stmt_edge ) );
            IResult::Done(input, ())
        };
        match b().is_done() {
            false   => IResult::Done(&input[1..], "{"),
            true    => IResult::Error(NomErr::Code(ErrorKind::Custom(0))),
        }
    }

    let (i, text) = try_parse!(input,
        chain!( v: many1!(
            alt!( map_res!( is_not!("{"), str::from_utf8 ) | try_brace )
        ), || v.join("") )
    );
    IResult::Done(i, ParsedNode::Text(text))
}



// ---------------------------------------------------------------------------


//#[cfg(test)]
//mod tests {
//    #![cfg_attr(feature = "clippy", allow(used_underscore_binding))]
//
//    use nom::IResult::Done;
//
//}