Skip to main content

oak_elm/parser/
element_type.rs

1use oak_core::{ElementType, UniversalElementRole};
2
3/// Element types for Elm.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub enum ElmElementType {
7    /// The root node of the AST.
8    Root,
9    /// A module declaration.
10    Module,
11    /// An import statement.
12    Import,
13    /// A custom type declaration.
14    TypeDeclaration,
15    /// A type alias declaration.
16    TypeAlias,
17    /// A function declaration.
18    FunctionDeclaration,
19    /// A generic expression.
20    Expression,
21    /// A literal value.
22    Literal,
23    /// An identifier.
24    Identifier,
25    /// A binary expression.
26    BinaryExpression,
27    /// A unary expression.
28    UnaryExpression,
29    /// An `if` expression.
30    IfExpression,
31    /// A `case` expression.
32    CaseExpression,
33    /// A `let` expression.
34    LetExpression,
35    /// A tuple expression.
36    TupleExpression,
37    /// A list expression.
38    ListExpression,
39    /// A record expression.
40    RecordExpression,
41    /// A field access expression.
42    FieldExpression,
43    /// A lambda expression.
44    LambdaExpression,
45    /// A type signature.
46    TypeSignature,
47    /// A value declaration.
48    ValueDeclaration,
49    /// A pattern.
50    Pattern,
51    /// An error element.
52    Error,
53}
54
55impl ElementType for ElmElementType {
56    type Role = UniversalElementRole;
57
58    fn role(&self) -> Self::Role {
59        match self {
60            Self::Root => UniversalElementRole::Root,
61            Self::Module => UniversalElementRole::Definition,
62            Self::Import => UniversalElementRole::Statement,
63            Self::FunctionDeclaration => UniversalElementRole::Definition,
64            Self::Expression => UniversalElementRole::Expression,
65            Self::Identifier => UniversalElementRole::Name,
66            _ => UniversalElementRole::None,
67        }
68    }
69}
70
71impl From<crate::lexer::token_type::ElmTokenType> for ElmElementType {
72    fn from(token: crate::lexer::token_type::ElmTokenType) -> Self {
73        match token {
74            crate::lexer::token_type::ElmTokenType::Root => Self::Root,
75            crate::lexer::token_type::ElmTokenType::Identifier => Self::Identifier,
76            _ => unsafe { std::mem::transmute(token) },
77        }
78    }
79}