Skip to main content

fatou_parser/documentation/
syntax.rs

1//! Syntax kinds and Rowan bindings for documentation trees.
2
3use rowan::Language;
4
5/// Nodes and tokens in a lossless Julia Markdown tree.
6#[allow(non_camel_case_types)]
7#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
8#[repr(u16)]
9pub enum SyntaxKind {
10    // Nodes.
11    ROOT,
12    PARAGRAPH,
13    ATX_HEADING,
14    SETEXT_HEADING,
15    BLOCK_QUOTE,
16    ADMONITION,
17    LIST,
18    LIST_ITEM,
19    INDENTED_CODE_BLOCK,
20    FENCED_CODE_BLOCK,
21    MATH_BLOCK,
22    FOOTNOTE_DEFINITION,
23    TABLE,
24    TABLE_ROW,
25    TABLE_CELL,
26    THEMATIC_BREAK,
27    EMPHASIS,
28    STRONG,
29    INLINE_CODE,
30    INLINE_MATH,
31    LINK,
32    IMAGE,
33    AUTOLINK,
34    FOOTNOTE_REFERENCE,
35    INTERPOLATION,
36    HARD_BREAK,
37    ERROR,
38
39    // Tokens.
40    TEXT,
41    WHITESPACE,
42    NEWLINE,
43    BLANK_LINE,
44    MARKER,
45    HEADING_MARKER,
46    LIST_MARKER,
47    QUOTE_MARKER,
48    ADMONITION_CATEGORY,
49    ADMONITION_TITLE,
50    FOOTNOTE_LABEL,
51    FENCE,
52    INFO_STRING,
53    CODE_CONTENT,
54    MATH_CONTENT,
55    LINK_DESTINATION,
56    ESCAPE,
57    SOFT_BREAK,
58    EN_DASH,
59    EM_DASH,
60    ERROR_TOKEN,
61}
62
63impl SyntaxKind {
64    const LAST: Self = Self::ERROR_TOKEN;
65}
66
67/// Rowan language used by the documentation CST.
68#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
69pub enum DocumentationLanguage {}
70
71impl Language for DocumentationLanguage {
72    type Kind = SyntaxKind;
73
74    fn kind_from_raw(raw: rowan::SyntaxKind) -> Self::Kind {
75        assert!(
76            raw.0 <= SyntaxKind::LAST as u16,
77            "invalid documentation syntax kind"
78        );
79        // SAFETY: every discriminant is contiguous from `ROOT` through
80        // `ERROR_TOKEN`, and the bound check above excludes other values.
81        unsafe { std::mem::transmute::<u16, SyntaxKind>(raw.0) }
82    }
83
84    fn kind_to_raw(kind: Self::Kind) -> rowan::SyntaxKind {
85        rowan::SyntaxKind(kind as u16)
86    }
87}
88
89pub type SyntaxNode = rowan::SyntaxNode<DocumentationLanguage>;
90pub type SyntaxToken = rowan::SyntaxToken<DocumentationLanguage>;
91pub type SyntaxElement = rowan::SyntaxElement<DocumentationLanguage>;