Skip to main content

oak_java/parser/
element_type.rs

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