oak_ada/parser/
element_type.rs1use crate::lexer::AdaTokenType;
2use oak_core::{ElementType, GreenNode, UniversalElementRole};
3#[cfg(feature = "serde")]
4use serde::{Deserialize, Serialize};
5use std::sync::Arc;
6
7pub type AdaElement<'a> = Arc<GreenNode<'a, AdaElementType>>;
9
10#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
12#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
13pub enum AdaElementType {
14 Root,
16 CompilationUnit,
18 ContextClause,
20 Pragma,
22 SubprogramDeclaration,
24 PackageDeclaration,
26 TypeDeclaration,
28 ObjectDeclaration,
30 Statement,
32 Expression,
34 Error,
36
37 Identifier,
39 LiteralExpression,
41 IdentifierExpression,
43 ParenthesizedExpression,
45 SourceFile,
47 ParameterList,
49 BlockExpression,
51 UseItem,
53 ModuleItem,
55 StructItem,
57 EnumItem,
59 LetStatement,
61 IfExpression,
63 WhileExpression,
65 LoopExpression,
67 ForExpression,
69 CallExpression,
71 IndexExpression,
73 FieldExpression,
75 BinaryExpression,
77 UnaryExpression,
79}
80
81impl ElementType for AdaElementType {
82 type Role = UniversalElementRole;
83
84 fn is_root(&self) -> bool {
85 matches!(self, Self::Root | Self::SourceFile)
86 }
87
88 fn is_error(&self) -> bool {
89 matches!(self, Self::Error)
90 }
91
92 fn role(&self) -> Self::Role {
93 use UniversalElementRole::*;
94 match self {
95 Self::Root | Self::SourceFile => Root,
96 Self::CompilationUnit | Self::SubprogramDeclaration | Self::PackageDeclaration | Self::TypeDeclaration | Self::ObjectDeclaration | Self::ModuleItem | Self::StructItem | Self::EnumItem => Definition,
97 Self::ContextClause | Self::BlockExpression | Self::ParameterList | Self::ParenthesizedExpression => Container,
98 Self::Statement | Self::Pragma | Self::UseItem | Self::LetStatement => Statement,
99 Self::Expression
100 | Self::BinaryExpression
101 | Self::UnaryExpression
102 | Self::IfExpression
103 | Self::WhileExpression
104 | Self::LoopExpression
105 | Self::ForExpression
106 | Self::IdentifierExpression
107 | Self::LiteralExpression
108 | Self::IndexExpression
109 | Self::FieldExpression => Expression,
110 Self::CallExpression => Call,
111 Self::Identifier => Reference,
112 Self::Error => Error,
113 }
114 }
115}
116
117impl From<AdaTokenType> for AdaElementType {
118 fn from(token_type: AdaTokenType) -> Self {
119 match token_type {
120 AdaTokenType::Identifier => Self::Identifier,
121 AdaTokenType::StringLiteral | AdaTokenType::CharacterLiteral | AdaTokenType::NumberLiteral => Self::LiteralExpression,
122 _ => Self::Error,
123 }
124 }
125}