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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use std::collections::HashMap;
use super::core::*;
use super::error::*;
use super::parser::*;
use super::stmt::UnknownStmt;
pub type StmtKeywordFn = fn() -> Keyword;
pub type SelectionKeywordFn = fn() -> Vec<Keyword>;
#[derive(Debug, Clone)]
pub enum SubStmtWith {
Stmt(StmtKeywordFn),
Selection(SelectionKeywordFn),
}
#[derive(Debug, Clone)]
pub enum SubStmtDef {
HasOne(SubStmtWith),
Optional(SubStmtWith),
ZeroOrMore(SubStmtWith),
OneOrMore(SubStmtWith),
}
#[derive(Debug, Clone)]
pub struct RepeatCount {
pub count: usize,
pub min: usize,
pub max: usize,
}
pub struct SubStmtUtil;
impl SubStmtUtil {
pub fn call_stmt_parser(parser: &mut Parser, keyword: &str) -> Result<YangStmt, YangError> {
let f = STMT_PARSER.get(keyword).unwrap();
f(parser)
}
pub fn parse_substmts(
parser: &mut Parser,
def: Vec<SubStmtDef>,
) -> Result<StmtCollection, YangError> {
let mut k2i = HashMap::new();
let mut i2rep = HashMap::new();
let mut i = 0;
for s in def {
let (rep, ssw) = match s {
SubStmtDef::HasOne(ssw) => (
RepeatCount {
count: 0,
min: 1,
max: 1,
},
ssw,
),
SubStmtDef::Optional(ssw) => (
RepeatCount {
count: 0,
min: 0,
max: 1,
},
ssw,
),
SubStmtDef::ZeroOrMore(ssw) => (
RepeatCount {
count: 0,
min: 0,
max: usize::MAX,
},
ssw,
),
SubStmtDef::OneOrMore(ssw) => (
RepeatCount {
count: 0,
min: 1,
max: usize::MAX,
},
ssw,
),
};
i2rep.insert(i, rep);
match ssw {
SubStmtWith::Stmt(func) => {
k2i.insert(func(), i);
}
SubStmtWith::Selection(func) => {
for k in func() {
k2i.insert(k, i);
}
}
}
i += 1;
}
let mut stmts: StmtCollection = HashMap::new();
loop {
let token = parser.get_token()?;
if parser.config().debug() {
println!("*** [DEBUG] parse_substmts_default {:?}", token);
}
match token {
Token::Identifier(ref keyword) => {
if k2i.contains_key(keyword as &str) {
if let Some(rep) = i2rep.get_mut(k2i.get(keyword as &str).unwrap()) {
let stmt = Self::call_stmt_parser(parser, &keyword)?;
let v = match stmts.get_mut(keyword as &str) {
Some(v) => v,
None => {
stmts.insert(keyword.to_string(), Vec::new());
stmts.get_mut(keyword as &str).unwrap()
}
};
v.push(stmt);
rep.count += 1;
} else {
break;
}
} else if !STMT_PARSER.contains_key(keyword as &str) {
let _stmt = UnknownStmt::parse(parser, keyword)?;
} else {
parser.save_token(token);
break;
}
}
_ => {
parser.save_token(token);
break;
}
}
}
for k in stmts.keys() {
match k2i.get(k as &str) {
Some(i) => {
let rep = i2rep.get(i).unwrap();
if rep.count < rep.min {
return Err(YangError::TooFewStatement(k.clone()));
}
if rep.max < rep.count {
return Err(YangError::TooManyStatements(k.clone()));
}
}
None => return Err(YangError::UnexpectedStatement(k.clone())),
}
}
if parser.config().debug() {
println!("*** [DEBUG] end");
}
Ok(stmts)
}
}