From a81bdc960dd2d9007089e8ca0ff878a3d7c34d9e Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Fri, 17 Oct 2025 21:20:05 +1300 Subject: Restrict variable names to lower-case characters and hyphens This is so that the characters < and > can be used in patterns without over-zealously interpreting rules as variable names. --- src/highlighter.rs | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/highlighter.rs b/src/highlighter.rs index 40e618e..fe40160 100644 --- a/src/highlighter.rs +++ b/src/highlighter.rs @@ -105,22 +105,28 @@ impl Highlighter { fn replace_variables(pattern: &str, variables: &HashMap) -> String { let mut output = String::new(); - let mut chars = pattern.chars(); - while let Some(c) = chars.next() { - if c == '<' { - let mut name = String::from('<'); - loop { - match chars.next() { - Some('>') => { name.push('>'); break; } - Some(c) => name.push(c), - None => panic!("Missing '>' character"), + let chars: Vec = pattern.chars().collect(); + let mut i = 0; + 'outer: while let Some(c) = chars.get(i) { + if *c == '<' { + let mut name = String::from(*c); + let mut j = i+1; + while let Some(d) = chars.get(j) { + name.push(*d); + if *d == '>' { + let pattern = variables.get(&name).expect(&format!("Missing definition for {name:?}")); + output.push_str(&format!("(?:{pattern})")); + i = j+1; + continue 'outer; } + if !d.is_lowercase() && *d != '-' { + break; + } + j += 1; } - let pattern = variables.get(&name).expect(&format!("Missing definition for {name:?}")); - output.push_str(&format!("(?:{pattern})")); - } else { - output.push(c); } + output.push(*c); + i += 1; } return output; } -- cgit v1.2.3-70-g09d2