bnf 0.6.0

A library for parsing Backus–Naur form context-free grammars
Documentation
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
use crate::error::Error;
use crate::parsers::{self, BNF};
use crate::term::Term;
use std::fmt;
use std::ops;
use std::str::FromStr;

use nom::Parser;
use nom::combinator::all_consuming;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// An Expression is comprised of any number of Terms
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct Expression {
    pub(crate) terms: Vec<Term>,
}

impl Expression {
    /// Construct a new `Expression`
    #[must_use]
    pub const fn new() -> Expression {
        Expression { terms: vec![] }
    }

    /// Construct an `Expression` from `Term`s
    #[must_use]
    pub const fn from_parts(v: Vec<Term>) -> Expression {
        Expression { terms: v }
    }

    /// Add `Term` to `Expression`
    pub fn add_term(&mut self, term: Term) {
        self.terms.push(term);
    }

    /// Remove `Term` from `Expression`
    ///
    /// If interested if `Term` was removed, then inspect the returned `Option`.
    ///
    /// # Example
    ///
    /// ```
    /// use bnf::{Expression, Term};
    ///
    /// let mut expression = Expression::from_parts(vec![]);
    /// let to_remove = Term::Terminal(String::from("a_terminal"));
    /// let removed = expression.remove_term(&to_remove);
    /// # let removed_clone = removed.clone();
    /// match removed {
    ///     Some(term) => println!("removed {}", term),
    ///     None => println!("term was not in expression, so could not be removed"),
    /// }
    ///
    /// # assert_eq!(removed_clone, None);
    /// ```
    pub fn remove_term(&mut self, term: &Term) -> Option<Term> {
        if let Some(pos) = self.terms.iter().position(|x| *x == *term) {
            Some(self.terms.remove(pos))
        } else {
            None
        }
    }

    /// Get iterator of `Term`s within `Expression`
    pub fn terms_iter(&self) -> impl Iterator<Item = &Term> {
        self.terms.iter()
    }

    /// Get mutable iterator of `Term`s within `Expression`
    pub fn terms_iter_mut(&mut self) -> impl Iterator<Item = &mut Term> {
        self.terms.iter_mut()
    }

    /// Determine if this expression terminates or not, i.e contains all terminal elements or every
    /// non-terminal element in the expression exists in the (optional) list of 'terminating rules'
    pub(crate) fn terminates(&self, terminating_rules: Option<&Vec<&Term>>) -> bool {
        if !self.terms.iter().any(|t| matches!(t, Term::Nonterminal(_))) {
            return true;
        }

        let Some(terminating_rules) = terminating_rules else {
            return false;
        };

        let nonterms = self
            .terms_iter()
            .filter(|t| matches!(t, Term::Nonterminal(_)));

        for nt in nonterms {
            if !terminating_rules.contains(&nt) {
                return false;
            }
        }

        true
    }
}

/// Create an expression from a series of terms
/// ```
/// bnf::expression!(<a> "and" <b>);
/// ```
#[macro_export]
macro_rules! expression {
    // Entry: nonterminal followed by more tokens
    (<$nt:ident> $($tt:tt)*) => {
        $crate::expression!(@collect [$crate::term!(<$nt>)] $($tt)*)
    };
    // Entry: literal followed by more tokens
    ($t:literal $($tt:tt)*) => {
        $crate::expression!(@collect [$crate::term!($t)] $($tt)*)
    };
    // Collect nonterminal
    (@collect [$($collected:expr),*] <$nt:ident> $($rest:tt)*) => {
        $crate::expression!(@collect [$($collected,)* $crate::term!(<$nt>)] $($rest)*)
    };
    // Collect literal
    (@collect [$($collected:expr),*] $t:literal $($rest:tt)*) => {
        $crate::expression!(@collect [$($collected,)* $crate::term!($t)] $($rest)*)
    };
    // Base case - build Expression from collected terms
    (@collect [$($collected:expr),*]) => {
        $crate::Expression::from_parts(vec![$($collected),*])
    };
}

impl fmt::Display for Expression {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let display = self
            .terms
            .iter()
            .map(|s| s.to_string())
            .collect::<Vec<_>>()
            .join(" ");

        write!(f, "{display}")
    }
}

impl FromStr for Expression {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match all_consuming(parsers::expression::<BNF>).parse(s) {
            Ok((_, o)) => Ok(o),
            Err(e) => Err(Error::from(e)),
        }
    }
}

impl ops::Add<Expression> for &Expression {
    type Output = Expression;
    fn add(self, rhs: Expression) -> Self::Output {
        let mut new_expression = Expression::new();
        for t in self.terms_iter() {
            new_expression.add_term(t.clone());
        }
        for t in rhs.terms_iter() {
            new_expression.add_term(t.clone());
        }
        new_expression
    }
}

impl ops::Add<Term> for &Expression {
    type Output = Expression;
    fn add(self, rhs: Term) -> Self::Output {
        let mut new_expression = Expression::new();
        for t in self.terms_iter() {
            new_expression.add_term(t.clone());
        }
        new_expression.add_term(rhs);
        new_expression
    }
}

impl ops::Add<Expression> for Expression {
    type Output = Expression;
    fn add(mut self, rhs: Expression) -> Self::Output {
        for t in rhs.terms_iter() {
            self.add_term(t.clone());
        }
        self
    }
}

impl ops::Add<Term> for Expression {
    type Output = Expression;
    fn add(mut self, rhs: Term) -> Self::Output {
        self.add_term(rhs);
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use quickcheck::{Arbitrary, Gen, QuickCheck, TestResult};

    impl Arbitrary for Expression {
        fn arbitrary(g: &mut Gen) -> Self {
            let mut terms = Vec::<Term>::arbitrary(g);
            // expressions must always have at least one term
            if terms.is_empty() {
                terms.push(Term::arbitrary(g));
            }
            Expression { terms }
        }
    }

    fn prop_to_string_and_back(expr: Expression) -> TestResult {
        let to_string: String = expr.to_string();
        let from_str: Result<Expression, _> = to_string.parse();
        match from_str {
            Ok(from_expr) => TestResult::from_bool(from_expr == expr),
            _ => TestResult::error(format!("{expr} to string and back should be safe")),
        }
    }

    #[test]
    fn to_string_and_back() {
        QuickCheck::new().quickcheck(prop_to_string_and_back as fn(Expression) -> TestResult)
    }

    #[test]
    fn new_expressions() {
        let t2 = crate::term!("terminal");
        let nt2 = crate::term!(<nonterminal>);

        let e1 = crate::expression!(<nonterminal> "terminal");
        let mut e2 = Expression::new();
        e2.add_term(nt2);
        e2.add_term(t2);

        assert_eq!(e1, e2);
    }

    #[test]
    fn add_term() {
        let mut terms = vec![crate::term!("A"), crate::term!("C"), crate::term!("G")];

        let mut dna_expression = Expression::from_parts(terms.clone());
        assert_eq!(dna_expression.terms_iter().count(), terms.len());

        // oops forgot "T"
        let forgotten = crate::term!("T");
        dna_expression.add_term(forgotten.clone());
        terms.push(forgotten);
        assert_eq!(dna_expression.terms_iter().count(), terms.len());

        // check all terms are there
        for term in dna_expression.terms_iter() {
            assert!(terms.contains(term), "{term} was not in terms");
        }
    }

    #[test]
    fn remove_term() {
        let terms = vec![
            crate::term!("A"),
            crate::term!("C"),
            crate::term!("G"),
            crate::term!("T"),
            crate::term!("Z"),
        ];

        let mut dna_expression = Expression::from_parts(terms.clone());
        assert_eq!(dna_expression.terms_iter().count(), terms.len());

        // oops "Z" isn't a dna base
        let accident = crate::term!("Z");
        let removed = dna_expression.remove_term(&accident);

        // the removed element should be the accident
        assert_eq!(Some(accident.clone()), removed);
        // number of terms should have decreased
        assert_eq!(dna_expression.terms_iter().count(), terms.len() - 1);
        // the accident should no longer be found in the terms
        assert_eq!(
            dna_expression.terms_iter().find(|&term| *term == accident),
            None
        );
    }

    #[test]
    fn remove_nonexistent_term() {
        let terms = vec![
            crate::term!("A"),
            crate::term!("C"),
            crate::term!("G"),
            crate::term!("T"),
        ];

        let mut dna_expression = Expression::from_parts(terms.clone());
        assert_eq!(dna_expression.terms_iter().count(), terms.len());

        // oops "Z" isn't a dna base
        let nonexistent = crate::term!("Z");
        let removed = dna_expression.remove_term(&nonexistent);

        // the nonexistent term should not be found in the terms
        assert_eq!(
            dna_expression
                .terms_iter()
                .find(|&term| *term == nonexistent),
            None
        );
        // no term should have been removed
        assert_eq!(None, removed);
        // number of terms should not have decreased
        assert_eq!(dna_expression.terms_iter().count(), terms.len());
    }

    #[test]
    fn parse_complete() {
        let expression = crate::expression!(<base> <dna>);
        assert_eq!(Ok(expression), Expression::from_str("<base> <dna>"));
    }

    #[test]
    fn parse_error() {
        let expression = Expression::from_str("<base> <dna");
        assert!(
            matches!(expression, Err(Error::ParseError(_))),
            "{expression:?} should be error"
        );
    }

    #[test]
    fn parse_incomplete() {
        let result = Expression::from_str("");
        assert!(
            matches!(result, Err(Error::ParseError(_))),
            "{result:?} should be ParseError"
        );
    }

    #[test]
    fn add_operator() {
        let t3 = crate::term!("terminal");
        let t5 = crate::term!("terminal");

        let e1 = crate::expression!(<nonterminal> "terminal");
        // &expression + expression
        let e2_1 = crate::expression!(<nonterminal>);
        let e2_2 = crate::expression!("terminal");
        let e2 = &e2_1 + e2_2;
        // &expression + term
        let e3_1 = crate::expression!(<nonterminal>);
        let e3 = &e3_1 + t3;
        // expression + expression
        let e4_1 = crate::expression!(<nonterminal>);
        let e4_2 = crate::expression!("terminal");
        let e4 = e4_1 + e4_2;
        // expression + term
        let e5_1 = crate::expression!(<nonterminal>);
        let e5 = e5_1 + t5;

        assert_eq!(e1, e2);
        assert_eq!(e1, e3);
        assert_eq!(e1, e4);
        assert_eq!(e1, e5);
    }

    #[test]
    fn iterate_terms() {
        let expression: Expression = "<b> 'A' <b>".parse().unwrap();
        let terms = expression.terms_iter().cloned().collect::<Vec<_>>();
        assert_eq!(terms, expression.terms);
    }

    #[test]
    fn mutate_iterable_terms() {
        let mut expression: Expression = "'END'".parse().unwrap();
        let new_term = crate::term!("X");
        for term in expression.terms_iter_mut() {
            *term = new_term.clone();
        }
        assert_eq!(expression.terms, vec![new_term])
    }

    #[test]
    fn does_not_terminate() {
        let mut expression: Expression;

        expression = "<a> <b> <c>".parse().unwrap();
        assert!(!expression.terminates(None));

        expression = "'a' <b> <c>".parse().unwrap();
        assert!(!expression.terminates(None));

        expression = "<a> 'b' <c>".parse().unwrap();
        assert!(!expression.terminates(None));

        expression = "<a> <b> 'c'".parse().unwrap();
        assert!(!expression.terminates(None));

        expression = "'a' 'b' <c>".parse().unwrap();
        assert!(!expression.terminates(None));

        expression = "'a' <b> 'c'".parse().unwrap();
        assert!(!expression.terminates(None));

        expression = "<a> 'b' 'c'".parse().unwrap();
        assert!(!expression.terminates(None));

        expression = "'a' <b> <c>".parse().unwrap();
        assert!(!expression.terminates(Some(&vec![
            &Term::from_str("<b>").unwrap(),
            &Term::from_str("<1>").unwrap(),
            &Term::from_str("<2>").unwrap(),
        ])));

        expression = "<a> <b> <c>".parse().unwrap();
        assert!(!expression.terminates(Some(&vec![
            &Term::from_str("<c>").unwrap(),
            &Term::from_str("<b>").unwrap(),
            &Term::from_str("<1>").unwrap(),
        ])));
    }

    #[test]
    fn does_terminate() {
        let mut expression: Expression = "'a' 'b' 'c'".parse().unwrap();
        assert!(expression.terminates(None));

        expression = "'a' 'b'".parse().unwrap();
        assert!(expression.terminates(None));

        expression = "'a'".parse().unwrap();
        assert!(expression.terminates(None));

        let mut expression: Expression = "'a' 'b' <c>".parse().unwrap();
        assert!(expression.terminates(Some(&vec![&Term::from_str("<c>").unwrap()])));

        expression = "'a' <b> <c>".parse().unwrap();
        assert!(expression.terminates(Some(&vec![
            &Term::from_str("<c>").unwrap(),
            &Term::from_str("<b>").unwrap(),
        ])));

        expression = "'a' <b> <c>".parse().unwrap();
        assert!(expression.terminates(Some(&vec![
            &Term::from_str("<c>").unwrap(),
            &Term::from_str("<b>").unwrap(),
            &Term::from_str("<1>").unwrap(),
            &Term::from_str("<2>").unwrap(),
        ])));

        expression = "<a> <b> <c>".parse().unwrap();
        assert!(expression.terminates(Some(&vec![
            &Term::from_str("<c>").unwrap(),
            &Term::from_str("<b>").unwrap(),
            &Term::from_str("<1>").unwrap(),
            &Term::from_str("<2>").unwrap(),
            &Term::from_str("<a>").unwrap(),
        ],)));
    }

    #[test]
    fn macro_builds() {
        let expr = crate::expression!(<a> "and" <b>);
        let expected = crate::expression!(<a> "and" <b>);

        assert_eq!(expr, expected);
    }
}