forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
115 lines (104 loc) · 3.08 KB
/
Copy pathmod.rs
File metadata and controls
115 lines (104 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
pub mod attr;
pub mod expr;
pub mod item;
pub mod lit;
pub mod param;
pub mod pat;
pub mod path;
pub mod stmt;
pub mod types;
pub mod use_tree;
pub use attr::*;
pub use expr::*;
pub use item::*;
pub use lit::*;
pub use param::*;
pub use pat::*;
pub use path::*;
pub use stmt::*;
pub use types::*;
pub use use_tree::*;
pub type AstChildren<T> = rowan::ast::AstChildren<T>;
pub type SyntaxText = rowan::SyntaxText;
pub type AstPtr<T> = rowan::ast::AstPtr<T>;
pub type SyntaxNodePtr = rowan::ast::SyntaxNodePtr<FeLang>;
pub mod prelude {
pub use super::{
AttrListOwner, GenericArgsOwner, GenericParamsOwner, ItemModifierOwner, WhereClauseOwner,
};
pub use rowan::ast::AstNode;
}
macro_rules! ast_node {
(
$(#[$attrs: meta])*
$visibility: vis struct $name: ident $({
$($field_vis: vis $field: ident: $ty: ty),*
})?,
$kind: pat $(,)?
) => {
$(#[$attrs])*
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
$visibility struct $name {
__syntax: crate::SyntaxNode,
$($($field: $ty),*)?
}
impl rowan::ast::AstNode for $name {
type Language = $crate::FeLang;
fn can_cast(node: crate::SyntaxKind) -> bool {
matches!(node, $kind)
}
fn cast(node: crate::SyntaxNode) -> Option<Self> {
Self::can_cast(node.kind()).then(|| Self{
__syntax: node.into(),
$($($field: Default::default(),)*)?
})
}
fn syntax(&self) -> &crate::SyntaxNode {
&self.__syntax
}
}
impl std::fmt::Display for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(rowan::ast::AstNode::syntax(self), f)
}
}
};
(
$(#[$attrs: meta])*
$visibility: vis struct $name: ident $({
$($field_vis: vis $field: ident: $ty: ty),*
})?,
$kind: pat,
IntoIterator<Item=$item_ty:ty> $(,)?
) => {
ast_node!{
$(#[$attrs])*
$visibility struct $name $({
$($field_vis $field: $ty),*
})?,
$kind
}
impl IntoIterator for $name {
type Item = $item_ty;
type IntoIter = crate::ast::AstChildren<$item_ty>;
fn into_iter(self) -> Self::IntoIter {
rowan::ast::support::children(rowan::ast::AstNode::syntax(&self))
}
}
impl IntoIterator for &$name {
type Item = $item_ty;
type IntoIter = crate::ast::AstChildren<$item_ty>;
fn into_iter(self) -> Self::IntoIter {
rowan::ast::support::children(rowan::ast::AstNode::syntax(self))
}
}
impl $name {
/// Returns an iterator over the children of this node.
pub fn iter(&self) -> crate::ast::AstChildren<$item_ty> {
self.into_iter()
}
}
};
}
use ast_node;
use crate::FeLang;