Skip to main content

oak_c/parser/
element_type.rs

1use crate::lexer::CTokenType;
2use oak_core::{ElementType, UniversalElementRole};
3#[cfg(feature = "serde")]
4use serde::{Deserialize, Serialize};
5
6/// Represents all possible element kinds in the C programming language.
7#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
8#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
9pub enum CElementType {
10    /// A wrapper for tokens.
11    Token(CTokenType),
12    /// Root node representing the entire source file.
13    Root,
14    /// A function definition.
15    FunctionDefinition,
16    /// A list of parameters in a function declaration or definition.
17    ParameterList,
18    /// A compound statement (a block of code enclosed in braces).
19    CompoundStatement,
20    /// An expression statement.
21    ExpressionStatement,
22    /// A declaration statement.
23    DeclarationStatement,
24    /// A declarator.
25    Declarator,
26    /// An `if` statement.
27    IfStatement,
28    /// A `while` statement.
29    WhileStatement,
30    /// A `for` statement.
31    ForStatement,
32    /// A `return` statement.
33    ReturnStatement,
34    /// An error element used for recovery.
35    Error,
36}
37
38impl From<CTokenType> for CElementType {
39    fn from(token: CTokenType) -> Self {
40        Self::Token(token)
41    }
42}
43
44impl ElementType for CElementType {
45    type Role = UniversalElementRole;
46
47    fn is_root(&self) -> bool {
48        matches!(self, Self::Root)
49    }
50
51    fn is_error(&self) -> bool {
52        matches!(self, Self::Error)
53    }
54
55    fn role(&self) -> Self::Role {
56        match self {
57            Self::Root => UniversalElementRole::Root,
58            Self::FunctionDefinition => UniversalElementRole::Definition,
59            Self::CompoundStatement | Self::ExpressionStatement | Self::DeclarationStatement | Self::IfStatement | Self::WhileStatement | Self::ForStatement | Self::ReturnStatement => UniversalElementRole::Statement,
60            Self::Error => UniversalElementRole::Error,
61            _ => UniversalElementRole::None,
62        }
63    }
64}