wagon-parser 1.0.0

Parser for the WAGon DSL.
Documentation
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}"),
        }
    }
}