Skip to main content

oak_purescript/lexer/
token_type.rs

1use oak_core::{Token, TokenType, UniversalTokenRole};
2
3/// Type alias for a PureScript token.
4pub type PurescriptToken = Token<PurescriptTokenType>;
5
6/// Token types for PureScript.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9pub enum PurescriptTokenType {
10    /// Whitespace.
11    Whitespace,
12    /// Newline.
13    Newline,
14    /// Comment.
15    Comment,
16    /// `ado`
17    Ado,
18    /// `case`
19    Case,
20    /// `class`
21    Class,
22    /// `data`
23    Data,
24    /// `derive`
25    Derive,
26    /// `do`
27    Do,
28    /// `else`
29    Else,
30    /// `false`
31    False,
32    /// `forall`
33    Forall,
34    /// `foreign`
35    Foreign,
36    /// `if`
37    If,
38    /// `import`
39    Import,
40    /// `in`
41    In,
42    /// `infix`
43    Infix,
44    /// `infixl`
45    Infixl,
46    /// `infixr`
47    Infixr,
48    /// `instance`
49    Instance,
50    /// `let`
51    Let,
52    /// `module`
53    Module,
54    /// `newtype`
55    Newtype,
56    /// `of`
57    Of,
58    /// `then`
59    Then,
60    /// `true`
61    True,
62    /// `type`
63    Type,
64    /// `where`
65    Where,
66    /// `->`
67    Arrow,
68    /// `=>`
69    FatArrow,
70    /// `\`
71    Backslash,
72    /// `|`
73    Pipe,
74    /// `=`
75    Equal,
76    /// `::`
77    ColonColon,
78    /// `.`
79    Dot,
80    /// `..`
81    DotDot,
82    /// `+`
83    Plus,
84    /// `-`
85    Minus,
86    /// `*`
87    Star,
88    /// `/`
89    Slash,
90    /// `%`
91    Percent,
92    /// `^`
93    Caret,
94    /// `==`
95    EqualEqual,
96    /// `/=`
97    NotEqual,
98    /// `<`
99    Less,
100    /// `>`
101    Greater,
102    /// `<=`
103    LessEqual,
104    /// `>=`
105    GreaterEqual,
106    /// `&&`
107    And,
108    /// `||`
109    Or,
110    /// `<>`
111    Append,
112    /// `<<<`
113    Compose,
114    /// `>>>`
115    ComposeFlipped,
116    /// `$`
117    Apply,
118    /// `#`
119    ApplyFlipped,
120    /// `>>=`
121    Bind,
122    /// `=<<`
123    BindFlipped,
124    /// `(`
125    LeftParen,
126    /// `)`
127    RightParen,
128    /// `{`
129    LeftBrace,
130    /// `}`
131    RightBrace,
132    /// `[`
133    LeftBracket,
134    /// `]`
135    RightBracket,
136    /// `,`
137    Comma,
138    /// `;`
139    Semicolon,
140    /// `:`
141    Colon,
142    /// `?`
143    Question,
144    /// `!`
145    Exclamation,
146    /// `@`
147    At,
148    /// `_`
149    Underscore,
150    /// `` ` ``
151    Tick,
152    /// Integer literal.
153    IntLiteral,
154    /// Floating point literal.
155    NumberLiteral,
156    /// String literal.
157    StringLiteral,
158    /// Character literal.
159    CharLiteral,
160    /// Boolean literal.
161    BooleanLiteral,
162    /// Lowercase identifier.
163    Identifier,
164    /// Uppercase identifier.
165    UpperIdentifier,
166    /// Operator.
167    Operator,
168    /// Qualified identifier.
169    QualifiedIdentifier,
170    /// Root node.
171    Root,
172    /// Source file node.
173    SourceFile,
174    /// Error token.
175    Error,
176    /// End of file.
177    Eof,
178}
179
180impl From<crate::parser::element_type::PurescriptElementType> for PurescriptTokenType {
181    fn from(element: crate::parser::element_type::PurescriptElementType) -> Self {
182        use crate::parser::element_type::PurescriptElementType as E;
183        match element {
184            E::Whitespace => Self::Whitespace,
185            E::Newline => Self::Newline,
186            E::Comment => Self::Comment,
187            E::Ado => Self::Ado,
188            E::Case => Self::Case,
189            E::Class => Self::Class,
190            E::Data => Self::Data,
191            E::Derive => Self::Derive,
192            E::Do => Self::Do,
193            E::Else => Self::Else,
194            E::False => Self::False,
195            E::Forall => Self::Forall,
196            E::Foreign => Self::Foreign,
197            E::If => Self::If,
198            E::Import => Self::Import,
199            E::In => Self::In,
200            E::Infix => Self::Infix,
201            E::Infixl => Self::Infixl,
202            E::Infixr => Self::Infixr,
203            E::Instance => Self::Instance,
204            E::Let => Self::Let,
205            E::Module => Self::Module,
206            E::Newtype => Self::Newtype,
207            E::Of => Self::Of,
208            E::Then => Self::Then,
209            E::True => Self::True,
210            E::Type => Self::Type,
211            E::Where => Self::Where,
212            E::Arrow => Self::Arrow,
213            E::FatArrow => Self::FatArrow,
214            E::Backslash => Self::Backslash,
215            E::Pipe => Self::Pipe,
216            E::Equal => Self::Equal,
217            E::ColonColon => Self::ColonColon,
218            E::Dot => Self::Dot,
219            E::DotDot => Self::DotDot,
220            E::Plus => Self::Plus,
221            E::Minus => Self::Minus,
222            E::Star => Self::Star,
223            E::Slash => Self::Slash,
224            E::Percent => Self::Percent,
225            E::Caret => Self::Caret,
226            E::EqualEqual => Self::EqualEqual,
227            E::NotEqual => Self::NotEqual,
228            E::Less => Self::Less,
229            E::Greater => Self::Greater,
230            E::LessEqual => Self::LessEqual,
231            E::GreaterEqual => Self::GreaterEqual,
232            E::And => Self::And,
233            E::Or => Self::Or,
234            E::Append => Self::Append,
235            E::Compose => Self::Compose,
236            E::ComposeFlipped => Self::ComposeFlipped,
237            E::Apply => Self::Apply,
238            E::ApplyFlipped => Self::ApplyFlipped,
239            E::Bind => Self::Bind,
240            E::BindFlipped => Self::BindFlipped,
241            E::LeftParen => Self::LeftParen,
242            E::RightParen => Self::RightParen,
243            E::LeftBrace => Self::LeftBrace,
244            E::RightBrace => Self::RightBrace,
245            E::LeftBracket => Self::LeftBracket,
246            E::RightBracket => Self::RightBracket,
247            E::Comma => Self::Comma,
248            E::Semicolon => Self::Semicolon,
249            E::Colon => Self::Colon,
250            E::Question => Self::Question,
251            E::Exclamation => Self::Exclamation,
252            E::At => Self::At,
253            E::Underscore => Self::Underscore,
254            E::Tick => Self::Tick,
255            E::IntLiteral => Self::IntLiteral,
256            E::NumberLiteral => Self::NumberLiteral,
257            E::StringLiteral => Self::StringLiteral,
258            E::CharLiteral => Self::CharLiteral,
259            E::BooleanLiteral => Self::BooleanLiteral,
260            E::Identifier => Self::Identifier,
261            E::UpperIdentifier => Self::UpperIdentifier,
262            E::Operator => Self::Operator,
263            E::QualifiedIdentifier => Self::QualifiedIdentifier,
264            E::Root => Self::Root,
265            E::SourceFile => Self::SourceFile,
266            E::Error => Self::Error,
267            E::Eof => Self::Eof,
268            _ => Self::Error,
269        }
270    }
271}
272
273impl TokenType for PurescriptTokenType {
274    type Role = UniversalTokenRole;
275    const END_OF_STREAM: Self = Self::Eof;
276
277    fn is_ignored(&self) -> bool {
278        match self {
279            Self::Whitespace | Self::Newline | Self::Comment => true,
280            _ => false,
281        }
282    }
283
284    fn role(&self) -> Self::Role {
285        match self {
286            Self::Whitespace | Self::Newline => UniversalTokenRole::Whitespace,
287            Self::Comment => UniversalTokenRole::Comment,
288            Self::Eof => UniversalTokenRole::Eof,
289            Self::Error => UniversalTokenRole::Error,
290            _ => UniversalTokenRole::None,
291        }
292    }
293}