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