ellie_core 0.7.3

Core modules for ellie
Documentation
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
use alloc::{string::String, vec::Vec};

#[cfg(feature = "compiler_utils")]
use alloc::{borrow::ToOwned, format};
use core::fmt::{Display, Error, Formatter};

#[cfg(feature = "compiler_utils")]
use regex::Regex;

#[cfg(feature = "compiler_utils")]
use serde::{Deserialize, Serialize};

#[cfg(feature = "compiler_utils")]
#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
#[derive(Default)]
pub enum TokenizerType {
    #[default]
    Raw,
    ClassParser,
    FunctionParser,
    HeaderParser,
}

#[cfg(feature = "compiler_utils")]

#[cfg(feature = "compiler_utils")]
#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
pub struct TokenizerOptions {
    pub path: String,
    pub functions: bool,
    pub break_on_error: bool,
    pub loops: bool,
    pub enums: bool,
    pub classes: bool,
    pub getters: bool,
    pub setters: bool,
    pub conditions: bool,
    pub global_variables: bool,
    pub line_ending: String,
    pub dynamics: bool,
    pub collectives: bool,
    pub variables: bool,
    pub import_std: bool,
    pub constants: bool,
    pub ignore_imports: bool,
    pub parser_type: TokenizerType,
    pub allow_import: bool,
}

#[cfg(feature = "compiler_utils")]
impl Default for TokenizerOptions {
    fn default() -> Self {
        TokenizerOptions {
            path: "".to_owned(),
            functions: true,
            break_on_error: false,
            loops: true,
            conditions: true,
            getters: true,
            setters: true,
            classes: true,
            enums: true,
            global_variables: true,
            line_ending: "\\r\\n".to_owned(),
            dynamics: true,
            import_std: true,
            collectives: true,
            ignore_imports: false,
            variables: true,
            constants: true,
            parser_type: TokenizerType::Raw,
            allow_import: true,
        }
    }
}

/// A struct that represents a position in a file.
/// (line, column)
#[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize, Default)]
#[cfg(feature = "compiler_utils")]
pub struct CursorPosition(pub usize, pub usize);

/// A struct that represents a position in a file.
/// (line, column)
#[cfg(not(feature = "compiler_utils"))]
#[derive(PartialEq, Debug, Clone, Copy, Default)]
pub struct CursorPosition(pub usize, pub usize);

impl core::fmt::Display for CursorPosition {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{}:{}", self.0, self.1)
    }
}

impl CursorPosition {
    pub fn is_bigger(&self, other: &CursorPosition) -> bool {
        self.0 > other.0 || (self.0 == other.0 && self.1 > other.1)
    }

    pub fn skip_char(&mut self, n: usize) -> CursorPosition {
        let mut clone = *self;
        clone.1 += n;
        clone
    }

    pub fn pop_char(&mut self, n: usize) -> CursorPosition {
        let mut clone = *self;
        if clone.1 != 0 {
            clone.1 -= n;
        }
        clone
    }

    pub fn is_zero(&self) -> bool {
        self.0 == 0 && self.1 == 0
    }

    pub fn increase_line(&mut self, n: usize) -> CursorPosition {
        let mut clone = *self;
        clone.0 += n;
        clone
    }
}

/// Cursor position
/// ## Fields
/// * `range_start` - Start of range [`CursorPosition`]
/// * `range_end` - End of range [`CursorPosition`]
#[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize, Default)]
#[cfg(feature = "compiler_utils")]
pub struct Cursor {
    pub range_start: CursorPosition,
    pub range_end: CursorPosition,
}

/// Cursor position
/// ## Fields
/// * `range_start` - Start of range [`CursorPosition`]
/// * `range_end` - End of range [`CursorPosition`]
#[derive(PartialEq, Debug, Clone, Copy, Default)]
#[cfg(not(feature = "compiler_utils"))]
pub struct Cursor {
    pub range_start: CursorPosition,
    pub range_end: CursorPosition,
}

impl Cursor {
    /// Check range_start and range_end is zero by column and lines
    pub fn is_zero(&self) -> bool {
        self.range_start.is_zero() && self.range_end.is_zero()
    }

    /// Check current cursor bigger than given [`Cursor`]
    /// ## Arguments
    /// * `cursor` - [`Cursor`] to compare
    pub fn is_bigger(&self, than: Cursor) -> bool {
        if than.range_end.0 == self.range_end.0 {
            self.range_end.1 > than.range_end.1
        } else {
            than.range_end.0 <= self.range_end.0
        }
    }

    /// Create new [`Cursor`] range start and skip one column pos to define the end
    /// ## Arguments
    /// * `start` - Start of range [`CursorPosition`]
    pub fn build_with_skip_char(range_start: CursorPosition) -> Self {
        Cursor {
            range_start,
            range_end: range_start.clone().skip_char(1),
        }
    }

    /// Create new [`Cursor`]
    /// ## Arguments
    /// * `start` - Start of range [`CursorPosition`]
    pub fn build_from_cursor(range_start: CursorPosition) -> Self {
        Cursor {
            range_start,
            range_end: range_start,
        }
    }

    /// Gets [`Cursor`] range end and skip one char
    /// ## Arguments
    /// * `n` - Number of chars to skip
    /// ## Returns
    /// [`Cursor`] with new range end
    pub fn range_end_skip_char(&self, n: usize) -> Self {
        self.range_end.clone().skip_char(n);
        *self
    }

    /// Gets [`Cursor`] range start and skip one char
    /// ## Arguments
    /// * `n` - Number of chars to skip
    /// ## Returns
    /// [`Cursor`] with new range start and end
    pub fn range_start_skip_char(&self, n: usize) -> Self {
        self.range_start.clone().skip_char(n);
        *self
    }
}

/// Version
/// ## Fields
/// * `major` - Major version [`u8`]
/// * `minor` - Minor version [`u8`]
/// * `bug` - Bug version [`u8`]
#[cfg(feature = "compiler_utils")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Version {
    pub major: usize,
    pub minor: usize,
    pub patch: usize,
    pub pre_release: Option<String>,
    pub build_metadata: Option<String>,
}

#[cfg(feature = "compiler_utils")]
impl PartialEq for Version {
    fn eq(&self, other: &Self) -> bool {
        //Ignore bug
        self.minor == other.minor && self.major == other.major
    }
}

#[cfg(feature = "compiler_utils")]
impl Version {
    /// Create new [`Version`] from given [`String`]
    /// ## Arguments
    /// * `version` - [`String`] to parse
    pub fn build_from_string(input: &String) -> Version {
        let semver_regex = Regex::new(r"^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$").unwrap();
        let caps = semver_regex.captures(input).unwrap();

        Version {
            major: caps
                .name("major")
                .unwrap()
                .as_str()
                .parse::<usize>()
                .unwrap(),
            minor: caps
                .name("minor")
                .unwrap()
                .as_str()
                .parse::<usize>()
                .unwrap(),
            patch: caps
                .name("patch")
                .unwrap()
                .as_str()
                .parse::<usize>()
                .unwrap(),
            pre_release: caps.name("prerelease").map(|x| x.as_str().to_owned()),
            build_metadata: caps.name("buildmetadata").map(|x| x.as_str().to_owned()),
        }
    }

    /// Create new [`Version`] from given [`String`] with checks
    /// ## Arguments
    /// * `input` - [`String`] to parse
    /// ## Return
    /// [`Result`] - If versionb is valid [`Ok(Version)`] otherwise [`Err(u8)`]-
    pub fn build_from_string_checked(input: &String) -> Result<Version, u8> {
        let semver_regex = Regex::new(r"^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$").unwrap();
        match semver_regex.captures(input) {
            Some(caps) => {
                let major = caps
                    .name("major")
                    .unwrap()
                    .as_str()
                    .parse::<usize>()
                    .unwrap_or(0);
                let minor = caps
                    .name("minor")
                    .unwrap()
                    .as_str()
                    .parse::<usize>()
                    .unwrap_or(0);
                let patch = caps
                    .name("patch")
                    .unwrap()
                    .as_str()
                    .parse::<usize>()
                    .unwrap_or(0);

                let pre_release = caps.name("prerelease").map(|x| x.as_str().to_owned());
                let build_metadata = caps.name("buildmetadata").map(|x| x.as_str().to_owned());

                if major == 0 && minor == 0 && patch == 0 {
                    Err(1)
                } else {
                    Ok(Version {
                        minor,
                        major,
                        patch,
                        pre_release,
                        build_metadata,
                    })
                }
            }
            None => Err(1),
        }
    }

    pub fn to_string(&self) -> String {
        format!(
            "{}.{}.{}{}{}",
            self.major,
            self.minor,
            self.patch,
            if let Some(ref pre_release) = self.pre_release {
                format!("-{}", pre_release)
            } else {
                "".to_owned()
            },
            if let Some(ref build_metadata) = self.build_metadata {
                format!("+{}", build_metadata)
            } else {
                "".to_owned()
            }
        )
    }
}

#[derive(Clone, Debug, PartialEq, Copy)]
pub enum PlatformArchitecture {
    B16,
    B32,
    B64,
}

impl Display for PlatformArchitecture {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        match self {
            PlatformArchitecture::B16 => write!(f, "b16"),
            PlatformArchitecture::B32 => write!(f, "b32"),
            PlatformArchitecture::B64 => write!(f, "b64"),
        }
    }
}

impl PlatformArchitecture {
    pub fn is_16(&self) -> bool {
        match self {
            PlatformArchitecture::B16 => true,
            _ => false,
        }
    }

    pub fn is_32(&self) -> bool {
        match self {
            PlatformArchitecture::B32 => true,
            _ => false,
        }
    }

    pub fn get_code(&self) -> u8 {
        match self {
            PlatformArchitecture::B16 => 16,
            PlatformArchitecture::B32 => 32,
            PlatformArchitecture::B64 => 64,
        }
    }

    pub fn type_id_size(&self) -> u8 {
        match self {
            PlatformArchitecture::B16 => 3,
            PlatformArchitecture::B32 => 5,
            PlatformArchitecture::B64 => 9,
        }
    }

    pub fn usize_len(&self) -> u8 {
        match self {
            PlatformArchitecture::B16 => 2,
            PlatformArchitecture::B32 => 4,
            PlatformArchitecture::B64 => 8,
        }
    }

    pub fn from_byte(byte: u8) -> Option<PlatformArchitecture> {
        match byte {
            16 => Some(PlatformArchitecture::B16),
            32 => Some(PlatformArchitecture::B32),
            64 => Some(PlatformArchitecture::B64),
            _ => None,
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub enum DebugHeaderType {
    Variable,
    SetterCall,
    GetterCall,
    Class,
    Parameter,
    Function,
    NativeFunction,
    Condition,
}

#[derive(Clone, Debug)]
pub struct DebugHeader {
    /// Element Type
    pub rtype: DebugHeaderType,
    /// Element's hash
    pub hash: usize,
    /// Module Name
    pub module_name: String,
    /// Module Hash
    pub module_hash: usize,
    /// Element Name
    pub name: String,
    /// Instruction start -> end,
    pub start_end: (usize, usize),
    /// Code pos
    pub pos: Cursor,
}

#[derive(Debug, Clone)]
pub struct ModuleMap {
    pub module_name: String,
    pub module_hash: usize,
    pub module_path: Option<String>,
}

#[derive(Debug, Clone)]
pub struct NativeCallTrace {
    pub module_name: String,
    pub function_hash: usize,
    pub function_name: String,
}

#[derive(Debug, Clone)]
pub struct DebugInfo {
    pub module_map: Vec<ModuleMap>,
    pub debug_headers: Vec<DebugHeader>,
}