@@ -232,6 +232,21 @@ fn expand_subtree<S: Span>(
232232 . into ( ) ,
233233 ) ;
234234 }
235+ Op :: Length { depth } => {
236+ let length = ctx. nesting . get ( ctx. nesting . len ( ) - 1 - depth) . map_or ( 0 , |_nest| {
237+ // FIXME: to be implemented
238+ 0
239+ } ) ;
240+ arena. push (
241+ tt:: Leaf :: Literal ( tt:: Literal {
242+ text : length. to_string ( ) . into ( ) ,
243+ // FIXME
244+ #[ allow( deprecated) ]
245+ span : S :: DUMMY ,
246+ } )
247+ . into ( ) ,
248+ ) ;
249+ }
235250 Op :: Count { name, depth } => {
236251 let mut binding = match ctx. bindings . get ( name. as_str ( ) ) {
237252 Ok ( b) => b,
@@ -518,28 +533,18 @@ fn fix_up_and_push_path_tt<S: Span>(buf: &mut Vec<tt::TokenTree<S>>, subtree: tt
518533fn count < S > (
519534 ctx : & ExpandCtx < ' _ , S > ,
520535 binding : & Binding < S > ,
521- our_depth : usize ,
522- count_depth : Option < usize > ,
536+ depth_curr : usize ,
537+ depth_max : usize ,
523538) -> Result < usize , CountError > {
524539 match binding {
525- Binding :: Nested ( bs) => match count_depth {
526- None => bs. iter ( ) . map ( |b| count ( ctx, b, our_depth + 1 , None ) ) . sum ( ) ,
527- Some ( 0 ) => Ok ( bs. len ( ) ) ,
528- Some ( d) => bs. iter ( ) . map ( |b| count ( ctx, b, our_depth + 1 , Some ( d - 1 ) ) ) . sum ( ) ,
529- } ,
530- Binding :: Empty => Ok ( 0 ) ,
531- Binding :: Fragment ( _) | Binding :: Missing ( _) => {
532- if our_depth == 0 {
533- // `${count(t)}` is placed inside the innermost repetition. This includes cases
534- // where `t` is not a repeated fragment.
535- Err ( CountError :: Misplaced )
536- } else if count_depth. is_none ( ) {
537- Ok ( 1 )
540+ Binding :: Nested ( bs) => {
541+ if depth_curr == depth_max {
542+ Ok ( bs. len ( ) )
538543 } else {
539- // We've reached at the innermost repeated fragment, but the user wants us to go
540- // further!
541- Err ( CountError :: OutOfBounds )
544+ bs. iter ( ) . map ( |b| count ( ctx, b, depth_curr + 1 , depth_max) ) . sum ( )
542545 }
543546 }
547+ Binding :: Empty => Ok ( 0 ) ,
548+ Binding :: Fragment ( _) | Binding :: Missing ( _) => Ok ( 1 ) ,
544549 }
545550}
0 commit comments