Skip to content
Merged
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
7 changes: 6 additions & 1 deletion shared/yeast-macros/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub(crate) struct Node {
/// Optional leaf content such as `"foo"` or `#{name}` in
/// `(identifier "foo")` or `(identifier #{name})`.
pub(crate) content: Option<Content>,
/// Named fields such as `method: {name}` in `(call method: {name})`.
/// Named fields and source-location metadata.
pub(crate) fields: Vec<Field>,
}

Expand All @@ -131,4 +131,9 @@ pub(crate) enum Field {
/// Whether the field value has a trailing `?`.
optional: bool,
},
/// Source-location metadata such as `location: {source}`.
Location {
/// A Rust block yielding the node or nodes whose locations should be combined.
block: syn::Block,
},
}
34 changes: 2 additions & 32 deletions shared/yeast-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ pub fn query(input: TokenStream) -> TokenStream {
/// field: {expr} - extend a named field with `{expr}`'s ids
/// field: (kind ...)? - set the field only if every `#{expr}`
/// beneath it has a value (see below)
/// location: {expr} - override this node's source range with the
/// span of one or more nodes; may be repeated
/// ```
///
/// # Optional fields
Expand Down Expand Up @@ -125,38 +127,6 @@ pub fn trees(input: TokenStream) -> TokenStream {
}
}

/// Build one AST node whose root uses another node's source range.
///
/// Uses the `BuildCtx` binding named `ctx` from the surrounding scope:
///
/// ```text
/// tree_at!(source, (kind ...))
/// ```
#[proc_macro]
pub fn tree_at(input: TokenStream) -> TokenStream {
let input2: TokenStream2 = input.into();
match parse::parse_tree_at_top(input2) {
Ok(output) => output.into(),
Err(err) => err.to_compile_error().into(),
}
}

/// Build one AST node whose root spans a collection of nodes.
///
/// Uses the `BuildCtx` binding named `ctx` from the surrounding scope:
///
/// ```text
/// tree_spanning!(sources, (kind ...))
/// ```
#[proc_macro]
pub fn tree_spanning(input: TokenStream) -> TokenStream {
let input2: TokenStream2 = input.into();
match parse::parse_tree_spanning_top(input2) {
Ok(output) => output.into(),
Err(err) => err.to_compile_error().into(),
}
}

/// Define a desugaring rule with query and transform in one declaration.
///
/// ```text
Expand Down
137 changes: 84 additions & 53 deletions shared/yeast-macros/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ impl Node {
let ctx = Ident::new("ctx", Span::call_site());
let kind_str = self.kind.to_string();

if let Some(content) = &self.content {
return match content {
let node_init = if let Some(content) = &self.content {
let node = match content {
Content::Static(lit) => quote! { #ctx.literal(#kind_str, #lit) },
Content::Computed(block) => {
let block = rust_block(block);
Expand Down Expand Up @@ -362,74 +362,105 @@ impl Node {
}
}
};
}
quote! { let __node: yeast::Id = #node; }
} else {
TokenStream::new()
};

let mut stmts = Vec::new();
let mut field_args = Vec::new();

for (field_counter, field) in self.fields.iter().enumerate() {
let Field::Named {
name,
value,
optional,
} = field;
let field_name = name.to_string();
let field_str = field_name
.strip_prefix("r#")
.unwrap_or(&field_name)
.to_string();
let temp = Ident::new(
&format!("__field_{field_str}_{field_counter}"),
Span::call_site(),
);

match value {
Template::Splice(block) => {
debug_assert!(!optional);
match field {
Field::Location { block } => {
let block = rust_block(block);
stmts.push(quote! {
let mut #temp: Vec<yeast::Id> = Vec::new();
yeast::IntoFieldIds::extend_into(#block, &mut #temp);
});
field_args.push(quote! {
if !#temp.is_empty() {
__fields.push((#field_str, #temp));
}
yeast::IntoFieldIds::extend_into(#block, &mut __location_ids);
});
}
Template::Node(child) if *optional => {
let label = fresh_fallible_label();
let value = child.lower(&LoweringContext {
fallible_scope: Some(&label),
});
stmts.push(quote! {
#[allow(unused_labels)]
let #temp: ::std::option::Option<yeast::Id> = #label: {
::std::option::Option::Some(#value)
};
});
field_args.push(quote! {
if let ::std::option::Option::Some(__id) = #temp {
__fields.push((#field_str, vec![__id]));
Field::Named {
name,
value,
optional,
} => {
let field_name = name.to_string();
let field_str = field_name
.strip_prefix("r#")
.unwrap_or(&field_name)
.to_string();
let temp = Ident::new(
&format!("__field_{field_str}_{field_counter}"),
Span::call_site(),
);

match value {
Template::Splice(block) => {
debug_assert!(!optional);
let block = rust_block(block);
stmts.push(quote! {
let mut #temp: Vec<yeast::Id> = Vec::new();
yeast::IntoFieldIds::extend_into(#block, &mut #temp);
});
field_args.push(quote! {
if !#temp.is_empty() {
__fields.push((#field_str, #temp));
}
});
}
});
}
Template::Node(child) => {
let value = child.lower(context);
stmts.push(quote! { let #temp: yeast::Id = #value; });
field_args.push(quote! {
__fields.push((#field_str, vec![#temp]));
});
Template::Node(child) if *optional => {
let label = fresh_fallible_label();
let value = child.lower(&LoweringContext {
fallible_scope: Some(&label),
});
stmts.push(quote! {
#[allow(unused_labels)]
let #temp: ::std::option::Option<yeast::Id> = #label: {
::std::option::Option::Some(#value)
};
});
field_args.push(quote! {
if let ::std::option::Option::Some(__id) = #temp {
__fields.push((#field_str, vec![__id]));
}
});
}
Template::Node(child) => {
let value = child.lower(context);
stmts.push(quote! { let #temp: yeast::Id = #value; });
field_args.push(quote! {
__fields.push((#field_str, vec![#temp]));
});
}
}
}
}
}

let build_node = if self.content.is_none() {
quote! {
let mut __fields: Vec<(&str, Vec<yeast::Id>)> = Vec::new();
#(#field_args)*
let __node: yeast::Id = #ctx.node(#kind_str, __fields);
}
} else {
TokenStream::new()
};

quote! {
{
let mut __location_ids: Vec<yeast::Id> = Vec::new();
#node_init
#(#stmts)*
let mut __fields: Vec<(&str, Vec<yeast::Id>)> = Vec::new();
#(#field_args)*
#ctx.node(#kind_str, __fields)
#build_node
let __source_range = __location_ids
.into_iter()
.filter_map(|__source| {
#ctx.ast
.get_node(__source)
.and_then(|__node| __node.source_range())
})
.reduce(yeast::Range::union);
#ctx.set_node_source_range(__node, __source_range)
}
}
}
Expand Down
59 changes: 0 additions & 59 deletions shared/yeast-macros/src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use proc_macro2::{Span, TokenStream, TokenTree};
use quote::quote;
use syn::{
parse::{Parse, ParseStream},
Expr, Token,
};

type Result<T> = std::result::Result<T, syn::Error>;

Expand All @@ -28,47 +24,6 @@ pub fn parse_trees_top(input: TokenStream) -> Result<TokenStream> {
Ok(crate::ast::Template::lower_list(&templates))
}

pub fn parse_tree_at_top(input: TokenStream) -> Result<TokenStream> {
let LocatedTreeInput { source, template } = syn::parse2(input)?;
let template = crate::template_parse::parse_template(template)?;
let node = template.lower_root();
let ctx = syn::Ident::new("ctx", Span::call_site());

Ok(quote! {
{
let __yeast_source: yeast::Id = { #source };
let __yeast_source_range = #ctx
.ast
.get_node(__yeast_source)
.and_then(|node| node.source_range());
let __yeast_node: yeast::Id = #node;
#ctx.set_node_source_range(__yeast_node, __yeast_source_range)
}
})
}

pub fn parse_tree_spanning_top(input: TokenStream) -> Result<TokenStream> {
let LocatedTreeInput {
source: sources,
template,
} = syn::parse2(input)?;
let template = crate::template_parse::parse_template(template)?;
let node = template.lower_root();
let ctx = syn::Ident::new("ctx", Span::call_site());

Ok(quote! {
{
let __yeast_source_range = ::std::iter::IntoIterator::into_iter({ #sources })
.filter_map(|source: yeast::Id| {
#ctx.ast.get_node(source).and_then(|node| node.source_range())
})
.reduce(yeast::Range::union);
let __yeast_node: yeast::Id = #node;
#ctx.set_node_source_range(__yeast_node, __yeast_source_range)
}
})
}

pub fn parse_rule_top(input: TokenStream) -> Result<TokenStream> {
Ok(crate::rule_parse::parse_rule(input)?.lower())
}
Expand Down Expand Up @@ -127,20 +82,6 @@ pub fn parse_rules_top(input: TokenStream) -> Result<TokenStream> {
})
}

struct LocatedTreeInput {
source: Expr,
template: TokenStream,
}

impl Parse for LocatedTreeInput {
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
let source = input.parse()?;
input.parse::<Token![,]>()?;
let template = input.parse()?;
Ok(Self { source, template })
}
}

struct NamedString {
value: String,
}
Expand Down
Loading
Loading