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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
use crate::parse::keyword::Keyword;
use crate::parse::rules::*;
use crate::parse::tokenize::*;
use crate::{Error, Result};
#[derive(Clone)]
pub(crate) struct SectionRules<T: Keyword> {
rules: Vec<Option<TokenFmt<T>>>,
}
#[derive(Clone)]
enum TokVal<'a, K: Keyword> {
None,
Some([Item<'a, K>; 1]),
Multi(Vec<Item<'a, K>>),
}
impl<'a, K: Keyword> TokVal<'a, K> {
fn count(&self) -> usize {
match self {
TokVal::None => 0,
TokVal::Some(_) => 1,
TokVal::Multi(v) => v.len(),
}
}
fn first(&self) -> Option<&Item<'a, K>> {
match self {
TokVal::None => None,
TokVal::Some([t]) => Some(t),
TokVal::Multi(v) => Some(&v[0]),
}
}
fn singleton(&self) -> Option<&Item<'a, K>> {
match self {
TokVal::None => None,
TokVal::Some([t]) => Some(t),
TokVal::Multi(_) => None,
}
}
fn as_slice(&self) -> &[Item<'a, K>] {
match self {
TokVal::None => &[],
TokVal::Some(t) => &t[..],
TokVal::Multi(v) => &v[..],
}
}
fn last(&self) -> Option<&Item<'a, K>> {
match self {
TokVal::None => None,
TokVal::Some([t]) => Some(t),
TokVal::Multi(v) => Some(&v[v.len() - 1]),
}
}
}
pub struct Section<'a, T: Keyword> {
v: Vec<TokVal<'a, T>>,
first: Option<T>,
last: Option<T>,
}
impl<'a, T: Keyword> Section<'a, T> {
fn new() -> Self {
let n = T::n_vals();
let mut v = Vec::with_capacity(n);
v.resize(n, TokVal::None);
Section {
v,
first: None,
last: None,
}
}
fn tokval(&self, t: T) -> &TokVal<'a, T> {
let idx = t.idx();
&self.v[idx]
}
pub(crate) fn slice(&self, t: T) -> &[Item<'a, T>] {
self.tokval(t).as_slice()
}
pub(crate) fn get(&self, t: T) -> Option<&Item<'a, T>> {
self.tokval(t).singleton()
}
pub(crate) fn required(&self, t: T) -> Result<&Item<'a, T>> {
self.get(t).ok_or_else(|| Error::MissingToken(t.to_str()))
}
pub(crate) fn maybe<'b>(&'b self, t: T) -> MaybeItem<'b, 'a, T> {
MaybeItem::from_option(self.get(t))
}
pub(crate) fn first_item(&self) -> Option<&Item<'a, T>> {
match self.first {
None => None,
Some(t) => self.tokval(t).first(),
}
}
pub(crate) fn last_item(&self) -> Option<&Item<'a, T>> {
match self.last {
None => None,
Some(t) => self.tokval(t).last(),
}
}
fn add_tok(&mut self, t: T, item: Item<'a, T>) {
let idx = Keyword::idx(t);
if idx >= self.v.len() {
self.v.resize(idx + 1, TokVal::None);
}
let m = &mut self.v[idx];
match m {
TokVal::None => *m = TokVal::Some([item]),
TokVal::Some([x]) => {
*m = TokVal::Multi(vec![x.clone(), item]);
}
TokVal::Multi(ref mut v) => {
v.push(item);
}
};
if self.first.is_none() {
self.first = Some(t);
}
self.last = Some(t);
}
}
impl<T: Keyword> SectionRules<T> {
pub(crate) fn new() -> Self {
let n = T::n_vals();
let mut rules = Vec::with_capacity(n);
rules.resize(n, None);
SectionRules { rules }
}
pub(crate) fn add(&mut self, t: TokenFmtBuilder<T>) {
let rule: TokenFmt<_> = t.into();
let idx = rule.kwd().idx();
assert!(self.rules[idx].is_none());
self.rules[idx] = Some(rule);
}
fn parse_unverified<'a, I>(&self, tokens: &mut I, section: &mut Section<'a, T>) -> Result<()>
where
I: Iterator<Item = Result<Item<'a, T>>>,
{
for item in tokens {
let item = item?;
let tok = item.kwd();
let tok_idx = tok.idx();
if let Some(rule) = &self.rules[tok_idx] {
assert!(rule.kwd() == tok);
section.add_tok(tok, item);
rule.check_multiplicity(section.v[tok_idx].as_slice())?;
} else {
return Err(Error::UnexpectedToken(tok.to_str(), item.pos()));
}
}
Ok(())
}
fn validate<'a>(&self, s: &Section<'a, T>) -> Result<()> {
assert_eq!(s.v.len(), self.rules.len());
for (rule, t) in self.rules.iter().zip(s.v.iter()) {
match rule {
None => {
if t.count() > 0 {
unreachable!(
"This item should have been rejected earlier, in parse_unverified()"
);
}
}
Some(rule) => {
rule.check_multiplicity(t.as_slice())?;
for item in t.as_slice() {
rule.check_item(item)?
}
}
}
}
Ok(())
}
fn validate_objects<'a>(&self, s: &Section<'a, T>, kwd: T) -> Result<()> {
for item in s.slice(kwd).iter() {
let _ = item.obj_raw()?;
}
Ok(())
}
pub(crate) fn parse<'a, I>(&self, tokens: &mut I) -> Result<Section<'a, T>>
where
I: Iterator<Item = Result<Item<'a, T>>>,
{
let mut section = Section::new();
self.parse_unverified(tokens, &mut section)?;
self.validate(§ion)?;
self.validate_objects(§ion, T::unrecognized())?;
self.validate_objects(§ion, T::ann_unrecognized())?;
Ok(section)
}
}
#[cfg(test)]
mod test {
use super::SectionRules;
use crate::parse::keyword::Keyword;
use crate::parse::macros::test::Fruit;
use crate::parse::tokenize::{Item, NetDocReader};
use crate::{Error, Result};
use once_cell::sync::Lazy;
static FRUIT_SALAD: Lazy<SectionRules<Fruit>> = Lazy::new(|| {
use Fruit::*;
let mut rules = SectionRules::new();
rules.add(ANN_TASTY.rule().required().args(1..=1));
rules.add(ORANGE.rule().args(1..));
rules.add(STONEFRUIT.rule().may_repeat());
rules.add(GUAVA.rule().obj_optional());
rules.add(LEMON.rule().no_args().obj_required());
rules
});
#[test]
fn parse_section() -> Result<()> {
use Fruit::*;
let s = "\
@tasty yes
orange soda
cherry cobbler
cherry pie
plum compote
guava fresh from 7 trees
-----BEGIN GUAVA MANIFESTO-----
VGhlIGd1YXZhIGVtb2ppIGlzIG5vdCBjdXJyZW50bHkgc3VwcG9ydGVkIGluI
HVuaWNvZGUgMTMuMC4gTGV0J3MgZmlnaHQgYWdhaW5zdCBhbnRpLWd1YXZhIG
JpYXMu
-----END GUAVA MANIFESTO-----
lemon
-----BEGIN LEMON-----
8J+Niw==
-----END LEMON-----
";
let mut r: NetDocReader<'_, Fruit> = NetDocReader::new(s);
let sec = FRUIT_SALAD.parse(&mut r.iter()).unwrap();
assert_eq!(sec.required(ANN_TASTY)?.arg(0), Some("yes"));
assert!(sec.get(ORANGE).is_some());
assert_eq!(sec.get(ORANGE).unwrap().args_as_str(), "soda");
let stonefruit_slice = sec.slice(STONEFRUIT);
assert_eq!(stonefruit_slice.len(), 3);
let kwds: Vec<&str> = stonefruit_slice.iter().map(Item::kwd_str).collect();
assert_eq!(kwds, &["cherry", "cherry", "plum"]);
assert_eq!(sec.maybe(GUAVA).args_as_str(), Some("fresh from 7 trees"));
assert_eq!(sec.maybe(GUAVA).parse_arg::<u32>(2).unwrap(), Some(7));
assert!(sec.maybe(GUAVA).parse_arg::<u32>(1).is_err());
assert_eq!(sec.get(GUAVA).unwrap().obj("GUAVA MANIFESTO").unwrap(),
&b"The guava emoji is not currently supported in unicode 13.0. Let's fight against anti-guava bias."[..]);
assert_eq!(
sec.get(ANN_TASTY).unwrap() as *const Item<'_, _>,
sec.first_item().unwrap() as *const Item<'_, _>
);
assert_eq!(
sec.get(LEMON).unwrap() as *const Item<'_, _>,
sec.last_item().unwrap() as *const Item<'_, _>
);
Ok(())
}
#[test]
fn rejected() {
use crate::Pos;
fn check(s: &str, e: Error) {
let mut r: NetDocReader<'_, Fruit> = NetDocReader::new(s);
let res = FRUIT_SALAD.parse(&mut r.iter());
assert!(res.is_err());
assert_eq!(res.err().unwrap().within(s), e);
}
check(
"orange foo\nfoobar x\n@tasty yes\n",
Error::UnexpectedToken("<unrecognized>", Pos::from_line(2, 1)),
);
check(
"@tasty yes\norange foo\norange bar\n",
Error::DuplicateToken("orange", Pos::from_line(3, 1)),
);
check("orange foo\n", Error::MissingToken("@tasty"));
check(
"@tasty nope\norange\n",
Error::TooFewArguments("orange", Pos::from_line(2, 1)),
);
check(
"@tasty yup indeed\norange normal\n",
Error::TooManyArguments("@tasty", Pos::from_line(1, 1)),
);
check(
"@tasty yes\nlemon\norange no\n",
Error::MissingObject("lemon", Pos::from_line(2, 1)),
);
}
}