Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for plugins to change parsing, printing, and visiting behaviors #213

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
**/target
**/*.rs.bk
Cargo.lock
Cargo.lock

# I `cargo expand > EXPAND.rs` sometimes
EXPAND.rs
6 changes: 0 additions & 6 deletions full-moon-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ extern crate proc_macro;

mod derive;
mod node;
mod symbols;
mod visit;

use derive::DeriveGenerator;
Expand All @@ -20,8 +19,3 @@ pub fn derive_visit(input: TokenStream) -> TokenStream {
pub fn derive_node(input: TokenStream) -> TokenStream {
node::NodeGenerator::derive(input)
}

#[proc_macro]
pub fn symbols(input: TokenStream) -> TokenStream {
symbols::parse(input)
}
2 changes: 1 addition & 1 deletion full-moon-derive/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl DeriveGenerator for NodeGenerator {
}
}

impl #impl_generics crate::node::Node #impl_generics for #input_ident #ty_generics #where_clause {
impl #impl_generics crate::node::Node for #input_ident #ty_generics #where_clause {
fn start_position(&self) -> Option<crate::tokenizer::Position> {
Some(#macro_name!("range", { #tokens })?.0)
}
Expand Down
154 changes: 0 additions & 154 deletions full-moon-derive/src/symbols.rs

This file was deleted.

35 changes: 30 additions & 5 deletions full-moon-derive/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ impl VisitGenerator {
for field in data_fields
.iter()
.filter(|field| search_hint("visit", &field.attrs) != Some(VisitHint::Skip))
.filter(|field| {
field
.ident
.as_ref()
.expect("field requires an ident")
.to_string()
!= "plugin_info"
})
{
let ident = field.ident.as_ref().unwrap();
let token_stream = quote! { #prefix#ident };
Expand Down Expand Up @@ -95,7 +103,7 @@ impl VisitGenerator {
impl DeriveGenerator for VisitGenerator {
fn complete(input: &syn::DeriveInput, tokens: TokenStream) -> TokenStream {
let input_ident = &input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let (_, ty_generics, where_clause) = input.generics.split_for_impl();

let (visit_self, visit_self_end) = match search_hint("visit", &input.attrs) {
Some(VisitHint::SkipVisitSelf) => (quote! {}, quote! {}),
Expand Down Expand Up @@ -137,8 +145,8 @@ impl DeriveGenerator for VisitGenerator {
};

quote! {
impl #impl_generics crate::visitors::Visit for #input_ident #ty_generics #where_clause {
fn visit<V: crate::visitors::Visitor>(&self, visitor: &mut V) {
impl<P: crate::plugins::Plugin> crate::visitors::Visit<P> for #input_ident #ty_generics #where_clause {
fn visit<V: crate::visitors::Visitor<P>>(&self, visitor: &mut V) {
macro_rules! visit {
($visit_what: expr, $visitor: expr) => {
$visit_what.visit($visitor);
Expand Down Expand Up @@ -171,8 +179,8 @@ impl DeriveGenerator for VisitGenerator {
}
}

impl #impl_generics crate::visitors::VisitMut for #input_ident #ty_generics #where_clause {
fn visit_mut<V: crate::visitors::VisitorMut>(mut self, visitor: &mut V) -> Self {
impl<P: crate::plugins::Plugin> crate::visitors::VisitMut<P> for #input_ident #ty_generics #where_clause {
fn visit_mut<V: crate::visitors::VisitorMut<P>>(mut self, visitor: &mut V) -> Self {
macro_rules! visit {
($visit_what: expr, $visitor: expr) => {
$visit_what = $visit_what.visit_mut($visitor);
Expand Down Expand Up @@ -225,6 +233,12 @@ impl MatchEnumGenerator for VisitGenerator {
variant: &syn::Ident,
named: &syn::FieldsNamed,
) -> TokenStream {
// Plugins should create their own visitors, there's no good way
// to handle this generically.
if *variant == "Plugin" {
panic!("Plugin wasn't expected as a named variant");
}

let fields: Vec<_> = named
.named
.iter()
Expand Down Expand Up @@ -255,6 +269,17 @@ impl MatchEnumGenerator for VisitGenerator {
variant: &syn::Ident,
fields: &syn::FieldsUnnamed,
) -> TokenStream {
// Plugins should create their own visitors, there's no good way
// to handle this generically.
if *variant == "Plugin" {
return quote! {
#input::#variant(node_info) => {
// TODO: This is probably wrong
unimplemented!("Plugin variant should not be being matched, plugins need to handle their own visit behavior");
}
};
}

let fields: Vec<_> = fields
.unnamed
.iter()
Expand Down
2 changes: 1 addition & 1 deletion full-moon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ smol_str = { version = "0.1.17", features = ["serde"] }

[dev-dependencies]
criterion = "0.2"
insta = { version = "1.7.0", features = ["glob"] }
insta = { version = "1.8.0", features = ["glob"] }
pretty_assertions = "0.6.1"

[[bench]]
Expand Down
Loading