Skip to content
Snippets Groups Projects
Commit a09aa7d6 authored by Michael Aaron Murphy's avatar Michael Aaron Murphy
Browse files

Refactor quote termination logic

parent 9f8e9e53
No related branches found
No related tags found
No related merge requests found
//! Contains the binary logic of Ion. //! Contains the binary logic of Ion.
mod prompt; mod prompt;
mod readln; mod readln;
mod terminate;
use self::prompt::{prompt, prompt_fn}; use self::prompt::{prompt, prompt_fn};
use self::readln::readln; use self::readln::readln;
use self::terminate::{terminate_quotes, terminate_script_quotes};
use super::{FlowLogic, JobControl, Shell, ShellHistory}; use super::{FlowLogic, JobControl, Shell, ShellHistory};
use super::flags::*; use super::flags::*;
use super::flow_control::Statement; use super::flow_control::Statement;
use super::library::IonLibrary; use super::library::IonLibrary;
use super::status::*; use super::status::*;
use liner::{Buffer, Context}; use liner::{Buffer, Context};
use parser::QuoteTerminator;
use smallvec::SmallVec; use smallvec::SmallVec;
use std::env; use std::env;
use std::fs::File; use std::fs::File;
...@@ -49,50 +50,12 @@ impl Binary for Shell { ...@@ -49,50 +50,12 @@ impl Binary for Shell {
fn readln(&mut self) -> Option<String> { readln(self) } fn readln(&mut self) -> Option<String> { readln(self) }
fn terminate_script_quotes<I: Iterator<Item = String>>(&mut self, mut lines: I) -> i32 { fn terminate_script_quotes<I: Iterator<Item = String>>(&mut self, lines: I) -> i32 {
while let Some(command) = lines.next() { terminate_script_quotes(self, lines)
let mut buffer = QuoteTerminator::new(command);
while !buffer.check_termination() {
loop {
if let Some(command) = lines.next() {
buffer.append(command);
break;
} else {
let stderr = io::stderr();
let _ = writeln!(stderr.lock(), "ion: unterminated quote in script");
return FAILURE;
}
}
}
self.on_command(&buffer.consume());
}
// The flow control level being non zero means that we have a statement that has
// only been partially parsed.
if self.flow_control.level != 0 {
eprintln!(
"ion: unexpected end of script: expected end block for `{}`",
self.flow_control.current_statement.short()
);
return FAILURE;
}
SUCCESS
} }
fn terminate_quotes(&mut self, command: String) -> Result<String, ()> { fn terminate_quotes(&mut self, command: String) -> Result<String, ()> {
let mut buffer = QuoteTerminator::new(command); terminate_quotes(self, command)
self.flow_control.level += 1;
while !buffer.check_termination() {
if let Some(command) = self.readln() {
buffer.append(command);
} else {
return Err(());
}
}
self.flow_control.level -= 1;
let terminated = buffer.consume();
Ok(terminated)
} }
fn execute_arguments<A: Iterator<Item = String>>(&mut self, mut args: A) { fn execute_arguments<A: Iterator<Item = String>>(&mut self, mut args: A) {
......
use super::super::{Binary, FlowLogic, Shell};
use super::super::status::*;
use parser::QuoteTerminator;
pub(crate) fn terminate_script_quotes<I: Iterator<Item = String>>(
shell: &mut Shell,
mut lines: I,
) -> i32 {
while let Some(command) = lines.next() {
let mut buffer = QuoteTerminator::new(command);
while !buffer.check_termination() {
loop {
if let Some(command) = lines.next() {
buffer.append(command);
break;
} else {
eprintln!("ion: unterminated quote in script");
return FAILURE;
}
}
}
shell.on_command(&buffer.consume());
}
// The flow control level being non zero means that we have a statement that has
// only been partially parsed.
if shell.flow_control.level != 0 {
eprintln!(
"ion: unexpected end of script: expected end block for `{}`",
shell.flow_control.current_statement.short()
);
return FAILURE;
}
SUCCESS
}
pub(crate) fn terminate_quotes(shell: &mut Shell, command: String) -> Result<String, ()> {
let mut buffer = QuoteTerminator::new(command);
shell.flow_control.level += 1;
while !buffer.check_termination() {
if let Some(command) = shell.readln() {
buffer.append(command);
} else {
return Err(());
}
}
shell.flow_control.level -= 1;
let terminated = buffer.consume();
Ok(terminated)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment