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
#[macro_export]
macro_rules! stmt_simple {
    ( $i:expr, $cmd: expr ) => ({
        use nom::multispace;

        chaining_parser!($i, 0usize,
            tag!("{%")          ~
            l: opt!(tag!("-"))  ~
            many0!(multispace)  ~
            tag!($cmd)          ~
            many0!(multispace)  ~
            r: opt!(tag!("-"))  ~
            tag!("%}")          ,
            || SimpleStatement { strip_left: l.is_some(), strip_right: r.is_some() }
        )
    });
}


#[macro_export]
macro_rules! stmt_named {
    ( $i:expr, $cmd: expr ) => ({
        use nom::multispace;
        use parser::expressions::identifier;

        chaining_parser!($i, 0usize,
            tag!("{%")          ~
            l: opt!(tag!("-"))  ~
            many0!(multispace)  ~
            tag!($cmd)          ~
            many0!(multispace)  ~
            e: identifier       ~
            many0!(multispace)  ~
            r: opt!(tag!("-"))  ~
            tag!("%}")          ,
            || NamedStatement { strip_left: l.is_some(), strip_right: r.is_some(), name: e }
        )
    });
}


#[macro_export]
macro_rules! stmt_expr {
    ( $i:expr, $cmd: expr ) => ({
        use nom::multispace;
        use parser::expressions::full_expression;

        chaining_parser!($i, 0usize,
            tag!("{%")          ~
            l: opt!(tag!("-"))  ~
            many0!(multispace)  ~
            tag!($cmd)          ~
            many0!(multispace)  ~
            e: full_expression  ~
            many0!(multispace)  ~
            r: opt!(tag!("-"))  ~
            tag!("%}")          ,
            || ExprStatement { strip_left: l.is_some(), strip_right: r.is_some(), expression: e }
        )
    });
}


/// `take_till!(T -> bool) => &[T] -> IResult<&[T], &[T]>`
/// returns the longest list of bytes until the provided function succeeds
///
/// The argument is either a function `&[T] -> bool` or a macro returning a `bool
#[macro_export]
macro_rules! take_till_slc (
  ($input:expr, $submac:ident!( $($args:tt)* )) => (
    {
      use nom::InputLength;
      match $input.iter().enumerate().position(|(i, _)| $submac!(&$input[i..], $($args)*)) {
        Some(n) => IResult::Done(&$input[n..], &$input[..n]),
        None    => IResult::Done(&$input[($input).input_len()..], $input)
      }
    }
  );
  ($input:expr, $f:expr) => (
    take_till_slc!($input, call!($f));
  );
);