Skip to main content

oak_yaml/kind/
mod.rs

1use oak_core::{ElementType, TokenType, UniversalElementRole, UniversalTokenRole};
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
5#[repr(u16)]
6pub enum YamlSyntaxKind {
7    // Trivia
8    Whitespace,
9    Comment,
10
11    // Literals
12    StringLiteral,
13    NumberLiteral,
14    BooleanLiteral,
15    NullLiteral,
16
17    // Identifiers
18    Identifier,
19
20    // Operators and punctuation
21    Colon,       // :
22    Dash,        // -
23    Pipe,        // |
24    GreaterThan, // >
25    Question,    // ?
26    Ampersand,   // &
27    Asterisk,    // *
28    Exclamation, // !
29
30    // Brackets
31    LeftBracket,  // [
32    RightBracket, // ]
33    LeftBrace,    // {
34    RightBrace,   // }
35
36    // Special
37    Anchor, // &anchor
38    Alias,  // *alias
39    Tag,    // !tag
40
41    // Document markers
42    DocumentStart, // ---
43    DocumentEnd,   // ...
44    Document,
45    Root,
46
47    // Newlines and indentation
48    Newline,
49
50    // Error and EOF
51    Error,
52    Eof,
53}
54
55impl TokenType for YamlSyntaxKind {
56    type Role = UniversalTokenRole;
57    const END_OF_STREAM: Self = Self::Eof;
58
59    fn is_comment(&self) -> bool {
60        matches!(self, Self::Comment)
61    }
62
63    fn is_whitespace(&self) -> bool {
64        matches!(self, Self::Whitespace | Self::Newline)
65    }
66
67    fn role(&self) -> Self::Role {
68        use oak_core::UniversalTokenRole::*;
69        match self {
70            Self::Colon
71            | Self::Dash
72            | Self::Pipe
73            | Self::GreaterThan
74            | Self::Question
75            | Self::Ampersand
76            | Self::Asterisk
77            | Self::Exclamation
78            | Self::LeftBracket
79            | Self::RightBracket
80            | Self::LeftBrace
81            | Self::RightBrace
82            | Self::DocumentStart
83            | Self::DocumentEnd => Punctuation,
84
85            Self::Identifier | Self::Anchor | Self::Alias | Self::Tag => Name,
86
87            Self::StringLiteral | Self::NumberLiteral | Self::BooleanLiteral | Self::NullLiteral => Literal,
88
89            Self::Whitespace | Self::Newline => Whitespace,
90            Self::Comment => Comment,
91            Self::Error => Error,
92            _ => None,
93        }
94    }
95}
96
97impl ElementType for YamlSyntaxKind {
98    type Role = UniversalElementRole;
99
100    fn is_error(&self) -> bool {
101        matches!(self, Self::Error)
102    }
103
104    fn role(&self) -> Self::Role {
105        match self {
106            Self::Error => UniversalElementRole::Error,
107            _ => UniversalElementRole::None,
108        }
109    }
110}