2016-02-15 05:47:02 +00:00
|
|
|
//! Memo runner of printf
|
2015-12-24 06:11:00 +00:00
|
|
|
//! Takes a format string and arguments
|
|
|
|
//! 1. tokenizes format string into tokens, consuming
|
|
|
|
//! any subst. arguments along the way.
|
|
|
|
//! 2. feeds remaining arguments into function
|
|
|
|
//! that prints tokens.
|
|
|
|
|
|
|
|
use std::iter::Peekable;
|
|
|
|
use std::slice::Iter;
|
|
|
|
use itertools::PutBackN;
|
|
|
|
use cli;
|
|
|
|
use tokenize::token::{Token, Tokenizer};
|
|
|
|
use tokenize::unescaped_text::UnescapedText;
|
|
|
|
use tokenize::sub::Sub;
|
|
|
|
|
|
|
|
pub struct Memo {
|
|
|
|
tokens: Vec<Box<Token>>,
|
|
|
|
}
|
|
|
|
|
2016-02-15 05:47:02 +00:00
|
|
|
fn warn_excess_args(first_arg: &str) {
|
2015-12-24 06:11:00 +00:00
|
|
|
cli::err_msg(&format!("warning: ignoring excess arguments, starting with '{}'",
|
2016-02-15 05:47:02 +00:00
|
|
|
first_arg));
|
2015-12-24 06:11:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Memo {
|
2016-02-15 05:47:02 +00:00
|
|
|
pub fn new(pf_string: &String, pf_args_it: &mut Peekable<Iter<String>>) -> Memo {
|
2015-12-24 06:11:00 +00:00
|
|
|
let mut pm = Memo { tokens: Vec::new() };
|
2016-02-15 05:47:02 +00:00
|
|
|
let mut tmp_token: Option<Box<Token>>;
|
2015-12-24 06:11:00 +00:00
|
|
|
let mut it = PutBackN::new(pf_string.chars());
|
|
|
|
let mut has_sub = false;
|
|
|
|
loop {
|
|
|
|
tmp_token = UnescapedText::from_it(&mut it, pf_args_it);
|
|
|
|
match tmp_token {
|
|
|
|
Some(x) => pm.tokens.push(x),
|
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
tmp_token = Sub::from_it(&mut it, pf_args_it);
|
|
|
|
match tmp_token {
|
|
|
|
Some(x) => {
|
2016-02-15 05:47:02 +00:00
|
|
|
if !has_sub {
|
|
|
|
has_sub = true;
|
|
|
|
}
|
2015-12-24 06:11:00 +00:00
|
|
|
pm.tokens.push(x);
|
2016-02-15 05:47:02 +00:00
|
|
|
}
|
2015-12-24 06:11:00 +00:00
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
if let Some(x) = it.next() {
|
|
|
|
it.put_back(x);
|
2016-02-15 05:47:02 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2015-12-24 06:11:00 +00:00
|
|
|
}
|
2016-02-15 05:47:02 +00:00
|
|
|
if !has_sub {
|
|
|
|
let mut drain = false;
|
2015-12-24 06:11:00 +00:00
|
|
|
if let Some(first_arg) = pf_args_it.peek() {
|
|
|
|
warn_excess_args(first_arg);
|
|
|
|
drain = true;
|
|
|
|
}
|
|
|
|
if drain {
|
|
|
|
loop {
|
2016-02-15 05:47:02 +00:00
|
|
|
// drain remaining args;
|
2015-12-24 06:11:00 +00:00
|
|
|
if pf_args_it.next().is_none() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pm
|
|
|
|
}
|
|
|
|
pub fn apply(&self, pf_args_it: &mut Peekable<Iter<String>>) {
|
|
|
|
for tkn in self.tokens.iter() {
|
|
|
|
tkn.print(pf_args_it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn run_all(pf_string: &String, pf_args: &[String]) {
|
|
|
|
let mut arg_it = pf_args.iter().peekable();
|
|
|
|
let pm = Memo::new(pf_string, &mut arg_it);
|
|
|
|
loop {
|
|
|
|
if arg_it.peek().is_none() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pm.apply(&mut arg_it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|