diff options
-rw-r--r-- | src/readline.rs | 16 |
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 | } |