11use proc_macro2:: { Ident , Span , TokenStream } ;
22use quote:: quote;
3- use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
4- use syn:: Lifetime ;
53
64use crate :: ast:: { Cardinality , Content , Field , Node , Pattern , Replacement , Rule , Template } ;
75
8- struct LoweringContext < ' a > {
9- /// Enclosing optional-field label to exit when a computed value is absent.
10- fallible_scope : Option < & ' a Lifetime > ,
11- }
12-
13- fn fresh_fallible_label ( ) -> Lifetime {
14- static COUNTER : AtomicUsize = AtomicUsize :: new ( 0 ) ;
15- let n = COUNTER . fetch_add ( 1 , Ordering :: Relaxed ) ;
16- Lifetime :: new ( & format ! ( "'__yeast_field_{n}" ) , Span :: call_site ( ) )
17- }
18-
196#[ derive( Clone ) ]
207struct CaptureBinding {
218 name : Ident ,
@@ -286,20 +273,15 @@ fn capture_bindings<'a>(
286273
287274impl Template {
288275 pub ( crate ) fn lower_root ( & self ) -> TokenStream {
289- self . lower ( & LoweringContext {
290- fallible_scope : None ,
291- } )
276+ self . lower ( )
292277 }
293278
294279 pub ( crate ) fn lower_list ( templates : & [ Self ] ) -> TokenStream {
295- let context = LoweringContext {
296- fallible_scope : None ,
297- } ;
298280 let items = templates
299281 . iter ( )
300282 . map ( |template| match template {
301283 Template :: Node ( node) => {
302- let node = node. lower ( & context ) ;
284+ let node = node. lower ( ) ;
303285 quote ! { __nodes. push( #node) ; }
304286 }
305287 Template :: Splice ( block) => {
@@ -322,9 +304,9 @@ impl Template {
322304 }
323305 }
324306
325- fn lower ( & self , context : & LoweringContext < ' _ > ) -> TokenStream {
307+ fn lower ( & self ) -> TokenStream {
326308 match self {
327- Template :: Node ( node) => node. lower ( context ) ,
309+ Template :: Node ( node) => node. lower ( ) ,
328310 Template :: Splice ( block) => {
329311 let block = rust_block ( block) ;
330312 quote ! { :: std:: convert:: Into :: <yeast:: OutputId >:: into( #block) }
@@ -334,7 +316,7 @@ impl Template {
334316}
335317
336318impl Node {
337- fn lower ( & self , context : & LoweringContext < ' _ > ) -> TokenStream {
319+ fn lower ( & self ) -> TokenStream {
338320 let ctx = Ident :: new ( "ctx" , Span :: call_site ( ) ) ;
339321 let kind_str = self . kind . to_string ( ) ;
340322
@@ -343,40 +325,18 @@ impl Node {
343325 Content :: Static ( lit) => quote ! { #ctx. literal( #kind_str, #lit) } ,
344326 Content :: Computed ( block) => {
345327 let block = rust_block ( block) ;
346- if let Some ( label) = context. fallible_scope {
347- quote ! {
348- {
349- let __expr = #block;
350- let :: std:: option:: Option :: Some ( __value_ref) =
351- yeast:: MaybeYeastValue :: maybe_yeast_value( & __expr)
352- else {
353- break #label :: std:: option:: Option :: None ;
354- } ;
355- let __value =
356- yeast:: YeastDisplay :: yeast_to_string( __value_ref, & #ctx) ;
357- let __source_range =
358- yeast:: YeastSourceRange :: yeast_source_range( __value_ref, & #ctx) ;
359- #ctx. literal_with_source_range(
360- #kind_str,
361- & __value,
362- __source_range,
363- )
364- }
365- }
366- } else {
367- quote ! {
368- {
369- let __expr = #block;
370- let __value =
371- yeast:: YeastDisplay :: yeast_to_string( & __expr, & #ctx) ;
372- let __source_range =
373- yeast:: YeastSourceRange :: yeast_source_range( & __expr, & #ctx) ;
374- #ctx. literal_with_source_range(
375- #kind_str,
376- & __value,
377- __source_range,
378- )
379- }
328+ quote ! {
329+ {
330+ let __expr = #block;
331+ let __value =
332+ yeast:: YeastDisplay :: yeast_to_string( & __expr, & #ctx) ;
333+ let __source_range =
334+ yeast:: YeastSourceRange :: yeast_source_range( & __expr, & #ctx) ;
335+ #ctx. literal_with_source_range(
336+ #kind_str,
337+ & __value,
338+ __source_range,
339+ )
380340 }
381341 }
382342 }
@@ -399,11 +359,7 @@ impl Node {
399359 #ctx. extend_location_ranges( __value, & mut __location_ranges) ;
400360 } ) ;
401361 }
402- Field :: Named {
403- name,
404- value,
405- optional,
406- } => {
362+ Field :: Named { name, value } => {
407363 let field_name = name. to_string ( ) ;
408364 let field_str = field_name
409365 . strip_prefix ( "r#" )
@@ -413,10 +369,8 @@ impl Node {
413369 & format ! ( "__field_{field_str}_{field_counter}" ) ,
414370 Span :: call_site ( ) ,
415371 ) ;
416-
417372 match value {
418373 Template :: Splice ( block) => {
419- debug_assert ! ( !optional) ;
420374 let block = rust_block ( block) ;
421375 stmts. push ( quote ! {
422376 let mut #temp: Vec <yeast:: OutputId > = Vec :: new( ) ;
@@ -430,25 +384,8 @@ impl Node {
430384 }
431385 } ) ;
432386 }
433- Template :: Node ( child) if * optional => {
434- let label = fresh_fallible_label ( ) ;
435- let value = child. lower ( & LoweringContext {
436- fallible_scope : Some ( & label) ,
437- } ) ;
438- stmts. push ( quote ! {
439- #[ allow( unused_labels) ]
440- let #temp: :: std:: option:: Option <yeast:: OutputId > = #label: {
441- :: std:: option:: Option :: Some ( #value)
442- } ;
443- } ) ;
444- field_args. push ( quote ! {
445- if let :: std:: option:: Option :: Some ( __id) = #temp {
446- __fields. push( ( #field_str, vec![ __id] ) ) ;
447- }
448- } ) ;
449- }
450387 Template :: Node ( child) => {
451- let value = child. lower ( context ) ;
388+ let value = child. lower ( ) ;
452389 stmts. push ( quote ! { let #temp: yeast:: OutputId = #value; } ) ;
453390 field_args. push ( quote ! {
454391 __fields. push( ( #field_str, vec![ #temp] ) ) ;
0 commit comments