2018-09-21 19:43:50 -07:00
|
|
|
use std::fmt::Write as FmtWrite;
|
|
|
|
use std::io::{Read, Write};
|
|
|
|
use std::fs::File;
|
2018-10-02 00:46:23 -07:00
|
|
|
use std::sync::Arc;
|
2018-09-21 19:43:50 -07:00
|
|
|
|
|
|
|
use colored::*;
|
|
|
|
use itertools::Itertools;
|
2019-03-12 02:39:25 -07:00
|
|
|
use crate::language::{ProgrammingLanguageInterface, EvalOptions,
|
2019-03-13 22:43:44 -07:00
|
|
|
PassDebugOptionsDescriptor, ComputationRequest, ComputationResponse};
|
2018-10-15 20:52:34 -07:00
|
|
|
mod command_tree;
|
|
|
|
use self::command_tree::CommandTree;
|
2018-09-21 19:43:50 -07:00
|
|
|
|
2018-10-01 20:53:41 -07:00
|
|
|
const HISTORY_SAVE_FILE: &'static str = ".schala_history";
|
|
|
|
const OPTIONS_SAVE_FILE: &'static str = ".schala_repl";
|
|
|
|
|
2019-03-12 00:01:30 -07:00
|
|
|
pub struct NewRepl {
|
|
|
|
interpreter_directive_sigil: char,
|
|
|
|
line_reader: ::linefeed::interface::Interface<::linefeed::terminal::DefaultTerminal>,
|
2019-03-13 10:10:42 -07:00
|
|
|
language_states: Vec<Box<ProgrammingLanguageInterface>>,
|
2019-03-12 00:01:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl NewRepl {
|
2019-03-13 20:07:41 -07:00
|
|
|
pub fn new(initial_states: Vec<Box<ProgrammingLanguageInterface>>) -> NewRepl {
|
2019-03-12 00:01:30 -07:00
|
|
|
use linefeed::Interface;
|
|
|
|
let line_reader = Interface::new("schala-repl").unwrap();
|
|
|
|
let interpreter_directive_sigil = ':';
|
|
|
|
|
|
|
|
NewRepl {
|
2019-03-13 20:07:41 -07:00
|
|
|
interpreter_directive_sigil, line_reader, language_states: initial_states,
|
2019-03-12 00:01:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run_repl(&mut self) {
|
2019-03-12 02:39:25 -07:00
|
|
|
self.line_reader.load_history(HISTORY_SAVE_FILE).unwrap_or(());
|
|
|
|
|
|
|
|
println!("Schala MetaInterpreter version {}", crate::VERSION_STRING);
|
2019-03-12 00:01:30 -07:00
|
|
|
println!("Type {}help for help with the REPL", self.interpreter_directive_sigil);
|
|
|
|
|
|
|
|
self.handle_repl_loop();
|
2019-03-12 02:39:25 -07:00
|
|
|
|
2019-03-12 00:01:30 -07:00
|
|
|
self.line_reader.save_history(HISTORY_SAVE_FILE).unwrap_or(());
|
|
|
|
self.save_options();
|
2019-03-12 02:39:25 -07:00
|
|
|
|
2019-03-12 00:01:30 -07:00
|
|
|
println!("Exiting...");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_repl_loop(&mut self) {
|
|
|
|
use linefeed::ReadResult::*;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
self.update_line_reader();
|
|
|
|
match self.line_reader.read_line() {
|
|
|
|
Err(e) => {
|
|
|
|
println!("readline IO Error: {}", e);
|
|
|
|
break;
|
|
|
|
},
|
2019-03-12 02:39:25 -07:00
|
|
|
Ok(Eof) | Ok(Signal(_)) => break,
|
2019-03-12 00:01:30 -07:00
|
|
|
Ok(Input(ref input)) => {
|
|
|
|
self.line_reader.add_history_unique(input.to_string());
|
|
|
|
let output = match input.chars().nth(0) {
|
|
|
|
Some(ch) if ch == self.interpreter_directive_sigil => self.handle_interpreter_directive(input),
|
|
|
|
_ => Some(self.handle_input(input)),
|
|
|
|
};
|
|
|
|
if let Some(o) = output {
|
|
|
|
println!("=> {}", o);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_line_reader(&mut self) {
|
|
|
|
let tab_complete_handler = TabCompleteHandler::new(self.interpreter_directive_sigil, self.get_directives());
|
|
|
|
self.line_reader.set_completer(Arc::new(tab_complete_handler));
|
|
|
|
let prompt_str = format!(" >> ");
|
|
|
|
self.line_reader.set_prompt(&prompt_str).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn save_options(&self) {
|
|
|
|
/*
|
|
|
|
let ref options = self.options;
|
|
|
|
let read = File::create(OPTIONS_SAVE_FILE)
|
|
|
|
.and_then(|mut file| {
|
|
|
|
let buf = ::serde_json::to_string(options).unwrap();
|
|
|
|
file.write_all(buf.as_bytes())
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Err(err) = read {
|
|
|
|
println!("Error saving {} file {}", OPTIONS_SAVE_FILE, err);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_directives(&self) -> CommandTree {
|
|
|
|
CommandTree::Top(vec![])
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_interpreter_directive(&mut self, input: &str) -> Option<String> {
|
2019-03-13 22:43:44 -07:00
|
|
|
Some(format!("you typed {}, which is unsupported", input))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_cur_language_state(&mut self) -> &mut Box<ProgrammingLanguageInterface> {
|
|
|
|
//TODO this is obviously not complete
|
|
|
|
&mut self.language_states[0]
|
2019-03-12 00:01:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_input(&mut self, input: &str) -> String {
|
2019-03-13 22:43:44 -07:00
|
|
|
let ref mut language_state = self.get_cur_language_state();
|
|
|
|
|
|
|
|
let request = ComputationRequest {
|
|
|
|
source: input.to_string(),
|
|
|
|
debug_requests: vec![],
|
|
|
|
};
|
|
|
|
|
|
|
|
let ComputationResponse { main_output, global_output_stats, debug_responses }
|
|
|
|
= language_state.run_computation(request);
|
|
|
|
|
|
|
|
match main_output {
|
|
|
|
Ok(s) => s,
|
|
|
|
Err(e) => format!("{} {}", "Error".red(), e)
|
|
|
|
}
|
2019-03-12 00:01:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 00:13:39 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* --------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-09-21 19:43:50 -07:00
|
|
|
pub struct Repl {
|
|
|
|
options: EvalOptions,
|
|
|
|
languages: Vec<Box<ProgrammingLanguageInterface>>,
|
|
|
|
current_language_index: usize,
|
|
|
|
interpreter_directive_sigil: char,
|
|
|
|
line_reader: ::linefeed::interface::Interface<::linefeed::terminal::DefaultTerminal>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Repl {
|
|
|
|
pub fn new(languages: Vec<Box<ProgrammingLanguageInterface>>, initial_index: usize) -> Repl {
|
|
|
|
use linefeed::Interface;
|
2019-01-10 20:10:54 -08:00
|
|
|
let current_language_index = if initial_index < languages.len() { initial_index } else { 0 };
|
2018-09-21 19:43:50 -07:00
|
|
|
|
|
|
|
let line_reader = Interface::new("schala-repl").unwrap();
|
|
|
|
|
|
|
|
Repl {
|
|
|
|
options: Repl::get_options(),
|
2019-01-10 20:10:54 -08:00
|
|
|
languages,
|
|
|
|
current_language_index,
|
2018-09-21 19:43:50 -07:00
|
|
|
interpreter_directive_sigil: ':',
|
|
|
|
line_reader
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_cur_language(&self) -> &ProgrammingLanguageInterface {
|
|
|
|
self.languages[self.current_language_index].as_ref()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_options() -> EvalOptions {
|
2018-10-01 20:53:41 -07:00
|
|
|
File::open(OPTIONS_SAVE_FILE)
|
2018-09-21 19:43:50 -07:00
|
|
|
.and_then(|mut file| {
|
|
|
|
let mut contents = String::new();
|
|
|
|
file.read_to_string(&mut contents)?;
|
|
|
|
Ok(contents)
|
|
|
|
})
|
|
|
|
.and_then(|contents| {
|
|
|
|
let options: EvalOptions = ::serde_json::from_str(&contents)?;
|
|
|
|
Ok(options)
|
|
|
|
}).unwrap_or(EvalOptions::default())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn save_options(&self) {
|
|
|
|
let ref options = self.options;
|
2018-10-01 20:53:41 -07:00
|
|
|
let read = File::create(OPTIONS_SAVE_FILE)
|
2018-09-21 19:43:50 -07:00
|
|
|
.and_then(|mut file| {
|
|
|
|
let buf = ::serde_json::to_string(options).unwrap();
|
|
|
|
file.write_all(buf.as_bytes())
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Err(err) = read {
|
2018-10-01 20:53:41 -07:00
|
|
|
println!("Error saving {} file {}", OPTIONS_SAVE_FILE, err);
|
2018-09-21 19:43:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run(&mut self) {
|
|
|
|
use linefeed::ReadResult;
|
|
|
|
|
2019-03-12 02:39:25 -07:00
|
|
|
println!("Schala MetaInterpreter version {}", crate::VERSION_STRING);
|
2018-09-21 19:43:50 -07:00
|
|
|
println!("Type {}help for help with the REPL", self.interpreter_directive_sigil);
|
|
|
|
|
2018-10-01 20:53:41 -07:00
|
|
|
self.line_reader.load_history(HISTORY_SAVE_FILE).unwrap_or(());
|
2018-09-21 19:43:50 -07:00
|
|
|
|
|
|
|
loop {
|
|
|
|
let language_name = self.get_cur_language().get_language_name();
|
|
|
|
let directives = self.get_directives();
|
|
|
|
let tab_complete_handler = TabCompleteHandler::new(self.interpreter_directive_sigil, directives);
|
2018-10-02 00:46:23 -07:00
|
|
|
self.line_reader.set_completer(Arc::new(tab_complete_handler));
|
2018-09-21 19:43:50 -07:00
|
|
|
|
|
|
|
let prompt_str = format!("{} >> ", language_name);
|
|
|
|
self.line_reader.set_prompt(&prompt_str).unwrap();
|
|
|
|
|
|
|
|
match self.line_reader.read_line() {
|
|
|
|
Err(e) => {
|
|
|
|
println!("Terminal read error: {}", e);
|
|
|
|
},
|
|
|
|
Ok(ReadResult::Eof) => break,
|
|
|
|
Ok(ReadResult::Signal(_)) => break,
|
|
|
|
Ok(ReadResult::Input(ref input)) => {
|
|
|
|
self.line_reader.add_history_unique(input.to_string());
|
|
|
|
let output = match input.chars().nth(0) {
|
|
|
|
Some(ch) if ch == self.interpreter_directive_sigil => self.handle_interpreter_directive(input),
|
|
|
|
_ => Some(self.input_handler(input)),
|
|
|
|
};
|
|
|
|
if let Some(o) = output {
|
|
|
|
println!("=> {}", o);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-01 20:53:41 -07:00
|
|
|
self.line_reader.save_history(HISTORY_SAVE_FILE).unwrap_or(());
|
2018-09-21 19:43:50 -07:00
|
|
|
self.save_options();
|
|
|
|
println!("Exiting...");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn input_handler(&mut self, input: &str) -> String {
|
|
|
|
let ref mut language = self.languages[self.current_language_index];
|
|
|
|
let interpreter_output = language.execute_pipeline(input, &self.options);
|
|
|
|
interpreter_output.to_repl()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_directives(&self) -> CommandTree {
|
|
|
|
let ref passes = self.get_cur_language().get_passes();
|
|
|
|
|
|
|
|
let passes_directives: Vec<CommandTree> = passes.iter()
|
|
|
|
.map(|pass_descriptor| {
|
|
|
|
let name = &pass_descriptor.name;
|
|
|
|
if pass_descriptor.debug_options.len() == 0 {
|
|
|
|
CommandTree::term(name, None)
|
|
|
|
} else {
|
2018-10-02 01:37:43 -07:00
|
|
|
let children: Vec<CommandTree> = pass_descriptor.debug_options.iter()
|
2018-09-21 19:43:50 -07:00
|
|
|
.map(|o| CommandTree::term(o, None)).collect();
|
2018-10-02 01:37:43 -07:00
|
|
|
CommandTree::NonTerminal {
|
|
|
|
name: name.clone(),
|
|
|
|
children,
|
|
|
|
help_msg: None,
|
|
|
|
function: None,
|
|
|
|
}
|
2018-09-21 19:43:50 -07:00
|
|
|
}
|
|
|
|
}).collect();
|
|
|
|
|
|
|
|
CommandTree::Top(vec![
|
|
|
|
CommandTree::term("exit", Some("exit the REPL")),
|
|
|
|
CommandTree::term("quit", Some("exit the REPL")),
|
|
|
|
CommandTree::term("help", Some("Print this help message")),
|
2018-10-15 21:46:27 -07:00
|
|
|
CommandTree::nonterm("debug",
|
|
|
|
Some("show or hide pass debug info for a given pass, or display the names of all passes, or turn timing on/off"),
|
|
|
|
vec![
|
|
|
|
CommandTree::term("passes", None),
|
|
|
|
CommandTree::nonterm("show", None, passes_directives.clone()),
|
|
|
|
CommandTree::nonterm("hide", None, passes_directives.clone()),
|
|
|
|
CommandTree::nonterm("timing", None, vec![
|
|
|
|
CommandTree::term("on", None),
|
|
|
|
CommandTree::term("off", None),
|
|
|
|
])
|
|
|
|
]
|
|
|
|
),
|
|
|
|
CommandTree::nonterm("lang",
|
|
|
|
Some("switch between languages, or go directly to a langauge by name"),
|
|
|
|
vec![
|
|
|
|
CommandTree::term("next", None),
|
|
|
|
CommandTree::term("prev", None),
|
|
|
|
CommandTree::nonterm("go", None, vec![]),
|
|
|
|
]
|
|
|
|
),
|
2018-09-21 19:43:50 -07:00
|
|
|
CommandTree::term("doc", Some("Get language-specific help for an item")),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_interpreter_directive(&mut self, input: &str) -> Option<String> {
|
|
|
|
let mut iter = input.chars();
|
|
|
|
iter.next();
|
|
|
|
let commands: Vec<&str> = iter
|
|
|
|
.as_str()
|
|
|
|
.split_whitespace()
|
|
|
|
.collect();
|
|
|
|
|
2018-10-15 20:52:34 -07:00
|
|
|
let initial_cmd: &str = match commands.get(0).clone() {
|
2018-09-21 19:43:50 -07:00
|
|
|
None => return None,
|
|
|
|
Some(s) => s
|
|
|
|
};
|
|
|
|
|
2018-10-15 20:52:34 -07:00
|
|
|
match initial_cmd {
|
2018-09-21 19:43:50 -07:00
|
|
|
"exit" | "quit" => {
|
|
|
|
self.save_options();
|
|
|
|
::std::process::exit(0)
|
|
|
|
},
|
|
|
|
"lang" | "language" => match commands.get(1) {
|
|
|
|
Some(&"show") => {
|
|
|
|
let mut buf = String::new();
|
|
|
|
for (i, lang) in self.languages.iter().enumerate() {
|
|
|
|
write!(buf, "{}{}\n", if i == self.current_language_index { "* "} else { "" }, lang.get_language_name()).unwrap();
|
|
|
|
}
|
|
|
|
Some(buf)
|
|
|
|
},
|
|
|
|
Some(&"go") => match commands.get(2) {
|
|
|
|
None => Some(format!("Must specify a language name")),
|
|
|
|
Some(&desired_name) => {
|
|
|
|
for (i, _) in self.languages.iter().enumerate() {
|
|
|
|
let lang_name = self.languages[i].get_language_name();
|
|
|
|
if lang_name.to_lowercase() == desired_name.to_lowercase() {
|
|
|
|
self.current_language_index = i;
|
|
|
|
return Some(format!("Switching to {}", self.languages[self.current_language_index].get_language_name()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(format!("Language {} not found", desired_name))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Some(&"next") | Some(&"n") => {
|
|
|
|
self.current_language_index = (self.current_language_index + 1) % self.languages.len();
|
|
|
|
Some(format!("Switching to {}", self.languages[self.current_language_index].get_language_name()))
|
|
|
|
},
|
|
|
|
Some(&"previous") | Some(&"p") | Some(&"prev") => {
|
|
|
|
self.current_language_index = if self.current_language_index == 0 { self.languages.len() - 1 } else { self.current_language_index - 1 };
|
|
|
|
Some(format!("Switching to {}", self.languages[self.current_language_index].get_language_name()))
|
|
|
|
},
|
|
|
|
Some(e) => Some(format!("Bad `lang(uage)` argument: {}", e)),
|
|
|
|
None => Some(format!("Valid arguments for `lang(uage)` are `show`, `next`|`n`, `previous`|`prev`|`n`"))
|
|
|
|
},
|
|
|
|
"help" => {
|
|
|
|
let mut buf = String::new();
|
|
|
|
let ref lang = self.languages[self.current_language_index];
|
|
|
|
let directives = match self.get_directives() {
|
|
|
|
CommandTree::Top(children) => children,
|
|
|
|
_ => panic!("Top-level CommandTree not Top")
|
|
|
|
};
|
|
|
|
|
|
|
|
writeln!(buf, "MetaInterpreter options").unwrap();
|
|
|
|
writeln!(buf, "-----------------------").unwrap();
|
|
|
|
|
|
|
|
for directive in directives {
|
|
|
|
let trailer = " ";
|
|
|
|
writeln!(buf, "{}{}- {}", directive.get_cmd(), trailer, directive.get_help()).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
writeln!(buf, "").unwrap();
|
|
|
|
writeln!(buf, "Language-specific help for {}", lang.get_language_name()).unwrap();
|
|
|
|
writeln!(buf, "-----------------------").unwrap();
|
|
|
|
writeln!(buf, "{}", lang.custom_interpreter_directives_help()).unwrap();
|
|
|
|
Some(buf)
|
|
|
|
},
|
|
|
|
"debug" => self.handle_debug(commands),
|
|
|
|
"doc" => self.languages[self.current_language_index]
|
|
|
|
.get_doc(&commands)
|
|
|
|
.or(Some(format!("No docs implemented"))),
|
2018-10-15 20:28:18 -07:00
|
|
|
e => {
|
|
|
|
self.languages[self.current_language_index]
|
2018-09-21 19:43:50 -07:00
|
|
|
.handle_custom_interpreter_directives(&commands)
|
|
|
|
.or(Some(format!("Unknown command: {}", e)))
|
2018-10-15 20:28:18 -07:00
|
|
|
}
|
2018-09-21 19:43:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fn handle_debug(&mut self, commands: Vec<&str>) -> Option<String> {
|
|
|
|
let passes = self.get_cur_language().get_passes();
|
|
|
|
match commands.get(1) {
|
2018-10-01 02:05:07 -07:00
|
|
|
Some(&"timing") => match commands.get(2) {
|
|
|
|
Some(&"on") => { self.options.debug_timing = true; None }
|
|
|
|
Some(&"off") => { self.options.debug_timing = false; None }
|
|
|
|
_ => return Some(format!(r#"Argument to "timing" must be "on" or "off""#)),
|
|
|
|
},
|
2018-09-21 19:43:50 -07:00
|
|
|
Some(&"passes") => Some(
|
|
|
|
passes.into_iter()
|
|
|
|
.map(|desc| {
|
|
|
|
if self.options.debug_passes.contains_key(&desc.name) {
|
|
|
|
let color = "green";
|
|
|
|
format!("*{}", desc.name.color(color))
|
|
|
|
} else {
|
|
|
|
desc.name
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.intersperse(format!(" -> "))
|
|
|
|
.collect()),
|
|
|
|
b @ Some(&"show") | b @ Some(&"hide") => {
|
|
|
|
let show = b == Some(&"show");
|
|
|
|
let debug_pass: String = match commands.get(2) {
|
|
|
|
Some(s) => s.to_string(),
|
|
|
|
None => return Some(format!("Must specify a stage to debug")),
|
|
|
|
};
|
|
|
|
let pass_opt = commands.get(3);
|
|
|
|
if let Some(desc) = passes.iter().find(|desc| desc.name == debug_pass) {
|
|
|
|
let mut opts = vec![];
|
|
|
|
if let Some(opt) = pass_opt {
|
|
|
|
opts.push(opt.to_string());
|
|
|
|
}
|
|
|
|
let msg = format!("{} debug for pass {}", if show { "Enabling" } else { "Disabling" }, debug_pass);
|
|
|
|
if show {
|
|
|
|
self.options.debug_passes.insert(desc.name.clone(), PassDebugOptionsDescriptor { opts });
|
|
|
|
} else {
|
|
|
|
self.options.debug_passes.remove(&desc.name);
|
|
|
|
}
|
|
|
|
Some(msg)
|
|
|
|
} else {
|
|
|
|
Some(format!("Couldn't find stage: {}", debug_pass))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => Some(format!("Unknown debug command"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TabCompleteHandler {
|
|
|
|
sigil: char,
|
|
|
|
top_level_commands: CommandTree,
|
|
|
|
}
|
|
|
|
|
|
|
|
use linefeed::complete::{Completion, Completer};
|
|
|
|
use linefeed::terminal::Terminal;
|
|
|
|
|
|
|
|
impl TabCompleteHandler {
|
|
|
|
fn new(sigil: char, top_level_commands: CommandTree) -> TabCompleteHandler {
|
|
|
|
TabCompleteHandler {
|
|
|
|
top_level_commands,
|
|
|
|
sigil,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Terminal> Completer<T> for TabCompleteHandler {
|
|
|
|
fn complete(&self, word: &str, prompter: &::linefeed::prompter::Prompter<T>, start: usize, _end: usize) -> Option<Vec<Completion>> {
|
|
|
|
let line = prompter.buffer();
|
|
|
|
|
|
|
|
if line.starts_with(&format!("{}", self.sigil)) {
|
|
|
|
let mut words = line[1..(if start == 0 { 1 } else { start })].split_whitespace();
|
|
|
|
let mut completions = Vec::new();
|
|
|
|
let mut command_tree: Option<&CommandTree> = Some(&self.top_level_commands);
|
|
|
|
|
|
|
|
loop {
|
|
|
|
match words.next() {
|
|
|
|
None => {
|
|
|
|
let top = match command_tree {
|
|
|
|
Some(CommandTree::Top(_)) => true,
|
|
|
|
_ => false
|
|
|
|
};
|
|
|
|
let word = if top { word.get(1..).unwrap() } else { word };
|
|
|
|
for cmd in command_tree.map(|x| x.get_children()).unwrap_or(vec![]).into_iter() {
|
|
|
|
if cmd.starts_with(word) {
|
|
|
|
completions.push(Completion {
|
|
|
|
completion: format!("{}{}", if top { ":" } else { "" }, cmd),
|
2018-10-01 20:46:58 -07:00
|
|
|
display: Some(cmd.to_string()),
|
2018-09-21 19:43:50 -07:00
|
|
|
suffix: ::linefeed::complete::Suffix::Some(' ')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
},
|
|
|
|
Some(s) => {
|
|
|
|
let new_ptr: Option<&CommandTree> = command_tree.and_then(|cm| match cm {
|
|
|
|
CommandTree::Top(children) => children.iter().find(|c| c.get_cmd() == s),
|
2018-10-02 01:37:43 -07:00
|
|
|
CommandTree::NonTerminal { children, .. } => children.iter().find(|c| c.get_cmd() == s),
|
2018-10-02 00:46:23 -07:00
|
|
|
CommandTree::Terminal { .. } => None,
|
2018-09-21 19:43:50 -07:00
|
|
|
});
|
|
|
|
command_tree = new_ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(completions)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|