Skip to main content

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
10#[allow(non_camel_case_types)]
11#[derive(
12    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
13)]
14#[repr(u16)]
15pub enum SyntaxKind {
16    // --- Nodes ---
17    ROOT,
18    LITERAL,
19    STRING_LITERAL,
20    CMD_LITERAL,
21    NONSTANDARD_IDENTIFIER,
22    INTERPOLATION,
23    NAME,
24    BINARY_EXPR,
25    RANGE_EXPR,
26    COMPARISON_EXPR,
27    UNARY_EXPR,
28    PAREN_EXPR,
29    TUPLE_EXPR,
30    BARE_TUPLE_EXPR,
31    PAREN_BLOCK,
32    TOPLEVEL_SEMICOLON,
33    DOC,
34    VECT_EXPR,
35    MATRIX_EXPR,
36    MATRIX_ROW,
37    TYPED_MATRIX_EXPR,
38    BRACESCAT_EXPR,
39    COMPREHENSION,
40    BRACES_COMPREHENSION,
41    TYPED_COMPREHENSION,
42    GENERATOR,
43    COMPREHENSION_IF,
44    CALL_EXPR,
45    INDEX_EXPR,
46    DOT_CALL_EXPR,
47    CURLY_EXPR,
48    BRACES,
49    ARG_LIST,
50    ARG,
51    KEYWORD_ARG,
52    PARAMETERS,
53    TYPE_ANNOTATION,
54    WHERE_EXPR,
55    SPLAT_EXPR,
56    POSTFIX_EXPR,
57    END_MARKER,
58    BEGIN_MARKER,
59    OPERATOR_ATOM,
60    ASSIGNMENT_EXPR,
61    ARROW_EXPR,
62    JUXTAPOSE_EXPR,
63    TERNARY_EXPR,
64    IF_EXPR,
65    ELSEIF_CLAUSE,
66    ELSE_CLAUSE,
67    CONDITION,
68    FUNCTION_DEF,
69    MACRO_DEF,
70    SIGNATURE,
71    BLOCK,
72    BEGIN_EXPR,
73    WHILE_EXPR,
74    FOR_EXPR,
75    FOR_BINDING,
76    LET_EXPR,
77    LET_BINDINGS,
78    QUOTE_EXPR,
79    QUOTE_SYM,
80    TRY_EXPR,
81    CATCH_CLAUSE,
82    FINALLY_CLAUSE,
83    STRUCT_DEF,
84    ABSTRACT_DEF,
85    PRIMITIVE_DEF,
86    TYPEGROUP_DEF,
87    MODULE_DEF,
88    DO_EXPR,
89    DO_PARAMS,
90    RETURN_EXPR,
91    BREAK_EXPR,
92    CONTINUE_EXPR,
93    CONST_STMT,
94    GLOBAL_STMT,
95    LOCAL_STMT,
96    IMPORT_STMT,
97    USING_STMT,
98    EXPORT_STMT,
99    PUBLIC_STMT,
100    IMPORT_PATH,
101    IMPORT_ALIAS,
102    MACRO_CALL,
103    MACRO_NAME,
104
105    // --- Trivia tokens ---
106    WHITESPACE,
107    NEWLINE,
108    COMMENT,
109    BLOCK_COMMENT,
110
111    // --- Literal / identifier tokens ---
112    IDENT,
113    INTEGER,
114    BIN_INT,
115    OCT_INT,
116    HEX_INT,
117    FLOAT,
118    FLOAT32,
119    /// A malformed numeric literal Julia keeps as a single error token (a hex
120    /// float with an empty `p`/`P` exponent, or a hex constant with no mantissa
121    /// digits). Projects to `(ErrorInvalidNumericConstant)`.
122    ERROR_INVALID_NUMBER,
123    /// A hex float with a `.` fraction but no `p`/`P` binary exponent. Projects
124    /// to `(ErrorHexFloatMustContainP)`.
125    ERROR_HEX_FLOAT_NO_P,
126    /// A stray character Julia does not recognize (a subscript that cannot start
127    /// an identifier, a lone unknown glyph). Projects to `(ErrorUnknownCharacter)`.
128    ERROR_UNKNOWN_CHAR,
129    CHAR,
130    STRING_DELIM_OPEN,
131    STRING_DELIM_CLOSE,
132    CMD_DELIM_OPEN,
133    CMD_DELIM_CLOSE,
134    STRING_CONTENT,
135    STRING_PREFIX,
136    STRING_SUFFIX,
137
138    // --- Keyword tokens ---
139    FUNCTION_KW,
140    MACRO_KW,
141    END_KW,
142    IF_KW,
143    ELSEIF_KW,
144    ELSE_KW,
145    BEGIN_KW,
146    TRUE_KW,
147    FALSE_KW,
148    WHILE_KW,
149    FOR_KW,
150    LET_KW,
151    QUOTE_KW,
152    TRY_KW,
153    CATCH_KW,
154    FINALLY_KW,
155    STRUCT_KW,
156    MUTABLE_KW,
157    MODULE_KW,
158    BAREMODULE_KW,
159    DO_KW,
160    RETURN_KW,
161    BREAK_KW,
162    CONTINUE_KW,
163    CONST_KW,
164    GLOBAL_KW,
165    LOCAL_KW,
166    IMPORT_KW,
167    USING_KW,
168    EXPORT_KW,
169    WHERE_KW,
170
171    // --- Operator tokens ---
172    EQ,
173    PLUS,
174    MINUS,
175    STAR,
176    SLASH,
177    BACKSLASH,
178    SLASH_SLASH,
179    CARET,
180    PERCENT,
181    /// The wrapping arithmetic operators `+%`, `-%`, `*%` (Julia 1.14). Each
182    /// keeps its own name in an ordinary infix call (`(call-i a +% b)`).
183    PLUS_PERCENT,
184    /// The `+`-tier operator name `++`.
185    PLUS_PLUS,
186    MINUS_PERCENT,
187    STAR_PERCENT,
188    // Invalid doubled operators `**`/`--` (project `(Error**)` /
189    // `(ErrorInvalidOperator)`).
190    STAR_STAR,
191    MINUS_MINUS,
192    EQ_EQ,
193    NOT_EQ,
194    EQ_EQ_EQ,
195    NOT_EQ_EQ,
196    LT,
197    LE,
198    GT,
199    GE,
200    AND_AND,
201    OR_OR,
202    COLON,
203    COLON_COLON,
204    SUBTYPE,
205    SUPERTYPE,
206    ARROW,
207    LONG_ARROW,
208    LEFT_RIGHT_ARROW,
209    LEFT_LONG_ARROW,
210    FAT_ARROW,
211    SHL,
212    SHR,
213    USHR,
214    // Augmented (compound) assignment operators `op=`.
215    PLUS_EQ,
216    MINUS_EQ,
217    STAR_EQ,
218    SLASH_EQ,
219    BACKSLASH_EQ,
220    SLASH_SLASH_EQ,
221    CARET_EQ,
222    PERCENT_EQ,
223    /// Augmented assignment for the wrapping arithmetic operators: `+%=`, `-%=`,
224    /// `*%=`.
225    PLUS_PERCENT_EQ,
226    MINUS_PERCENT_EQ,
227    STAR_PERCENT_EQ,
228    PIPE_EQ,
229    DOLLAR_EQ,
230    AMP_EQ,
231    SHL_EQ,
232    SHR_EQ,
233    USHR_EQ,
234    DIV_EQ,
235    XOR_EQ,
236    DOT,
237    DOT_DOT,
238    DOT_DOT_DOT,
239    PIPE_GT,
240    PIPE_LT,
241    BANG,
242    AMP,
243    PIPE,
244    TILDE,
245    QUESTION,
246    TRANSPOSE,
247
248    // --- Broadcasting (dotted) operator tokens ---
249    DOT_PLUS,
250    DOT_MINUS,
251    DOT_STAR,
252    // Broadcast invalid doubled operators `.**`/`.--`.
253    DOT_STAR_STAR,
254    DOT_MINUS_MINUS,
255    DOT_SLASH,
256    DOT_BACKSLASH,
257    DOT_SLASH_SLASH,
258    DOT_CARET,
259    DOT_PERCENT,
260    DOT_EQ,
261    DOT_EQ_EQ,
262    DOT_NOT_EQ,
263    DOT_EQ_EQ_EQ,
264    DOT_NOT_EQ_EQ,
265    DOT_LT,
266    DOT_LE,
267    DOT_GT,
268    DOT_GE,
269    DOT_SHL,
270    DOT_SHR,
271    DOT_USHR,
272    DOT_SUBTYPE,
273    DOT_SUPERTYPE,
274    DOT_FAT_ARROW,
275    DOT_LONG_ARROW,
276    DOT_LEFT_LONG_ARROW,
277    DOT_LEFT_RIGHT_ARROW,
278    DOT_PIPE_GT,
279    DOT_TILDE,
280    DOT_AND_AND,
281    DOT_OR_OR,
282    DOT_AMP,
283    DOT_PIPE,
284    /// Broadcast unary-not `.!`.
285    DOT_BANG,
286    // Broadcast augmented assignment `.op=`.
287    DOT_PLUS_EQ,
288    DOT_AMP_EQ,
289    DOT_PIPE_EQ,
290    DOT_MINUS_EQ,
291    DOT_STAR_EQ,
292    DOT_SLASH_EQ,
293    DOT_BACKSLASH_EQ,
294    DOT_SLASH_SLASH_EQ,
295    DOT_CARET_EQ,
296    DOT_PERCENT_EQ,
297    DOT_SHL_EQ,
298    DOT_SHR_EQ,
299    DOT_USHR_EQ,
300    DOT_DIV_EQ,
301    DOT_XOR_EQ,
302
303    /// The assignment-tier operator `:=`. Parses like the Unicode `≔`
304    /// (right-associative, loose as `=`) and projects with its own head.
305    COLON_EQ,
306
307    // Single-codepoint Unicode operator tokens. The tier distinctions the parser
308    // needs live in the `TokKind`; here only the projection shape matters, so the
309    // six `call-i` tiers collapse to `UNICODE_OP`, the assignment tier projects
310    // its own head, and the radicals are prefix-only.
311    UNICODE_OP,
312    UNICODE_ASSIGN_OP,
313    UNICODE_RADICAL,
314
315    // --- Delimiter / punctuation tokens ---
316    LPAREN,
317    RPAREN,
318    LBRACKET,
319    RBRACKET,
320    LBRACE,
321    RBRACE,
322    COMMA,
323    SEMICOLON,
324    AT,
325    DOLLAR,
326
327    /// The error-recovery node kind: unknown tokens and recovered runs. Projected
328    /// `(error)`, or `(error-t)` for the byte-bearing recovery runs that the
329    /// projector identifies from the diagnostics side-channel. Recovery that is
330    /// merely *absent* (missing `end`, disallowed whitespace) lives only in the
331    /// diagnostics, not the tree (the rust-analyzer model). Keep this the **last**
332    /// variant: [`JuliaLanguage::kind_from_raw`] uses it as the upper bound of the
333    /// valid discriminant range.
334    ERROR,
335}
336
337impl SyntaxKind {
338    /// The number of distinct kinds. The discriminants are contiguous from
339    /// `ROOT` (0) through `ERROR`, so this is the size of a flat table indexed
340    /// by `kind as usize` (used by the linter's node-dispatch table).
341    pub const COUNT: usize = SyntaxKind::ERROR as usize + 1;
342
343    /// Whether this token kind is an operator symbol (including dotted,
344    /// assignment, and unicode forms). One list shared by the sexpr
345    /// projector and the semantic builder (operator names are importable
346    /// and exportable: `import A: +`, `export ==`).
347    pub fn is_operator(self) -> bool {
348        use SyntaxKind::*;
349        let kind = self;
350        matches!(
351            kind,
352            EQ | PLUS
353                | MINUS
354                | STAR
355                | STAR_STAR
356                | MINUS_MINUS
357                | SLASH
358                | BACKSLASH
359                | SLASH_SLASH
360                | CARET
361                | PERCENT
362                | PLUS_PERCENT
363                | PLUS_PLUS
364                | MINUS_PERCENT
365                | STAR_PERCENT
366                | EQ_EQ
367                | NOT_EQ
368                | EQ_EQ_EQ
369                | NOT_EQ_EQ
370                | LT
371                | LE
372                | GT
373                | GE
374                | AND_AND
375                | OR_OR
376                | DOT_AND_AND
377                | DOT_OR_OR
378                | COLON
379                | COLON_EQ
380                | DOT_DOT
381                | COLON_COLON
382                | TILDE
383                | DOT_TILDE
384                | SUBTYPE
385                | SUPERTYPE
386                | ARROW
387                | LONG_ARROW
388                | LEFT_RIGHT_ARROW
389                | LEFT_LONG_ARROW
390                | FAT_ARROW
391                | SHL
392                | SHR
393                | USHR
394                | DOT
395                | PIPE_GT
396                | PIPE_LT
397                | BANG
398                | AMP
399                | PIPE
400                | DOT_PLUS
401                | DOT_MINUS
402                | DOT_STAR
403                | DOT_STAR_STAR
404                | DOT_MINUS_MINUS
405                | DOT_SLASH
406                | DOT_BACKSLASH
407                | DOT_SLASH_SLASH
408                | DOT_CARET
409                | DOT_PERCENT
410                | DOT_EQ
411                | DOT_EQ_EQ
412                | DOT_NOT_EQ
413                | DOT_EQ_EQ_EQ
414                | DOT_NOT_EQ_EQ
415                | DOT_LT
416                | DOT_LE
417                | DOT_GT
418                | DOT_GE
419                | DOT_SHL
420                | DOT_SHR
421                | DOT_USHR
422                | DOT_SUBTYPE
423                | DOT_SUPERTYPE
424                | DOT_FAT_ARROW
425                | DOT_LONG_ARROW
426                | DOT_LEFT_LONG_ARROW
427                | DOT_LEFT_RIGHT_ARROW
428                | DOT_PIPE_GT
429                | DOT_AMP
430                | DOT_PIPE
431                | DOT_BANG
432                | PLUS_EQ
433                | MINUS_EQ
434                | STAR_EQ
435                | SLASH_EQ
436                | BACKSLASH_EQ
437                | SLASH_SLASH_EQ
438                | CARET_EQ
439                | PERCENT_EQ
440                | PLUS_PERCENT_EQ
441                | MINUS_PERCENT_EQ
442                | STAR_PERCENT_EQ
443                | PIPE_EQ
444                | DOLLAR_EQ
445                | AMP_EQ
446                | SHL_EQ
447                | SHR_EQ
448                | USHR_EQ
449                | DIV_EQ
450                | XOR_EQ
451                | DOT_PLUS_EQ
452                | DOT_AMP_EQ
453                | DOT_PIPE_EQ
454                | DOT_MINUS_EQ
455                | DOT_STAR_EQ
456                | DOT_SLASH_EQ
457                | DOT_BACKSLASH_EQ
458                | DOT_SLASH_SLASH_EQ
459                | DOT_CARET_EQ
460                | DOT_PERCENT_EQ
461                | DOT_SHL_EQ
462                | DOT_SHR_EQ
463                | DOT_USHR_EQ
464                | DOT_DIV_EQ
465                | DOT_XOR_EQ
466                | UNICODE_OP
467                | UNICODE_ASSIGN_OP
468                | UNICODE_RADICAL
469        )
470    }
471}
472
473impl From<SyntaxKind> for rowan::SyntaxKind {
474    fn from(kind: SyntaxKind) -> Self {
475        Self(kind as u16)
476    }
477}
478
479#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
480pub enum JuliaLanguage {}
481
482impl Language for JuliaLanguage {
483    type Kind = SyntaxKind;
484
485    fn kind_from_raw(raw: rowan::SyntaxKind) -> Self::Kind {
486        assert!(
487            raw.0 <= SyntaxKind::ERROR as u16,
488            "raw syntax kind {} out of range",
489            raw.0
490        );
491        // SAFETY: `SyntaxKind` is `#[repr(u16)]` with contiguous discriminants
492        // `0..=ERROR` and no holes, so any `u16` in that (asserted) range is a
493        // valid discriminant.
494        unsafe { std::mem::transmute::<u16, SyntaxKind>(raw.0) }
495    }
496
497    fn kind_to_raw(kind: Self::Kind) -> rowan::SyntaxKind {
498        kind.into()
499    }
500}
501
502pub type SyntaxNode = rowan::SyntaxNode<JuliaLanguage>;
503pub type SyntaxToken = rowan::SyntaxToken<JuliaLanguage>;
504pub type SyntaxElement = rowan::SyntaxElement<JuliaLanguage>;
505/// A node's identity without the tree: its kind plus its text range, resolvable
506/// back to a [`SyntaxNode`] against the root it came from. The key a per-file
507/// analysis uses to name a node it does not hold ([`rowan`]'s cursors are `Rc`
508/// based, so keeping one alive keeps the whole tree alive).
509pub type NodePtr = rowan::ast::SyntaxNodePtr<JuliaLanguage>;
510
511#[cfg(test)]
512mod tests {
513    use super::*;
514
515    #[test]
516    fn kind_round_trips_through_raw() {
517        for kind in [
518            SyntaxKind::ROOT,
519            SyntaxKind::STRING_LITERAL,
520            SyntaxKind::IDENT,
521            SyntaxKind::STRING_CONTENT,
522            SyntaxKind::FUNCTION_KW,
523            SyntaxKind::DOLLAR,
524            SyntaxKind::ERROR,
525        ] {
526            let raw = JuliaLanguage::kind_to_raw(kind);
527            assert_eq!(JuliaLanguage::kind_from_raw(raw), kind);
528        }
529    }
530}