Skip to main content

oak_msil/kind/
mod.rs

1use oak_core::{ElementType, Token, TokenType, UniversalElementRole, UniversalTokenRole};
2use serde::{Deserialize, Serialize};
3
4pub type MsilToken = Token<MsilSyntaxKind>;
5
6/// 统一MSIL 语法种类(包含节点与词法
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
8pub enum MsilSyntaxKind {
9    // 节点种类
10    Root,
11    Assembly,
12    Module,
13    Class,
14    Method,
15    Instruction,
16    Label,
17    Directive,
18    Type,
19    Identifier,
20    Number,
21    String,
22    Comment,
23    ErrorNode,
24
25    // 词法种类 - 关键
26    AssemblyKeyword, // .assembly
27    ExternKeyword,   // extern
28    ModuleKeyword,   // .module
29    ClassKeyword,    // .class
30    MethodKeyword,   // .method
31    PublicKeyword,   // public
32    PrivateKeyword,  // private
33    StaticKeyword,   // static
34
35    // 词法种类 - 符号
36    LeftBrace,    // {
37    RightBrace,   // }
38    LeftParen,    // (
39    RightParen,   // )
40    LeftBracket,  // [
41    RightBracket, // ]
42    Dot,          // .
43    Colon,        // :
44    Semicolon,    // ;
45    Comma,        // ,
46
47    // 词法种类 - 字面
48    IdentifierToken,
49    NumberToken,
50    StringToken,
51
52    // 词法种类 - 其他
53    Whitespace,
54    CommentToken,
55    Eof,
56    Error,
57}
58
59impl TokenType for MsilSyntaxKind {
60    const END_OF_STREAM: Self = Self::Eof;
61    type Role = UniversalTokenRole;
62
63    fn role(&self) -> Self::Role {
64        match self {
65            Self::Whitespace => UniversalTokenRole::Whitespace,
66            Self::CommentToken | Self::Comment => UniversalTokenRole::Comment,
67            Self::Eof => UniversalTokenRole::Eof,
68            _ => UniversalTokenRole::None,
69        }
70    }
71
72    fn is_comment(&self) -> bool {
73        matches!(self, Self::CommentToken | Self::Comment)
74    }
75
76    fn is_whitespace(&self) -> bool {
77        matches!(self, Self::Whitespace)
78    }
79}
80
81impl ElementType for MsilSyntaxKind {
82    type Role = UniversalElementRole;
83
84    fn role(&self) -> Self::Role {
85        match self {
86            Self::ErrorNode | Self::Error => UniversalElementRole::Error,
87            Self::Root => UniversalElementRole::Root,
88            Self::Method | Self::Class => UniversalElementRole::Detail,
89            _ => UniversalElementRole::None,
90        }
91    }
92
93    fn is_root(&self) -> bool {
94        matches!(self, Self::Root)
95    }
96
97    fn is_error(&self) -> bool {
98        matches!(self, Self::Error | Self::ErrorNode)
99    }
100}