Skip to main content

oak_jasm/parser/
element_type.rs

1//! Element types for the JASM language.
2use oak_core::{ElementType, UniversalElementRole};
3#[cfg(feature = "serde")]
4use serde::{Deserialize, Serialize};
5
6/// Element types for the JASM AST.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9#[repr(u16)]
10pub enum JasmElementType {
11    // --- Tokens (mirrored from JasmTokenType) ---
12    // These MUST be in the same order as JasmTokenType
13    /// Root token.
14    RootToken,
15    /// `class` keyword.
16    ClassKw,
17    /// `version` keyword.
18    VersionKw,
19    /// `method` keyword.
20    MethodKw,
21    /// `field` keyword.
22    FieldKw,
23    /// `string` keyword.
24    StringKw,
25    /// `source_file` keyword.
26    SourceFileKw,
27    /// `stack` keyword.
28    StackKw,
29    /// `locals` keyword.
30    LocalsKw,
31    /// `end` keyword.
32    EndKw,
33    /// `compiled` keyword.
34    CompiledKw,
35    /// `from` keyword.
36    FromKw,
37    /// `inner_class` keyword.
38    InnerClassKw,
39    /// `nest_members` keyword.
40    NestMembersKw,
41    /// `bootstrap_method` keyword.
42    BootstrapMethodKw,
43    /// `public` access modifier.
44    Public,
45    /// `private` access modifier.
46    Private,
47    /// `protected` access modifier.
48    Protected,
49    /// `static` modifier.
50    Static,
51    /// `super` modifier.
52    Super,
53    /// `final` modifier.
54    Final,
55    /// `abstract` modifier.
56    Abstract,
57    /// `synchronized` modifier.
58    Synchronized,
59    /// `native` modifier.
60    Native,
61    /// `synthetic` modifier.
62    Synthetic,
63    /// `deprecated` modifier.
64    Deprecated,
65    /// `varargs` modifier.
66    Varargs,
67    /// `aload_0` instruction.
68    ALoad0,
69    /// `aload_1` instruction.
70    ALoad1,
71    /// `aload_2` instruction.
72    ALoad2,
73    /// `aload_3` instruction.
74    ALoad3,
75    /// `iload_0` instruction.
76    ILoad0,
77    /// `iload_1` instruction.
78    ILoad1,
79    /// `iload_2` instruction.
80    ILoad2,
81    /// `iload_3` instruction.
82    ILoad3,
83    /// `ldc` instruction.
84    Ldc,
85    /// `ldc_w` instruction.
86    LdcW,
87    /// `ldc2_w` instruction.
88    Ldc2W,
89    /// `invokespecial` instruction.
90    InvokeSpecial,
91    /// `invokevirtual` instruction.
92    InvokeVirtual,
93    /// `invokestatic` instruction.
94    InvokeStatic,
95    /// `invokeinterface` instruction.
96    InvokeInterface,
97    /// `invokedynamic` instruction.
98    InvokeDynamic,
99    /// `getstatic` instruction.
100    GetStatic,
101    /// `putstatic` instruction.
102    PutStatic,
103    /// `getfield` instruction.
104    GetField,
105    /// `putfield` instruction.
106    PutField,
107    /// `new` instruction.
108    New,
109    /// `checkcast` instruction.
110    CheckCast,
111    /// `instanceof` instruction.
112    InstanceOf,
113    /// `newarray` instruction.
114    NewArray,
115    /// `anewarray` instruction.
116    ANewArray,
117    /// `arraylength` instruction.
118    ArrayLength,
119    /// `athrow` instruction.
120    AThrow,
121    /// `monitorenter` instruction.
122    MonitorEnter,
123    /// `monitorexit` instruction.
124    MonitorExit,
125    /// `multianewarray` instruction.
126    MultiANewArray,
127    /// `ifnull` instruction.
128    IfNull,
129    /// `ifnonnull` instruction.
130    IfNonNull,
131    /// `goto` instruction.
132    Goto,
133    /// `goto_w` instruction.
134    GotoW,
135    /// `jsr` instruction.
136    Jsr,
137    /// `jsr_w` instruction.
138    JsrW,
139    /// `ret` instruction.
140    Ret,
141    /// `tableswitch` instruction.
142    TableSwitch,
143    /// `lookupswitch` instruction.
144    LookupSwitch,
145    /// `ireturn` instruction.
146    IReturn,
147    /// `lreturn` instruction.
148    LReturn,
149    /// `freturn` instruction.
150    FReturn,
151    /// `dreturn` instruction.
152    DReturn,
153    /// `areturn` instruction.
154    AReturn,
155    /// `return` instruction.
156    Return,
157    /// `bipush` instruction.
158    BiPush,
159    /// `sipush` instruction.
160    SiPush,
161    /// `iinc` instruction.
162    IInc,
163    /// `wide` instruction.
164    Wide,
165    /// `breakpoint` instruction.
166    BreakPoint,
167    /// `impdep1` instruction.
168    ImpDep1,
169    /// `impdep2` instruction.
170    ImpDep2,
171    /// `nop` instruction.
172    Nop,
173    /// `dup` instruction.
174    Dup,
175    /// `pop` instruction.
176    Pop,
177    /// Left brace `{`.
178    LeftBrace,
179    /// Right brace `}`.
180    RightBrace,
181    /// Left bracket `[`.
182    LeftBracket,
183    /// Right bracket `]`.
184    RightBracket,
185    /// Left parenthesis `(`.
186    LeftParen,
187    /// Right parenthesis `)`.
188    RightParen,
189    /// Comma `,`.
190    Comma,
191    /// Colon `:`.
192    Colon,
193    /// Semicolon `;`.
194    Semicolon,
195    /// Equals `=`.
196    Eq,
197    /// Dot `.`.
198    Dot,
199    /// Slash `/`.
200    Slash,
201    /// Identifier.
202    Identifier,
203    /// String literal.
204    String,
205    /// Number literal.
206    Number,
207    /// Comment.
208    Comment,
209    /// Whitespace.
210    Whitespace,
211    /// Newline.
212    Newline,
213    /// End of file.
214    Eof,
215
216    // --- High-level AST Elements ---
217    /// Root node of the AST.
218    Root,
219    /// Class definition.
220    Class,
221    /// Method definition.
222    Method,
223    /// Field definition.
224    Field,
225    /// Constant pool entry.
226    Constant,
227    /// Attribute definition.
228    Attribute,
229    /// Instruction.
230    Instruction,
231    /// Exception handler.
232    ExceptionHandler,
233    /// Stack map frame.
234    StackMapFrame,
235    /// Inner class definition.
236    InnerClass,
237    /// Annotation definition.
238    Annotation,
239    /// Annotation parameter.
240    AnnotationParam,
241    /// Annotation array parameter.
242    AnnotationArray,
243    /// Error node.
244    Error,
245}
246
247impl ElementType for JasmElementType {
248    type Role = UniversalElementRole;
249
250    fn role(&self) -> Self::Role {
251        use UniversalElementRole::*;
252        match self {
253            Self::Root => Root,
254            Self::Class => Definition,
255            Self::Method => Definition,
256            Self::Field => Definition,
257            _ => None,
258        }
259    }
260}
261
262impl From<crate::lexer::token_type::JasmTokenType> for JasmElementType {
263    fn from(token: crate::lexer::token_type::JasmTokenType) -> Self {
264        unsafe { std::mem::transmute(token) }
265    }
266}