Skip to main content

oak_ada/parser/
element_type.rs

1use crate::lexer::AdaTokenType;
2use oak_core::{ElementType, GreenNode, UniversalElementRole};
3#[cfg(feature = "serde")]
4use serde::{Deserialize, Serialize};
5use std::sync::Arc;
6
7/// Ada 语法树元素的类型别名
8pub type AdaElement<'a> = Arc<GreenNode<'a, AdaElementType>>;
9
10/// Ada 语法树中所有可能的元素类型。
11#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
12#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
13pub enum AdaElementType {
14    /// Root node
15    Root,
16    /// Compilation unit
17    CompilationUnit,
18    /// Context clause (with, use)
19    ContextClause,
20    /// Pragma
21    Pragma,
22    /// Subprogram declaration
23    SubprogramDeclaration,
24    /// Package declaration
25    PackageDeclaration,
26    /// Type declaration
27    TypeDeclaration,
28    /// Object declaration
29    ObjectDeclaration,
30    /// Statement
31    Statement,
32    /// Expression
33    Expression,
34    /// Error node
35    Error,
36
37    /// Identifier
38    Identifier,
39    /// Literal
40    LiteralExpression,
41    /// Identifier expression
42    IdentifierExpression,
43    /// Parenthesized expression
44    ParenthesizedExpression,
45    /// Source file
46    SourceFile,
47    /// Parameter list
48    ParameterList,
49    /// Block expression
50    BlockExpression,
51    /// Use item
52    UseItem,
53    /// Module item
54    ModuleItem,
55    /// Struct item
56    StructItem,
57    /// Enum item
58    EnumItem,
59    /// Let statement
60    LetStatement,
61    /// If expression
62    IfExpression,
63    /// While expression
64    WhileExpression,
65    /// Loop expression
66    LoopExpression,
67    /// For expression
68    ForExpression,
69    /// Call expression
70    CallExpression,
71    /// Index expression
72    IndexExpression,
73    /// Field expression
74    FieldExpression,
75    /// Binary expression
76    BinaryExpression,
77    /// Unary expression
78    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}