oak_ada/parser/
element_type.rs1use crate::lexer::AdaTokenType;
2use oak_core::{ElementType, GreenNode, UniversalElementRole};
3use serde::{Deserialize, Serialize};
4use std::sync::Arc;
5
6pub type AdaElement<'a> = Arc<GreenNode<'a, AdaElementType>>;
8
9#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
11pub enum AdaElementType {
12 Root,
14 CompilationUnit,
16 ContextClause,
18 Pragma,
20 SubprogramDeclaration,
22 PackageDeclaration,
24 TypeDeclaration,
26 ObjectDeclaration,
28 Statement,
30 Expression,
32 Error,
34
35 Identifier,
37 LiteralExpression,
39 IdentifierExpression,
41 ParenthesizedExpression,
43 SourceFile,
45 ParameterList,
47 BlockExpression,
49 UseItem,
51 ModuleItem,
53 StructItem,
55 EnumItem,
57 LetStatement,
59 IfExpression,
61 WhileExpression,
63 LoopExpression,
65 ForExpression,
67 CallExpression,
69 IndexExpression,
71 FieldExpression,
73 BinaryExpression,
75 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}