Skip to main content

oak_cpp/parser/
element_type.rs

1use 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)]
7/// Syntax element types for the C++ language.
8pub enum CppElementType {
9    /// A wrapper for tokens
10    Token(CppTokenType),
11    /// Root node of the source file
12    SourceFile,
13    /// A function definition
14    FunctionDefinition,
15    /// A class definition
16    ClassDefinition,
17    /// A namespace definition
18    NamespaceDefinition,
19    /// A declaration statement
20    DeclarationStatement,
21    /// An expression statement
22    ExpressionStatement,
23    /// A compound statement (block)
24    CompoundStatement,
25    /// An if statement
26    IfStatement,
27    /// A while statement
28    WhileStatement,
29    /// A for statement
30    ForStatement,
31    /// A return statement
32    ReturnStatement,
33    /// A function call
34    FunctionCall,
35    /// An error token
36    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}