msp430-repl/src/parser/directive.rs

33 lines
962 B
Rust

// © 2023 John Breaux
//! A [Directive] issues commands directly to the [Tokenizer](crate::Tokenizer) and
//! [Linker](crate::Linker)
use super::*;
use crate::hash::FromHash;
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Directive(pub Hash, pub String);
impl Directive {
fn str<S: ToString>(mut self, s: S) -> Self {
self.1 = s.to_string();
self
}
}
impl From<Hash> for Directive {
fn from(value: Hash) -> Self { Self(value, String::new()) }
}
impl Parsable for Directive {
fn parse<'text, T>(_p: &Parser, stream: &mut T) -> Result<Self, Error>
where T: TokenStream<'text> {
// expect a directive
let d = stream.expect(Type::Directive)?;
// send the directive to the listener
Ok(Self::from_hash(d.lexeme()).str(d.lexeme()))
}
}
impl Display for Directive {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.1) }
}