Skip to main content

oak_vala/parser/
element_type.rs

1use crate::lexer::token_type::ValaTokenType;
2use oak_core::{ElementType, UniversalElementRole};
3
4/// Vala language syntax element types.
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7#[repr(u8)]
8pub enum ValaElementType {
9    /// Source file.
10    SourceFile,
11    /// Root element.
12    Root,
13    /// Namespace declaration.
14    Namespace,
15    /// Class declaration.
16    Class,
17    /// Interface declaration.
18    Interface,
19    /// Struct declaration.
20    Struct,
21    /// Enum declaration.
22    Enum,
23    /// Method declaration.
24    Method,
25    /// Field declaration.
26    Field,
27    /// Property declaration.
28    Property,
29    /// Parameter.
30    Parameter,
31    /// Code block.
32    Block,
33    /// Statement.
34    Statement,
35    /// Expression.
36    Expression,
37    /// Type reference.
38    Type,
39    /// Error element.
40    Error,
41
42    /// Element derived from a token.
43    Token(ValaTokenType),
44}
45
46impl From<ValaTokenType> for ValaElementType {
47    fn from(token: ValaTokenType) -> Self {
48        Self::Token(token)
49    }
50}
51
52impl ElementType for ValaElementType {
53    type Role = UniversalElementRole;
54
55    fn is_root(&self) -> bool {
56        matches!(self, Self::SourceFile | Self::Root)
57    }
58
59    fn is_error(&self) -> bool {
60        matches!(self, Self::Error)
61    }
62
63    fn role(&self) -> Self::Role {
64        match self {
65            Self::SourceFile | Self::Root => UniversalElementRole::Root,
66            Self::Namespace | Self::Class | Self::Interface | Self::Struct | Self::Enum | Self::Method | Self::Field | Self::Property => UniversalElementRole::Definition,
67            Self::Block | Self::Parameter => UniversalElementRole::Container,
68            Self::Statement => UniversalElementRole::Statement,
69            Self::Expression | Self::Type => UniversalElementRole::Expression,
70            Self::Error => UniversalElementRole::Error,
71            Self::Token(_) => UniversalElementRole::None,
72        }
73    }
74}
75
76impl oak_core::language::TokenType for ValaElementType {
77    type Role = oak_core::UniversalTokenRole;
78    const END_OF_STREAM: Self = Self::Token(ValaTokenType::Eof);
79
80    fn is_ignored(&self) -> bool {
81        match self {
82            Self::Token(t) => t.is_ignored(),
83            _ => false,
84        }
85    }
86
87    fn role(&self) -> Self::Role {
88        match self {
89            Self::Token(t) => t.role(),
90            _ => oak_core::UniversalTokenRole::None,
91        }
92    }
93}