Skip to main content

oak_apl/lexer/
token_type.rs

1use oak_core::UniversalTokenRole;
2
3/// Token types for the APL language.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub enum AplTokenType {
7    /// Whitespace.
8    Whitespace,
9    /// A newline.
10    Newline,
11    /// A comment (⍝).
12    Comment,
13
14    /// A string literal.
15    StringLiteral,
16    /// A number literal.
17    NumberLiteral,
18    /// An identifier.
19    Identifier,
20
21    // Core Symbols
22    /// Left arrow (←).
23    LeftArrow,
24    /// Right arrow (→).
25    RightArrow,
26    /// Diamond (⋄).
27    Diamond,
28    /// Quad (⎕).
29    Quad,
30    /// Quote quad (⍞).
31    QuoteQuad,
32    /// Rho (⍴).
33    Rho,
34    /// Iota (⍳).
35    Iota,
36    /// Epsilon (∊).
37    Epsilon,
38    /// Up arrow (↑).
39    UpArrow,
40    /// Down arrow (↓).
41    DownArrow,
42    /// Del (∇).
43    Del,
44    /// Delta (∆).
45    Delta,
46    /// Alpha (⍺).
47    Alpha,
48    /// Omega (⍵).
49    Omega,
50    /// Zilde (⍬).
51    Zilde,
52
53    // Operators/Functions
54    /// Plus (+).
55    Plus,
56    /// Minus (-).
57    Minus,
58    /// Times (×).
59    Times,
60    /// Divide (÷).
61    Divide,
62    /// Star (*).
63    Star,
64    /// Log (⍟).
65    Log,
66    /// Circle (○).
67    Circle,
68    /// Or (∨).
69    Or,
70    /// And (∧).
71    And,
72    /// Not (∼).
73    Not,
74    /// Nor (⍱).
75    Nor,
76    /// Nand (⍲).
77    Nand,
78    /// Equal (=).
79    Equal,
80    /// Not equal (≠).
81    NotEqual,
82    /// Less than (<).
83    LessThan,
84    /// Less equal (≤).
85    LessEqual,
86    /// Greater equal (≥).
87    GreaterEqual,
88    /// Greater than (>).
89    GreaterThan,
90    /// Up stile (⌈).
91    UpStile,
92    /// Down stile (⌊).
93    DownStile,
94    /// Bar (|).
95    Bar,
96    /// Tilde (∼).
97    Tilde,
98    /// Question (?).
99    Question,
100    /// Factorial (!).
101    Factorial,
102
103    // Operators (Higher Order)
104    /// Slash (/).
105    Slash,
106    /// Backslash (\).
107    Backslash,
108    /// Slash bar (⌿).
109    SlashBar,
110    /// Backslash bar (⍀).
111    BackslashBar,
112    /// Dot (.).
113    Dot,
114    /// Jot (∘).
115    Jot,
116    /// Diaeresis (¨).
117    Diaeresis,
118    /// Power (⍣).
119    Power,
120    /// Rank (⍤).
121    Rank,
122    /// Tally (≢).
123    Tally,
124
125    // Structural
126    /// Left parenthesis (().
127    LeftParen,
128    /// Right parenthesis ()).
129    RightParen,
130    /// Left bracket ([).
131    LeftBracket,
132    /// Right bracket (]).
133    RightBracket,
134    /// Left brace ({).
135    LeftBrace,
136    /// Right brace (}).
137    RightBrace,
138    /// Semicolon (;).
139    Semicolon,
140
141    /// End of stream.
142    Eof,
143    /// An error token.
144    Error,
145}
146
147impl AplTokenType {
148    /// Returns true if this token is an identifier.
149    pub fn is_identifier(&self) -> bool {
150        matches!(self, Self::Identifier | Self::Alpha | Self::Omega)
151    }
152
153    /// Returns true if this token is a literal.
154    pub fn is_literal(&self) -> bool {
155        matches!(self, Self::StringLiteral | Self::NumberLiteral | Self::Zilde)
156    }
157}
158
159/// Type alias for `AplTokenType`.
160pub type TokenType = AplTokenType;
161
162impl oak_core::TokenType for AplTokenType {
163    type Role = UniversalTokenRole;
164    const END_OF_STREAM: Self = Self::Eof;
165
166    fn is_comment(&self) -> bool {
167        matches!(self, Self::Comment)
168    }
169
170    fn is_whitespace(&self) -> bool {
171        matches!(self, Self::Whitespace | Self::Newline)
172    }
173
174    fn is_error(&self) -> bool {
175        matches!(self, Self::Error)
176    }
177
178    fn role(&self) -> Self::Role {
179        use UniversalTokenRole::*;
180        match self {
181            Self::Identifier | Self::Alpha | Self::Omega => Name,
182            Self::StringLiteral | Self::NumberLiteral | Self::Zilde => Literal,
183            Self::Comment => Comment,
184            Self::Whitespace | Self::Newline => Whitespace,
185            Self::Error => Error,
186            Self::Eof => Eof,
187
188            // Core symbols and operators
189            Self::LeftArrow
190            | Self::RightArrow
191            | Self::Plus
192            | Self::Minus
193            | Self::Times
194            | Self::Divide
195            | Self::Star
196            | Self::Log
197            | Self::Circle
198            | Self::Or
199            | Self::And
200            | Self::Not
201            | Self::Nor
202            | Self::Nand
203            | Self::Equal
204            | Self::NotEqual
205            | Self::LessThan
206            | Self::LessEqual
207            | Self::GreaterEqual
208            | Self::GreaterThan
209            | Self::UpStile
210            | Self::DownStile
211            | Self::Bar
212            | Self::Tilde
213            | Self::Question
214            | Self::Factorial
215            | Self::Slash
216            | Self::Backslash
217            | Self::SlashBar
218            | Self::BackslashBar
219            | Self::Dot
220            | Self::Jot
221            | Self::Diaeresis
222            | Self::Power
223            | Self::Rank
224            | Self::Tally
225            | Self::Rho
226            | Self::Iota
227            | Self::Epsilon
228            | Self::UpArrow
229            | Self::DownArrow => Operator,
230
231            Self::Diamond | Self::Quad | Self::QuoteQuad | Self::Del | Self::Delta | Self::LeftParen | Self::RightParen | Self::LeftBracket | Self::RightBracket | Self::LeftBrace | Self::RightBrace | Self::Semicolon => Punctuation,
232        }
233    }
234}