hemtt_tokens/
lib.rs

1#![deny(clippy::all, clippy::nursery, missing_docs)]
2#![warn(clippy::pedantic)]
3
4//! HEMTT - Arma 3 Tokenizer
5
6mod position;
7mod symbol;
8pub mod whitespace;
9
10pub use position::Position;
11pub use symbol::Symbol;
12
13#[derive(Clone, Debug, PartialEq, Eq)]
14/// A token from the tokenizer
15pub struct Token {
16    symbol: Symbol,
17    source: Position,
18    parent: Option<Box<Self>>,
19}
20
21impl Token {
22    #[must_use]
23    /// Create a new token
24    pub const fn new(symbol: Symbol, source: Position, parent: Option<Box<Self>>) -> Self {
25        Self {
26            symbol,
27            source,
28            parent,
29        }
30    }
31
32    #[must_use]
33    /// Create a new token built-in token
34    pub fn builtin(parent: Option<Box<Self>>) -> Self {
35        Self {
36            symbol: Symbol::Void,
37            source: Position::builtin(),
38            parent,
39        }
40    }
41
42    #[must_use]
43    /// Create a newline token
44    pub fn ending_newline(parent: Option<Box<Self>>) -> Self {
45        Self {
46            symbol: Symbol::Newline,
47            source: Position::builtin(),
48            parent,
49        }
50    }
51
52    #[must_use]
53    /// Get the [`Symbol`] of the token
54    pub const fn symbol(&self) -> &Symbol {
55        &self.symbol
56    }
57
58    #[must_use]
59    /// Get the [`Position`] of the token
60    pub const fn source(&self) -> &Position {
61        &self.source
62    }
63
64    #[must_use]
65    /// Get the parent token
66    pub const fn parent(&self) -> &Option<Box<Self>> {
67        &self.parent
68    }
69
70    /// Set the parent token
71    pub fn set_parent(&mut self, parent: Option<Box<Self>>) {
72        self.parent = parent;
73    }
74
75    #[must_use]
76    /// Get the string value of a [`Symbol::Word`] token
77    pub const fn word(&self) -> Option<&String> {
78        if let Symbol::Word(word) = &self.symbol {
79            Some(word)
80        } else {
81            None
82        }
83    }
84
85    #[must_use]
86    /// For writing to a file for later parsing
87    pub fn to_source(&self) -> String {
88        if self.symbol == Symbol::Join {
89            String::new()
90        } else {
91            self.symbol.to_string()
92        }
93    }
94}
95
96impl ToString for Token {
97    fn to_string(&self) -> String {
98        self.symbol.to_string()
99    }
100}