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_PLUS_PERCENT
207 | DOT_MINUS_PERCENT
208 | DOT_STAR_PERCENT
209 | DOT_STAR_STAR
210 | DOT_MINUS_MINUS
211 | DOT_SLASH
212 | DOT_BACKSLASH
213 | DOT_SLASH_SLASH
214 | DOT_CARET
215 | DOT_PERCENT
216 | DOT_EQ
217 | DOT_EQ_EQ
218 | DOT_NOT_EQ
219 | DOT_EQ_EQ_EQ
220 | DOT_NOT_EQ_EQ
221 | DOT_LT
222 | DOT_LE
223 | DOT_GT
224 | DOT_GE
225 | DOT_SHL
226 | DOT_SHR
227 | DOT_USHR
228 | DOT_SUBTYPE
229 | DOT_SUPERTYPE
230 | DOT_FAT_ARROW
231 | DOT_LONG_ARROW
232 | DOT_LEFT_LONG_ARROW
233 | DOT_LEFT_RIGHT_ARROW
234 | DOT_PIPE_GT
235 | DOT_AMP
236 | DOT_PIPE
237 | DOT_BANG
238 | PLUS_EQ
239 | MINUS_EQ
240 | STAR_EQ
241 | SLASH_EQ
242 | BACKSLASH_EQ
243 | SLASH_SLASH_EQ
244 | CARET_EQ
245 | PERCENT_EQ
246 | PLUS_PERCENT_EQ
247 | MINUS_PERCENT_EQ
248 | STAR_PERCENT_EQ
249 | PIPE_EQ
250 | DOLLAR_EQ
251 | AMP_EQ
252 | SHL_EQ
253 | SHR_EQ
254 | USHR_EQ
255 | DIV_EQ
256 | XOR_EQ
257 | DOT_PLUS_EQ
258 | DOT_PLUS_PERCENT_EQ
259 | DOT_MINUS_PERCENT_EQ
260 | DOT_STAR_PERCENT_EQ
261 | DOT_AMP_EQ
262 | DOT_PIPE_EQ
263 | DOT_MINUS_EQ
264 | DOT_STAR_EQ
265 | DOT_SLASH_EQ
266 | DOT_BACKSLASH_EQ
267 | DOT_SLASH_SLASH_EQ
268 | DOT_CARET_EQ
269 | DOT_PERCENT_EQ
270 | DOT_SHL_EQ
271 | DOT_SHR_EQ
272 | DOT_USHR_EQ
273 | DOT_DIV_EQ
274 | DOT_XOR_EQ
275 | UNICODE_OP
276 | UNICODE_ASSIGN_OP
277 | UNICODE_RADICAL
278 )
279 }
280}
281
282/// Generate [`SyntaxKind::is_keyword`] from the shared keyword table, so the
283/// kind-side predicate cannot drift from the token-side one in the lexer.
284macro_rules! define_keyword_predicate {
285 ($($text:literal $tok:ident $syn:ident,)*) => {
286 impl SyntaxKind {
287 /// Whether this token kind is one of Julia's reserved words. The
288 /// value keywords `true`/`false` are included: they are keywords to
289 /// the lexer, whatever a consumer then makes of them (the sexpr
290 /// projector, for one, treats them as literals rather than syntax).
291 pub fn is_keyword(self) -> bool {
292 matches!(self, $(SyntaxKind::$syn)|*)
293 }
294 }
295 };
296}
297
298keyword_table!(define_keyword_predicate);
299
300impl From<SyntaxKind> for rowan::SyntaxKind {
301 fn from(kind: SyntaxKind) -> Self {
302 Self(kind as u16)
303 }
304}
305
306#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
307pub enum JuliaLanguage {}
308
309impl Language for JuliaLanguage {
310 type Kind = SyntaxKind;
311
312 fn kind_from_raw(raw: rowan::SyntaxKind) -> Self::Kind {
313 assert!(
314 raw.0 <= SyntaxKind::ERROR as u16,
315 "raw syntax kind {} out of range",
316 raw.0
317 );
318 // SAFETY: `SyntaxKind` is `#[repr(u16)]` with contiguous discriminants
319 // `0..=ERROR` and no holes, so any `u16` in that (asserted) range is a
320 // valid discriminant.
321 unsafe { std::mem::transmute::<u16, SyntaxKind>(raw.0) }
322 }
323
324 fn kind_to_raw(kind: Self::Kind) -> rowan::SyntaxKind {
325 kind.into()
326 }
327}
328
329pub type SyntaxNode = rowan::SyntaxNode<JuliaLanguage>;
330pub type SyntaxToken = rowan::SyntaxToken<JuliaLanguage>;
331pub type SyntaxElement = rowan::SyntaxElement<JuliaLanguage>;
332/// A node's identity without the tree: its kind plus its text range, resolvable
333/// back to a [`SyntaxNode`] against the root it came from. The key a per-file
334/// analysis uses to name a node it does not hold ([`rowan`]'s cursors are `Rc`
335/// based, so keeping one alive keeps the whole tree alive).
336pub type NodePtr = rowan::ast::SyntaxNodePtr<JuliaLanguage>;
337
338#[cfg(test)]
339mod tests {
340 use super::*;
341
342 #[test]
343 fn kind_round_trips_through_raw() {
344 for kind in [
345 SyntaxKind::ROOT,
346 SyntaxKind::STRING_LITERAL,
347 SyntaxKind::IDENT,
348 SyntaxKind::STRING_CONTENT,
349 SyntaxKind::FUNCTION_KW,
350 SyntaxKind::DOLLAR,
351 SyntaxKind::ERROR,
352 ] {
353 let raw = JuliaLanguage::kind_to_raw(kind);
354 assert_eq!(JuliaLanguage::kind_from_raw(raw), kind);
355 }
356 }
357}