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
//
// YANG - Yet Another Next Generation
//   The parser and libraries supporting RFC7950.
//
//   Copyright (C) 2021 Toshiaki Takada
//

pub mod arg;
pub mod compound;
pub mod config;
pub mod core;
pub mod error;
pub mod parser;
pub mod stmt;
pub mod substmt;

#[macro_use]
extern crate lazy_static;
extern crate derive_getters;

#[macro_export]
macro_rules! collect_a_stmt {
    ($stmts:expr, $st:ident) => {
        match $stmts.get_mut(<$st>::keyword()) {
            Some(v) => match v.pop() {
                Some(en) => match en {
                    YangStmt::$st(stmt) => Ok(stmt),
                    _ => Err(YangError::MissingStatement(<$st>::keyword())),
                },
                None => Err(YangError::MissingStatement(<$st>::keyword())),
            },
            None => Err(YangError::MissingStatement(<$st>::keyword())),
        }
    };
}

#[macro_export]
macro_rules! collect_vec_stmt {
    ($stmts:expr, $st:ident) => {
        match $stmts.get_mut(<$st>::keyword()) {
            Some(v) => {
                let mut error = false;
                let mut w = Vec::new();
                for en in v.drain(..) {
                    if let YangStmt::$st(stmt) = en {
                        w.push(stmt)
                    } else {
                        error = true;
                    }
                }

                if error {
                    Err(YangError::PlaceHolder)
                } else {
                    Ok(w)
                }
            }
            None => Ok(Vec::new()),
        }
    };
}

#[macro_export]
macro_rules! collect_opt_stmt {
    ($stmts:expr, $st:ident) => {
        match $stmts.get_mut(<$st>::keyword()) {
            Some(v) => match v.pop() {
                Some(en) => match en {
                    YangStmt::$st(stmt) => Ok(Some(stmt)),
                    _ => Err(YangError::MissingStatement(<$st>::keyword())),
                },
                None => Ok(None),
            },
            None => Ok(None),
        }
    };
}

#[macro_export]
macro_rules! parse_a_stmt {
    ($st:ident, $parser:ident) => {
        match <$st>::parse($parser)? {
            YangStmt::$st(stmt) => Ok(stmt),
            _ => Err(YangError::MissingStatement(String::from(<$st>::keyword()))),
        }
    };
}