Skip to content

Commit e4b7b91

Browse files
authored
Merge pull request #19334 from github/redsun82/rust-expand-attr-macros
Rust: expand attribute macros
2 parents 8f5a2a9 + 3d38d77 commit e4b7b91

File tree

119 files changed

+17147
-1391
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

119 files changed

+17147
-1391
lines changed

rust/ast-generator/BUILD.bazel

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ codeql_rust_binary(
4646
) + [":codegen"],
4747
aliases = aliases(),
4848
args = ["$(rlocationpath :rust.ungram)"],
49-
compile_data = glob(["src/templates/*.mustache"]),
50-
data = [":rust.ungram"],
49+
data = [":rust.ungram"] + glob(["templates/*.mustache"]),
5150
proc_macro_deps = all_crate_deps(
5251
proc_macro = True,
5352
),

rust/ast-generator/src/main.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ fn fix_blank_lines(s: &str) -> String {
144144
fn write_schema(
145145
grammar: &AstSrc,
146146
super_types: BTreeMap<String, BTreeSet<String>>,
147+
mustache_ctx: &mustache::Context,
147148
) -> mustache::Result<String> {
148149
let mut schema = Schema::default();
149150
schema.classes.extend(
@@ -158,7 +159,7 @@ fn write_schema(
158159
.iter()
159160
.map(|node| node_src_to_schema_class(node, &super_types)),
160161
);
161-
let template = mustache::compile_str(include_str!("templates/schema.mustache"))?;
162+
let template = mustache_ctx.compile_path("schema")?;
162163
let res = template.render_to_string(&schema)?;
163164
Ok(fix_blank_lines(&res))
164165
}
@@ -543,7 +544,7 @@ fn node_to_extractor_info(node: &AstNodeSrc) -> ExtractorNodeInfo {
543544
}
544545
}
545546

546-
fn write_extractor(grammar: &AstSrc) -> mustache::Result<String> {
547+
fn write_extractor(grammar: &AstSrc, mustache_ctx: &mustache::Context) -> mustache::Result<String> {
547548
let extractor_info = ExtractorInfo {
548549
enums: grammar
549550
.enums
@@ -552,7 +553,7 @@ fn write_extractor(grammar: &AstSrc) -> mustache::Result<String> {
552553
.collect(),
553554
nodes: grammar.nodes.iter().map(node_to_extractor_info).collect(),
554555
};
555-
let template = mustache::compile_str(include_str!("templates/extractor.mustache"))?;
556+
let template = mustache_ctx.compile_path("extractor")?;
556557
let res = template.render_to_string(&extractor_info)?;
557558
Ok(fix_blank_lines(&res))
558559
}
@@ -580,16 +581,21 @@ fn main() -> anyhow::Result<()> {
580581
let super_class_y = super_types.get(&y.name).into_iter().flatten().max();
581582
super_class_x.cmp(&super_class_y).then(x.name.cmp(&y.name))
582583
});
583-
let schema = write_schema(&grammar, super_types)?;
584-
let schema_path = project_root().join("schema/ast.py");
584+
let root = project_root();
585+
let mustache_ctx = mustache::Context {
586+
template_path: root.join("ast-generator").join("templates"),
587+
template_extension: "mustache".to_string(),
588+
};
589+
let schema = write_schema(&grammar, super_types, &mustache_ctx)?;
590+
let schema_path = root.join("schema/ast.py");
585591
codegen::ensure_file_contents(
586592
crate::flags::CodegenType::Grammar,
587593
&schema_path,
588594
&schema,
589595
false,
590596
);
591597

592-
let extractor = write_extractor(&grammar)?;
598+
let extractor = write_extractor(&grammar, &mustache_ctx)?;
593599
let extractor_path = project_root().join("extractor/src/translate/generated.rs");
594600
codegen::ensure_file_contents(
595601
crate::flags::CodegenType::Grammar,

rust/ast-generator/src/templates/extractor.mustache renamed to rust/ast-generator/templates/extractor.mustache

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//! Generated by `ast-generator`, do not edit by hand.
2-
{{! <- denotes empty line that should be kept, all blank lines are removed otherwise}}
3-
#![cfg_attr(any(), rustfmt::skip)]
4-
2+
53
use super::base::Translator;
64
use super::mappings::TextValue;
75
use crate::emit_detached;
@@ -11,30 +9,33 @@ use ra_ap_syntax::ast::{
119
HasArgList, HasAttrs, HasGenericArgs, HasGenericParams, HasLoopBody, HasModuleItem, HasName,
1210
HasTypeBounds, HasVisibility, RangeItem,
1311
};
14-
use ra_ap_syntax::{ast, AstNode};
15-
12+
#[rustfmt::skip]
13+
use ra_ap_syntax::{AstNode, ast};
14+
1615
impl Translator<'_> {
17-
fn emit_else_branch(&mut self, node: ast::ElseBranch) -> Option<Label<generated::Expr>> {
16+
fn emit_else_branch(&mut self, node: &ast::ElseBranch) -> Option<Label<generated::Expr>> {
1817
match node {
1918
ast::ElseBranch::IfExpr(inner) => self.emit_if_expr(inner).map(Into::into),
2019
ast::ElseBranch::Block(inner) => self.emit_block_expr(inner).map(Into::into),
2120
}
2221
}
2322
{{#enums}}
24-
25-
pub(crate) fn emit_{{snake_case_name}}(&mut self, node: ast::{{ast_name}}) -> Option<Label<generated::{{name}}>> {
26-
match node {
23+
24+
pub(crate) fn emit_{{snake_case_name}}(&mut self, node: &ast::{{ast_name}}) -> Option<Label<generated::{{name}}>> {
25+
let label = match node {
2726
{{#variants}}
2827
ast::{{ast_name}}::{{variant_ast_name}}(inner) => self.emit_{{snake_case_name}}(inner).map(Into::into),
2928
{{/variants}}
30-
}
29+
}?;
30+
emit_detached!({{name}}, self, node, label);
31+
Some(label)
3132
}
3233
{{/enums}}
3334
{{#nodes}}
34-
35-
pub(crate) fn emit_{{snake_case_name}}(&mut self, node: ast::{{ast_name}}) -> Option<Label<generated::{{name}}>> {
35+
36+
pub(crate) fn emit_{{snake_case_name}}(&mut self, node: &ast::{{ast_name}}) -> Option<Label<generated::{{name}}>> {
3637
{{#has_attrs}}
37-
if self.should_be_excluded(&node) { return None; }
38+
if self.should_be_excluded(node) { return None; }
3839
{{/has_attrs}}
3940
{{#fields}}
4041
{{#predicate}}
@@ -44,10 +45,10 @@ impl Translator<'_> {
4445
let {{name}} = node.try_get_text();
4546
{{/string}}
4647
{{#list}}
47-
let {{name}} = node.{{method}}().filter_map(|x| self.emit_{{snake_case_ty}}(x)).collect();
48+
let {{name}} = node.{{method}}().filter_map(|x| self.emit_{{snake_case_ty}}(&x)).collect();
4849
{{/list}}
4950
{{#optional}}
50-
let {{name}} = node.{{method}}().and_then(|x| self.emit_{{snake_case_ty}}(x));
51+
let {{name}} = node.{{method}}().and_then(|x| self.emit_{{snake_case_ty}}(&x));
5152
{{/optional}}
5253
{{/fields}}
5354
let label = self.trap.emit(generated::{{name}} {
@@ -56,9 +57,9 @@ impl Translator<'_> {
5657
{{name}},
5758
{{/fields}}
5859
});
59-
self.emit_location(label, &node);
60+
self.emit_location(label, node);
6061
emit_detached!({{name}}, self, node, label);
61-
self.emit_tokens(&node, label.into(), node.syntax().children_with_tokens());
62+
self.emit_tokens(node, label.into(), node.syntax().children_with_tokens());
6263
Some(label)
6364
}
6465
{{/nodes}}

rust/codegen/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ _args = [
77
"//rust/ast-generator:Cargo.toml",
88
"//misc/codegen",
99
"//rust:codegen-conf",
10+
"@rules_rust//tools/rustfmt:upstream_rustfmt",
1011
]
1112

1213
sh_binary(

rust/codegen/codegen.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ grammar_file="$(rlocation "$2")"
99
ast_generator_manifest="$(rlocation "$3")"
1010
codegen="$(rlocation "$4")"
1111
codegen_conf="$(rlocation "$5")"
12-
shift 5
12+
rustfmt="$(rlocation "$6")"
13+
shift 6
1314

1415
CARGO_MANIFEST_DIR="$(dirname "$ast_generator_manifest")" "$ast_generator" "$grammar_file"
16+
"$rustfmt" "$(dirname "$ast_generator_manifest")/../extractor/src/translate/generated.rs"
1517
"$codegen" --configuration-file="$codegen_conf" "$@"

0 commit comments

Comments
 (0)