From 1131c5d8a1ba573701b101ef0939b9505e1e2943 Mon Sep 17 00:00:00 2001 From: Qrius Date: Thu, 26 Sep 2024 00:11:05 +0200 Subject: Add some sensible parsing of nested parantheses --- src/macro_processor/macro_processor.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/macro_processor/macro_processor.rs b/src/macro_processor/macro_processor.rs index cec742e..5556388 100644 --- a/src/macro_processor/macro_processor.rs +++ b/src/macro_processor/macro_processor.rs @@ -207,7 +207,7 @@ fn smp_builtin_indent( let mut out = String::with_capacity(args[1].len()); for l in args[1].lines() { let mut lin = String::with_capacity(indent_size.try_into().unwrap_or(0) + l.len()); - if args.len() <= 2 && (args[2] != "skip_first") { + if args.len() <= 2 || (args[2] != "skip_first") { for _ in 0..indent_size { lin.push(' '); } @@ -373,6 +373,8 @@ impl MacroProcessor { let mut in_quote_single = false; let mut in_quote_double = false; + let mut parens_level = 0; + for (i, c) in input.char_indices() { highlight_debug!(input, macro_name_start, i); @@ -401,6 +403,7 @@ impl MacroProcessor { if c.is_alphanumeric() || c == '_' { macro_name.push(c); } else if c == '(' { + parens_level += 1; state = ParserState::InMacroArgs; } else { if self.macros.contains_key(¯o_name) { @@ -424,20 +427,27 @@ impl MacroProcessor { } } ParserState::InMacroArgs => { - if c == ')' { + if (c == ')') && (parens_level == 1) { highlight_debug!("\x1b[32m\x1b[7m", input, (macro_name_start -> i)); - + parens_level = 0; macro_args.push(argument.trim().to_string()); + println!("{:?}", macro_args); let expanded = self.expand_macro(¯o_name, &mut macro_args)?; output.push_str(&expanded); state = ParserState::Normal; macro_name.clear(); macro_args.clear(); argument.clear(); - } else if c == ',' { + } else if (c == ',') && (parens_level == 1) { macro_args.push(argument.trim().to_string()); argument.clear(); } else { + if c == '(' { + parens_level += 1; + } + if c == ')' { + parens_level -= 1; + } argument.push(c); } } -- cgit v1.2.3