erl_tokenize/
lexical_token.rs

1use std::fmt;
2
3use crate::tokens::{
4    AtomToken, CharToken, FloatToken, IntegerToken, KeywordToken, StringToken, SymbolToken,
5    VariableToken,
6};
7use crate::{Position, PositionRange};
8
9/// Lexical token.
10///
11/// This kind of token is meaningful in lexical analysis.
12#[allow(missing_docs)]
13#[derive(Debug, Clone)]
14pub enum LexicalToken {
15    Atom(AtomToken),
16    Char(CharToken),
17    Float(FloatToken),
18    Integer(IntegerToken),
19    Keyword(KeywordToken),
20    String(StringToken),
21    Symbol(SymbolToken),
22    Variable(VariableToken),
23}
24impl LexicalToken {
25    /// Returns the original textual representation of this token.
26    pub fn text(&self) -> &str {
27        match *self {
28            LexicalToken::Atom(ref t) => t.text(),
29            LexicalToken::Char(ref t) => t.text(),
30            LexicalToken::Float(ref t) => t.text(),
31            LexicalToken::Integer(ref t) => t.text(),
32            LexicalToken::Keyword(ref t) => t.text(),
33            LexicalToken::String(ref t) => t.text(),
34            LexicalToken::Symbol(ref t) => t.text(),
35            LexicalToken::Variable(ref t) => t.text(),
36        }
37    }
38
39    /// Tries to return the reference to the inner `AtomToken`.
40    pub fn as_atom_token(&self) -> Option<&AtomToken> {
41        if let LexicalToken::Atom(ref t) = *self {
42            Some(t)
43        } else {
44            None
45        }
46    }
47
48    /// Tries to return the reference to the inner `CharToken`.
49    pub fn as_char_token(&self) -> Option<&CharToken> {
50        if let LexicalToken::Char(ref t) = *self {
51            Some(t)
52        } else {
53            None
54        }
55    }
56
57    /// Tries to return the reference to the inner `FloatToken`.
58    pub fn as_float_token(&self) -> Option<&FloatToken> {
59        if let LexicalToken::Float(ref t) = *self {
60            Some(t)
61        } else {
62            None
63        }
64    }
65
66    /// Tries to return the reference to the inner `IntegerToken`.
67    pub fn as_integer_token(&self) -> Option<&IntegerToken> {
68        if let LexicalToken::Integer(ref t) = *self {
69            Some(t)
70        } else {
71            None
72        }
73    }
74
75    /// Tries to return the reference to the inner `KeywordToken`.
76    pub fn as_keyword_token(&self) -> Option<&KeywordToken> {
77        if let LexicalToken::Keyword(ref t) = *self {
78            Some(t)
79        } else {
80            None
81        }
82    }
83
84    /// Tries to return the reference to the inner `StringToken`.
85    pub fn as_string_token(&self) -> Option<&StringToken> {
86        if let LexicalToken::String(ref t) = *self {
87            Some(t)
88        } else {
89            None
90        }
91    }
92
93    /// Tries to return the reference to the inner `SymbolToken`.
94    pub fn as_symbol_token(&self) -> Option<&SymbolToken> {
95        if let LexicalToken::Symbol(ref t) = *self {
96            Some(t)
97        } else {
98            None
99        }
100    }
101
102    /// Tries to return the reference to the inner `VariableToken`.
103    pub fn as_variable_token(&self) -> Option<&VariableToken> {
104        if let LexicalToken::Variable(ref t) = *self {
105            Some(t)
106        } else {
107            None
108        }
109    }
110
111    /// Tries to return the inner `AtomToken`.
112    pub fn into_atom_token(self) -> Result<AtomToken, Self> {
113        if let LexicalToken::Atom(t) = self {
114            Ok(t)
115        } else {
116            Err(self)
117        }
118    }
119
120    /// Tries to return the inner `CharToken`.
121    pub fn into_char_token(self) -> Result<CharToken, Self> {
122        if let LexicalToken::Char(t) = self {
123            Ok(t)
124        } else {
125            Err(self)
126        }
127    }
128
129    /// Tries to return the inner `FloatToken`.
130    pub fn into_float_token(self) -> Result<FloatToken, Self> {
131        if let LexicalToken::Float(t) = self {
132            Ok(t)
133        } else {
134            Err(self)
135        }
136    }
137
138    /// Tries to return the inner `IntegerToken`.
139    pub fn into_integer_token(self) -> Result<IntegerToken, Self> {
140        if let LexicalToken::Integer(t) = self {
141            Ok(t)
142        } else {
143            Err(self)
144        }
145    }
146
147    /// Tries to return the inner `KeywordToken`.
148    pub fn into_keyword_token(self) -> Result<KeywordToken, Self> {
149        if let LexicalToken::Keyword(t) = self {
150            Ok(t)
151        } else {
152            Err(self)
153        }
154    }
155
156    /// Tries to return the inner `StringToken`.
157    pub fn into_string_token(self) -> Result<StringToken, Self> {
158        if let LexicalToken::String(t) = self {
159            Ok(t)
160        } else {
161            Err(self)
162        }
163    }
164
165    /// Tries to return the inner `SymbolToken`.
166    pub fn into_symbol_token(self) -> Result<SymbolToken, Self> {
167        if let LexicalToken::Symbol(t) = self {
168            Ok(t)
169        } else {
170            Err(self)
171        }
172    }
173
174    /// Tries to return the inner `VariableToken`.
175    pub fn into_variable_token(self) -> Result<VariableToken, Self> {
176        if let LexicalToken::Variable(t) = self {
177            Ok(t)
178        } else {
179            Err(self)
180        }
181    }
182}
183impl From<AtomToken> for LexicalToken {
184    fn from(f: AtomToken) -> Self {
185        LexicalToken::Atom(f)
186    }
187}
188impl From<CharToken> for LexicalToken {
189    fn from(f: CharToken) -> Self {
190        LexicalToken::Char(f)
191    }
192}
193impl From<FloatToken> for LexicalToken {
194    fn from(f: FloatToken) -> Self {
195        LexicalToken::Float(f)
196    }
197}
198impl From<IntegerToken> for LexicalToken {
199    fn from(f: IntegerToken) -> Self {
200        LexicalToken::Integer(f)
201    }
202}
203impl From<KeywordToken> for LexicalToken {
204    fn from(f: KeywordToken) -> Self {
205        LexicalToken::Keyword(f)
206    }
207}
208impl From<StringToken> for LexicalToken {
209    fn from(f: StringToken) -> Self {
210        LexicalToken::String(f)
211    }
212}
213impl From<SymbolToken> for LexicalToken {
214    fn from(f: SymbolToken) -> Self {
215        LexicalToken::Symbol(f)
216    }
217}
218impl From<VariableToken> for LexicalToken {
219    fn from(f: VariableToken) -> Self {
220        LexicalToken::Variable(f)
221    }
222}
223impl PositionRange for LexicalToken {
224    fn start_position(&self) -> Position {
225        match *self {
226            LexicalToken::Atom(ref t) => t.start_position(),
227            LexicalToken::Char(ref t) => t.start_position(),
228            LexicalToken::Float(ref t) => t.start_position(),
229            LexicalToken::Integer(ref t) => t.start_position(),
230            LexicalToken::Keyword(ref t) => t.start_position(),
231            LexicalToken::String(ref t) => t.start_position(),
232            LexicalToken::Symbol(ref t) => t.start_position(),
233            LexicalToken::Variable(ref t) => t.start_position(),
234        }
235    }
236    fn end_position(&self) -> Position {
237        match *self {
238            LexicalToken::Atom(ref t) => t.end_position(),
239            LexicalToken::Char(ref t) => t.end_position(),
240            LexicalToken::Float(ref t) => t.end_position(),
241            LexicalToken::Integer(ref t) => t.end_position(),
242            LexicalToken::Keyword(ref t) => t.end_position(),
243            LexicalToken::String(ref t) => t.end_position(),
244            LexicalToken::Symbol(ref t) => t.end_position(),
245            LexicalToken::Variable(ref t) => t.end_position(),
246        }
247    }
248}
249impl fmt::Display for LexicalToken {
250    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
251        self.text().fmt(f)
252    }
253}