Skip to main content

oak_ocaml/parser/
element_type.rs

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