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

Convert ContainedSpan to ContainedSpan<T> #187

Closed
wants to merge 7 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
79 changes: 21 additions & 58 deletions full-moon-derive/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,6 @@ fn token_getter(
}
}

#[derive(PartialEq)]
enum NodeHint {
FullRange,
}

impl Hint for NodeHint {
fn unit(name: String) -> Option<Self> {
if name == "full_range" {
Some(NodeHint::FullRange)
} else {
None
}
}
}

pub struct NodeGenerator;

impl DeriveGenerator for NodeGenerator {
Expand Down Expand Up @@ -122,41 +107,29 @@ impl StructGenerator for StructRangeGenerator {
.map(|field| field.ident.as_ref().unwrap())
.collect::<Vec<_>>();

let full_range = strukt
.fields
.iter()
.find(|field| search_hint("node", &field.attrs) == Some(NodeHint::FullRange));

if let Some(full_range) = full_range {
let ident = full_range.ident.as_ref().unwrap();
quote! {
self.#ident.range()
}
} else {
let (mut start_position, mut end_position) = (
Vec::with_capacity(fields.len()),
Vec::with_capacity(fields.len()),
);
let (mut start_position, mut end_position) = (
Vec::with_capacity(fields.len()),
Vec::with_capacity(fields.len()),
);

for field in &fields {
start_position.push(quote! {
.or_else(|| {
self.#field.start_position()
})
});
}
for field in &fields {
start_position.push(quote! {
.or_else(|| {
self.#field.start_position()
})
});
}

for field in fields.iter().rev() {
end_position.push(quote! {
.or_else(|| {
self.#field.end_position()
})
});
}
for field in fields.iter().rev() {
end_position.push(quote! {
.or_else(|| {
self.#field.end_position()
})
});
}

quote! {
Some((None#(#start_position)*?, None#(#end_position)*?))
}
quote! {
Some((None#(#start_position)*?, None#(#end_position)*?))
}
}
}
Expand Down Expand Up @@ -238,17 +211,7 @@ impl MatchEnumGenerator for EnumRangeGenerator {
.map(|field| field.ident.as_ref().unwrap())
.collect::<Vec<_>>();

let full_range = named
.named
.iter()
.find(|field| search_hint("node", &field.attrs) == Some(NodeHint::FullRange));

let body = if let Some(full_range) = full_range {
let ident = full_range.ident.as_ref().unwrap();
quote! {
#ident.range()
}
} else {
let body = {
let (mut start_position, mut end_position) = (
Vec::with_capacity(fields.len()),
Vec::with_capacity(fields.len()),
Expand Down
28 changes: 1 addition & 27 deletions full-moon-derive/src/visit.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::derive::*;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use std::collections::HashMap;

// Not 100% accurate, but it is to full-moon's codebase
fn snake_case(pascal_case: &str) -> String {
Expand All @@ -22,7 +21,6 @@ fn snake_case(pascal_case: &str) -> String {

#[derive(Debug, PartialEq)]
enum VisitHint {
Contains(String),
Skip,
SkipVisitSelf,
VisitAs(String),
Expand All @@ -32,8 +30,6 @@ impl Hint for VisitHint {
fn key_value(key: String, value: String) -> Option<Self> {
if key == "visit_as" {
Some(VisitHint::VisitAs(value))
} else if key == "contains" {
Some(VisitHint::Contains(value))
} else {
None
}
Expand All @@ -53,7 +49,6 @@ pub struct VisitGenerator;
impl VisitGenerator {
fn visit_fields(data_fields: &syn::Fields, prefix: TokenStream) -> TokenStream {
let mut fields = Vec::new();
let mut contains = HashMap::new();

for field in data_fields
.iter()
Expand All @@ -62,28 +57,7 @@ impl VisitGenerator {
let ident = field.ident.as_ref().unwrap();
let token_stream = quote! { #prefix#ident };

if let Some(VisitHint::Contains(contains_node)) = search_hint("visit", &field.attrs) {
contains.insert(contains_node, ident);
} else if let Some(contains_me) = contains.remove(&ident.to_string()) {
fields.push(quote! {
#prefix#contains_me.tokens.0
});

fields.push(token_stream);

fields.push(quote! {
#prefix#contains_me.tokens.1
});
} else {
fields.push(token_stream);
}
}

if !contains.is_empty() {
panic!(
"#[visit(contains = \"...\")] used in wrong order: {:?}",
contains
);
fields.push(token_stream);
}

quote! {
Expand Down
Loading