|
| 1 | +use crate::{parser::keyword::types::{attributes::AttributeKeyword, functions::FunctionKeyword}, Number, EMPTY}; |
| 2 | +use core::{fmt, mem}; |
| 3 | + |
| 4 | +#[derive(Debug, PartialEq, Eq)] |
| 5 | +pub enum Attribute { |
| 6 | + Indirection, |
| 7 | + Keyword(AttributeKeyword), |
| 8 | + User(String), |
| 9 | +} |
| 10 | + |
| 11 | +#[expect(clippy::min_ident_chars)] |
| 12 | +impl fmt::Display for Attribute { |
| 13 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 14 | + match self { |
| 15 | + Self::Indirection => '*'.fmt(f), |
| 16 | + Self::Keyword(keywd) => keywd.fmt(f), |
| 17 | + Self::User(val) => write!(f, "'{val}'"), |
| 18 | + } |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +#[derive(Debug, PartialEq)] |
| 23 | +pub enum Literal { |
| 24 | + Char(char), |
| 25 | + ConstantBool(bool), |
| 26 | + Empty, |
| 27 | + Nullptr, |
| 28 | + Number(Number), |
| 29 | + Str(String), |
| 30 | + Variable(Variable), |
| 31 | +} |
| 32 | + |
| 33 | +#[expect(clippy::min_ident_chars)] |
| 34 | +impl fmt::Display for Literal { |
| 35 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 36 | + match self { |
| 37 | + Self::Empty => EMPTY.fmt(f), |
| 38 | + Self::Nullptr => "NULL".fmt(f), |
| 39 | + Self::Char(val) => write!(f, "'{val}'"), |
| 40 | + Self::Str(val) => write!(f, "\"{val}\""), |
| 41 | + Self::Number(val) => val.fmt(f), |
| 42 | + Self::ConstantBool(val) => val.fmt(f), |
| 43 | + Self::Variable(val) => val.fmt(f), |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | +#[derive(Debug, PartialEq, Default, Eq)] |
| 50 | +pub struct Variable { |
| 51 | + pub attrs: Vec<Attribute>, |
| 52 | + pub name: VariableName, |
| 53 | +} |
| 54 | + |
| 55 | +impl Variable { |
| 56 | + pub const fn from_keyword(keyword: FunctionKeyword) -> Self { |
| 57 | + Self { |
| 58 | + name: VariableName::Keyword(keyword), |
| 59 | + attrs: vec![], |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + pub fn push_attr(&mut self, attr: AttributeKeyword) { |
| 64 | + self.attrs.push(Attribute::Keyword(attr)); |
| 65 | + } |
| 66 | + |
| 67 | + pub fn push_indirection(&mut self) -> Result<(), String> { |
| 68 | + match mem::take(&mut self.name) { |
| 69 | + VariableName::Empty => (), |
| 70 | + VariableName::Keyword(keywd) => return Err(format!("Found '*' after function keyword {keywd}.")), |
| 71 | + VariableName::UserDefined(name) => self.attrs.push(Attribute::User(name)), |
| 72 | + } |
| 73 | + self.attrs.push(Attribute::Indirection); |
| 74 | + Ok(()) |
| 75 | + } |
| 76 | + |
| 77 | + pub fn push_str(&mut self, value: String) -> Result<(), String> { |
| 78 | + match mem::take(&mut self.name) { |
| 79 | + VariableName::Empty => {self.name = VariableName::UserDefined(value); Ok(())}, |
| 80 | + VariableName::Keyword(keyword) => Err(format!("Found 2 successive literals, found identifier {value} after function keyword {keyword}.")), |
| 81 | + VariableName::UserDefined(old) => { |
| 82 | + self.attrs.push(Attribute::User(old)); |
| 83 | + self.name = VariableName::UserDefined(value); |
| 84 | + Ok(()) |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | +} |
| 90 | + |
| 91 | +impl From<String> for Variable { |
| 92 | + fn from(name: String) -> Self { |
| 93 | + Self { |
| 94 | + name: VariableName::UserDefined(name), |
| 95 | + attrs: vec![], |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +impl From<AttributeKeyword> for Variable { |
| 101 | + fn from(attr: AttributeKeyword) -> Self { |
| 102 | + Self { |
| 103 | + name: VariableName::Empty, |
| 104 | + attrs: vec![Attribute::Keyword(attr)], |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +#[expect(clippy::min_ident_chars)] |
| 110 | +impl fmt::Display for Variable { |
| 111 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 112 | + if self.attrs.is_empty() { |
| 113 | + self.name.fmt(f) |
| 114 | + } else { |
| 115 | + write!( |
| 116 | + f, |
| 117 | + "({} {})", |
| 118 | + self.attrs |
| 119 | + .iter() |
| 120 | + .map(|attr| format!("{attr}")) |
| 121 | + .collect::<Vec<_>>() |
| 122 | + .join(" "), |
| 123 | + self.name |
| 124 | + ) |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +#[derive(Debug, PartialEq, Eq, Default)] |
| 130 | +pub enum VariableName { |
| 131 | + #[default] |
| 132 | + Empty, |
| 133 | + Keyword(FunctionKeyword), |
| 134 | + UserDefined(String), |
| 135 | +} |
| 136 | + |
| 137 | +impl From<&str> for VariableName { |
| 138 | + fn from(name: &str) -> Self { |
| 139 | + Self::UserDefined(name.to_owned()) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +#[expect(clippy::min_ident_chars)] |
| 144 | +impl fmt::Display for VariableName { |
| 145 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 146 | + match self { |
| 147 | + Self::Empty => EMPTY.fmt(f), |
| 148 | + Self::UserDefined(val) => val.fmt(f), |
| 149 | + Self::Keyword(val) => val.fmt(f), |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments