Skip to content

Commit a7ca658

Browse files
committed
Handwrite Stmt
1 parent 4d38b0d commit a7ca658

File tree

4 files changed

+61
-51
lines changed

4 files changed

+61
-51
lines changed

crates/ra_syntax/src/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::{
1717

1818
pub use self::{
1919
expr_ext::{ArrayExprKind, BinOp, Effect, ElseBranch, LiteralKind, PrefixOp, RangeOp},
20-
generated::{nodes::*, tokens::*},
20+
generated::*,
2121
node_ext::{
2222
AttrKind, FieldKind, NameOrNameRef, PathSegmentKind, SelfParamKind, SlicePatComponents,
2323
StructKind, TypeBoundKind, VisibilityKind,

crates/ra_syntax/src/ast/generated.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
11
//! This file is actually hand-written, but the submodules are indeed generated.
2-
32
#[rustfmt::skip]
4-
pub(super) mod nodes;
3+
mod nodes;
54
#[rustfmt::skip]
6-
pub(super) mod tokens;
5+
mod tokens;
6+
7+
use crate::{
8+
AstNode,
9+
SyntaxKind::{self, *},
10+
SyntaxNode,
11+
};
12+
13+
pub use {nodes::*, tokens::*};
14+
15+
// Stmt is the only nested enum, so it's easier to just hand-write it
16+
impl 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+
}

crates/ra_syntax/src/ast/generated/nodes.rs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3380,28 +3380,6 @@ impl From<LetStmt> for Stmt {
33803380
impl From<ExprStmt> for Stmt {
33813381
fn from(node: ExprStmt) -> Stmt { Stmt::ExprStmt(node) }
33823382
}
3383-
impl 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-
}
34053383
impl std::fmt::Display for Item {
34063384
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34073385
std::fmt::Display::fmt(self.syntax(), f)

xtask/src/codegen/gen_syntax.rs

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -153,25 +153,10 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: &AstSrc) -> Result<String> {
153153
quote!(impl ast::#trait_name for #name {})
154154
});
155155

156-
(
157-
quote! {
158-
#[pretty_doc_comment_placeholder_workaround]
159-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
160-
pub enum #name {
161-
#(#variants(#variants),)*
162-
}
163-
164-
#(#traits)*
165-
},
156+
let ast_node = if en.name == "Stmt" {
157+
quote! {}
158+
} else {
166159
quote! {
167-
#(
168-
impl From<#variants> for #name {
169-
fn from(node: #variants) -> #name {
170-
#name::#variants(node)
171-
}
172-
}
173-
)*
174-
175160
impl AstNode for #name {
176161
fn can_cast(kind: SyntaxKind) -> bool {
177162
match kind {
@@ -196,6 +181,28 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: &AstSrc) -> Result<String> {
196181
}
197182
}
198183
}
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
199206
},
200207
)
201208
})
@@ -497,13 +504,7 @@ fn lower(grammar: &Grammar) -> AstSrc {
497504
let mut res = AstSrc::default();
498505
res.tokens = vec!["Whitespace".into(), "Comment".into(), "String".into(), "RawString".into()];
499506

500-
let nodes = grammar
501-
.iter()
502-
.filter(|&node| match grammar[node].rule {
503-
Rule::Node(it) if it == node => false,
504-
_ => true,
505-
})
506-
.collect::<Vec<_>>();
507+
let nodes = grammar.iter().collect::<Vec<_>>();
507508

508509
for &node in &nodes {
509510
let name = grammar[node].name.clone();

0 commit comments

Comments
 (0)