Skip to content

Commit 49cf173

Browse files
redsun82Paolo Tranquilli
authored andcommitted
Rust: expand attribute macros
1 parent 3437210 commit 49cf173

Some content is hidden

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

66 files changed

+16565
-1376
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
@@ -142,6 +142,7 @@ fn fix_blank_lines(s: &str) -> String {
142142
fn write_schema(
143143
grammar: &AstSrc,
144144
super_types: BTreeMap<String, BTreeSet<String>>,
145+
mustache_ctx: &mustache::Context,
145146
) -> mustache::Result<String> {
146147
let mut schema = Schema::default();
147148
schema.classes.extend(
@@ -156,7 +157,7 @@ fn write_schema(
156157
.iter()
157158
.map(|node| node_src_to_schema_class(node, &super_types)),
158159
);
159-
let template = mustache::compile_str(include_str!("templates/schema.mustache"))?;
160+
let template = mustache_ctx.compile_path("schema")?;
160161
let res = template.render_to_string(&schema)?;
161162
Ok(fix_blank_lines(&res))
162163
}
@@ -541,7 +542,7 @@ fn node_to_extractor_info(node: &AstNodeSrc) -> ExtractorNodeInfo {
541542
}
542543
}
543544

544-
fn write_extractor(grammar: &AstSrc) -> mustache::Result<String> {
545+
fn write_extractor(grammar: &AstSrc, mustache_ctx: &mustache::Context) -> mustache::Result<String> {
545546
let extractor_info = ExtractorInfo {
546547
enums: grammar
547548
.enums
@@ -550,7 +551,7 @@ fn write_extractor(grammar: &AstSrc) -> mustache::Result<String> {
550551
.collect(),
551552
nodes: grammar.nodes.iter().map(node_to_extractor_info).collect(),
552553
};
553-
let template = mustache::compile_str(include_str!("templates/extractor.mustache"))?;
554+
let template = mustache_ctx.compile_path("extractor")?;
554555
let res = template.render_to_string(&extractor_info)?;
555556
Ok(fix_blank_lines(&res))
556557
}
@@ -578,16 +579,21 @@ fn main() -> anyhow::Result<()> {
578579
let super_class_y = super_types.get(&y.name).into_iter().flatten().max();
579580
super_class_x.cmp(&super_class_y).then(x.name.cmp(&y.name))
580581
});
581-
let schema = write_schema(&grammar, super_types)?;
582-
let schema_path = project_root().join("schema/ast.py");
582+
let root = project_root();
583+
let mustache_ctx = mustache::Context {
584+
template_path: root.join("ast-generator").join("templates"),
585+
template_extension: "mustache".to_string(),
586+
};
587+
let schema = write_schema(&grammar, super_types, &mustache_ctx)?;
588+
let schema_path = root.join("schema/ast.py");
583589
codegen::ensure_file_contents(
584590
crate::flags::CodegenType::Grammar,
585591
&schema_path,
586592
&schema,
587593
false,
588594
);
589595

590-
let extractor = write_extractor(&grammar)?;
596+
let extractor = write_extractor(&grammar, &mustache_ctx)?;
591597
let extractor_path = project_root().join("extractor/src/translate/generated.rs");
592598
codegen::ensure_file_contents(
593599
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" "$@"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Element extends @element {
2+
string toString() { none() }
3+
}
4+
5+
query predicate new_macro_call_expandeds(Element id, Element expanded) {
6+
item_expandeds(id, expanded) and macro_calls(id)
7+
}

0 commit comments

Comments
 (0)