Skip to main content

oak_ocaml/parser/
element_type.rs

1use oak_core::{ElementType, UniversalElementRole};
2
3/// Element types for the OCaml parser.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub enum OCamlElementType {
7    /// Root node of the OCaml AST.
8    Root,
9    /// A module definition.
10    ModuleDef,
11    /// A let binding.
12    LetBinding,
13    /// A match expression.
14    MatchExpr,
15    /// A function definition.
16    FunctionDef,
17    /// A type definition.
18    TypeDefinition,
19    /// An expression.
20    Expression,
21    /// A binary expression.
22    BinaryExpr,
23    /// A unary expression.
24    UnaryExpr,
25    /// A function call.
26    CallExpr,
27    /// A literal expression.
28    LiteralExpr,
29    /// An identifier expression.
30    IdentifierExpr,
31    /// An error element.
32    Error,
33}
34
35impl ElementType for OCamlElementType {
36    type Role = UniversalElementRole;
37
38    fn role(&self) -> Self::Role {
39        match self {
40            _ => UniversalElementRole::None,
41        }
42    }
43}
44
45impl From<crate::lexer::token_type::OCamlTokenType> for OCamlElementType {
46    fn from(token: crate::lexer::token_type::OCamlTokenType) -> Self {
47        use crate::lexer::token_type::OCamlTokenType;
48        match token {
49            OCamlTokenType::Root => OCamlElementType::Root,
50            OCamlTokenType::ModuleDef => OCamlElementType::ModuleDef,
51            OCamlTokenType::LetBinding => OCamlElementType::LetBinding,
52            OCamlTokenType::MatchExpr => OCamlElementType::MatchExpr,
53            OCamlTokenType::FunctionDef => OCamlElementType::FunctionDef,
54            OCamlTokenType::TypeDefinition => OCamlElementType::TypeDefinition,
55            OCamlTokenType::Expression => OCamlElementType::Expression,
56            OCamlTokenType::BinaryExpr => OCamlElementType::BinaryExpr,
57            OCamlTokenType::UnaryExpr => OCamlElementType::UnaryExpr,
58            OCamlTokenType::CallExpr => OCamlElementType::CallExpr,
59            OCamlTokenType::LiteralExpr => OCamlElementType::LiteralExpr,
60            OCamlTokenType::IdentifierExpr => OCamlElementType::IdentifierExpr,
61            _ => OCamlElementType::Error,
62        }
63    }
64}