daisy/src/parser/token/token.rs

86 lines
1.9 KiB
Rust
Raw Normal View History

2023-06-11 13:53:45 -07:00
use std::collections::VecDeque;
use crate::quantity::Quantity;
use super::Operator;
use super::Constant;
/// Tokens represent logical objects in an expession.
#[derive(Debug)]
#[derive(Clone)]
pub enum Token {
2023-06-14 14:04:32 -07:00
Variable(String),
2023-06-11 13:53:45 -07:00
Quantity(Quantity),
Constant(Constant),
Operator(Operator, VecDeque<Token>),
}
impl ToString for Token {
fn to_string(&self) -> String {
match self {
Token::Quantity(v) => v.to_string(),
Token::Constant(c) => c.to_string(),
2023-06-14 14:04:32 -07:00
Token::Variable(s) => s.clone(),
2023-06-11 13:53:45 -07:00
Token::Operator(o,a) => o.print(a)
}
}
}
impl Token {
// This is called only when this is the outermost token.
// This sometimes leads to different--usually more verbose--behavior.
pub fn to_string_outer(&self) -> String {
match self {
Token::Quantity(v) => v.to_string_outer(),
Token::Constant(c) => c.to_string(),
2023-06-14 14:04:32 -07:00
Token::Variable(s) => s.clone(),
2023-06-11 13:53:45 -07:00
Token::Operator(o,a) => o.print(a)
}
}
2023-06-14 20:18:28 -07:00
pub fn is_quantity(&self) -> bool {
match self {
Token::Quantity(_) => true,
_ => false
}
}
2023-06-11 13:53:45 -07:00
#[inline(always)]
pub fn get_args_mut(&mut self) -> Option<&mut VecDeque<Token>> {
match self {
Token::Operator(_, ref mut a) => Some(a),
_ => None
}
}
#[inline(always)]
2023-06-14 20:18:28 -07:00
pub fn get_args(&self) -> Option<&VecDeque<Token>> {
match self {
Token::Operator(_, ref a) => Some(a),
_ => None
}
}
2023-06-11 13:53:45 -07:00
2023-06-14 20:18:28 -07:00
#[inline(always)]
pub fn get_at_coords<'a, 'b, I>(&'a self, coords: I) -> Option<&'a Token>
where I: IntoIterator<Item = &'b usize> + Sized {
let mut g = self;
for t in coords.into_iter() {
let args = g.get_args();
let Some(args) = args else { return None; };
g = &args[*t];
2023-06-11 13:53:45 -07:00
}
2023-06-14 20:18:28 -07:00
return Some(g);
}
2023-06-11 13:53:45 -07:00
2023-06-14 20:18:28 -07:00
#[inline(always)]
pub fn get_at_coords_mut<'a, 'b, I>(&'a mut self, coords: I) -> Option<&'a mut Token>
where I: IntoIterator<Item = &'b usize> + Sized {
let mut g = self;
for t in coords.into_iter() {
let args = g.get_args_mut();
let Some(args) = args else { return None; };
g = &mut args[*t];
}
return Some(g);
2023-06-11 13:53:45 -07:00
}
}