Skip to content

Commit

Permalink
visit_XXX_end
Browse files Browse the repository at this point in the history
  • Loading branch information
Kampfkarren committed Oct 4, 2019
1 parent 78eb1a8 commit 6ee6843
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 14 deletions.
69 changes: 69 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ full_moon_derive = { path = "./full-moon-derive", version = "0.2.0" }
generational-arena = "0.2"
itertools = "0.8"
lazy_static = "1.3"
paste = "0.1"
regex = "1.1"
serde = { version = "1.0", features = ["derive"], optional = true }

Expand Down
26 changes: 20 additions & 6 deletions full-moon-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,23 +185,35 @@ pub fn derive_visit(input: TokenStream) -> TokenStream {

let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();

let visit_self = match visit_self_hint(&input.attrs) {
Some(VisitSelfHint::Skip) => quote! {},
let (visit_self, visit_self_end) = match visit_self_hint(&input.attrs) {
Some(VisitSelfHint::Skip) => (quote! {}, quote! {}),
Some(VisitSelfHint::VisitAs(visit_as)) => {
let visit_as_end = syn::Ident::new(&format!("visit_{}_end", visit_as), input_ident.span());
let visit_as = syn::Ident::new(&format!("visit_{}", visit_as), input_ident.span());
quote! {

(quote! {
visitor.#visit_as(self);
}
}, quote! {
visitor.#visit_as_end(self);
})
}
None => {
// name of self in snake_case
let ssself = syn::Ident::new(
&format!("visit_{}", snake_case(&input_ident.to_string())),
input_ident.span(),
);
quote! {

let ssself_end = syn::Ident::new(
&format!("visit_{}_end", snake_case(&input_ident.to_string())),
input_ident.span(),
);

(quote! {
visitor.#ssself(self);
}
}, quote! {
visitor.#ssself_end(self);
})
}
};

Expand All @@ -216,6 +228,7 @@ pub fn derive_visit(input: TokenStream) -> TokenStream {

#visit_self
#expanded
#visit_self_end
}
}

Expand All @@ -229,6 +242,7 @@ pub fn derive_visit(input: TokenStream) -> TokenStream {

#visit_self
#expanded
#visit_self_end
}
}
};
Expand Down
24 changes: 16 additions & 8 deletions src/visitors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@ macro_rules! create_visitor {
ast.nodes().visit(self);
}

$(
#[allow(missing_docs)]
fn $visit_name(&mut self, _node: &$ast_type<'ast>) { }
)+
paste::item! {
$(
#[allow(missing_docs)]
fn $visit_name(&mut self, _node: &$ast_type<'ast>) { }
#[allow(missing_docs)]
fn [<$visit_name _end>](&mut self, _node: &$ast_type<'ast>) { }
)+
}

$(
#[allow(missing_docs)]
Expand All @@ -75,10 +79,14 @@ macro_rules! create_visitor {
ast.nodes_mut().visit_mut(self);
}

$(
#[allow(missing_docs)]
fn $visit_name(&mut self, _node: &mut $ast_type<'ast>) { }
)+
paste::item! {
$(
#[allow(missing_docs)]
fn $visit_name(&mut self, _node: &mut $ast_type<'ast>) { }
#[allow(missing_docs)]
fn [<$visit_name _end>](&mut self, _node: &mut $ast_type<'ast>) { }
)+
}

$(
#[allow(missing_docs)]
Expand Down
39 changes: 39 additions & 0 deletions tests/visitors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,42 @@ fn test_visit_token() {
vec!["-- bla bla bla", "-- comment here", "-- and here"]
);
}

#[test]
fn test_end_visit() {
#[derive(Default)]
struct LogVisitor {
instructions: usize,
if_start_at: usize,
if_end_at: usize,
called_at: usize,
}

impl Visitor<'_> for LogVisitor {
fn visit_if(&mut self, _: &ast::If) {
self.instructions += 1;
self.if_start_at = self.instructions
}

fn visit_if_end(&mut self, _: &ast::If) {
self.instructions += 1;
self.if_end_at = self.instructions;
}

fn visit_call(&mut self, _: &ast::Call) {
self.instructions += 1;
self.called_at = self.instructions;
}
}

let mut visitor = LogVisitor::default();
visitor.visit_ast(&parse(r#"
if true then
call()
end
"#).unwrap());

assert_eq!(visitor.if_start_at, 1);
assert_eq!(visitor.called_at, 2);
assert_eq!(visitor.if_end_at, 3);
}

0 comments on commit 6ee6843

Please sign in to comment.