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

Parse gen blocks and functions #1549

Closed
wants to merge 1 commit 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
14 changes: 7 additions & 7 deletions codegen/src/fold.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{file, full, gen};
use crate::{file, full, r#gen};
use anyhow::Result;
use proc_macro2::{Ident, Span, TokenStream};
use quote::{format_ident, quote};
Expand All @@ -8,7 +8,7 @@ use syn_codegen::{Data, Definitions, Features, Node, Type};
const FOLD_SRC: &str = "src/gen/fold.rs";

fn simple_visit(item: &str, name: &TokenStream) -> TokenStream {
let ident = gen::under_name(item);
let ident = r#gen::under_name(item);
let method = format_ident!("fold_{}", ident);
quote! {
f.#method(#name)
Expand Down Expand Up @@ -73,13 +73,13 @@ fn visit(
}
Some(res)
}
Type::Ext(t) if gen::TERMINAL_TYPES.contains(&&t[..]) => Some(simple_visit(t, name)),
Type::Ext(t) if r#gen::TERMINAL_TYPES.contains(&&t[..]) => Some(simple_visit(t, name)),
Type::Ext(_) | Type::Std(_) | Type::Token(_) | Type::Group(_) => None,
}
}

fn node(traits: &mut TokenStream, impls: &mut TokenStream, s: &Node, defs: &Definitions) {
let under_name = gen::under_name(&s.ident);
let under_name = r#gen::under_name(&s.ident);
let ty = Ident::new(&s.ident, Span::call_site());
let fold_fn = format_ident!("fold_{}", under_name);

Expand Down Expand Up @@ -182,7 +182,7 @@ fn node(traits: &mut TokenStream, impls: &mut TokenStream, s: &Node, defs: &Defi
}

let fold_span_only =
s.data == Data::Private && !gen::TERMINAL_TYPES.contains(&s.ident.as_str());
s.data == Data::Private && !r#gen::TERMINAL_TYPES.contains(&s.ident.as_str());
if fold_span_only {
fold_impl = quote! {
let span = f.fold_span(node.span());
Expand All @@ -209,7 +209,7 @@ fn node(traits: &mut TokenStream, impls: &mut TokenStream, s: &Node, defs: &Defi
}

pub fn generate(defs: &Definitions) -> Result<()> {
let (traits, impls) = gen::traverse(defs, node);
let (traits, impls) = r#gen::traverse(defs, node);
let full_macro = full::get_macro();
file::write(
FOLD_SRC,
Expand All @@ -223,7 +223,7 @@ pub fn generate(defs: &Definitions) -> Result<()> {
)]

#[cfg(any(feature = "full", feature = "derive"))]
use crate::gen::helper::fold::*;
use crate::r#gen::helper::fold::*;
use crate::*;
use proc_macro2::Span;

Expand Down
2 changes: 1 addition & 1 deletion codegen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ mod eq;
mod file;
mod fold;
mod full;
mod gen;
mod r#gen;
mod hash;
mod json;
mod lookup;
Expand Down
10 changes: 5 additions & 5 deletions codegen/src/visit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::operand::{Borrowed, Operand, Owned};
use crate::{file, full, gen};
use crate::{file, full, r#gen};
use anyhow::Result;
use proc_macro2::{Ident, Span, TokenStream};
use quote::{format_ident, quote};
Expand All @@ -9,7 +9,7 @@ use syn_codegen::{Data, Definitions, Features, Node, Type};
const VISIT_SRC: &str = "src/gen/visit.rs";

fn simple_visit(item: &str, name: &Operand) -> TokenStream {
let ident = gen::under_name(item);
let ident = r#gen::under_name(item);
let method = format_ident!("visit_{}", ident);
let name = name.ref_tokens();
quote! {
Expand Down Expand Up @@ -89,13 +89,13 @@ fn visit(
}
Some(res)
}
Type::Ext(t) if gen::TERMINAL_TYPES.contains(&&t[..]) => Some(simple_visit(t, name)),
Type::Ext(t) if r#gen::TERMINAL_TYPES.contains(&&t[..]) => Some(simple_visit(t, name)),
Type::Ext(_) | Type::Std(_) | Type::Token(_) | Type::Group(_) => None,
}
}

fn node(traits: &mut TokenStream, impls: &mut TokenStream, s: &Node, defs: &Definitions) {
let under_name = gen::under_name(&s.ident);
let under_name = r#gen::under_name(&s.ident);
let ty = Ident::new(&s.ident, Span::call_site());
let visit_fn = format_ident!("visit_{}", under_name);

Expand Down Expand Up @@ -195,7 +195,7 @@ fn node(traits: &mut TokenStream, impls: &mut TokenStream, s: &Node, defs: &Defi
}

pub fn generate(defs: &Definitions) -> Result<()> {
let (traits, impls) = gen::traverse(defs, node);
let (traits, impls) = r#gen::traverse(defs, node);
let full_macro = full::get_macro();
file::write(
VISIT_SRC,
Expand Down
10 changes: 5 additions & 5 deletions codegen/src/visit_mut.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::operand::{Borrowed, Operand, Owned};
use crate::{file, full, gen};
use crate::{file, full, r#gen};
use anyhow::Result;
use proc_macro2::{Ident, Span, TokenStream};
use quote::{format_ident, quote};
Expand All @@ -9,7 +9,7 @@ use syn_codegen::{Data, Definitions, Features, Node, Type};
const VISIT_MUT_SRC: &str = "src/gen/visit_mut.rs";

fn simple_visit(item: &str, name: &Operand) -> TokenStream {
let ident = gen::under_name(item);
let ident = r#gen::under_name(item);
let method = format_ident!("visit_{}_mut", ident);
let name = name.ref_mut_tokens();
quote! {
Expand Down Expand Up @@ -89,13 +89,13 @@ fn visit(
}
Some(res)
}
Type::Ext(t) if gen::TERMINAL_TYPES.contains(&&t[..]) => Some(simple_visit(t, name)),
Type::Ext(t) if r#gen::TERMINAL_TYPES.contains(&&t[..]) => Some(simple_visit(t, name)),
Type::Ext(_) | Type::Std(_) | Type::Token(_) | Type::Group(_) => None,
}
}

fn node(traits: &mut TokenStream, impls: &mut TokenStream, s: &Node, defs: &Definitions) {
let under_name = gen::under_name(&s.ident);
let under_name = r#gen::under_name(&s.ident);
let ty = Ident::new(&s.ident, Span::call_site());
let visit_mut_fn = format_ident!("visit_{}_mut", under_name);

Expand Down Expand Up @@ -191,7 +191,7 @@ fn node(traits: &mut TokenStream, impls: &mut TokenStream, s: &Node, defs: &Defi
}

pub fn generate(defs: &Definitions) -> Result<()> {
let (traits, impls) = gen::traverse(defs, node);
let (traits, impls) = r#gen::traverse(defs, node);
let full_macro = full::get_macro();
file::write(
VISIT_MUT_SRC,
Expand Down
49 changes: 49 additions & 0 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ ast_enum_of_structs! {
/// A for loop: `for pat in expr { ... }`.
ForLoop(ExprForLoop),

/// A gen block: `gen { }`.
Gen(ExprGen),

/// An expression contained within invisible delimiters.
///
/// This variant is important for faithfully representing the precedence
Expand Down Expand Up @@ -400,6 +403,18 @@ ast_struct! {
}
}

ast_struct! {
/// A gen block: `gen { }`.
#[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
pub struct ExprGen #full {
pub attrs: Vec<Attribute>,
pub async_token: Option<Token![async]>,
pub gen_token: Token![gen],
pub capture: Option<Token![move]>,
pub block: Block,
}
}

ast_struct! {
/// An expression contained within invisible delimiters.
///
Expand Down Expand Up @@ -699,6 +714,7 @@ impl Expr {
| Expr::Continue(ExprContinue { attrs, .. })
| Expr::Field(ExprField { attrs, .. })
| Expr::ForLoop(ExprForLoop { attrs, .. })
| Expr::Gen(ExprGen { attrs, .. })
| Expr::Group(ExprGroup { attrs, .. })
| Expr::If(ExprIf { attrs, .. })
| Expr::Index(ExprIndex { attrs, .. })
Expand Down Expand Up @@ -932,6 +948,7 @@ pub(crate) fn requires_terminator(expr: &Expr) -> bool {
| Expr::Closure(_)
| Expr::Continue(_)
| Expr::Field(_)
| Expr::Gen(_)
| Expr::Group(_)
| Expr::Index(_)
| Expr::Infer(_)
Expand Down Expand Up @@ -1589,6 +1606,12 @@ pub(crate) mod parsing {
&& (input.peek2(token::Brace) || input.peek2(Token![move]) && input.peek3(token::Brace))
{
input.parse().map(Expr::Async)
} else if (input.peek(Token![async])
&& input.peek2(Token![gen])
&& (input.peek3(token::Brace) || input.peek3(Token![move]) && input.peek4(token::Brace)))
|| (input.peek(Token![gen]) && (input.peek2(token::Brace) || input.peek2(Token![move]) && input.peek3(token::Brace)))
{
input.parse().map(Expr::Gen)
} else if input.peek(Token![try]) && input.peek2(token::Brace) {
input.parse().map(Expr::TryBlock)
} else if input.peek(Token![|])
Expand Down Expand Up @@ -2404,6 +2427,20 @@ pub(crate) mod parsing {
}
}

#[cfg(feature = "full")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
impl Parse for ExprGen {
fn parse(input: ParseStream) -> Result<Self> {
Ok(ExprGen {
attrs: Vec::new(),
async_token: input.parse()?,
gen_token: input.parse()?,
capture: input.parse()?,
block: input.parse()?,
})
}
}

#[cfg(feature = "full")]
fn closure_arg(input: ParseStream) -> Result<Pat> {
let attrs = input.call(Attribute::parse_outer)?;
Expand Down Expand Up @@ -3106,6 +3143,18 @@ pub(crate) mod printing {
}
}

#[cfg(feature = "full")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
impl ToTokens for ExprGen {
fn to_tokens(&self, tokens: &mut TokenStream) {
outer_attrs_to_tokens(&self.attrs, tokens);
self.async_token.to_tokens(tokens);
self.gen_token.to_tokens(tokens);
self.capture.to_tokens(tokens);
self.block.to_tokens(tokens);
}
}

#[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
impl ToTokens for ExprGroup {
fn to_tokens(&self, tokens: &mut TokenStream) {
Expand Down
16 changes: 16 additions & 0 deletions src/gen/clone.rs

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

21 changes: 21 additions & 0 deletions src/gen/debug.rs

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

21 changes: 17 additions & 4 deletions src/gen/eq.rs

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

Loading
Loading