oak_json/kind/
mod.rs

1use oak_core::{TokenType, UniversalElementRole, UniversalTokenRole};
2
3/// 统一JSON 语法种类(包含节点与词法
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize)]
5pub enum JsonSyntaxKind {
6    // 节点种类
7    Root,
8    Value,
9    Object,
10    Array,
11    String,
12    Number,
13    Boolean,
14    Null,
15    ObjectEntry,
16    ArrayElement,
17    ErrorNode,
18
19    // 词法种类
20    LeftBrace,    // {
21    RightBrace,   // }
22    LeftBracket,  // [
23    RightBracket, // ]
24    Comma,        // ,
25    Colon,        // :
26    StringLiteral,
27    NumberLiteral,
28    BooleanLiteral,
29    NullLiteral,
30    BareKey, // For JSON5
31    Whitespace,
32    Comment,
33    Eof,
34    Error,
35}
36
37impl TokenType for JsonSyntaxKind {
38    type Role = UniversalTokenRole;
39    const END_OF_STREAM: Self = Self::Eof;
40
41    fn is_comment(&self) -> bool {
42        matches!(self, JsonSyntaxKind::Comment)
43    }
44
45    fn is_whitespace(&self) -> bool {
46        matches!(self, JsonSyntaxKind::Whitespace)
47    }
48
49    fn role(&self) -> Self::Role {
50        use UniversalTokenRole::*;
51        match self {
52            Self::LeftBrace | Self::RightBrace | Self::LeftBracket | Self::RightBracket | Self::Comma | Self::Colon => Punctuation,
53
54            Self::StringLiteral | Self::NumberLiteral | Self::BooleanLiteral | Self::NullLiteral => Literal,
55
56            Self::BareKey => Name,
57            Self::Whitespace => Whitespace,
58            Self::Comment => Comment,
59            Self::Error => Error,
60            _ => None,
61        }
62    }
63}
64
65impl oak_core::ElementType for JsonSyntaxKind {
66    type Role = UniversalElementRole;
67
68    fn is_root(&self) -> bool {
69        matches!(self, Self::Root)
70    }
71
72    fn is_error(&self) -> bool {
73        matches!(self, Self::ErrorNode)
74    }
75
76    fn role(&self) -> Self::Role {
77        use UniversalElementRole::*;
78        match self {
79            Self::Root => Root,
80
81            // Hierarchy & Scoping
82            Self::Object | Self::Array => Container,
83
84            // Flow Control & Logic (Data logic)
85            Self::ObjectEntry | Self::ArrayElement => Statement,
86
87            // Atomic Values
88            Self::Value | Self::String | Self::Number | Self::Boolean | Self::Null => Value,
89
90            Self::ErrorNode => Error,
91            _ => None,
92        }
93    }
94}