wagon_parser/parser/
inverse.rs

1use std::{fmt::Display, write};
2
3use crate::firstpass::{GetReqAttributes, RewriteToSynth};
4
5use super::{Parse, LexerBridge, ParseResult, Tokens, SpannableNode, Peek};
6
7use wagon_lexer::math::Math;
8
9use super::comp::Comparison;
10
11use wagon_macros::new_unspanned;
12
13#[derive(PartialEq, Debug, Eq, Hash, Clone)]
14#[new_unspanned]
15/// Either a [`Comparison`] prepend by `!` or just a [`Comparison`].
16///
17/// # Grammar
18/// <code>Inverse -> "!"? [Comparison];</code>
19pub enum Inverse {
20    /// `!`
21	Not(SpannableNode<Comparison>),
22    /// The next layer down.
23	Comparison(SpannableNode<Comparison>)
24}
25
26impl Parse for Inverse {
27
28    fn parse(lexer: &mut LexerBridge) -> ParseResult<Self> where Self: Sized {
29        if lexer.next_if_eq(&Ok(Tokens::MathToken(Math::Not))).is_some() {
30            Ok(Self::Not(SpannableNode::parse(lexer)?))
31        } else {
32            Ok(Self::Comparison(SpannableNode::parse(lexer)?))
33        }
34    }
35
36}
37
38impl GetReqAttributes for Inverse {
39    fn get_req_attributes(&self) -> crate::firstpass::ReqAttributes {
40        match self {
41            Self::Not(i) => i.get_req_attributes(),
42            Self::Comparison(c) => c.get_req_attributes(),
43        }
44    }
45}
46
47impl RewriteToSynth for Inverse {
48    fn rewrite_to_synth(&mut self) -> crate::firstpass::ReqAttributes {
49        match self {
50            Self::Not(i) => i.rewrite_to_synth(),
51            Self::Comparison(c) => c.rewrite_to_synth(),
52        }
53    }
54}
55
56impl Display for Inverse {
57    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58        match self {
59            Self::Not(i) => write!(f, "!{i}"),
60            Self::Comparison(c) => write!(f, "{c}"),
61        }
62    }
63}