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    /// 解析树的根节点。
15    Root,
16    /// 编译单元节点。
17    CompilationUnit,
18    /// 上下文子句节点(例如 `with`,`use`)。
19    ContextClause,
20    /// Pragma 节点。
21    Pragma,
22    /// 子程序声明节点。
23    SubprogramDeclaration,
24    /// 包声明节点。
25    PackageDeclaration,
26    /// 类型声明节点。
27    TypeDeclaration,
28    /// 对象声明节点。
29    ObjectDeclaration,
30    /// 语句节点。
31    Statement,
32    /// 表达式节点。
33    Expression,
34    /// 解析树中的错误节点。
35    Error,
36
37    /// 标识符节点。
38    Identifier,
39    /// 字面量表达式节点。
40    LiteralExpression,
41    /// 标识符表达式节点。
42    IdentifierExpression,
43    /// 带括号的表达式节点。
44    ParenthesizedExpression,
45    /// 源文件节点。
46    SourceFile,
47    /// 参数列表节点。
48    ParameterList,
49    /// 块表达式节点。
50    BlockExpression,
51    /// Use 项节点。
52    UseItem,
53    /// 模块项节点。
54    ModuleItem,
55    /// 结构项节点。
56    StructItem,
57    /// 枚举项节点。
58    EnumItem,
59    /// Let 语句节点。
60    LetStatement,
61    /// If 表达式节点。
62    IfExpression,
63    /// While 表达式节点。
64    WhileExpression,
65    /// 循环表达式节点。
66    LoopExpression,
67    /// For 表达式节点。
68    ForExpression,
69    /// 调用表达式节点。
70    CallExpression,
71    /// 索引表达式节点。
72    IndexExpression,
73    /// 字段表达式节点。
74    FieldExpression,
75    /// 二元表达式节点。
76    BinaryExpression,
77    /// 一元表达式节点。
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}