Skip to content

Commit 70fb3a6

Browse files
committed
yeast: Remove now-redundant tree_at! and tree_spanning! macros
1 parent 2ed43ae commit 70fb3a6

5 files changed

Lines changed: 14 additions & 141 deletions

File tree

‎shared/yeast-macros/src/lib.rs‎

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -127,38 +127,6 @@ pub fn trees(input: TokenStream) -> TokenStream {
127127
}
128128
}
129129

130-
/// Build one AST node whose root uses another node's source range.
131-
///
132-
/// Uses the `BuildCtx` binding named `ctx` from the surrounding scope:
133-
///
134-
/// ```text
135-
/// tree_at!(source, (kind ...))
136-
/// ```
137-
#[proc_macro]
138-
pub fn tree_at(input: TokenStream) -> TokenStream {
139-
let input2: TokenStream2 = input.into();
140-
match parse::parse_tree_at_top(input2) {
141-
Ok(output) => output.into(),
142-
Err(err) => err.to_compile_error().into(),
143-
}
144-
}
145-
146-
/// Build one AST node whose root spans a collection of nodes.
147-
///
148-
/// Uses the `BuildCtx` binding named `ctx` from the surrounding scope:
149-
///
150-
/// ```text
151-
/// tree_spanning!(sources, (kind ...))
152-
/// ```
153-
#[proc_macro]
154-
pub fn tree_spanning(input: TokenStream) -> TokenStream {
155-
let input2: TokenStream2 = input.into();
156-
match parse::parse_tree_spanning_top(input2) {
157-
Ok(output) => output.into(),
158-
Err(err) => err.to_compile_error().into(),
159-
}
160-
}
161-
162130
/// Define a desugaring rule with query and transform in one declaration.
163131
///
164132
/// ```text

‎shared/yeast-macros/src/parse.rs‎

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
use proc_macro2::{Span, TokenStream, TokenTree};
22
use quote::quote;
3-
use syn::{
4-
parse::{Parse, ParseStream},
5-
Expr, Token,
6-
};
73

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

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

31-
pub fn parse_tree_at_top(input: TokenStream) -> Result<TokenStream> {
32-
let LocatedTreeInput { source, template } = syn::parse2(input)?;
33-
let template = crate::template_parse::parse_template(template)?;
34-
let node = template.lower_root();
35-
let ctx = syn::Ident::new("ctx", Span::call_site());
36-
37-
Ok(quote! {
38-
{
39-
let __yeast_source: yeast::Id = { #source };
40-
let __yeast_source_range = #ctx
41-
.ast
42-
.get_node(__yeast_source)
43-
.and_then(|node| node.source_range());
44-
let __yeast_node: yeast::Id = #node;
45-
#ctx.set_node_source_range(__yeast_node, __yeast_source_range)
46-
}
47-
})
48-
}
49-
50-
pub fn parse_tree_spanning_top(input: TokenStream) -> Result<TokenStream> {
51-
let LocatedTreeInput {
52-
source: sources,
53-
template,
54-
} = syn::parse2(input)?;
55-
let template = crate::template_parse::parse_template(template)?;
56-
let node = template.lower_root();
57-
let ctx = syn::Ident::new("ctx", Span::call_site());
58-
59-
Ok(quote! {
60-
{
61-
let __yeast_source_range = ::std::iter::IntoIterator::into_iter({ #sources })
62-
.filter_map(|source: yeast::Id| {
63-
#ctx.ast.get_node(source).and_then(|node| node.source_range())
64-
})
65-
.reduce(yeast::Range::union);
66-
let __yeast_node: yeast::Id = #node;
67-
#ctx.set_node_source_range(__yeast_node, __yeast_source_range)
68-
}
69-
})
70-
}
71-
7227
pub fn parse_rule_top(input: TokenStream) -> Result<TokenStream> {
7328
Ok(crate::rule_parse::parse_rule(input)?.lower())
7429
}
@@ -127,20 +82,6 @@ pub fn parse_rules_top(input: TokenStream) -> Result<TokenStream> {
12782
})
12883
}
12984

130-
struct LocatedTreeInput {
131-
source: Expr,
132-
template: TokenStream,
133-
}
134-
135-
impl Parse for LocatedTreeInput {
136-
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
137-
let source = input.parse()?;
138-
input.parse::<Token![,]>()?;
139-
let template = input.parse()?;
140-
Ok(Self { source, template })
141-
}
142-
}
143-
14485
struct NamedString {
14586
value: String,
14687
}

‎shared/yeast/doc/yeast.md‎

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -297,45 +297,8 @@ leaf nodes:
297297
(identifier "synthetic" location: {source_node})
298298
```
299299

300-
The following macros can be used to explicitly set the location associated
301-
with a newly-created node. They assign a location only to the root of their
302-
template; nested nodes still derive their locations normally.
303-
304-
`tree_at!` assigns the range of one captured input node to the template root:
305-
306-
```rust
307-
rule!(
308-
(wrapper
309-
source: (_) @source_node
310-
child: (_) @child)
311-
=>
312-
synthetic_node {
313-
tree_at!(
314-
source_node,
315-
(synthetic_node child: (nested value: {child}))
316-
)
317-
}
318-
)
319-
```
320-
321-
`tree_spanning!` assigns the smallest range containing several captured input
322-
nodes:
323-
324-
```rust
325-
rule!(
326-
(wrapper
327-
first: (_) @first
328-
second: (_) @second
329-
child: (_) @child)
330-
=>
331-
synthetic_node {
332-
tree_spanning!(
333-
[first, second],
334-
(synthetic_node child: {child})
335-
)
336-
}
337-
)
338-
```
300+
The override applies only to the node carrying the `location` field; nested
301+
nodes still derive their locations normally.
339302

340303
For input fields whose leading or trailing syntax should never belong to rule
341304
results, configure them once with

‎shared/yeast/src/lib.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub mod schema;
1515
mod visitor;
1616

1717
pub use range::{Point, Range};
18-
pub use yeast_macros::{query, rule, rules, tree, tree_at, tree_spanning, trees};
18+
pub use yeast_macros::{query, rule, rules, tree, trees};
1919

2020
use captures::Captures;
2121
use query::QueryNode;

‎shared/yeast/tests/test.rs‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,17 +1841,19 @@ fn test_explicit_recursive_translation_keeps_nested_rule_location() {
18411841
assert_eq!(identifier.byte_range(), 4..7);
18421842
}
18431843

1844-
/// `tree_at!` assigns the captured node's range only to the template root.
1844+
/// A `location` field assigns the captured node's range only to its node.
18451845
#[test]
1846-
fn test_tree_at_assigns_capture_range_to_root_only() {
1846+
fn test_location_field_assigns_capture_range_to_node_only() {
18471847
let rule: Rule = rule!(
18481848
(call
18491849
method: (identifier) @name
18501850
receiver: (identifier) @recv
18511851
) @@source
18521852
=>
18531853
call {
1854-
let arguments = tree_at!(source, (argument_list argument: (integer "0")));
1854+
let arguments = tree!((argument_list
1855+
argument: (integer "0")
1856+
location: {source}));
18551857
tree!((call method: {name} receiver: {recv} arguments: {arguments}))
18561858
}
18571859
);
@@ -1874,21 +1876,20 @@ fn test_tree_at_assigns_capture_range_to_root_only() {
18741876
assert_eq!(integer.byte_range(), 0..0);
18751877
}
18761878

1877-
/// `tree_spanning!` assigns the union of the captured node ranges only to the
1878-
/// template root.
1879+
/// Multiple `location` fields assign their union only to their node.
18791880
#[test]
1880-
fn test_tree_spanning_assigns_union_to_root_only() {
1881+
fn test_multiple_location_fields_assign_union_to_node_only() {
18811882
let rule: Rule = rule!(
18821883
(call
18831884
method: (identifier) @name
18841885
receiver: (identifier) @recv
18851886
)
18861887
=>
18871888
call {
1888-
let arguments = tree_spanning!(
1889-
[recv, name],
1890-
(argument_list argument: (integer "0"))
1891-
);
1889+
let arguments = tree!((argument_list
1890+
argument: (integer "0")
1891+
location: {recv}
1892+
location: {name}));
18921893
tree!((call method: {name} receiver: {recv} arguments: {arguments}))
18931894
}
18941895
);

0 commit comments

Comments
 (0)