Tholp's bespoke website generator
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Require for_each_arg replacement marker have a prefix defined by the first arg

Tholp1 4a70532e 05434635

+33 -15
+33 -15
src/macros/simple_blocks.rs
··· 1 1 // This file for implementations of short blocks, im qualifying that as less than 30ish lines 2 2 3 - use std::{env::args, fmt::format, process::exit}; 4 - 5 3 use crate::{ 6 - console::error_skid, 7 - projectparse::{FileIndexing, ProjectContext}, 8 - stringtools::{find_pattern, split_to_tokens, TokenTools, WhitespaceChecks}, 4 + console::{error_skid, warn_skid}, 5 + projectparse::ProjectContext, 6 + stringtools::{find_pattern, split_to_tokens, TokenTools}, 9 7 types::{InputFile, Token}, 10 8 }; 11 9 ··· 67 65 ) -> Vec<Token> { 68 66 let mut output = Vec::new(); 69 67 let block: Vec<Token> = scope.into(); 68 + let varname = &args[0]; 69 + let real_args = &args[1..]; 70 70 71 71 let mut replacement_count: usize = 0; 72 72 73 - let mut replacement_pattern = find_pattern(scope, "[[..1]]".into()); 73 + let mut replacement_pattern = find_pattern(scope, format!("[[{}..1]]", varname)); 74 + 75 + if replacement_pattern.is_none() { 76 + warn_skid( 77 + context, 78 + origin_index, 79 + origin_line, 80 + format!( 81 + "Macro `for_each_arg` given block with no \"[[{}..1]]\", intentional?", 82 + varname 83 + ), 84 + ); 85 + } 86 + 74 87 while replacement_pattern.is_some() { 75 88 replacement_count += 1; 76 89 replacement_pattern = 77 - find_pattern(scope, format!("[[..{}]]", replacement_count + 1).into()); 90 + find_pattern(scope, format!("[[{}..{}]]", varname, replacement_count + 1)); 78 91 } 79 92 80 93 if replacement_count == 0 { 81 - for _i in 0..args.iter().count() { 94 + for _i in 0..real_args.iter().count() { 82 95 output.append(&mut block.clone()); 83 96 } 84 97 return output; 85 98 } 86 99 87 - if args.len() % replacement_count != 0 { 100 + if real_args.len() % replacement_count != 0 { 88 101 error_skid(context, origin_index, origin_line, 89 102 format!("`for_each_var` was not given a number of arguments({}) that was a multiple of its replacement posistions({}) (got {:?})", 90 - args.len(), 103 + real_args.len(), 91 104 replacement_count, 92 - args)); 105 + real_args)); 93 106 } 94 107 95 108 let mut replacement_index: usize = 0; 96 109 let mut arg_output: Vec<Token> = block.clone(); 97 - for arg in args { 98 - let mut found_pattern = 99 - find_pattern(&arg_output, format!("[[..{}]]", replacement_index + 1)); 110 + for arg in real_args { 111 + let mut found_pattern = find_pattern( 112 + &arg_output, 113 + format!("[[{}..{}]]", varname, replacement_index + 1), 114 + ); 100 115 101 116 while found_pattern.is_some() { 102 117 let (start, len) = found_pattern.unwrap(); 103 118 let replacement = split_to_tokens(arg.clone(), origin_index); 104 119 arg_output.splice(start..start + len, replacement); 105 - found_pattern = find_pattern(&arg_output, format!("[[..{}]]", replacement_index + 1)); 120 + found_pattern = find_pattern( 121 + &arg_output, 122 + format!("[[{}..{}]]", varname, replacement_index + 1), 123 + ); 106 124 //println!("{}", replacement_index + 1); 107 125 } 108 126