Skip to main content

oak_ada/parser/
element_type.rs

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