aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaximilian Bosch <[email protected]>2022-01-04 16:46:33 +0000
committerMaximilian Bosch <[email protected]>2022-01-04 16:47:02 +0000
commit3f0f8d9f0f910b4c9477297049d4a508eacc3734 (patch)
tree597a3ac1bad860fa62737faa58aeb71184c249be
parent7e673fc429df46f1792f5586965245b59d92990e (diff)
readline: explain constant highlighting regex
-rw-r--r--src/readline.rs16
1 files changed, 16 insertions, 0 deletions
diff --git a/src/readline.rs b/src/readline.rs
index 6734674..8906a4f 100644
--- a/src/readline.rs
+++ b/src/readline.rs
@@ -59,6 +59,22 @@ impl Highlighter for LineHighlighter {
59 let mut coloured: String = ops.replace_all(line, "\x1b[35m$o\x1b[0m").into(); 59 let mut coloured: String = ops.replace_all(line, "\x1b[35m$o\x1b[0m").into();
60 60
61 for c in constants { 61 for c in constants {
62 // This regex consists of the following pieces:
63 // * the constant (`o`) to highlight (to be substituted as `{}` via `format!`),
64 // e.g. `e` or `pi`.
65 // * (optionally) an ANSI escape-code (`\x1b\[35m`) that is used to highlight
66 // a binary operator (e.g. `+`/`-`/...). With this one it is ensured that
67 // binary operators are always correctly detected after a constant
68 // (see the next bullet-point for why that's needed).
69 // * the following operator (e.g. `+`/`-`/...), a space or the end
70 // of the expression (to highlight e.g. `1+e` correctly). This is
71 // required to distinguish a constant in an expression from a function-call,
72 // e.g. `e+1` from `exp(1)`, without this matching logic, the `e` from
73 // `exp` would be improperly interpreted as constant.
74 //
75 // To make sure none of existing highlighting (i.e. highlighting
76 // of binary operators that happens before) breaks, the escape-codes & operator
77 // (called `r`) are appended after the highlighted constant.
62 let re = Regex::new(format!("(?P<o>{})(?P<r>(\x1b\\[35m)?([\\+-/\\*%\\^! ]|$))", c).as_str()).unwrap(); 78 let re = Regex::new(format!("(?P<o>{})(?P<r>(\x1b\\[35m)?([\\+-/\\*%\\^! ]|$))", c).as_str()).unwrap();
63 coloured = re.replace_all(&coloured, "\x1b[33m$o\x1b[0m$r").into(); 79 coloured = re.replace_all(&coloured, "\x1b[33m$o\x1b[0m$r").into();
64 } 80 }