oak_cpp/parser/
element_type.rs1use crate::lexer::CppTokenType;
2use oak_core::{ElementType, UniversalElementRole};
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6#[repr(u16)]
7pub enum CppElementType {
9 Token(CppTokenType),
11 SourceFile,
13 FunctionDefinition,
15 ClassDefinition,
17 NamespaceDefinition,
19 DeclarationStatement,
21 ExpressionStatement,
23 CompoundStatement,
25 IfStatement,
27 WhileStatement,
29 ForStatement,
31 ReturnStatement,
33 FunctionCall,
35 Error,
37}
38
39impl From<CppTokenType> for CppElementType {
40 fn from(token: CppTokenType) -> Self {
41 Self::Token(token)
42 }
43}
44
45impl ElementType for CppElementType {
46 type Role = UniversalElementRole;
47
48 fn is_root(&self) -> bool {
49 matches!(self, Self::SourceFile)
50 }
51
52 fn is_error(&self) -> bool {
53 matches!(self, Self::Error)
54 }
55
56 fn role(&self) -> Self::Role {
57 match self {
58 Self::SourceFile => UniversalElementRole::Root,
59 Self::FunctionDefinition | Self::ClassDefinition | Self::NamespaceDefinition => UniversalElementRole::Definition,
60 Self::DeclarationStatement | Self::ExpressionStatement | Self::CompoundStatement | Self::IfStatement | Self::WhileStatement | Self::ForStatement | Self::ReturnStatement => UniversalElementRole::Statement,
61 Self::Error => UniversalElementRole::Error,
62 _ => UniversalElementRole::Container,
63 }
64 }
65}