Skip to main content

oak_ada/parser/
element_type.rs

1use crate::lexer::AdaTokenType;
2use oak_core::{ElementType, GreenNode, UniversalElementRole};
3use std::sync::Arc;
4
5/// Type alias for Ada syntax tree elements
6pub type AdaElement<'a> = Arc<GreenNode<'a, AdaElementType>>;
7
8/// Ada parser element types.
9#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11pub enum AdaElementType {
12    /// Root node of the parse tree.
13    Root,
14    /// Compilation unit node.
15    CompilationUnit,
16    /// Context clause node (e.g., `with`, `use`).
17    ContextClause,
18    /// Pragma node.
19    Pragma,
20    /// Subprogram declaration node.
21    SubprogramDeclaration,
22    /// Package declaration node.
23    PackageDeclaration,
24    /// Type declaration node.
25    TypeDeclaration,
26    /// Object declaration node.
27    ObjectDeclaration,
28    /// Statement node.
29    Statement,
30    /// Expression node.
31    Expression,
32    /// Error node in the parse tree.
33    Error,
34
35    /// Identifier node.
36    Identifier,
37    /// Literal expression node.
38    LiteralExpression,
39    /// Identifier expression node.
40    IdentifierExpression,
41    /// Parenthesized expression node.
42    ParenthesizedExpression,
43    /// Source file node.
44    SourceFile,
45    /// Parameter list node.
46    ParameterList,
47    /// Block expression node.
48    BlockExpression,
49    /// Use item node.
50    UseItem,
51    /// Module item node.
52    ModuleItem,
53    /// Struct item node.
54    StructItem,
55    /// Enum item node.
56    EnumItem,
57    /// Let statement node.
58    LetStatement,
59    /// If expression node.
60    IfExpression,
61    /// While expression node.
62    WhileExpression,
63    /// Loop expression node.
64    LoopExpression,
65    /// For expression node.
66    ForExpression,
67    /// Call expression node.
68    CallExpression,
69    /// Index expression node.
70    IndexExpression,
71    /// Field expression node.
72    FieldExpression,
73    /// Binary expression node.
74    BinaryExpression,
75    /// Unary expression node.
76    UnaryExpression,
77}
78
79impl ElementType for AdaElementType {
80    type Role = UniversalElementRole;
81
82    fn is_root(&self) -> bool {
83        matches!(self, Self::Root | Self::SourceFile)
84    }
85
86    fn is_error(&self) -> bool {
87        matches!(self, Self::Error)
88    }
89
90    fn role(&self) -> Self::Role {
91        use UniversalElementRole::*;
92        match self {
93            Self::Root | Self::SourceFile => Root,
94            Self::CompilationUnit | Self::SubprogramDeclaration | Self::PackageDeclaration | Self::TypeDeclaration | Self::ObjectDeclaration | Self::ModuleItem | Self::StructItem | Self::EnumItem => Definition,
95            Self::ContextClause | Self::BlockExpression | Self::ParameterList | Self::ParenthesizedExpression => Container,
96            Self::Statement | Self::Pragma | Self::UseItem | Self::LetStatement => Statement,
97            Self::Expression
98            | Self::BinaryExpression
99            | Self::UnaryExpression
100            | Self::IfExpression
101            | Self::WhileExpression
102            | Self::LoopExpression
103            | Self::ForExpression
104            | Self::IdentifierExpression
105            | Self::LiteralExpression
106            | Self::IndexExpression
107            | Self::FieldExpression => Expression,
108            Self::CallExpression => Call,
109            Self::Identifier => Reference,
110            Self::Error => Error,
111        }
112    }
113}
114
115impl From<AdaTokenType> for AdaElementType {
116    fn from(token_type: AdaTokenType) -> Self {
117        match token_type {
118            AdaTokenType::Identifier => Self::Identifier,
119            AdaTokenType::StringLiteral | AdaTokenType::CharacterLiteral | AdaTokenType::NumberLiteral => Self::LiteralExpression,
120            _ => Self::Error,
121        }
122    }
123}