oak_dhall/kind/mod.rs
1use oak_core::{ElementType, Token, TokenType, UniversalElementRole, UniversalTokenRole};
2use serde::{Deserialize, Serialize};
3
4pub type DHallToken = Token<DHallSyntaxKind>;
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
7pub enum DHallSyntaxKind {
8 /// Whitespace characters
9 Whitespace,
10 /// Newline character
11 Newline,
12 /// Comment text
13 Comment,
14 /// Identifier name
15 Identifier,
16 /// Number literal
17 Number,
18 /// String literal
19 String,
20
21 // Keywords
22 /// The 'if' keyword
23 If,
24 /// The 'then' keyword
25 Then,
26 /// The 'else' keyword
27 Else,
28 /// The 'let' keyword
29 Let,
30 /// The 'in' keyword
31 In,
32 /// The 'using' keyword
33 Using,
34 /// The 'as' keyword
35 As,
36 /// The 'merge' keyword
37 Merge,
38 /// The 'Some' keyword
39 Some,
40 /// The 'None' keyword
41 None,
42 /// The 'NaN' keyword
43 NaN,
44 /// The 'Infinity' keyword
45 Infinity,
46 /// The 'Type' keyword
47 Type,
48 /// The 'Kind' keyword
49 Kind,
50 /// The 'Sort' keyword
51 Sort,
52 /// The 'Bool' keyword
53 Bool,
54 /// The 'Natural' keyword
55 Natural,
56 /// The 'Integer' keyword
57 Integer,
58 /// The 'Double' keyword
59 Double,
60 /// The 'Text' keyword
61 Text,
62 /// The 'List' keyword
63 List,
64 /// The 'Optional' keyword
65 Optional,
66 /// The 'True' keyword
67 True,
68 /// The 'False' keyword
69 False,
70 /// The 'with' keyword
71 With,
72 /// The 'forall' keyword
73 Forall,
74 /// The 'assert' keyword
75 Assert,
76
77 // Operators
78 /// Arrow operator (-> or →)
79 Arrow,
80 /// Fat arrow operator (=>)
81 FatArrow,
82 /// Equality operator (== or ≡)
83 EqualEqual,
84 /// Inequality operator (!=)
85 NotEqual,
86 /// Logical AND (&& or ∧)
87 And,
88 /// Logical OR (|| or ∨)
89 Or,
90 /// List concatenation (++)
91 Append,
92 /// Record combination (// or ⫽)
93 Combine,
94 /// Type combination (/\ or ⩓)
95 CombineTypes,
96 /// Record preference (//\)
97 Prefer,
98 /// Lambda operator (\ or λ)
99 Lambda,
100
101 // Punctuation
102 /// Left parenthesis (()
103 LeftParen,
104 /// Right parenthesis ())
105 RightParen,
106 /// Left brace ({)
107 LeftBrace,
108 /// Right brace (})
109 RightBrace,
110 /// Left bracket ([)
111 LeftBracket,
112 /// Right bracket (])
113 RightBracket,
114 /// Comma (,)
115 Comma,
116 /// Semicolon (;)
117 Semicolon,
118 /// Dot (.)
119 Dot,
120 /// Colon (:)
121 Colon,
122 /// Assignment operator (=)
123 Equal,
124 /// Less than operator (<)
125 Less,
126 /// Greater than operator (>)
127 Greater,
128 /// Plus operator (+)
129 Plus,
130 /// Minus operator (-)
131 Minus,
132 /// Multiplication operator (*)
133 Star,
134 /// Division operator (/)
135 Slash,
136 /// Pipe operator (|)
137 Pipe,
138 /// At symbol (@)
139 At,
140 /// Hash symbol (#)
141 Hash,
142 /// Question mark (?)
143 Question,
144
145 /// Error token
146 Error,
147 /// End of file marker
148 Eof,
149
150 // Special
151 Root,
152 SourceFile,
153}
154
155impl TokenType for DHallSyntaxKind {
156 const END_OF_STREAM: Self = Self::Eof;
157 type Role = UniversalTokenRole;
158
159 fn role(&self) -> Self::Role {
160 match self {
161 Self::Whitespace | Self::Newline => UniversalTokenRole::Whitespace,
162 Self::Comment => UniversalTokenRole::Comment,
163 Self::Identifier => UniversalTokenRole::Name,
164 Self::Number | Self::String => UniversalTokenRole::Literal,
165 Self::Eof => UniversalTokenRole::Eof,
166 Self::Error => UniversalTokenRole::Error,
167 _ if self.is_keyword() => UniversalTokenRole::Keyword,
168 Self::Plus | Self::Minus | Self::Star | Self::Slash | Self::Equal | Self::Arrow | Self::FatArrow | Self::EqualEqual | Self::NotEqual | Self::And | Self::Or | Self::Append | Self::Combine | Self::CombineTypes | Self::Prefer | Self::Lambda => {
169 UniversalTokenRole::Operator
170 }
171 _ => UniversalTokenRole::Punctuation,
172 }
173 }
174}
175
176impl DHallSyntaxKind {
177 pub fn is_keyword(&self) -> bool {
178 matches!(
179 self,
180 Self::If
181 | Self::Then
182 | Self::Else
183 | Self::Let
184 | Self::In
185 | Self::Using
186 | Self::As
187 | Self::Merge
188 | Self::Some
189 | Self::None
190 | Self::NaN
191 | Self::Infinity
192 | Self::Type
193 | Self::Kind
194 | Self::Sort
195 | Self::Bool
196 | Self::Natural
197 | Self::Integer
198 | Self::Double
199 | Self::Text
200 | Self::List
201 | Self::Optional
202 | Self::True
203 | Self::False
204 | Self::With
205 | Self::Forall
206 | Self::Assert
207 )
208 }
209}
210
211impl ElementType for DHallSyntaxKind {
212 type Role = UniversalElementRole;
213
214 fn role(&self) -> Self::Role {
215 match self {
216 Self::Root | Self::SourceFile => UniversalElementRole::Root,
217 Self::Error => UniversalElementRole::Error,
218 _ => UniversalElementRole::None,
219 }
220 }
221}