aboutsummaryrefslogtreecommitdiff
path: root/crates/syntax/src/ast
diff options
context:
space:
mode:
Diffstat (limited to 'crates/syntax/src/ast')
-rw-r--r--crates/syntax/src/ast/generated/nodes.rs40
-rw-r--r--crates/syntax/src/ast/make.rs8
-rw-r--r--crates/syntax/src/ast/node_ext.rs22
3 files changed, 60 insertions, 10 deletions
diff --git a/crates/syntax/src/ast/generated/nodes.rs b/crates/syntax/src/ast/generated/nodes.rs
index 92ed2ee9d..1d722db3c 100644
--- a/crates/syntax/src/ast/generated/nodes.rs
+++ b/crates/syntax/src/ast/generated/nodes.rs
@@ -18,6 +18,9 @@ pub struct NameRef {
18} 18}
19impl NameRef { 19impl NameRef {
20 pub fn ident_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![ident]) } 20 pub fn ident_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![ident]) }
21 pub fn self_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![self]) }
22 pub fn super_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![super]) }
23 pub fn crate_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![crate]) }
21} 24}
22#[derive(Debug, Clone, PartialEq, Eq, Hash)] 25#[derive(Debug, Clone, PartialEq, Eq, Hash)]
23pub struct Lifetime { 26pub struct Lifetime {
@@ -42,9 +45,6 @@ pub struct PathSegment {
42 pub(crate) syntax: SyntaxNode, 45 pub(crate) syntax: SyntaxNode,
43} 46}
44impl PathSegment { 47impl PathSegment {
45 pub fn crate_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![crate]) }
46 pub fn self_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![self]) }
47 pub fn super_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![super]) }
48 pub fn coloncolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![::]) } 48 pub fn coloncolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![::]) }
49 pub fn name_ref(&self) -> Option<NameRef> { support::child(&self.syntax) } 49 pub fn name_ref(&self) -> Option<NameRef> { support::child(&self.syntax) }
50 pub fn generic_arg_list(&self) -> Option<GenericArgList> { support::child(&self.syntax) } 50 pub fn generic_arg_list(&self) -> Option<GenericArgList> { support::child(&self.syntax) }
@@ -931,6 +931,15 @@ impl WhileExpr {
931 pub fn condition(&self) -> Option<Condition> { support::child(&self.syntax) } 931 pub fn condition(&self) -> Option<Condition> { support::child(&self.syntax) }
932} 932}
933#[derive(Debug, Clone, PartialEq, Eq, Hash)] 933#[derive(Debug, Clone, PartialEq, Eq, Hash)]
934pub struct YieldExpr {
935 pub(crate) syntax: SyntaxNode,
936}
937impl ast::AttrsOwner for YieldExpr {}
938impl YieldExpr {
939 pub fn yield_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![yield]) }
940 pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) }
941}
942#[derive(Debug, Clone, PartialEq, Eq, Hash)]
934pub struct Label { 943pub struct Label {
935 pub(crate) syntax: SyntaxNode, 944 pub(crate) syntax: SyntaxNode,
936} 945}
@@ -1334,6 +1343,7 @@ pub enum Expr {
1334 TryExpr(TryExpr), 1343 TryExpr(TryExpr),
1335 TupleExpr(TupleExpr), 1344 TupleExpr(TupleExpr),
1336 WhileExpr(WhileExpr), 1345 WhileExpr(WhileExpr),
1346 YieldExpr(YieldExpr),
1337} 1347}
1338#[derive(Debug, Clone, PartialEq, Eq, Hash)] 1348#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1339pub enum Item { 1349pub enum Item {
@@ -2386,6 +2396,17 @@ impl AstNode for WhileExpr {
2386 } 2396 }
2387 fn syntax(&self) -> &SyntaxNode { &self.syntax } 2397 fn syntax(&self) -> &SyntaxNode { &self.syntax }
2388} 2398}
2399impl AstNode for YieldExpr {
2400 fn can_cast(kind: SyntaxKind) -> bool { kind == YIELD_EXPR }
2401 fn cast(syntax: SyntaxNode) -> Option<Self> {
2402 if Self::can_cast(syntax.kind()) {
2403 Some(Self { syntax })
2404 } else {
2405 None
2406 }
2407 }
2408 fn syntax(&self) -> &SyntaxNode { &self.syntax }
2409}
2389impl AstNode for Label { 2410impl AstNode for Label {
2390 fn can_cast(kind: SyntaxKind) -> bool { kind == LABEL } 2411 fn can_cast(kind: SyntaxKind) -> bool { kind == LABEL }
2391 fn cast(syntax: SyntaxNode) -> Option<Self> { 2412 fn cast(syntax: SyntaxNode) -> Option<Self> {
@@ -3028,6 +3049,9 @@ impl From<TupleExpr> for Expr {
3028impl From<WhileExpr> for Expr { 3049impl From<WhileExpr> for Expr {
3029 fn from(node: WhileExpr) -> Expr { Expr::WhileExpr(node) } 3050 fn from(node: WhileExpr) -> Expr { Expr::WhileExpr(node) }
3030} 3051}
3052impl From<YieldExpr> for Expr {
3053 fn from(node: YieldExpr) -> Expr { Expr::YieldExpr(node) }
3054}
3031impl AstNode for Expr { 3055impl AstNode for Expr {
3032 fn can_cast(kind: SyntaxKind) -> bool { 3056 fn can_cast(kind: SyntaxKind) -> bool {
3033 match kind { 3057 match kind {
@@ -3035,7 +3059,8 @@ impl AstNode for Expr {
3035 | CAST_EXPR | CLOSURE_EXPR | CONTINUE_EXPR | EFFECT_EXPR | FIELD_EXPR | FOR_EXPR 3059 | CAST_EXPR | CLOSURE_EXPR | CONTINUE_EXPR | EFFECT_EXPR | FIELD_EXPR | FOR_EXPR
3036 | IF_EXPR | INDEX_EXPR | LITERAL | LOOP_EXPR | MACRO_CALL | MATCH_EXPR 3060 | IF_EXPR | INDEX_EXPR | LITERAL | LOOP_EXPR | MACRO_CALL | MATCH_EXPR
3037 | METHOD_CALL_EXPR | PAREN_EXPR | PATH_EXPR | PREFIX_EXPR | RANGE_EXPR 3061 | METHOD_CALL_EXPR | PAREN_EXPR | PATH_EXPR | PREFIX_EXPR | RANGE_EXPR
3038 | RECORD_EXPR | REF_EXPR | RETURN_EXPR | TRY_EXPR | TUPLE_EXPR | WHILE_EXPR => true, 3062 | RECORD_EXPR | REF_EXPR | RETURN_EXPR | TRY_EXPR | TUPLE_EXPR | WHILE_EXPR
3063 | YIELD_EXPR => true,
3039 _ => false, 3064 _ => false,
3040 } 3065 }
3041 } 3066 }
@@ -3071,6 +3096,7 @@ impl AstNode for Expr {
3071 TRY_EXPR => Expr::TryExpr(TryExpr { syntax }), 3096 TRY_EXPR => Expr::TryExpr(TryExpr { syntax }),
3072 TUPLE_EXPR => Expr::TupleExpr(TupleExpr { syntax }), 3097 TUPLE_EXPR => Expr::TupleExpr(TupleExpr { syntax }),
3073 WHILE_EXPR => Expr::WhileExpr(WhileExpr { syntax }), 3098 WHILE_EXPR => Expr::WhileExpr(WhileExpr { syntax }),
3099 YIELD_EXPR => Expr::YieldExpr(YieldExpr { syntax }),
3074 _ => return None, 3100 _ => return None,
3075 }; 3101 };
3076 Some(res) 3102 Some(res)
@@ -3107,6 +3133,7 @@ impl AstNode for Expr {
3107 Expr::TryExpr(it) => &it.syntax, 3133 Expr::TryExpr(it) => &it.syntax,
3108 Expr::TupleExpr(it) => &it.syntax, 3134 Expr::TupleExpr(it) => &it.syntax,
3109 Expr::WhileExpr(it) => &it.syntax, 3135 Expr::WhileExpr(it) => &it.syntax,
3136 Expr::YieldExpr(it) => &it.syntax,
3110 } 3137 }
3111 } 3138 }
3112} 3139}
@@ -3983,6 +4010,11 @@ impl std::fmt::Display for WhileExpr {
3983 std::fmt::Display::fmt(self.syntax(), f) 4010 std::fmt::Display::fmt(self.syntax(), f)
3984 } 4011 }
3985} 4012}
4013impl std::fmt::Display for YieldExpr {
4014 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
4015 std::fmt::Display::fmt(self.syntax(), f)
4016 }
4017}
3986impl std::fmt::Display for Label { 4018impl std::fmt::Display for Label {
3987 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 4019 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3988 std::fmt::Display::fmt(self.syntax(), f) 4020 std::fmt::Display::fmt(self.syntax(), f)
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs
index 1ed8a96e5..9ffc3ae11 100644
--- a/crates/syntax/src/ast/make.rs
+++ b/crates/syntax/src/ast/make.rs
@@ -108,8 +108,12 @@ pub fn use_tree_list(use_trees: impl IntoIterator<Item = ast::UseTree>) -> ast::
108 ast_from_text(&format!("use {{{}}};", use_trees)) 108 ast_from_text(&format!("use {{{}}};", use_trees))
109} 109}
110 110
111pub fn use_(use_tree: ast::UseTree) -> ast::Use { 111pub fn use_(visibility: Option<ast::Visibility>, use_tree: ast::UseTree) -> ast::Use {
112 ast_from_text(&format!("use {};", use_tree)) 112 let visibility = match visibility {
113 None => String::new(),
114 Some(it) => format!("{} ", it),
115 };
116 ast_from_text(&format!("{}use {};", visibility, use_tree))
113} 117}
114 118
115pub fn record_expr_field(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordExprField { 119pub fn record_expr_field(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordExprField {
diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs
index 27381ba80..b8ce71d27 100644
--- a/crates/syntax/src/ast/node_ext.rs
+++ b/crates/syntax/src/ast/node_ext.rs
@@ -156,14 +156,28 @@ impl ast::PathSegment {
156 .expect("segments are always nested in paths") 156 .expect("segments are always nested in paths")
157 } 157 }
158 158
159 pub fn crate_token(&self) -> Option<SyntaxToken> {
160 self.name_ref().and_then(|it| it.crate_token())
161 }
162
163 pub fn self_token(&self) -> Option<SyntaxToken> {
164 self.name_ref().and_then(|it| it.self_token())
165 }
166
167 pub fn super_token(&self) -> Option<SyntaxToken> {
168 self.name_ref().and_then(|it| it.super_token())
169 }
170
159 pub fn kind(&self) -> Option<PathSegmentKind> { 171 pub fn kind(&self) -> Option<PathSegmentKind> {
160 let res = if let Some(name_ref) = self.name_ref() { 172 let res = if let Some(name_ref) = self.name_ref() {
161 PathSegmentKind::Name(name_ref) 173 match name_ref.syntax().first_token().map(|it| it.kind()) {
174 Some(T![self]) => PathSegmentKind::SelfKw,
175 Some(T![super]) => PathSegmentKind::SuperKw,
176 Some(T![crate]) => PathSegmentKind::CrateKw,
177 _ => PathSegmentKind::Name(name_ref),
178 }
162 } else { 179 } else {
163 match self.syntax().first_child_or_token()?.kind() { 180 match self.syntax().first_child_or_token()?.kind() {
164 T![self] => PathSegmentKind::SelfKw,
165 T![super] => PathSegmentKind::SuperKw,
166 T![crate] => PathSegmentKind::CrateKw,
167 T![<] => { 181 T![<] => {
168 // <T> or <T as Trait> 182 // <T> or <T as Trait>
169 // T is any TypeRef, Trait has to be a PathType 183 // T is any TypeRef, Trait has to be a PathType