kconfig-parser 0.1.1

Kconfig parser for the Kconfig file format from the Linux Kernel for the Cargo Kconfig crate
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
/*
 Cargo KConfig - KConfig parser
 Copyright (C) 2022  Sjoerd van Leent

--------------------------------------------------------------------------------

Copyright Notice: Apache

Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at

   https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.

--------------------------------------------------------------------------------

Copyright Notice: GPLv2

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

--------------------------------------------------------------------------------

Copyright Notice: MIT

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the “Software”), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

//! This file contains the parser for expressions in a Kconfig file.
//! It is used eventually by the dependency parser and on certain
//! other places.

use super::parser::Error;
use super::structs::*;
use crate::{
    lex::{structs::*, LexerBase},
    parse_string,
};
use std::collections::BTreeSet;

#[derive(Eq, PartialEq, Copy, Clone, Debug)]
enum Operator {
    Eq,
    Ne,
    Lt,
    Gt,
    Lte,
    Gte,
    And,
    Or,
}

impl Operator {
    fn get_precedence_number(&self) -> u8 {
        match self {
            Self::Lt | Self::Gt | Self::Lte | Self::Gte => 255,
            Self::Eq | Self::Ne => 127,
            Self::And => 63,
            Self::Or => 0,
        }
    }
}

fn parse_single<LB>(lexer: &mut LB, first_pass: bool) -> Result<(Option<Expr>, Token), Error>
where
    LB: LexerBase,
{
    let token = lexer.next_token();
    match token.term() {
        // If the token starts with an exclamation mark, then it is a negation
        // of the next token.
        Lexicon::Not => {
            let neg_token = lexer.next_token();
            // If the new token is an negation, start interior parsing
            // otherwise, it must be an identifier.
            match neg_token.term() {
                Lexicon::String(s) => match parse_string(&s) {
                    Ok(s) => Ok((Some(Expr::Not(Box::new(Expr::Sym(s)))), lexer.next_token())),
                    Err(e) => Err(Error::from(Error::new(token, &e))),
                },
                Lexicon::Identifier(s) => {
                    Ok((Some(Expr::Not(Box::new(Expr::Sym(s)))), lexer.next_token()))
                }
                Lexicon::Open => {
                    let (expr, token) = parse_interior(None, BTreeSet::new(), false, lexer)?;
                    match token.term() {
                        Lexicon::Close => Ok((
                            Some(Expr::Not(Box::new(Expr::Sub(Box::new(expr.unwrap()))))),
                            lexer.next_token(),
                        )),
                        _ => Err(Error::new(token.clone(), "Expected ')'")),
                    }
                }
                _ => Err(Error::new(token.clone(), "Expected identifier or '('")),
            }
        }

        // If the token starts with an open parentheses, then it is a subexpression
        Lexicon::Open => {
            let (expr, token) = parse_interior(None, BTreeSet::new(), false, lexer)?;
            match token.term() {
                Lexicon::Close => {
                    Ok((Some(Expr::Sub(Box::new(expr.unwrap()))), lexer.next_token()))
                }
                _ => Err(Error::new(token.clone(), "Expected ')'")),
            }
        }

        // If a token is a string, it will be converted to a symbol
        Lexicon::String(s) => match parse_string(&s) {
            Ok(s) => Ok((Some(Expr::Sym(s)), lexer.next_token())),
            Err(e) => Err(Error::from(Error::new(token, &e))),
        },

        // If a token is an identifier, it will be a symbol
        Lexicon::Identifier(s) => Ok((Some(Expr::Sym(s)), lexer.next_token())),

        _ => {
            if !first_pass {
                Err(Error::new(
                    token.clone(),
                    "Expected identifier, '$(', '(', '!'",
                ))
            } else {
                Ok((None, token))
            }
        }
    }
}

pub(super) fn parse<LB>(lexer: &mut LB) -> Result<(Option<Expr>, Token), Error>
where
    LB: LexerBase,
{
    parse_interior(None, BTreeSet::new(), true, lexer)
}

// Wraps an operator and set into a single expression type
fn wrap_operator_and_set(o: Operator, set: BTreeSet<Expr>) -> Expr {
    match o {
        Operator::And => Expr::And(set),
        Operator::Or => Expr::Or(set),
        Operator::Lt => Expr::Lt(set),
        Operator::Lte => Expr::Lte(set),
        Operator::Gt => Expr::Gt(set),
        Operator::Gte => Expr::Gte(set),
        Operator::Eq => Expr::Eq(set),
        Operator::Ne => Expr::Ne(set),
    }
}

// Converts an equality operator to an internal operator representatio
fn equality_operator_to_operator(eqo: EqualityOperator) -> Operator {
    match eqo {
        EqualityOperator::Gt => Operator::Gt,
        EqualityOperator::Gte => Operator::Gte,
        EqualityOperator::Lt => Operator::Lt,
        EqualityOperator::Lte => Operator::Lte,
        EqualityOperator::Eq => Operator::Eq,
        EqualityOperator::Ne => Operator::Ne,
    }
}

// Converts a token to an operator, if it is an operator
fn token_to_operator(token: &Token) -> Option<Operator> {
    let term = token.term();
    match term {
        Lexicon::EqualityOperator(eqo) => Some(equality_operator_to_operator(eqo)),
        Lexicon::And => Some(Operator::And),
        Lexicon::Or => Some(Operator::Or),
        _ => None,
    }
}

fn parse_interior<LB>(
    mut initial_operator: Option<Operator>,
    mut set: BTreeSet<Expr>,
    mut first_pass: bool,
    lexer: &mut LB,
) -> Result<(Option<Expr>, Token), Error>
where
    LB: LexerBase,
{
    loop {
        let (expr, maybe_operator_token) = parse_single(lexer, first_pass)?;
        match expr {
            None => break Ok((None, maybe_operator_token)),
            Some(expr) => {
                first_pass = false;
                let maybe_operator = token_to_operator(&maybe_operator_token);
                match maybe_operator {
                    Some(current_operator) => {
                        // If the last operator is None, then it is easy, it will become
                        // the current operator, if it is the same, it is also easy, nothing
                        // will happen. If it is however different, then the precedence rules
                        // need to be properly calculated.
                        match initial_operator {
                            None => {
                                initial_operator = Some(current_operator);
                                set.insert(expr);
                            }
                            Some(last) => {
                                if last != current_operator {
                                    let precedes = last.get_precedence_number()
                                        < current_operator.get_precedence_number();
                                    // Here it becomes difficult. We need to know whether the
                                    // current operator precedes the last operator;
                                    // or when the last operator either precedes the current
                                    // operator or is similar to the current operator.
                                    if precedes {
                                        // This is the simple variant,
                                        // the next part is to be parsed independently, with the
                                        // last token from the set moved to the new parser
                                        let mut sub_set = BTreeSet::new();
                                        sub_set.insert(expr);
                                        let (sub_expr, sub_token) = parse_interior(
                                            Some(current_operator),
                                            sub_set,
                                            false,
                                            lexer,
                                        )?;
                                        set.insert(sub_expr.unwrap());
                                        let final_expr = wrap_operator_and_set(last, set);
                                        // We should be ending here, as the interior parser will have parsed
                                        // until the end.
                                        break Ok((Some(final_expr), sub_token));
                                    } else {
                                        // The current set is to be placed into a single expression subset,
                                        // and a new subset with the current operator is to be created
                                        set.insert(expr);
                                        let sub_expr = wrap_operator_and_set(last, set.clone());
                                        set.clear();
                                        set.insert(sub_expr);
                                        initial_operator = Some(current_operator);
                                    }
                                } else {
                                    set.insert(expr);
                                }
                            }
                        }
                    }
                    None => {
                        break Ok((
                            Some({
                                // If reached, and the set is not filled, it is a symbol,
                                // otherwise this symbol needs to be added to the list, and
                                // the list needs to be returned as the apporiate Expr for
                                // the given operator.
                                match initial_operator {
                                    None => {
                                        if set.len() > 0 {
                                            return Err(Error::new(
                                                maybe_operator_token.clone(),
                                                "Operator expected if set length > 0",
                                            ));
                                        } else {
                                            expr
                                        }
                                    }
                                    Some(o) => {
                                        set.insert(expr);
                                        wrap_operator_and_set(o, set)
                                    }
                                }
                            }),
                            maybe_operator_token,
                        ));
                    }
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::super::super::lex::lexer::Lexer;
    use super::*;

    #[test]
    fn test_simple_symbol() -> Result<(), Error> {
        let input = &mut "FOO".as_bytes();
        let mut lexer = Lexer::create(input);
        let (expr, token) = parse(&mut lexer)?;
        let expr = expr.unwrap();
        assert_eq!(expr.is_sym(), true);
        assert_eq!(expr.get_sym().unwrap(), "FOO".to_string());
        assert_eq!(expr, Expr::Sym("FOO".to_string()));
        assert_eq!(token.term(), Lexicon::EOT);
        Ok(())
    }

    #[test]
    fn test_simple_string_symbol() -> Result<(), Error> {
        let input = &mut "\"FOO\"".as_bytes();
        let mut lexer = Lexer::create(input);
        let (expr, token) = parse(&mut lexer)?;
        let expr = expr.unwrap();
        assert_eq!(expr.is_sym(), true);
        assert_eq!(expr.get_sym().unwrap(), "FOO".to_string());
        assert_eq!(expr, Expr::Sym("FOO".to_string()));
        assert_eq!(token.term(), Lexicon::EOT);
        Ok(())
    }

    #[test]
    fn test_simple_numeric_symbol() -> Result<(), Error> {
        let input = &mut "123".as_bytes();
        let mut lexer = Lexer::create(input);
        let (expr, token) = parse(&mut lexer)?;
        let expr = expr.unwrap();
        assert_eq!(expr.is_sym(), true);
        assert_eq!(expr.get_sym().unwrap(), "123".to_string());
        assert_eq!(expr, Expr::Sym("123".to_string()));
        assert_eq!(token.term(), Lexicon::EOT);
        Ok(())
    }

    #[test]
    fn test_simple_negative_numeric_symbol() -> Result<(), Error> {
        let input = &mut "-123".as_bytes();
        let mut lexer = Lexer::create(input);
        let (expr, token) = parse(&mut lexer)?;
        let expr = expr.unwrap();
        assert_eq!(expr.is_sym(), true);
        assert_eq!(expr.get_sym().unwrap(), "-123".to_string());
        assert_eq!(expr, Expr::Sym("-123".to_string()));
        assert_eq!(token.term(), Lexicon::EOT);
        Ok(())
    }

    #[test]
    fn test_simple_hexadecimal_symbol() -> Result<(), Error> {
        let input = &mut "0x123".as_bytes();
        let mut lexer = Lexer::create(input);
        let (expr, token) = parse(&mut lexer)?;
        let expr = expr.unwrap();
        assert_eq!(expr.is_sym(), true);
        assert_eq!(expr.get_sym().unwrap(), "0x123".to_string());
        assert_eq!(expr, Expr::Sym("0x123".to_string()));
        assert_eq!(token.term(), Lexicon::EOT);
        Ok(())
    }

    #[test]
    fn test_simple_negative_hexadecimal_symbol() -> Result<(), Error> {
        let input = &mut "-0x123".as_bytes();
        let mut lexer = Lexer::create(input);
        let (expr, token) = parse(&mut lexer)?;
        let expr = expr.unwrap();
        assert_eq!(expr.is_sym(), true);
        assert_eq!(expr.get_sym().unwrap(), "-0x123".to_string());
        assert_eq!(expr, Expr::Sym("-0x123".to_string()));
        assert_eq!(token.term(), Lexicon::EOT);
        Ok(())
    }

    #[test]
    fn test_simple_negation() -> Result<(), Error> {
        let input = &mut "!FOO".as_bytes();
        let mut lexer = Lexer::create(input);
        let (expr, token) = parse(&mut lexer)?;
        let expr = expr.unwrap();
        assert_eq!(expr.is_not(), true);
        assert_eq!(expr.get_not_expr().unwrap(), Expr::Sym("FOO".to_string()));
        assert_eq!(token.term(), Lexicon::EOT);
        Ok(())
    }

    #[test]
    fn test_simple_sub() -> Result<(), Error> {
        let input = &mut "(FOO)".as_bytes();
        let mut lexer = Lexer::create(input);
        let (expr, token) = parse(&mut lexer)?;
        let expr = expr.unwrap();
        assert_eq!(expr.is_sub(), true);
        assert_eq!(expr.get_sub_expr().unwrap(), Expr::Sym("FOO".to_string()));
        assert_eq!(token.term(), Lexicon::EOT);
        Ok(())
    }

    #[test]
    fn test_not_with_sub() -> Result<(), Error> {
        let input = &mut "!(FOO)".as_bytes();
        let mut lexer = Lexer::create(input);
        let (expr, token) = parse(&mut lexer)?;
        let expr = expr.unwrap();
        assert_eq!(expr.is_not(), true);
        let expr = expr.get_not_expr().unwrap();
        assert_eq!(expr.is_sub(), true);
        assert_eq!(expr.get_sub_expr().unwrap(), Expr::Sym("FOO".to_string()));
        assert_eq!(token.term(), Lexicon::EOT);
        Ok(())
    }

    #[test]
    fn test_sub_with_not() -> Result<(), Error> {
        let input = &mut "(!FOO)".as_bytes();
        let mut lexer = Lexer::create(input);
        let (expr, token) = parse(&mut lexer)?;
        let expr = expr.unwrap();
        assert_eq!(expr.is_sub(), true);
        let expr = expr.get_sub_expr().unwrap();
        assert_eq!(expr.is_not(), true);
        assert_eq!(expr.get_not_expr().unwrap(), Expr::Sym("FOO".to_string()));
        assert_eq!(token.term(), Lexicon::EOT);
        Ok(())
    }

    #[test]
    fn test_simple_eq() -> Result<(), Error> {
        let input = &mut "FOO == BAR".as_bytes();
        let mut lexer = Lexer::create(input);
        let (expr, token) = parse(&mut lexer)?;
        let expr = expr.unwrap();
        assert_eq!(expr.is_comparison(), true);
        assert_eq!(expr.is_eq(), true);
        assert_eq!(expr.len(), 2);
        let mut counter: usize = 0;
        for sub_expr in expr.clone() {
            assert_eq!(sub_expr.is_sym(), true);
            counter = counter + 1;
        }
        assert_eq!(counter, expr.len());
        assert_eq!(token.term(), Lexicon::EOT);
        Ok(())
    }

    #[test]
    fn test_simple_ne() -> Result<(), Error> {
        let input = &mut "FOO != BAR".as_bytes();
        let mut lexer = Lexer::create(input);
        let (expr, token) = parse(&mut lexer)?;
        let expr = expr.unwrap();
        assert_eq!(expr.is_comparison(), true);
        assert_eq!(expr.is_ne(), true);
        assert_eq!(expr.len(), 2);
        let mut counter: usize = 0;
        for sub_expr in expr.clone() {
            assert_eq!(sub_expr.is_sym(), true);
            counter = counter + 1;
        }
        assert_eq!(counter, expr.len());
        assert_eq!(token.term(), Lexicon::EOT);
        Ok(())
    }

    #[test]
    fn test_simple_lt() -> Result<(), Error> {
        let input = &mut "FOO < BAR".as_bytes();
        let mut lexer = Lexer::create(input);
        let (expr, token) = parse(&mut lexer)?;
        let expr = expr.unwrap();
        assert_eq!(expr.is_comparison(), true);
        assert_eq!(expr.is_lt(), true);
        assert_eq!(expr.len(), 2);
        let mut counter: usize = 0;
        for sub_expr in expr.clone() {
            assert_eq!(sub_expr.is_sym(), true);
            counter = counter + 1;
        }
        assert_eq!(counter, expr.len());
        assert_eq!(token.term(), Lexicon::EOT);
        Ok(())
    }

    #[test]
    fn test_simple_gt() -> Result<(), Error> {
        let input = &mut "FOO > BAR".as_bytes();
        let mut lexer = Lexer::create(input);
        let (expr, token) = parse(&mut lexer)?;
        let expr = expr.unwrap();
        assert_eq!(expr.is_comparison(), true);
        assert_eq!(expr.is_gt(), true);
        assert_eq!(expr.len(), 2);
        let mut counter: usize = 0;
        for sub_expr in expr.clone() {
            assert_eq!(sub_expr.is_sym(), true);
            counter = counter + 1;
        }
        assert_eq!(counter, expr.len());
        assert_eq!(token.term(), Lexicon::EOT);
        Ok(())
    }

    #[test]
    fn test_simple_lte() -> Result<(), Error> {
        let input = &mut "FOO <= BAR".as_bytes();
        let mut lexer = Lexer::create(input);
        let (expr, token) = parse(&mut lexer)?;
        let expr = expr.unwrap();
        assert_eq!(expr.is_comparison(), true);
        assert_eq!(expr.is_lte(), true);
        assert_eq!(expr.len(), 2);
        let mut counter: usize = 0;
        for sub_expr in expr.clone() {
            assert_eq!(sub_expr.is_sym(), true);
            counter = counter + 1;
        }
        assert_eq!(counter, expr.len());
        assert_eq!(token.term(), Lexicon::EOT);
        Ok(())
    }

    #[test]
    fn test_simple_gte() -> Result<(), Error> {
        let input = &mut "FOO >= BAR".as_bytes();
        let mut lexer = Lexer::create(input);
        let (expr, token) = parse(&mut lexer)?;
        let expr = expr.unwrap();
        assert_eq!(expr.is_comparison(), true);
        assert_eq!(expr.is_gte(), true);
        assert_eq!(expr.len(), 2);
        let mut counter: usize = 0;
        for sub_expr in expr.clone() {
            assert_eq!(sub_expr.is_sym(), true);
            counter = counter + 1;
        }
        assert_eq!(counter, expr.len());
        assert_eq!(token.term(), Lexicon::EOT);
        Ok(())
    }

    #[test]
    fn test_simple_and() -> Result<(), Error> {
        let input = &mut "FOO && BAR".as_bytes();
        let mut lexer = Lexer::create(input);
        let (expr, token) = parse(&mut lexer)?;
        let expr = expr.unwrap();
        assert_eq!(expr.is_comparison(), true);
        assert_eq!(expr.is_and(), true);
        assert_eq!(expr.len(), 2);
        let mut counter: usize = 0;
        for sub_expr in expr.clone() {
            assert_eq!(sub_expr.is_sym(), true);
            counter = counter + 1;
        }
        assert_eq!(counter, expr.len());
        assert_eq!(token.term(), Lexicon::EOT);
        Ok(())
    }

    #[test]
    fn test_simple_or() -> Result<(), Error> {
        let input = &mut "FOO || BAR".as_bytes();
        let mut lexer = Lexer::create(input);
        let (expr, token) = parse(&mut lexer)?;
        let expr = expr.unwrap();
        assert_eq!(expr.is_comparison(), true);
        assert_eq!(expr.is_or(), true);
        assert_eq!(expr.len(), 2);
        let mut counter: usize = 0;
        for sub_expr in expr.clone() {
            assert_eq!(sub_expr.is_sym(), true);
            counter = counter + 1;
        }
        assert_eq!(counter, expr.len());
        assert_eq!(token.term(), Lexicon::EOT);
        Ok(())
    }

    #[test]
    fn test_and_then_or() -> Result<(), Error> {
        // HAM && SPAM Should be evaluated prior to SPAM || EGGS,
        // Therefore, HAM && SPAM are a subexpression
        let input = &mut "HAM && SPAM || EGGS".as_bytes();
        let mut lexer = Lexer::create(input);
        let (expr, token) = parse(&mut lexer)?;
        let expr = expr.unwrap();
        assert_eq!(expr.is_comparison(), true);
        assert_eq!(expr.is_or(), true);
        assert_eq!(expr.len(), 2);
        let mut count_is_and = 0;
        let mut count_is_not_and = 0;
        for sub_expr in expr.clone() {
            if sub_expr.is_and() {
                count_is_and = count_is_and + 1;
            } else {
                count_is_not_and = count_is_not_and + 1;
            }
        }
        assert_eq!(count_is_and, 1);
        assert_eq!(count_is_not_and, 1);
        assert_eq!(token.term(), Lexicon::EOT);
        Ok(())
    }

    #[test]
    fn test_or_then_and() -> Result<(), Error> {
        // HAM || SPAM Should be evaluated prior to SPAM && EGGS,
        // Therefore, SPAM && EGGS are a subexpression
        let input = &mut "HAM || SPAM && EGGS".as_bytes();
        let mut lexer = Lexer::create(input);
        let (expr, token) = parse(&mut lexer)?;
        let expr = expr.unwrap();
        assert_eq!(expr.is_comparison(), true);
        assert_eq!(expr.is_or(), true);
        assert_eq!(expr.len(), 2);
        let mut count_is_and = 0;
        let mut count_is_not_and = 0;
        for sub_expr in expr.clone() {
            if sub_expr.is_and() {
                count_is_and = count_is_and + 1;
            } else {
                count_is_not_and = count_is_not_and + 1;
            }
        }
        assert_eq!(count_is_and, 1);
        assert_eq!(count_is_not_and, 1);
        assert_eq!(token.term(), Lexicon::EOT);
        Ok(())
    }

    #[test]
    fn test_or_then_lt_then_and() -> Result<(), Error> {
        // Expected evaluation is:
        // HAM || ((SPAM < EGGS) && BACON)
        let input = &mut "HAM || SPAM < EGGS && BACON".as_bytes();
        let mut lexer = Lexer::create(input);
        let (expr, token) = parse(&mut lexer)?;
        let expr = expr.unwrap();
        assert_eq!(expr.is_comparison(), true);
        assert_eq!(expr.is_or(), true);
        assert_eq!(expr.len(), 2);
        let mut count_is_and = 0;
        let mut count_is_lt = 0;
        let mut count_is_not_and = 0;
        for sub_expr in expr.clone() {
            if sub_expr.is_and() {
                count_is_and += 1;
                for subsub_expr in sub_expr.clone() {
                    if subsub_expr.is_lt() {
                        count_is_lt += 1;
                    }
                }
            } else {
                count_is_not_and += 1;
            }
        }
        assert_eq!(count_is_lt, 1);
        assert_eq!(count_is_and, 1);
        assert_eq!(count_is_not_and, 1);
        assert_eq!(token.term(), Lexicon::EOT);
        Ok(())
    }

    #[test]
    fn test_or_then_lt_then_paren_and() -> Result<(), Error> {
        // Expected evaluation is:
        // HAM || (SPAM < (EGGS && BACON))
        let input = &mut "HAM || SPAM < (EGGS && BACON)".as_bytes();
        let mut lexer = Lexer::create(input);
        let (expr, token) = parse(&mut lexer)?;
        let expr = expr.unwrap();
        assert_eq!(expr.is_comparison(), true);
        assert_eq!(expr.is_or(), true);
        assert_eq!(expr.len(), 2);
        let mut count_is_sub = 0;
        let mut count_is_lt = 0;
        let mut count_is_normal = 0;
        for sub_expr in expr.clone() {
            if sub_expr.is_lt() {
                count_is_lt += 1;
                for subsub_expr in sub_expr.clone() {
                    if subsub_expr.is_sub() {
                        count_is_sub += 1;
                    }
                }
            } else {
                count_is_normal += 1;
            }
        }
        assert_eq!(count_is_lt, 1);
        assert_eq!(count_is_sub, 1);
        assert_eq!(count_is_normal, 1);
        assert_eq!(token.term(), Lexicon::EOT);
        Ok(())
    }
}