Skip to main content

oak_java/parser/
element_type.rs

1use oak_core::{ElementType, TokenType, UniversalElementRole, UniversalTokenRole};
2
3/// Java element type.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub enum JavaElementType {
7    /// Token.
8    Token(crate::lexer::token_type::JavaTokenType),
9    /// Root node.
10    Root,
11    /// Identifier.
12    Identifier,
13    /// Literal expression.
14    LiteralExpression,
15    /// Parenthesized expression.
16    ParenthesizedExpression,
17    /// Array creation expression.
18    ArrayCreation,
19    /// Method call expression.
20    MethodCall,
21    /// Unary expression.
22    UnaryExpression,
23    /// Binary expression.
24    BinaryExpression,
25    /// Assignment expression.
26    AssignmentExpression,
27    /// Cast expression.
28    CastExpression,
29    /// Postfix expression.
30    PostfixExpression,
31    /// Ternary expression.
32    TernaryExpression,
33    /// Member select expression.
34    MemberSelect,
35    /// Array access expression.
36    ArrayAccess,
37    /// New expression.
38    NewExpression,
39    /// Variable declaration.
40    VariableDeclaration,
41    /// Expression statement.
42    ExpressionStatement,
43    /// If statement.
44    IfStatement,
45    /// While statement.
46    WhileStatement,
47    /// Do-while statement.
48    DoWhileStatement,
49    /// For statement.
50    ForStatement,
51    /// Switch statement.
52    SwitchStatement,
53    /// Return statement.
54    ReturnStatement,
55    /// Break statement.
56    Break,
57    /// Continue statement.
58    Continue,
59    /// Parameter.
60    Parameter,
61    /// Catch clause.
62    CatchClause,
63    /// Try statement.
64    TryStatement,
65    /// Throw statement.
66    ThrowStatement,
67    /// Throws clause.
68    ThrowsClause,
69    /// Package declaration.
70    Package,
71    /// Import declaration.
72    Import,
73    /// Class declaration.
74    ClassDeclaration,
75    /// Interface declaration.
76    InterfaceDeclaration,
77    /// Enum declaration.
78    EnumDeclaration,
79    /// Struct declaration.
80    StructDeclaration,
81    /// Record declaration.
82    RecordDeclaration,
83    /// Method declaration.
84    MethodDeclaration,
85    /// Constructor declaration.
86    ConstructorDeclaration,
87    /// Field declaration.
88    FieldDeclaration,
89    /// Annotation.
90    Annotation,
91    /// Switch case.
92    SwitchCase,
93    /// Default case.
94    DefaultCase,
95    /// Block statement.
96    BlockStatement,
97    /// Compilation unit.
98    CompilationUnit,
99    /// Error element.
100    Error,
101    /// Anonymous class.
102    AnonymousClass,
103    /// Type parameter.
104    TypeParameter,
105    /// Type parameter constraint.
106    TypeParameterConstraint,
107    /// Generic type.
108    GenericType,
109    /// Lambda expression.
110    LambdaExpression,
111    /// Annotation type declaration.
112    AnnotationTypeDeclaration,
113    /// Annotation element.
114    AnnotationElement,
115    /// Enum constant.
116    EnumConstant,
117}
118
119impl ElementType for JavaElementType {
120    type Role = UniversalElementRole;
121
122    fn role(&self) -> Self::Role {
123        use UniversalElementRole::*;
124        match self {
125            Self::Token(token) => match token.role() {
126                UniversalTokenRole::Keyword => None,
127                UniversalTokenRole::Name => Name,
128                UniversalTokenRole::Literal => Value,
129                UniversalTokenRole::Operator => Expression,
130                UniversalTokenRole::Punctuation => None,
131                UniversalTokenRole::Comment => Documentation,
132                UniversalTokenRole::Whitespace => None,
133                UniversalTokenRole::Error => Error,
134                UniversalTokenRole::None => None,
135                UniversalTokenRole::Eof => None,
136                UniversalTokenRole::Escape => Value,
137            },
138            Self::Root | Self::CompilationUnit => Root,
139            Self::Identifier => Name,
140            Self::LiteralExpression => Expression,
141            Self::ParenthesizedExpression
142            | Self::ArrayCreation
143            | Self::MethodCall
144            | Self::UnaryExpression
145            | Self::BinaryExpression
146            | Self::AssignmentExpression
147            | Self::CastExpression
148            | Self::PostfixExpression
149            | Self::TernaryExpression
150            | Self::MemberSelect
151            | Self::ArrayAccess
152            | Self::NewExpression => Expression,
153            Self::VariableDeclaration
154            | Self::ExpressionStatement
155            | Self::IfStatement
156            | Self::WhileStatement
157            | Self::DoWhileStatement
158            | Self::ForStatement
159            | Self::SwitchStatement
160            | Self::ReturnStatement
161            | Self::Break
162            | Self::Continue
163            | Self::TryStatement
164            | Self::ThrowStatement => Statement,
165            Self::ClassDeclaration | Self::InterfaceDeclaration | Self::EnumDeclaration | Self::StructDeclaration | Self::RecordDeclaration | Self::MethodDeclaration | Self::FieldDeclaration | Self::ConstructorDeclaration | Self::AnonymousClass => {
166                Definition
167            }
168            Self::Parameter | Self::CatchClause | Self::SwitchCase | Self::DefaultCase | Self::ThrowsClause | Self::Annotation | Self::TypeParameter | Self::TypeParameterConstraint | Self::AnnotationElement | Self::EnumConstant => Detail,
169            Self::Package | Self::Import => Statement,
170            Self::BlockStatement => Container,
171            Self::LambdaExpression | Self::GenericType => Expression,
172            Self::AnnotationTypeDeclaration => Definition,
173            Self::Error => Error,
174        }
175    }
176}
177
178impl From<crate::lexer::token_type::JavaTokenType> for JavaElementType {
179    fn from(token: crate::lexer::token_type::JavaTokenType) -> Self {
180        Self::Token(token)
181    }
182}