Skip to main content

oak_matlab/parser/
element_type.rs

1use oak_core::{ElementType, Parser, UniversalElementRole};
2#[cfg(feature = "serde")]
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
6#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
7#[repr(u8)]
8pub enum MatlabElementType {
9    // 基础标记 (与 MatlabTokenType 保持一致)
10    Whitespace,
11    Newline,
12    Comment,
13    BlockComment,
14
15    // 标识符和字面量
16    Identifier,
17    Number,
18    String,
19    Character,
20
21    // 关键字
22    Function,
23    End,
24    If,
25    Else,
26    Elseif,
27    While,
28    For,
29    Break,
30    Continue,
31    Return,
32    Switch,
33    Case,
34    Otherwise,
35    Try,
36    Catch,
37    Global,
38    Persistent,
39    Classdef,
40    Properties,
41    Methods,
42    Events,
43
44    // 运算符
45    Plus,          // +
46    Minus,         // -
47    Times,         // *
48    Divide,        // /
49    Power,         // ^
50    LeftDivide,    // \
51    DotTimes,      // .*
52    DotDivide,     // ./
53    DotPower,      // .^
54    DotLeftDivide, // .\
55
56    // 比较运算符
57    Equal,        // ==
58    NotEqual,     // ~=
59    Less,         // <
60    Greater,      // >
61    LessEqual,    // <=
62    GreaterEqual, // >=
63
64    // 逻辑运算符
65    And,    // &
66    Or,     // |
67    Not,    // ~
68    AndAnd, // &&
69    OrOr,   // ||
70
71    // 赋值运算符
72    Assign, // =
73
74    // 分隔符
75    LeftParen,    // (
76    RightParen,   // )
77    LeftBracket,  // [
78    RightBracket, // ]
79    LeftBrace,    // {
80    RightBrace,   // }
81    Semicolon,    // ;
82    Comma,        // ,
83    Dot,          // .
84    Colon,        // :
85    Question,     // ?
86    At,           // @
87
88    // 特殊运算符
89    Transpose,    // '
90    DotTranspose, // .'
91
92    // 泛化类型
93    Operator,
94    Delimiter,
95
96    // 错误处理
97    Error,
98
99    // 文档结构 (Element)
100    Script,
101    FunctionDef,
102    ClassDef,
103    Block,
104    Expression,
105    Statement,
106
107    // EOF
108    Eof,
109}
110
111impl MatlabElementType {
112    pub fn is_token(&self) -> bool {
113        (*self as u8) <= (Self::Eof as u8) && !self.is_element()
114    }
115
116    pub fn is_element(&self) -> bool {
117        matches!(self, Self::Script | Self::FunctionDef | Self::ClassDef | Self::Block | Self::Expression | Self::Statement)
118    }
119}
120
121impl ElementType for MatlabElementType {
122    type Role = UniversalElementRole;
123
124    fn role(&self) -> Self::Role {
125        match self {
126            _ => UniversalElementRole::None,
127        }
128    }
129}
130
131impl From<crate::lexer::token_type::MatlabTokenType> for MatlabElementType {
132    fn from(token: crate::lexer::token_type::MatlabTokenType) -> Self {
133        unsafe { std::mem::transmute(token) }
134    }
135}