summaryrefslogtreecommitdiffstats
path: root/crates/rebel-lang/examples/repl.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/rebel-lang/examples/repl.rs')
-rw-r--r--crates/rebel-lang/examples/repl.rs35
1 files changed, 31 insertions, 4 deletions
diff --git a/crates/rebel-lang/examples/repl.rs b/crates/rebel-lang/examples/repl.rs
index 07c906f..e4c2550 100644
--- a/crates/rebel-lang/examples/repl.rs
+++ b/crates/rebel-lang/examples/repl.rs
@@ -5,6 +5,7 @@ use rebel_lang::{
value::{EvalError, Result, Value},
};
use rebel_parse::{recipe, tokenize};
+use rustyline::{error::ReadlineError, validate::ValidationResult};
fn intrinsic_array_len(params: &[Value]) -> Result<Value> {
assert!(params.len() == 1);
@@ -23,6 +24,24 @@ fn intrinsic_string_len(params: &[Value]) -> Result<Value> {
))
}
+#[derive(rustyline::Completer, rustyline::Hinter, rustyline::Highlighter, rustyline::Helper)]
+struct Helper;
+
+impl rustyline::validate::Validator for Helper {
+ fn validate(
+ &self,
+ ctx: &mut rustyline::validate::ValidationContext,
+ ) -> rustyline::Result<rustyline::validate::ValidationResult> {
+ let input = ctx.input();
+ let result = match tokenize::token_stream(input) {
+ Ok(_) => ValidationResult::Valid(None),
+ Err(_) => ValidationResult::Incomplete,
+ };
+
+ Ok(result)
+ }
+}
+
fn main() -> rustyline::Result<()> {
let mut ctx = Context::default();
@@ -47,12 +66,20 @@ fn main() -> rustyline::Result<()> {
},
);
- let mut rl = rustyline::DefaultEditor::new()?;
+ let mut rl = rustyline::Editor::<Helper, rustyline::history::DefaultHistory>::new()?;
+ rl.set_helper(Some(Helper));
- while let Ok(line) = rl.readline("> ") {
- rl.add_history_entry(line.as_str())?;
+ loop {
+ let input = match rl.readline("> ") {
+ Ok(input) => input,
+ Err(ReadlineError::Interrupted) => continue,
+ Err(ReadlineError::WindowResized) => continue,
+ Err(ReadlineError::Eof) => break,
+ Err(err) => panic!("{err}"),
+ };
+ rl.add_history_entry(input.as_str())?;
- let tokens = match tokenize::token_stream(&line) {
+ let tokens = match tokenize::token_stream(&input) {
Ok(value) => value,
Err(err) => {
println!("Tokenize error: {err}");