Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
maciektr committed Sep 16, 2024
1 parent d894490 commit 5eaeb58
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 81 deletions.
146 changes: 67 additions & 79 deletions scarb/src/compiler/plugin/proc_macro/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ impl ProcMacroHostPlugin {
) -> InnerAttrExpansionResult {
let mut context = InnerAttrExpansionContext::new(self);
let mut item_builder = PatchBuilder::new(db, &item_ast);
let mut all_none = true;

match item_ast.clone() {
ast::ModuleItem::Trait(trait_ast) => {
Expand All @@ -179,7 +180,6 @@ impl ProcMacroHostPlugin {
InnerAttrExpansionResult::None
}
MaybeTraitBody::Some(body) => {
let mut all_none = true;
item_builder.add_node(body.lbrace(db).as_syntax_node());

let item_list = body.items(db);
Expand All @@ -195,45 +195,16 @@ impl ProcMacroHostPlugin {
func_builder.add_node(func.declaration(db).as_syntax_node());
func_builder.add_node(func.body(db).as_syntax_node());
let token_stream = TokenStream::new(func_builder.build().0);
let (input, args, stable_ptr) = match found {
AttrExpansionFound::Last {
expansion,
args,
stable_ptr,
} => {
all_none = false;
(expansion, args, stable_ptr)
}
AttrExpansionFound::Some {
expansion,
args,
stable_ptr,
} => {
all_none = false;
(expansion, args, stable_ptr)
}
AttrExpansionFound::None => {
item_builder.add_node(func.as_syntax_node());
continue;
}
};

let result = self.instance(input.package_id).generate_code(
input.expansion.name.clone(),
args.clone(),
token_stream.clone(),
);

let expanded = context.register_result(
token_stream.to_string(),
input,
result,
stable_ptr,
);
item_builder.add_modified(RewriteNode::Mapped {
origin: func.as_syntax_node().span(db),
node: Box::new(RewriteNode::Text(expanded.to_string())),
});
all_none = all_none
&& self.do_expand_inner_attr(
db,
&mut context,
&mut item_builder,
found,
func,
token_stream,
);
}

item_builder.add_node(body.rbrace(db).as_syntax_node());
Expand Down Expand Up @@ -263,7 +234,6 @@ impl ProcMacroHostPlugin {
InnerAttrExpansionResult::None
}
MaybeImplBody::Some(body) => {
let mut all_none = true;
item_builder.add_node(body.lbrace(db).as_syntax_node());

let items = body.items(db);
Expand All @@ -280,45 +250,15 @@ impl ProcMacroHostPlugin {
func_builder.add_node(func.declaration(db).as_syntax_node());
func_builder.add_node(func.body(db).as_syntax_node());
let token_stream = TokenStream::new(func_builder.build().0);
let (input, args, stable_ptr) = match found {
AttrExpansionFound::Last {
expansion,
args,
stable_ptr,
} => {
all_none = false;
(expansion, args, stable_ptr)
}
AttrExpansionFound::Some {
expansion,
args,
stable_ptr,
} => {
all_none = false;
(expansion, args, stable_ptr)
}
AttrExpansionFound::None => {
item_builder.add_node(func.as_syntax_node());
continue;
}
};

let result = self.instance(input.package_id).generate_code(
input.expansion.name.clone(),
args.clone(),
token_stream.clone(),
);

let expanded = context.register_result(
token_stream.to_string(),
input,
result,
stable_ptr,
);
item_builder.add_modified(RewriteNode::Mapped {
origin: func.as_syntax_node().span(db),
node: Box::new(RewriteNode::Text(expanded.to_string())),
});
all_none = all_none
&& self.do_expand_inner_attr(
db,
&mut context,
&mut item_builder,
found,
&func,
token_stream,
);
}

item_builder.add_node(body.rbrace(db).as_syntax_node());
Expand All @@ -336,6 +276,54 @@ impl ProcMacroHostPlugin {
}
}

fn do_expand_inner_attr(
&self,
db: &dyn SyntaxGroup,
context: &mut InnerAttrExpansionContext<'_>,
item_builder: &mut PatchBuilder<'_>,
found: AttrExpansionFound,
func: &impl TypedSyntaxNode,
token_stream: TokenStream,
) -> bool {
let mut all_none = true;
let (input, args, stable_ptr) = match found {
AttrExpansionFound::Last {
expansion,
args,
stable_ptr,
} => {
all_none = false;
(expansion, args, stable_ptr)
}
AttrExpansionFound::Some {
expansion,
args,
stable_ptr,
} => {
all_none = false;
(expansion, args, stable_ptr)
}
AttrExpansionFound::None => {
item_builder.add_node(func.as_syntax_node());
return all_none;
}
};

let result = self.instance(input.package_id).generate_code(
input.expansion.name.clone(),
args.clone(),
token_stream.clone(),
);

let expanded = context.register_result(token_stream.to_string(), input, result, stable_ptr);
item_builder.add_modified(RewriteNode::Mapped {
origin: func.as_syntax_node().span(db),
node: Box::new(RewriteNode::Text(expanded.to_string())),
});

all_none
}

/// Find first attribute procedural macros that should be expanded.
///
/// Remove the attribute from the code.
Expand Down
4 changes: 2 additions & 2 deletions scarb/tests/build_cairo_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1489,7 +1489,7 @@ fn can_expand_trait_inner_func_attrr() {
.stdout_matches(indoc! {r#"
[..] Compiling some v1.0.0 ([..]Scarb.toml)
[..] Compiling hello v1.0.0 ([..]Scarb.toml)
[..]Finished release target(s) in [..]
[..]Finished `dev` profile target(s) in [..]
[..]Running hello
Run completed successfully, returning [34]
"#});
Expand Down Expand Up @@ -1598,7 +1598,7 @@ fn can_expand_impl_inner_func_attrr() {
.stdout_matches(indoc! {r#"
[..] Compiling some v1.0.0 ([..]Scarb.toml)
[..] Compiling test(hello_unittest) hello v1.0.0 ([..]Scarb.toml)
[..]Finished release target(s) in [..]
[..]Finished `dev` profile target(s) in [..]
testing hello ...
running 1 test
test hello::tests::test_flow ... ok (gas usage est.: [..])
Expand Down

0 comments on commit 5eaeb58

Please sign in to comment.