aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_syntax/src/ast.rs2
-rw-r--r--crates/ra_syntax/src/ast/generated.rs37
-rw-r--r--crates/ra_syntax/src/ast/generated/nodes.rs22
-rw-r--r--xtask/src/codegen/gen_syntax.rs51
4 files changed, 61 insertions, 51 deletions
diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs
index 8a0e3d27b..d536bb1e7 100644
--- a/crates/ra_syntax/src/ast.rs
+++ b/crates/ra_syntax/src/ast.rs
@@ -17,7 +17,7 @@ use crate::{
17 17
18pub use self::{ 18pub use self::{
19 expr_ext::{ArrayExprKind, BinOp, Effect, ElseBranch, LiteralKind, PrefixOp, RangeOp}, 19 expr_ext::{ArrayExprKind, BinOp, Effect, ElseBranch, LiteralKind, PrefixOp, RangeOp},
20 generated::{nodes::*, tokens::*}, 20 generated::*,
21 node_ext::{ 21 node_ext::{
22 AttrKind, FieldKind, NameOrNameRef, PathSegmentKind, SelfParamKind, SlicePatComponents, 22 AttrKind, FieldKind, NameOrNameRef, PathSegmentKind, SelfParamKind, SlicePatComponents,
23 StructKind, TypeBoundKind, VisibilityKind, 23 StructKind, TypeBoundKind, VisibilityKind,
diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs
index f5199e09f..ba55f1c42 100644
--- a/crates/ra_syntax/src/ast/generated.rs
+++ b/crates/ra_syntax/src/ast/generated.rs
@@ -1,6 +1,37 @@
1//! This file is actually hand-written, but the submodules are indeed generated. 1//! This file is actually hand-written, but the submodules are indeed generated.
2
3#[rustfmt::skip] 2#[rustfmt::skip]
4pub(super) mod nodes; 3mod nodes;
5#[rustfmt::skip] 4#[rustfmt::skip]
6pub(super) mod tokens; 5mod tokens;
6
7use crate::{
8 AstNode,
9 SyntaxKind::{self, *},
10 SyntaxNode,
11};
12
13pub use {nodes::*, tokens::*};
14
15// Stmt is the only nested enum, so it's easier to just hand-write it
16impl AstNode for Stmt {
17 fn can_cast(kind: SyntaxKind) -> bool {
18 match kind {
19 LET_STMT | EXPR_STMT => true,
20 _ => false,
21 }
22 }
23 fn cast(syntax: SyntaxNode) -> Option<Self> {
24 let res = match syntax.kind() {
25 LET_STMT => Stmt::LetStmt(LetStmt { syntax }),
26 EXPR_STMT => Stmt::ExprStmt(ExprStmt { syntax }),
27 _ => return None,
28 };
29 Some(res)
30 }
31 fn syntax(&self) -> &SyntaxNode {
32 match self {
33 Stmt::LetStmt(it) => &it.syntax,
34 Stmt::ExprStmt(it) => &it.syntax,
35 }
36 }
37}
diff --git a/crates/ra_syntax/src/ast/generated/nodes.rs b/crates/ra_syntax/src/ast/generated/nodes.rs
index 286be1032..8ef72fec7 100644
--- a/crates/ra_syntax/src/ast/generated/nodes.rs
+++ b/crates/ra_syntax/src/ast/generated/nodes.rs
@@ -3380,28 +3380,6 @@ impl From<LetStmt> for Stmt {
3380impl From<ExprStmt> for Stmt { 3380impl From<ExprStmt> for Stmt {
3381 fn from(node: ExprStmt) -> Stmt { Stmt::ExprStmt(node) } 3381 fn from(node: ExprStmt) -> Stmt { Stmt::ExprStmt(node) }
3382} 3382}
3383impl AstNode for Stmt {
3384 fn can_cast(kind: SyntaxKind) -> bool {
3385 match kind {
3386 LET_STMT | EXPR_STMT => true,
3387 _ => false,
3388 }
3389 }
3390 fn cast(syntax: SyntaxNode) -> Option<Self> {
3391 let res = match syntax.kind() {
3392 LET_STMT => Stmt::LetStmt(LetStmt { syntax }),
3393 EXPR_STMT => Stmt::ExprStmt(ExprStmt { syntax }),
3394 _ => return None,
3395 };
3396 Some(res)
3397 }
3398 fn syntax(&self) -> &SyntaxNode {
3399 match self {
3400 Stmt::LetStmt(it) => &it.syntax,
3401 Stmt::ExprStmt(it) => &it.syntax,
3402 }
3403 }
3404}
3405impl std::fmt::Display for Item { 3383impl std::fmt::Display for Item {
3406 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 3384 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3407 std::fmt::Display::fmt(self.syntax(), f) 3385 std::fmt::Display::fmt(self.syntax(), f)
diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs
index d6a72ccc0..e3d4269f6 100644
--- a/xtask/src/codegen/gen_syntax.rs
+++ b/xtask/src/codegen/gen_syntax.rs
@@ -153,25 +153,10 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: &AstSrc) -> Result<String> {
153 quote!(impl ast::#trait_name for #name {}) 153 quote!(impl ast::#trait_name for #name {})
154 }); 154 });
155 155
156 ( 156 let ast_node = if en.name == "Stmt" {
157 quote! { 157 quote! {}
158 #[pretty_doc_comment_placeholder_workaround] 158 } else {
159 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
160 pub enum #name {
161 #(#variants(#variants),)*
162 }
163
164 #(#traits)*
165 },
166 quote! { 159 quote! {
167 #(
168 impl From<#variants> for #name {
169 fn from(node: #variants) -> #name {
170 #name::#variants(node)
171 }
172 }
173 )*
174
175 impl AstNode for #name { 160 impl AstNode for #name {
176 fn can_cast(kind: SyntaxKind) -> bool { 161 fn can_cast(kind: SyntaxKind) -> bool {
177 match kind { 162 match kind {
@@ -196,6 +181,28 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: &AstSrc) -> Result<String> {
196 } 181 }
197 } 182 }
198 } 183 }
184 }
185 };
186
187 (
188 quote! {
189 #[pretty_doc_comment_placeholder_workaround]
190 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
191 pub enum #name {
192 #(#variants(#variants),)*
193 }
194
195 #(#traits)*
196 },
197 quote! {
198 #(
199 impl From<#variants> for #name {
200 fn from(node: #variants) -> #name {
201 #name::#variants(node)
202 }
203 }
204 )*
205 #ast_node
199 }, 206 },
200 ) 207 )
201 }) 208 })
@@ -497,13 +504,7 @@ fn lower(grammar: &Grammar) -> AstSrc {
497 let mut res = AstSrc::default(); 504 let mut res = AstSrc::default();
498 res.tokens = vec!["Whitespace".into(), "Comment".into(), "String".into(), "RawString".into()]; 505 res.tokens = vec!["Whitespace".into(), "Comment".into(), "String".into(), "RawString".into()];
499 506
500 let nodes = grammar 507 let nodes = grammar.iter().collect::<Vec<_>>();
501 .iter()
502 .filter(|&node| match grammar[node].rule {
503 Rule::Node(it) if it == node => false,
504 _ => true,
505 })
506 .collect::<Vec<_>>();
507 508
508 for &node in &nodes { 509 for &node in &nodes {
509 let name = grammar[node].name.clone(); 510 let name = grammar[node].name.clone();