1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
use crate::custom_types::Type;
use alloc::fmt::{Display, Formatter, Result as FmtResult};

/// An global object type name,either `static` or `const`.
#[derive(PartialEq, Eq, Debug)]
pub enum StaticType {
    Static,
    Const,
}

impl Display for StaticType {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        match self {
            Self::Const => write!(f, "const"),
            Self::Static => write!(f, "static"),
        }
    }
}

/// A global object interface;with their visibility,static type,name,value type and mutability.
pub struct StaticInterface<'a> {
    pub vis: &'a str,
    pub item_type: StaticType,
    pub name: &'a str,
    pub value_type: Type<'a>,
    pub mutability: bool,
}

impl<'a> StaticInterface<'a> {
    /// Construct a new StaticInterface from stringified rust syntax.
    pub fn new(mut a: &'a str) -> Result<Self, &'static str> {
        a = a.trim();

        let vis = if a.starts_with('p') {
            let mut index = match a.find(|c: char| c == ':') {
                Some(i) => i,
                None => return Err("No ':' in global object."),
            };

            let temp = &a[..index];

            index = if temp.contains("(") {
                match temp.find(')') {
                    Some(i) => i,
                    None => return Err("Detected unmatched '(' in visibility path."),
                }
            } else {
                match temp.find(|c: char| c.is_ascii_whitespace()) {
                    Some(i) => i - 1,
                    None => return Err("Expected whitespace after visibility keyword."),
                }
            };

            a = &a[index + 1..].trim();
            &temp[..=index]
        } else {
            ""
        };

        let (mut a, item_type) = Self::get_static_type(a);

        let item_type = item_type?;

        let mut mutability = false;

        let mut index = match a.find(|c: char| c.is_ascii_alphabetic()) {
            Some(i) => i,
            None => return Err("Expected letters after keyword."),
        };

        a = &a[index..];

        index = match a.find(|c: char| !c.is_ascii_alphabetic()) {
            Some(i) => i,
            None => return Err("Expected tokens after name or 'mut' keyword."),
        };

        let mut name = &a[..index];

        if item_type == StaticType::Static && name == "mut" {
            mutability = true;

            a = &a[index..];

            index = match a.find(|c: char| c.is_ascii_alphabetic()) {
                Some(i) => i,
                None => return Err("Expected letters after 'mut' keyword."),
            };

            a = &a[index..];

            index = match a.find(':') {
                Some(i) => i,
                None => return Err("Expected ':' after name."),
            };

            name = &a[..index].trim_end();
        }

        a = &a[index + 1..];

        index = a.find(';').unwrap_or_else(|| a.len());

        let value_type = Type::new((&a[..index]).trim());

        Ok(Self {
            vis,
            name,
            item_type,
            value_type,
            mutability,
        })
    }

    fn get_static_type(mut a: &str) -> (&str, Result<StaticType, &'static str>) {
        a = a.trim();

        let index = (match (&a[4..]).find(|c: char| c.is_ascii_whitespace()) {
            Some(i) => i,
            None => return (a, Err("Expected a whitespace after the keyword.")),
        }) + 4;

        let m = &a[..index];
        a = &a[index + 1..];

        (
            a,
            match m {
                "const" => Ok(StaticType::Const),
                "static" => Ok(StaticType::Static),
                _ => Err("Expected either 'const' or 'static' at start of the passed item."),
            },
        )
    }
}

impl<'a> Display for StaticInterface<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        if self.mutability {
            if self.vis == "" {
                write!(
                    f,
                    "{} mut {}: {};",
                    self.item_type, self.name, self.value_type
                )
            } else {
                write!(
                    f,
                    "{} {} mut {}: {};",
                    self.vis, self.item_type, self.name, self.value_type
                )
            }
        } else {
            if self.vis == "" {
                write!(f, "{} {}: {};", self.item_type, self.name, self.value_type)
            } else {
                write!(
                    f,
                    "{} {} {}: {};",
                    self.vis, self.item_type, self.name, self.value_type
                )
            }
        }
    }
}

/// A Static;with their interface and a value.
pub struct Static<'a> {
    pub interface: StaticInterface<'a>,
    pub value: &'a str,
}

impl<'a> Static<'a> {
    /// Constructs a new Static from stringified rust syntax.
    pub fn new(mut tp: &'a str) -> Result<Self, &'static str> {
        tp = tp.trim();
        let mut open_brace_count = 0;

        let mut index = match tp.find(|c: char| {
            if c == '<' || c == '(' || c == '[' || c == '{' {
                open_brace_count += 1;
                return false;
            }

            if  c == '>' || c == ')' || c == ']' || c == '}' {
                open_brace_count -= 1;
                return false;
            }

            if open_brace_count > 0 {
                return false;
            }

            c == '='
        }) {
            Some(i) => i,
            None => return Err("Expected assign operator '=' in the static item."),
        };

        let interface = StaticInterface::new(&tp[..index])?;

        tp = &tp[index + 1..];

        index = tp.rfind(';').unwrap_or_else(|| tp.len());

        Ok(Self {
            interface,
            value: &tp[..index].trim_start(),
        })
    }
}

impl<'a> Display for Static<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        if self.interface.mutability {
            if self.interface.vis == "" {
                write!(
                    f,
                    "{} mut {}: {} = {};",
                    self.interface.item_type,
                    self.interface.name,
                    self.interface.value_type,
                    self.value
                )
            } else {
                write!(
                    f,
                    "{} {} mut {}: {} = {};",
                    self.interface.vis,
                    self.interface.item_type,
                    self.interface.name,
                    self.interface.value_type,
                    self.value
                )
            }
        } else {
            if self.interface.vis == "" {
                write!(
                    f,
                    "{} {}: {} = {};",
                    self.interface.item_type,
                    self.interface.name,
                    self.interface.value_type,
                    self.value
                )
            } else {
                write!(
                    f,
                    "{} {} {}: {} = {};",
                    self.interface.vis,
                    self.interface.item_type,
                    self.interface.name,
                    self.interface.value_type,
                    self.value
                )
            }
        }
    }
}