From 091ce01a317411b6f426551c6026b090395e711c Mon Sep 17 00:00:00 2001 From: "kriko.eth" <43150707+coreggon11@users.noreply.github.com> Date: Mon, 8 Aug 2022 19:55:31 +0200 Subject: [PATCH] fix parsing of indices --- src/parser.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 6 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index a2a3009..dbd4808 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1959,14 +1959,26 @@ impl<'a> Parser<'a> { } if let Some(expression) = enclosed_expressions.clone().unwrap_or_default().get(raw) { - return Expression::Enclosed(bx!(expression.clone())) + let regex = Regex::new(r"___0\d+___").unwrap(); + return if regex.is_match(raw) { + expression.clone() + } else { + Expression::Enclosed(bx!(expression.clone())) + } } - let extracted = self.extract_parentheses(raw, constructor); + let extracted = self.extract_parentheses(raw, constructor, false); if extracted.1 > 0 { return self.parse_expression(&extracted.0, constructor, Some(extracted.2)) } + if enclosed_expressions.is_none() { + let indices = self.extract_parentheses(raw, constructor, true); + if indices.1 > 0 { + return self.parse_expression(&indices.0, constructor, Some(indices.2)) + } + } + let regex_hex_string = Regex::new(r#"(?x)^\s*hex"(?P.+?)"\s*$"#).unwrap(); let regex_hex = Regex::new(r#"(?x)^\s*(?P0x[0-9A-Fa-f]*?)\s*$"#).unwrap(); if regex_hex_string.is_match(raw) || regex_hex.is_match(raw) { @@ -2256,6 +2268,7 @@ impl<'a> Parser<'a> { &mut self, raw: &str, constructor: bool, + square_brackets: bool, ) -> (String, i32, HashMap) { let mut buffer_out = String::new(); let mut buffer_tmp = String::new(); @@ -2268,10 +2281,16 @@ impl<'a> Parser<'a> { for ch in raw.chars() { match ch { - PARENTHESIS_CLOSE => { + PARENTHESIS_CLOSE if !square_brackets => { close_parentheses += 1; } - PARENTHESIS_OPEN => { + PARENTHESIS_OPEN if !square_brackets => { + open_parentheses += 1; + } + BRACKET_CLOSE if square_brackets => { + close_parentheses += 1; + } + BRACKET_OPEN if square_brackets => { open_parentheses += 1; } _ => {} @@ -2287,11 +2306,17 @@ impl<'a> Parser<'a> { SPACE if group_possible => { buffer_out.push(ch); } - PARENTHESIS_OPEN if group_possible => { + PARENTHESIS_OPEN if group_possible && !square_brackets => { group = true; group_possible = false; } - PARENTHESIS_CLOSE if group && open_parentheses == close_parentheses => { + BRACKET_OPEN if square_brackets && !group => { + group = true; + buffer_out.push(ch); + } + PARENTHESIS_CLOSE + if group && open_parentheses == close_parentheses && !square_brackets => + { group = false; let arg_name = format!("___{args}___"); let expression = self.parse_expression(&buffer_tmp, constructor, None); @@ -2300,6 +2325,20 @@ impl<'a> Parser<'a> { buffer_tmp.clear(); args += 1; } + BRACKET_CLOSE + if group && open_parentheses == close_parentheses && square_brackets => + { + group = false; + if !buffer_tmp.is_empty() { + let arg_name = format!("___0{args}___"); + let expression = self.parse_expression(&buffer_tmp, constructor, None); + map.insert(arg_name.clone(), expression); + buffer_out.push_str(&arg_name); + args += 1; + } + buffer_out.push(ch); + buffer_tmp.clear(); + } _ => { if group_possible { group_possible = false;