Skip to main content

oak_go/parser/
element_type.rs

1use oak_core::{ElementType, 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))]
7pub enum GoElementType {
8    // 非终端节点
9    SourceFile,
10    PackageClause,
11    ImportDeclaration,
12    ImportSpec,
13    FunctionDeclaration,
14    ParameterList,
15    ParameterDecl,
16    Block,
17    VariableDeclaration,
18    VariableSpec,
19    ConstDeclaration,
20    ConstSpec,
21    TypeDeclaration,
22    TypeSpec,
23    StructType,
24    FieldDeclList,
25    FieldDecl,
26    InterfaceType,
27    MethodSpecList,
28    MethodSpec,
29    ExpressionList,
30    AssignmentStatement,
31    ShortVarDecl,
32    ReturnStatement,
33    IfStatement,
34    ForStatement,
35    SwitchStatement,
36    ExprCaseClause,
37    TypeSwitchStatement,
38    TypeCaseClause,
39    SelectStatement,
40    CommClause,
41    GoStatement,
42    DeferStatement,
43    CallExpression,
44    IndexExpression,
45    SelectorExpression,
46    SliceExpression,
47    TypeAssertion,
48    UnaryExpression,
49    BinaryExpression,
50    LiteralValue,
51    ElementList,
52    KeyedElement,
53
54    // 字面
55    IntLiteral,
56    FloatLiteral,
57    StringLiteral,
58    RuneLiteral,
59    BoolLiteral,
60
61    // 标识
62    Identifier,
63
64    // 关键
65    Break,
66    Case,
67    Chan,
68    Const,
69    Continue,
70    Default,
71    Defer,
72    Else,
73    Fallthrough,
74    For,
75    Func,
76    Go,
77    Goto,
78    If,
79    Import,
80    Interface,
81    Map,
82    Package,
83    Range,
84    Return,
85    Select,
86    Struct,
87    Switch,
88    Type,
89    Var,
90
91    // 内置类型
92    Bool,
93    Byte,
94    Complex64,
95    Complex128,
96    ErrorType,
97    Float32,
98    Float64,
99    Int,
100    Int8,
101    Int16,
102    Int32,
103    Int64,
104    Rune,
105    String,
106    Uint,
107    Uint8,
108    Uint16,
109    Uint32,
110    Uint64,
111    Uintptr,
112
113    // 特殊字面
114    NilLiteral,
115    NumberLiteral,
116    CharLiteral,
117
118    // 操作
119    Plus,           // +
120    Minus,          // -
121    Star,           // *
122    Slash,          // /
123    Percent,        // %
124    Ampersand,      // &
125    Pipe,           // |
126    Caret,          // ^
127    LeftShift,      // <<
128    RightShift,     // >>
129    AmpersandCaret, // &^
130
131    PlusAssign,           // +=
132    MinusAssign,          // -=
133    StarAssign,           // *=
134    SlashAssign,          // /=
135    PercentAssign,        // %=
136    AmpersandAssign,      // &=
137    PipeAssign,           // |=
138    CaretAssign,          // ^=
139    XorAssign,            // ^= (别名)
140    LeftShiftAssign,      // <<=
141    RightShiftAssign,     // >>=
142    AmpersandCaretAssign, // &^=
143    AndAssign,            // &=
144    OrAssign,             // |=
145    AndNotAssign,         // &^=
146    AndNot,               // &^
147
148    LogicalAnd, // &&
149    LogicalOr,  // ||
150    And,        // && (别名)
151    Or,         // || (别名)
152    Arrow,      // <-
153    LeftArrow,  // <- (别名)
154    Increment,  // ++
155    Decrement,  // --
156
157    Equal,      // ==
158    Less,       // <
159    Greater,    // >
160    Assign,     // =
161    LogicalNot, // !
162    Not,        // ! (别名)
163
164    NotEqual,     // !=
165    LessEqual,    // <=
166    GreaterEqual, // >=
167    ColonAssign,  // :=
168    Define,       // := (别名)
169    Ellipsis,     // ...
170
171    // 分隔
172    LeftParen,    // (
173    RightParen,   // )
174    LeftBracket,  // [
175    RightBracket, // ]
176    LeftBrace,    // {
177    RightBrace,   // }
178    Comma,        // ,
179    Period,       // .
180    Dot,          // . (别名)
181    Semicolon,    // ;
182    Colon,        // :
183
184    // 空白和注
185    Whitespace,
186    Comment,
187
188    // 特殊
189    Eof,
190    Error,
191}
192
193impl GoElementType {
194    pub fn is_ignored(&self) -> bool {
195        matches!(self, Self::Whitespace | Self::Comment)
196    }
197
198    pub fn is_keyword(&self) -> bool {
199        matches!(
200            self,
201            Self::Break
202                | Self::Case
203                | Self::Chan
204                | Self::Const
205                | Self::Continue
206                | Self::Default
207                | Self::Defer
208                | Self::Else
209                | Self::Fallthrough
210                | Self::For
211                | Self::Func
212                | Self::Go
213                | Self::Goto
214                | Self::If
215                | Self::Import
216                | Self::Interface
217                | Self::Map
218                | Self::Package
219                | Self::Range
220                | Self::Return
221                | Self::Select
222                | Self::Struct
223                | Self::Switch
224                | Self::Type
225                | Self::Var
226        )
227    }
228}
229
230impl ElementType for GoElementType {
231    type Role = UniversalElementRole;
232
233    fn role(&self) -> Self::Role {
234        match self {
235            Self::Error => UniversalElementRole::Error,
236            _ => UniversalElementRole::None,
237        }
238    }
239}
240
241impl From<crate::lexer::token_type::GoTokenType> for GoElementType {
242    fn from(token: crate::lexer::token_type::GoTokenType) -> Self {
243        unsafe { std::mem::transmute(token) }
244    }
245}