Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix REPL validator #110

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/interpreter/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::type_sys::{
freight_type_system::FenderTypeSystem,
};
use freight_vm::{execution_engine::ExecutionEngine, function::ArgCount};
use reedline::{DefaultValidator, Prompt, Reedline, Signal};
use reedline::{Prompt, Reedline, Signal, ValidationResult, Validator};
use std::error::Error;

pub struct FenderRepl<'a> {
Expand Down Expand Up @@ -63,14 +63,39 @@ impl Prompt for FenderPrompt {
}
}

struct FenderValidator;

impl Validator for FenderValidator {
fn validate(&self, line: &str) -> ValidationResult {
let mut iter = line.chars();
let mut balance = 0;
let mut quoted = None;
while let Some(c) = iter.next() {
match c {
'\\' => drop(iter.next()),
'(' | '[' | '{' if quoted.is_none() => balance += 1,
')' | ']' | '}' if quoted.is_none() => balance -= 1,
'"' | '\'' if quoted.is_none() => quoted = Some(c),
'"' | '\'' if quoted == Some(c) => quoted = None,
_ => (),
}
}
if balance == 0 && quoted.is_none() {
ValidationResult::Complete
} else {
ValidationResult::Incomplete
}
}
}

impl<'a> FenderRepl<'a> {
pub fn new() -> FenderRepl<'a> {
let mut engine = ExecutionEngine::new_default();
let scope = LexicalScope::new(ArgCount::Fixed(0), engine.create_return_target());
FenderRepl {
engine,
scope,
editor: Reedline::create().with_validator(Box::new(DefaultValidator)),
editor: Reedline::create().with_validator(Box::new(FenderValidator)),
}
}

Expand Down