Skip to content
Open
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
39 changes: 36 additions & 3 deletions shared/yeast-macros/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ impl Node {
let kind_str = self.kind.to_string();

if let Some(content) = &self.content {
return match 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,6 +362,7 @@ impl Node {
}
}
};
return self.with_location(node, &ctx);
}

let mut stmts = Vec::new();
Expand All @@ -372,7 +373,10 @@ impl Node {
name,
value,
optional,
} = field;
} = field
else {
continue;
};
let field_name = name.to_string();
let field_str = field_name
.strip_prefix("r#")
Expand Down Expand Up @@ -424,13 +428,42 @@ impl Node {
}
}

quote! {
let node = quote! {
{
#(#stmts)*
let mut __fields: Vec<(&str, Vec<yeast::Id>)> = Vec::new();
#(#field_args)*
#ctx.node(#kind_str, __fields)
}
};
self.with_location(node, &ctx)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means all location: fields are evaluated after normal fields right? I guess it shouldn't matter in practice, but it would be less surprising if evaluated left to right.

}

fn with_location(&self, node: TokenStream, ctx: &Ident) -> TokenStream {
let locations = self.fields.iter().filter_map(|field| match field {
Field::Named { .. } => None,
Field::Location { block } => Some(rust_block(block)),
});
let locations: Vec<_> = locations.collect();
if locations.is_empty() {
return node;
}

quote! {
{
let mut __location_ids: Vec<yeast::Id> = Vec::new();
#(yeast::IntoFieldIds::extend_into(#locations, &mut __location_ids);)*
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);
let __node: yeast::Id = #node;
#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
79 changes: 75 additions & 4 deletions shared/yeast-macros/src/template_parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,41 @@ fn parse_node(group: proc_macro2::Group) -> Result<Node> {
let mut fields = Vec::new();
while peek_is_field(&mut tokens) {
let name = expect_ident(&mut tokens, "expected field name")?;
if content.is_some() {
let is_location = name == "location";
if content.is_some() && !is_location {
return Err(syn::Error::new_spanned(
name,
"literal nodes cannot have named fields",
"literal nodes can only have the special `location` field",
));
}
expect_punct(&mut tokens, ':', "expected `:` after field name")?;
if is_location {
if !peek_is_group(&mut tokens, Delimiter::Brace) {
let error = match tokens.peek() {
Some(token) => syn::Error::new_spanned(
token.clone(),
"`location` field value must be a Rust block",
),
None => syn::Error::new(
Span::call_site(),
"`location` field value must be a Rust block",
),
};
return Err(error);
}
let block = parse_block(expect_group(&mut tokens, Delimiter::Brace)?)?;
if let Some(TokenTree::Punct(punctuation)) = tokens.peek() {
if punctuation.as_char() == '?' {
return Err(syn::Error::new_spanned(
punctuation.clone(),
"`?` is not valid on a `location` field",
));
}
}
fields.push(Field::Location { block });
continue;
}

let value = parse_one(&mut tokens)?;
let optional = if matches!(value, Template::Splice(_)) {
reject_stray_optional(&mut tokens, true)?;
Expand Down Expand Up @@ -240,6 +268,46 @@ mod tests {
assert_eq!(node.fields.len(), 2);
}

#[test]
fn parses_location_fields_on_literal_nodes() {
let template = parse_template(quote! {
(identifier "name" location: {source})
})
.unwrap();

let Template::Node(node) = template else {
panic!("expected node template");
};
assert_eq!(node.kind, "identifier");
assert!(node.content.is_some());
assert!(matches!(node.fields.as_slice(), [Field::Location { .. }]));
}

#[test]
fn rejects_non_block_location_fields() {
let result = parse_template(quote! {
(identifier "name" location: (identifier "source"))
});
let Err(error) = result else {
panic!("expected a non-block location value to be rejected");
};
assert_eq!(
error.to_string(),
"`location` field value must be a Rust block"
);
}

#[test]
fn rejects_optional_location_fields() {
let result = parse_template(quote! {
(identifier "name" location: {source}?)
});
let Err(error) = result else {
panic!("expected an optional location field to be rejected");
};
assert_eq!(error.to_string(), "`?` is not valid on a `location` field");
}

#[test]
fn parses_lists_and_skips_empty_nodes() {
let templates = parse_templates(quote! {
Expand All @@ -254,13 +322,16 @@ mod tests {
}

#[test]
fn rejects_fields_on_literal_nodes() {
fn rejects_named_fields_on_literal_nodes() {
let result = parse_template(quote! {
(identifier "name" child: (identifier "nested"))
});
let Err(error) = result else {
panic!("expected literal node fields to be rejected");
};
assert_eq!(error.to_string(), "literal nodes cannot have named fields");
assert_eq!(
error.to_string(),
"literal nodes can only have the special `location` field"
);
}
}
43 changes: 16 additions & 27 deletions shared/yeast/doc/yeast.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,46 +271,35 @@ same broad range to every synthetic descendant. A transform that simply
returns a translated capture does not widen that capture to the wrapper's
range.

The following macros can be used to explicitly set the location associated
with a newly-created node. They assign a location only to the root of their
template; nested nodes still derive their locations normally.

`tree_at!` assigns the range of one captured input node to the template root:
The special `location` field overrides the location of the node on which it
appears. Its value must be a Rust block and uses the same node-splicing rules
as an ordinary field block, but is metadata and is not added to the output AST:

```rust
rule!(
(wrapper
source: (_) @source_node
first: (_) @first
second: (_) @second
child: (_) @child)
=>
synthetic_node {
tree_at!(
source_node,
(synthetic_node child: (nested value: {child}))
)
}
(synthetic_node
child: {child}
location: {[first, second]})
)
```

`tree_spanning!` assigns the smallest range containing several captured input
nodes:
If a `location` value yields several nodes, their locations are combined into
the smallest range containing all of them. Multiple `location` fields on the
same node are combined in the same way. The field can also be used on literal
leaf nodes:

```rust
rule!(
(wrapper
first: (_) @first
second: (_) @second
child: (_) @child)
=>
synthetic_node {
tree_spanning!(
[first, second],
(synthetic_node child: {child})
)
}
)
(identifier "synthetic" location: {source_node})
```

The override applies only to the node carrying the `location` field; nested
nodes still derive their locations normally.

For input fields whose leading or trailing syntax should never belong to rule
results, configure them once with
`DesugaringConfig::with_ignored_location_fields(...)`. For example, ignoring
Expand Down
Loading
Loading