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
use std::{fmt::Display, write};

use crate::firstpass::{GetReqAttributes, RewriteToSynth};

use super::{Parse, LexerBridge, ParseResult, Tokens, SpannableNode, Peek};

use wagon_lexer::math::Math;

use super::comp::Comparison;

use wagon_macros::new_unspanned;

#[derive(PartialEq, Debug, Eq, Hash, Clone)]
#[new_unspanned]
/// Either a [`Comparison`] prepend by `!` or just a [`Comparison`].
///
/// # Grammar
/// <code>Inverse -> "!"? [Comparison];</code>
pub enum Inverse {
    /// `!`
	Not(SpannableNode<Comparison>),
    /// The next layer down.
	Comparison(SpannableNode<Comparison>)
}

impl Parse for Inverse {

    fn parse(lexer: &mut LexerBridge) -> ParseResult<Self> where Self: Sized {
        if lexer.next_if_eq(&Ok(Tokens::MathToken(Math::Not))).is_some() {
            Ok(Self::Not(SpannableNode::parse(lexer)?))
        } else {
            Ok(Self::Comparison(SpannableNode::parse(lexer)?))
        }
    }

}

impl GetReqAttributes for Inverse {
    fn get_req_attributes(&self) -> crate::firstpass::ReqAttributes {
        match self {
            Self::Not(i) => i.get_req_attributes(),
            Self::Comparison(c) => c.get_req_attributes(),
        }
    }
}

impl RewriteToSynth for Inverse {
    fn rewrite_to_synth(&mut self) -> crate::firstpass::ReqAttributes {
        match self {
            Self::Not(i) => i.rewrite_to_synth(),
            Self::Comparison(c) => c.rewrite_to_synth(),
        }
    }
}

impl Display for Inverse {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Not(i) => write!(f, "!{i}"),
            Self::Comparison(c) => write!(f, "{c}"),
        }
    }
}