Skip to main content

oak_dhall/parser/
element_type.rs

1use oak_core::{ElementType, UniversalElementRole};
2
3/// Element types for DHall AST.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6#[repr(u8)]
7pub enum DHallElementType {
8    /// Whitespace.
9    Whitespace,
10    /// Newline.
11    Newline,
12    /// Comment.
13    Comment,
14    /// Identifier.
15    Identifier,
16    /// Number.
17    Number,
18    /// String literal.
19    String,
20
21    // Keywords
22    /// `if`
23    If,
24    /// `then`
25    Then,
26    /// `else`
27    Else,
28    /// `let`
29    Let,
30    /// `in`
31    In,
32    /// `using`
33    Using,
34    /// `as`
35    As,
36    /// `merge`
37    Merge,
38    /// `Some`
39    Some,
40    /// `None`
41    None,
42    /// `NaN`
43    NaN,
44    /// `Infinity`
45    Infinity,
46    /// `Type`
47    Type,
48    /// `Kind`
49    Kind,
50    /// `Sort`
51    Sort,
52    /// `Bool`
53    Bool,
54    /// `Natural`
55    Natural,
56    /// `Integer`
57    Integer,
58    /// `Double`
59    Double,
60    /// `Text`
61    Text,
62    /// `List`
63    List,
64    /// `Optional`
65    Optional,
66    /// `True`
67    True,
68    /// `False`
69    False,
70    /// `with`
71    With,
72    /// `forall` or `∀`
73    Forall,
74    /// `assert`
75    Assert,
76
77    // Operators
78    /// `->` or `→`
79    Arrow,
80    /// `=>` or `⇒`
81    FatArrow,
82    /// `==`
83    EqualEqual,
84    /// `!=`
85    NotEqual,
86    /// `&&`
87    And,
88    /// `||`
89    Or,
90    /// `++`
91    Append,
92    /// `//`
93    Combine,
94    /// `//\\`
95    CombineTypes,
96    /// `///`
97    Prefer,
98    /// `\` or `λ`
99    Lambda,
100
101    // Punctuation
102    /// `(`
103    LeftParen,
104    /// `)`
105    RightParen,
106    /// `{`
107    LeftBrace,
108    /// `}`
109    RightBrace,
110    /// `[`
111    LeftBracket,
112    /// `]`
113    RightBracket,
114    /// `,`
115    Comma,
116    /// `;`
117    Semicolon,
118    /// `.`
119    Dot,
120    /// `:`
121    Colon,
122    /// `=`
123    Equal,
124    /// `<`
125    Less,
126    /// `>`
127    Greater,
128    /// `+`
129    Plus,
130    /// `-`
131    Minus,
132    /// `*`
133    Star,
134    /// `/`
135    Slash,
136    /// `|`
137    Pipe,
138    /// `@`
139    At,
140    /// `#`
141    Hash,
142    /// `?`
143    Question,
144    /// Error element.
145    Error,
146    /// End of file.
147    Eof,
148
149    // Special
150    /// Root node.
151    Root,
152    /// Source file node.
153    SourceFile,
154}
155
156impl DHallElementType {
157    /// Returns `true` if the element type is a keyword.
158    pub fn is_keyword(&self) -> bool {
159        matches!(
160            self,
161            Self::If
162                | Self::Then
163                | Self::Else
164                | Self::Let
165                | Self::In
166                | Self::Using
167                | Self::As
168                | Self::Merge
169                | Self::Some
170                | Self::None
171                | Self::NaN
172                | Self::Infinity
173                | Self::Type
174                | Self::Kind
175                | Self::Sort
176                | Self::Bool
177                | Self::Natural
178                | Self::Integer
179                | Self::Double
180                | Self::Text
181                | Self::List
182                | Self::Optional
183                | Self::True
184                | Self::False
185                | Self::With
186                | Self::Forall
187                | Self::Assert
188        )
189    }
190}
191
192impl ElementType for DHallElementType {
193    type Role = UniversalElementRole;
194
195    fn role(&self) -> Self::Role {
196        match self {
197            _ => UniversalElementRole::None,
198        }
199    }
200}
201
202impl From<crate::lexer::token_type::DHallTokenType> for DHallElementType {
203    fn from(token: crate::lexer::token_type::DHallTokenType) -> Self {
204        unsafe { std::mem::transmute(token) }
205    }
206}