Skip to main content

oak_jasm/parser/
element_type.rs

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