From 401e877a2fc3aecd08af1d721b24afbb116b2026 Mon Sep 17 00:00:00 2001 From: Mark Date: Fri, 28 Jul 2023 15:01:43 -0700 Subject: [PATCH] Fixed substitution bug --- src/parser/stage/find_subs.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/parser/stage/find_subs.rs b/src/parser/stage/find_subs.rs index aceb668..15b5d80 100644 --- a/src/parser/stage/find_subs.rs +++ b/src/parser/stage/find_subs.rs @@ -9,12 +9,12 @@ use super::super::{ pub fn find_subs( mut g: VecDeque, ) -> ( - Vec<(LineLocation, String)>, + VecDeque<(LineLocation, String)>, VecDeque ) { // Array of replacements - let mut r: Vec<(LineLocation, String)> = Vec::with_capacity(8); + let mut r: VecDeque<(LineLocation, String)> = VecDeque::with_capacity(8); // New token array, with updated locations let mut n: VecDeque = VecDeque::with_capacity(g.len()); @@ -22,8 +22,7 @@ pub fn find_subs( let mut offset: usize = 0; while g.len() > 0 { - // Read in reverse. Very important! - let mut t = g.pop_back().unwrap(); + let mut t = g.pop_front().unwrap(); let target: Option<&str> = match &mut t { Token::Operator(_, s) => { @@ -85,11 +84,14 @@ pub fn find_subs( } else { let target = target.unwrap(); let l = t.get_mut_line_location(); - r.push((l.clone(), String::from(target))); - *l = LineLocation{ pos: l.pos - offset, len: target.chars().count()}; - offset += l.len - target.chars().count(); + r.push_front((l.clone(), String::from(target))); + + let old_len = l.len; + let new_len = target.chars().count(); + *l = LineLocation{ pos: l.pos - offset, len: new_len}; + offset += old_len - new_len; } - n.push_front(t); + n.push_back(t); } return (r, n);