Skip to main content

oak_wolfram/kind/
mod.rs

1use oak_core::{ElementType, TokenType, UniversalElementRole, UniversalTokenRole};
2use serde::{Deserialize, Serialize};
3use std::fmt;
4
5#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
6pub enum WolframSyntaxKind {
7    Root,
8
9    // 基础 tokens
10    Whitespace,
11    Newline,
12
13    // 标识符和字面量
14    Identifier,
15    Integer,
16    Real,
17    String,
18
19    // 关键字
20    If,
21    Then,
22    Else,
23    While,
24    For,
25    Do,
26    Function,
27    Module,
28    Block,
29    With,
30    Table,
31    Map,
32    Apply,
33    Select,
34    Cases,
35    Rule,
36    RuleDelayed,
37    Set,
38    SetDelayed,
39    Unset,
40    Clear,
41    ClearAll,
42    Return,
43    Break,
44    Continue,
45    True,
46    False,
47    Null,
48    Export,
49    Import,
50
51    // 运算符
52    Plus,         // +
53    Minus,        // -
54    Times,        // *
55    Divide,       // /
56    Power,        // ^
57    Equal,        // ==
58    NotEqual,     // !=
59    Less,         // <
60    Greater,      // >
61    LessEqual,    // <=
62    GreaterEqual, // >=
63    And,          // &&
64    Or,           // ||
65    Not,          // !
66
67    // 赋值运算符
68    Assign,       // =
69    AddTo,        // +=
70    SubtractFrom, // -=
71    TimesBy,      // *=
72    DivideBy,     // /=
73
74    // 分隔符
75    LeftParen,    // (
76    RightParen,   // )
77    LeftBracket,  // [
78    RightBracket, // ]
79    LeftBrace,    // {
80    RightBrace,   // }
81    Comma,        // ,
82    Semicolon,    // ;
83    Colon,        // :
84    Dot,          // .
85
86    // 特殊符号
87    Arrow,            // ->
88    DoubleArrow,      // =>
89    Question,         // ?
90    Underscore,       // _
91    DoubleUnderscore, // __
92    TripleUnderscore, // ___
93    Slot,             // #
94    SlotSequence,     // ##
95
96    // 注释
97    Comment,
98
99    // 文本
100    Text,
101
102    // 错误处理
103    Error,
104
105    // EOF
106    Eof,
107}
108
109impl fmt::Display for WolframSyntaxKind {
110    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
111        write!(f, "{:?}", self)
112    }
113}
114
115impl TokenType for WolframSyntaxKind {
116    const END_OF_STREAM: Self = WolframSyntaxKind::Eof;
117    type Role = UniversalTokenRole;
118
119    fn role(&self) -> Self::Role {
120        UniversalTokenRole::None
121    }
122
123    fn is_ignored(&self) -> bool {
124        matches!(self, WolframSyntaxKind::Whitespace | WolframSyntaxKind::Newline | WolframSyntaxKind::Comment)
125    }
126
127    fn is_comment(&self) -> bool {
128        matches!(self, WolframSyntaxKind::Comment)
129    }
130
131    fn is_whitespace(&self) -> bool {
132        matches!(self, WolframSyntaxKind::Whitespace | WolframSyntaxKind::Newline)
133    }
134}
135
136impl ElementType for WolframSyntaxKind {
137    type Role = UniversalElementRole;
138
139    fn role(&self) -> Self::Role {
140        match self {
141            WolframSyntaxKind::Error => UniversalElementRole::Error,
142            _ => UniversalElementRole::None,
143        }
144    }
145}