Skip to main content

oak_rhombus/parser/
element_type.rs

1use oak_core::{ElementType, UniversalElementRole};
2
3/// Element types for the Rhombus language.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub enum RhombusElementType {
7    /// A source file.
8    SourceFile,
9    /// Whitespace.
10    Whitespace,
11    /// A newline.
12    Newline,
13    /// A comment.
14    Comment,
15    /// A line comment.
16    LineComment,
17    /// A numeric literal.
18    NumberLiteral,
19    /// A string literal.
20    StringLiteral,
21    /// A boolean literal.
22    BooleanLiteral,
23    /// An identifier.
24    Identifier,
25    /// A block.
26    Block,
27    /// A statement.
28    Statement,
29    /// An expression.
30    Expression,
31    /// An error token.
32    Error,
33    /// End of stream.
34    Eof,
35}
36
37impl ElementType for RhombusElementType {
38    type Role = UniversalElementRole;
39
40    fn role(&self) -> Self::Role {
41        match self {
42            Self::SourceFile => UniversalElementRole::Root,
43            Self::Error => UniversalElementRole::Error,
44            Self::Block => UniversalElementRole::Statement,
45            Self::Statement => UniversalElementRole::Statement,
46            Self::Expression => UniversalElementRole::Expression,
47            _ => UniversalElementRole::None,
48        }
49    }
50}
51
52impl From<crate::lexer::token_type::RhombusTokenType> for RhombusElementType {
53    fn from(token: crate::lexer::token_type::RhombusTokenType) -> Self {
54        use crate::lexer::token_type::RhombusTokenType as T;
55        match token {
56            T::Whitespace => RhombusElementType::Whitespace,
57            T::Newline => RhombusElementType::Newline,
58            T::Comment => RhombusElementType::Comment,
59            T::LineComment => RhombusElementType::LineComment,
60            T::NumberLiteral => RhombusElementType::NumberLiteral,
61            T::StringLiteral => RhombusElementType::StringLiteral,
62            T::BooleanLiteral => RhombusElementType::BooleanLiteral,
63            T::Identifier => RhombusElementType::Identifier,
64            T::Fun | T::Val | T::Var | T::Let | T::If | T::Else | T::Match | T::Case | T::Block | T::Module | T::Import | T::Export | T::Require | T::Provide => RhombusElementType::Identifier, // Keywords are identifiers for now
65            T::LeftParen | T::RightParen | T::LeftBracket | T::RightBracket | T::LeftBrace | T::RightBrace | T::Dot | T::Comma | T::Colon | T::Semicolon => RhombusElementType::Identifier,      // Punctuation
66            T::Error => RhombusElementType::Error,
67            T::Eof => RhombusElementType::Eof,
68            _ => RhombusElementType::Error,
69        }
70    }
71}