Skip to main content

oak_pascal/parser/
element_type.rs

1use oak_core::{ElementType, Parser, UniversalElementRole};
2
3/// Element types for Pascal.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub enum PascalElementType {
7    /// The root node of the AST.
8    Root,
9    /// A program node.
10    Program,
11    /// A unit node.
12    Unit,
13    /// An interface section.
14    Interface,
15    /// An implementation section.
16    Implementation,
17    /// An initialization section.
18    Initialization,
19    /// A finalization section.
20    Finalization,
21    /// A constant declaration section.
22    ConstSection,
23    /// A type declaration section.
24    TypeSection,
25    /// A variable declaration section.
26    VarSection,
27    /// A procedure declaration.
28    Procedure,
29    /// A function declaration.
30    Function,
31    /// A code block.
32    Block,
33    /// A statement.
34    Statement,
35    /// An expression.
36    Expression,
37    /// An error element.
38    Error,
39}
40
41impl ElementType for PascalElementType {
42    type Role = UniversalElementRole;
43
44    fn role(&self) -> Self::Role {
45        match self {
46            _ => UniversalElementRole::None,
47        }
48    }
49}
50
51impl From<crate::lexer::token_type::PascalTokenType> for PascalElementType {
52    fn from(token: crate::lexer::token_type::PascalTokenType) -> Self {
53        use crate::lexer::token_type::PascalTokenType;
54        match token {
55            PascalTokenType::Root => PascalElementType::Root,
56            PascalTokenType::ProgramBlock => PascalElementType::Program,
57            PascalTokenType::VarSection => PascalElementType::VarSection,
58            PascalTokenType::ConstSection => PascalElementType::ConstSection,
59            PascalTokenType::TypeSection => PascalElementType::TypeSection,
60            PascalTokenType::ProcedureDef => PascalElementType::Procedure,
61            PascalTokenType::FunctionDef => PascalElementType::Function,
62            PascalTokenType::CompoundStmt => PascalElementType::Block,
63            PascalTokenType::Expression => PascalElementType::Expression,
64            _ => PascalElementType::Error,
65        }
66    }
67}