oak_vala/parser/
element_type.rs1use crate::lexer::token_type::ValaTokenType;
2use oak_core::{ElementType, UniversalElementRole};
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7#[repr(u8)]
8pub enum ValaElementType {
9 SourceFile,
11 Root,
13 Namespace,
15 Class,
17 Interface,
19 Struct,
21 Enum,
23 Method,
25 Field,
27 Property,
29 Parameter,
31 Block,
33 Statement,
35 Expression,
37 Type,
39 Error,
41
42 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}