2020-06-04 18:55:08 +02:00
|
|
|
use crate::output;
|
2020-06-07 18:56:28 +02:00
|
|
|
use ansi_term::Colour::{self, Cyan};
|
2020-06-04 19:43:43 +02:00
|
|
|
use kalk::parser;
|
2020-06-07 18:56:28 +02:00
|
|
|
use regex::Captures;
|
|
|
|
use regex::Regex;
|
|
|
|
use rustyline::completion::Completer;
|
2020-06-04 18:55:08 +02:00
|
|
|
use rustyline::error::ReadlineError;
|
2020-06-07 18:56:28 +02:00
|
|
|
use rustyline::highlight::Highlighter;
|
|
|
|
use rustyline::hint::Hinter;
|
|
|
|
use rustyline::validate::MatchingBracketValidator;
|
|
|
|
use rustyline::validate::ValidationContext;
|
|
|
|
use rustyline::validate::ValidationResult;
|
|
|
|
use rustyline::validate::Validator;
|
|
|
|
use rustyline::{Editor, Helper};
|
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::borrow::Cow::Owned;
|
2020-06-04 18:55:08 +02:00
|
|
|
use std::process;
|
|
|
|
|
|
|
|
pub fn start(mut parser: &mut parser::Context) {
|
2020-06-07 18:56:28 +02:00
|
|
|
let mut editor = Editor::<RLHelper>::new();
|
|
|
|
editor.set_helper(Some(RLHelper {
|
|
|
|
highlighter: LineHighlighter {},
|
|
|
|
validator: MatchingBracketValidator::new(),
|
|
|
|
}));
|
2020-06-04 18:55:08 +02:00
|
|
|
|
|
|
|
loop {
|
2020-06-04 19:43:43 +02:00
|
|
|
let readline = editor.readline(&Cyan.paint(">> ").to_string());
|
2020-06-04 18:55:08 +02:00
|
|
|
|
|
|
|
match readline {
|
|
|
|
Ok(input) => {
|
2020-06-04 19:43:43 +02:00
|
|
|
editor.add_history_entry(input.as_str());
|
2020-06-04 18:55:08 +02:00
|
|
|
eval_repl(&mut parser, &input);
|
|
|
|
}
|
|
|
|
Err(ReadlineError::Interrupted) => break,
|
|
|
|
_ => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn eval_repl(parser: &mut parser::Context, input: &str) {
|
|
|
|
match input {
|
|
|
|
"" => eprint!(""),
|
|
|
|
"clear" => print!("\x1B[2J"),
|
|
|
|
"exit" => process::exit(0),
|
|
|
|
_ => output::eval(parser, input),
|
|
|
|
}
|
|
|
|
}
|
2020-06-07 18:56:28 +02:00
|
|
|
|
|
|
|
struct LineHighlighter {}
|
|
|
|
|
|
|
|
impl Highlighter for LineHighlighter {
|
|
|
|
fn highlight<'l>(&self, line: &'l str, _: usize) -> Cow<'l, str> {
|
|
|
|
let mut coloured = line.to_string();
|
|
|
|
|
2020-06-09 15:43:53 +02:00
|
|
|
let reg = Regex::new(
|
|
|
|
r"(?x)
|
|
|
|
(?P<identifier>[^!-@\s_|^⌊⌋⌈⌉]+(_\d+)?) |
|
|
|
|
(?P<op>[+\-/*^!])",
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-06-07 18:56:28 +02:00
|
|
|
|
|
|
|
coloured = reg
|
|
|
|
.replace_all(&coloured, |caps: &Captures| {
|
2020-06-09 15:43:53 +02:00
|
|
|
if let Some(cap) = caps.name("identifier") {
|
|
|
|
match cap.as_str() {
|
|
|
|
"rad" | "deg" | "°" => Colour::Yellow.paint(cap.as_str()).to_string(),
|
|
|
|
_ => Colour::Blue.paint(cap.as_str()).to_string(),
|
|
|
|
}
|
|
|
|
} else if let Some(cap) = caps.name("op") {
|
|
|
|
Colour::Fixed(172).paint(cap.as_str()).to_string()
|
2020-06-07 18:56:28 +02:00
|
|
|
} else {
|
2020-06-09 15:43:53 +02:00
|
|
|
caps[0].to_string()
|
2020-06-07 18:56:28 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.to_string();
|
|
|
|
|
|
|
|
Owned(coloured)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct RLHelper {
|
|
|
|
highlighter: LineHighlighter,
|
|
|
|
validator: MatchingBracketValidator,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Helper for RLHelper {}
|
|
|
|
|
|
|
|
const COMPLETION_FUNCS: phf::Map<&'static str, &'static str> = phf::phf_map! {
|
2020-06-09 15:53:29 +02:00
|
|
|
"ceil" => "⌈⌉",
|
2020-06-07 18:56:28 +02:00
|
|
|
"deg" => "°",
|
2020-06-08 21:58:55 +02:00
|
|
|
"floor" => "⌊⌋",
|
2020-06-09 15:53:29 +02:00
|
|
|
"gamma" => "Γ",
|
2020-06-09 10:37:57 +02:00
|
|
|
"sum" => "Σ()",
|
2020-06-12 00:49:50 +02:00
|
|
|
"phi" => "ϕ",
|
|
|
|
"pi" => "π",
|
2020-06-09 15:53:29 +02:00
|
|
|
"sqrt" => "√",
|
2020-06-12 00:49:50 +02:00
|
|
|
"tau" => "τ",
|
2020-06-09 15:53:29 +02:00
|
|
|
"(" => "()",
|
2020-06-07 18:56:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
impl Completer for RLHelper {
|
|
|
|
type Candidate = String;
|
|
|
|
fn complete(
|
|
|
|
&self,
|
|
|
|
line: &str,
|
|
|
|
pos: usize,
|
|
|
|
_ctx: &rustyline::Context<'_>,
|
|
|
|
) -> Result<(usize, Vec<Self::Candidate>), ReadlineError> {
|
|
|
|
for key in COMPLETION_FUNCS.keys() {
|
|
|
|
if line[..pos].ends_with(key) {
|
2020-06-08 21:58:55 +02:00
|
|
|
let value = *COMPLETION_FUNCS.get(key).unwrap();
|
|
|
|
return Ok((pos - key.len(), vec![value.to_string()]));
|
2020-06-07 18:56:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok((0, vec![line.to_string()]))
|
|
|
|
}
|
2020-06-08 21:58:55 +02:00
|
|
|
|
|
|
|
fn update(&self, line: &mut rustyline::line_buffer::LineBuffer, start: usize, elected: &str) {
|
|
|
|
line.backspace(line.pos() - start);
|
|
|
|
line.insert_str(line.pos(), elected);
|
2020-06-09 10:37:57 +02:00
|
|
|
line.move_forward(match elected {
|
|
|
|
"Σ()" => 2,
|
|
|
|
_ => 1,
|
|
|
|
});
|
2020-06-08 21:58:55 +02:00
|
|
|
}
|
2020-06-07 18:56:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Highlighter for RLHelper {
|
|
|
|
fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> {
|
2020-06-07 19:02:07 +02:00
|
|
|
Owned(Colour::Fixed(244).paint(hint).to_string())
|
2020-06-07 18:56:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn highlight<'l>(&self, line: &'l str, pos: usize) -> Cow<'l, str> {
|
|
|
|
self.highlighter.highlight(line, pos)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn highlight_candidate<'c>(
|
|
|
|
&self,
|
|
|
|
candidate: &'c str,
|
|
|
|
_completion: rustyline::CompletionType,
|
|
|
|
) -> Cow<'c, str> {
|
|
|
|
self.highlighter.highlight(candidate, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn highlight_char(&self, line: &str, _: usize) -> bool {
|
|
|
|
line.len() > 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Hinter for RLHelper {
|
2020-06-09 16:41:04 +02:00
|
|
|
fn hint(&self, _: &str, _: usize, _: &rustyline::Context) -> Option<String> {
|
|
|
|
None
|
2020-06-07 18:56:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Validator for RLHelper {
|
|
|
|
fn validate(&self, ctx: &mut ValidationContext) -> Result<ValidationResult, ReadlineError> {
|
|
|
|
self.validator.validate(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn validate_while_typing(&self) -> bool {
|
|
|
|
self.validator.validate_while_typing()
|
|
|
|
}
|
|
|
|
}
|