fatou_parser/syntax.rs
1//! The Julia syntax-kind set and the `rowan` [`Language`] binding.
2//!
3//! [`SyntaxKind`] holds both token kinds and node kinds in a single
4//! `#[repr(u16)]` enum (rust-analyzer style). The variants are contiguous and
5//! `ERROR` is the last one, so [`JuliaLanguage::kind_from_raw`] recovers a kind
6//! from its raw `u16` with a bounds-checked transmute instead of a large match.
7
8use rowan::Language;
9
10use crate::keywords::keyword_table;
11use crate::tokens::token_table;
12
13/// Generate [`SyntaxKind`] from the shared token table. The node kinds — which
14/// no token materializes as — are written out here; the token kinds come from
15/// the table, so a `TokKind` can never be lexed into a kind that does not exist.
16macro_rules! define_syntax_kind {
17 ($($(#[$meta:meta])* $tok:ident $syn:ident,)*) => {
18#[allow(non_camel_case_types)]
19#[derive(
20 Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
21)]
22#[repr(u16)]
23pub enum SyntaxKind {
24 // --- Nodes ---
25 ROOT,
26 LITERAL,
27 STRING_LITERAL,
28 CMD_LITERAL,
29 NONSTANDARD_IDENTIFIER,
30 INTERPOLATION,
31 NAME,
32 BINARY_EXPR,
33 RANGE_EXPR,
34 COMPARISON_EXPR,
35 UNARY_EXPR,
36 PAREN_EXPR,
37 TUPLE_EXPR,
38 BARE_TUPLE_EXPR,
39 PAREN_BLOCK,
40 TOPLEVEL_SEMICOLON,
41 DOC,
42 VECT_EXPR,
43 MATRIX_EXPR,
44 MATRIX_ROW,
45 TYPED_MATRIX_EXPR,
46 BRACESCAT_EXPR,
47 COMPREHENSION,
48 BRACES_COMPREHENSION,
49 TYPED_COMPREHENSION,
50 GENERATOR,
51 COMPREHENSION_IF,
52 CALL_EXPR,
53 INDEX_EXPR,
54 DOT_CALL_EXPR,
55 CURLY_EXPR,
56 BRACES,
57 ARG_LIST,
58 ARG,
59 KEYWORD_ARG,
60 PARAMETERS,
61 TYPE_ANNOTATION,
62 WHERE_EXPR,
63 SPLAT_EXPR,
64 POSTFIX_EXPR,
65 END_MARKER,
66 BEGIN_MARKER,
67 OPERATOR_ATOM,
68 ASSIGNMENT_EXPR,
69 ARROW_EXPR,
70 JUXTAPOSE_EXPR,
71 TERNARY_EXPR,
72 IF_EXPR,
73 ELSEIF_CLAUSE,
74 ELSE_CLAUSE,
75 CONDITION,
76 FUNCTION_DEF,
77 MACRO_DEF,
78 SIGNATURE,
79 BLOCK,
80 BEGIN_EXPR,
81 WHILE_EXPR,
82 FOR_EXPR,
83 FOR_BINDING,
84 /// The `outer i` pattern of an iteration spec: the contextual `outer`
85 /// keyword plus the loop variable it rebinds from the enclosing scope.
86 OUTER_BINDING,
87 LET_EXPR,
88 LET_BINDINGS,
89 QUOTE_EXPR,
90 QUOTE_SYM,
91 TRY_EXPR,
92 CATCH_CLAUSE,
93 FINALLY_CLAUSE,
94 STRUCT_DEF,
95 ABSTRACT_DEF,
96 PRIMITIVE_DEF,
97 TYPEGROUP_DEF,
98 MODULE_DEF,
99 DO_EXPR,
100 DO_PARAMS,
101 RETURN_EXPR,
102 BREAK_EXPR,
103 CONTINUE_EXPR,
104 CONST_STMT,
105 GLOBAL_STMT,
106 LOCAL_STMT,
107 IMPORT_STMT,
108 USING_STMT,
109 EXPORT_STMT,
110 PUBLIC_STMT,
111 IMPORT_PATH,
112 IMPORT_ALIAS,
113 MACRO_CALL,
114 MACRO_NAME,
115
116 // --- Tokens, from the shared token table ---
117 $($(#[$meta])* $syn,)*
118
119 /// The kind every one of the six `call-i` Unicode operator tiers
120 /// materializes as: the tier distinction the parser binds by is a `TokKind`
121 /// concern, and here only the projection shape matters. (The assignment
122 /// tier and the radicals do project their own heads, so they keep their own
123 /// kinds above.)
124 UNICODE_OP,
125
126 /// The error-recovery node kind: unknown tokens and recovered runs. Projected
127 /// `(error)`, or `(error-t)` for the byte-bearing recovery runs that the
128 /// projector identifies from the diagnostics side-channel. Recovery that is
129 /// merely *absent* (missing `end`, disallowed whitespace) lives only in the
130 /// diagnostics, not the tree (the rust-analyzer model). Keep this the **last**
131 /// variant: [`JuliaLanguage::kind_from_raw`] uses it as the upper bound of the
132 /// valid discriminant range.
133 ERROR,
134}
135 };
136}
137
138token_table!(define_syntax_kind);
139
140impl SyntaxKind {
141 /// The number of distinct kinds. The discriminants are contiguous from
142 /// `ROOT` (0) through `ERROR`, so this is the size of a flat table indexed
143 /// by `kind as usize` (used by the linter's node-dispatch table).
144 pub const COUNT: usize = SyntaxKind::ERROR as usize + 1;
145
146 /// Whether this token kind is an operator symbol (including dotted,
147 /// assignment, and unicode forms). One list shared by the sexpr
148 /// projector and the semantic builder (operator names are importable
149 /// and exportable: `import A: +`, `export ==`).
150 pub fn is_operator(self) -> bool {
151 use SyntaxKind::*;
152 let kind = self;
153 matches!(
154 kind,
155 EQ | PLUS
156 | MINUS
157 | STAR
158 | STAR_STAR
159 | MINUS_MINUS
160 | SLASH
161 | BACKSLASH
162 | SLASH_SLASH
163 | CARET
164 | PERCENT
165 | PLUS_PERCENT
166 | PLUS_PLUS
167 | MINUS_PERCENT
168 | STAR_PERCENT
169 | EQ_EQ
170 | NOT_EQ
171 | EQ_EQ_EQ
172 | NOT_EQ_EQ
173 | LT
174 | LE
175 | GT
176 | GE
177 | AND_AND
178 | OR_OR
179 | DOT_AND_AND
180 | DOT_OR_OR
181 | COLON
182 | COLON_EQ
183 | DOT_DOT
184 | COLON_COLON
185 | TILDE
186 | DOT_TILDE
187 | SUBTYPE
188 | SUPERTYPE
189 | ARROW
190 | LONG_ARROW
191 | LEFT_RIGHT_ARROW
192 | LEFT_LONG_ARROW
193 | FAT_ARROW
194 | SHL
195 | SHR
196 | USHR
197 | DOT
198 | PIPE_GT
199 | PIPE_LT
200 | BANG
201 | AMP
202 | PIPE
203 | DOT_PLUS
204 | DOT_MINUS
205 | DOT_STAR
206 | DOT_STAR_STAR
207 | DOT_MINUS_MINUS
208 | DOT_SLASH
209 | DOT_BACKSLASH
210 | DOT_SLASH_SLASH
211 | DOT_CARET
212 | DOT_PERCENT
213 | DOT_EQ
214 | DOT_EQ_EQ
215 | DOT_NOT_EQ
216 | DOT_EQ_EQ_EQ
217 | DOT_NOT_EQ_EQ
218 | DOT_LT
219 | DOT_LE
220 | DOT_GT
221 | DOT_GE
222 | DOT_SHL
223 | DOT_SHR
224 | DOT_USHR
225 | DOT_SUBTYPE
226 | DOT_SUPERTYPE
227 | DOT_FAT_ARROW
228 | DOT_LONG_ARROW
229 | DOT_LEFT_LONG_ARROW
230 | DOT_LEFT_RIGHT_ARROW
231 | DOT_PIPE_GT
232 | DOT_AMP
233 | DOT_PIPE
234 | DOT_BANG
235 | PLUS_EQ
236 | MINUS_EQ
237 | STAR_EQ
238 | SLASH_EQ
239 | BACKSLASH_EQ
240 | SLASH_SLASH_EQ
241 | CARET_EQ
242 | PERCENT_EQ
243 | PLUS_PERCENT_EQ
244 | MINUS_PERCENT_EQ
245 | STAR_PERCENT_EQ
246 | PIPE_EQ
247 | DOLLAR_EQ
248 | AMP_EQ
249 | SHL_EQ
250 | SHR_EQ
251 | USHR_EQ
252 | DIV_EQ
253 | XOR_EQ
254 | DOT_PLUS_EQ
255 | DOT_AMP_EQ
256 | DOT_PIPE_EQ
257 | DOT_MINUS_EQ
258 | DOT_STAR_EQ
259 | DOT_SLASH_EQ
260 | DOT_BACKSLASH_EQ
261 | DOT_SLASH_SLASH_EQ
262 | DOT_CARET_EQ
263 | DOT_PERCENT_EQ
264 | DOT_SHL_EQ
265 | DOT_SHR_EQ
266 | DOT_USHR_EQ
267 | DOT_DIV_EQ
268 | DOT_XOR_EQ
269 | UNICODE_OP
270 | UNICODE_ASSIGN_OP
271 | UNICODE_RADICAL
272 )
273 }
274}
275
276/// Generate [`SyntaxKind::is_keyword`] from the shared keyword table, so the
277/// kind-side predicate cannot drift from the token-side one in the lexer.
278macro_rules! define_keyword_predicate {
279 ($($text:literal $tok:ident $syn:ident,)*) => {
280 impl SyntaxKind {
281 /// Whether this token kind is one of Julia's reserved words. The
282 /// value keywords `true`/`false` are included: they are keywords to
283 /// the lexer, whatever a consumer then makes of them (the sexpr
284 /// projector, for one, treats them as literals rather than syntax).
285 pub fn is_keyword(self) -> bool {
286 matches!(self, $(SyntaxKind::$syn)|*)
287 }
288 }
289 };
290}
291
292keyword_table!(define_keyword_predicate);
293
294impl From<SyntaxKind> for rowan::SyntaxKind {
295 fn from(kind: SyntaxKind) -> Self {
296 Self(kind as u16)
297 }
298}
299
300#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
301pub enum JuliaLanguage {}
302
303impl Language for JuliaLanguage {
304 type Kind = SyntaxKind;
305
306 fn kind_from_raw(raw: rowan::SyntaxKind) -> Self::Kind {
307 assert!(
308 raw.0 <= SyntaxKind::ERROR as u16,
309 "raw syntax kind {} out of range",
310 raw.0
311 );
312 // SAFETY: `SyntaxKind` is `#[repr(u16)]` with contiguous discriminants
313 // `0..=ERROR` and no holes, so any `u16` in that (asserted) range is a
314 // valid discriminant.
315 unsafe { std::mem::transmute::<u16, SyntaxKind>(raw.0) }
316 }
317
318 fn kind_to_raw(kind: Self::Kind) -> rowan::SyntaxKind {
319 kind.into()
320 }
321}
322
323pub type SyntaxNode = rowan::SyntaxNode<JuliaLanguage>;
324pub type SyntaxToken = rowan::SyntaxToken<JuliaLanguage>;
325pub type SyntaxElement = rowan::SyntaxElement<JuliaLanguage>;
326/// A node's identity without the tree: its kind plus its text range, resolvable
327/// back to a [`SyntaxNode`] against the root it came from. The key a per-file
328/// analysis uses to name a node it does not hold ([`rowan`]'s cursors are `Rc`
329/// based, so keeping one alive keeps the whole tree alive).
330pub type NodePtr = rowan::ast::SyntaxNodePtr<JuliaLanguage>;
331
332#[cfg(test)]
333mod tests {
334 use super::*;
335
336 #[test]
337 fn kind_round_trips_through_raw() {
338 for kind in [
339 SyntaxKind::ROOT,
340 SyntaxKind::STRING_LITERAL,
341 SyntaxKind::IDENT,
342 SyntaxKind::STRING_CONTENT,
343 SyntaxKind::FUNCTION_KW,
344 SyntaxKind::DOLLAR,
345 SyntaxKind::ERROR,
346 ] {
347 let raw = JuliaLanguage::kind_to_raw(kind);
348 assert_eq!(JuliaLanguage::kind_from_raw(raw), kind);
349 }
350 }
351}