2013-05-30 01:13:35 -05:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! Parameterized string expansion
|
|
|
|
|
|
|
|
use core::prelude::*;
|
|
|
|
use core::{char, int, vec};
|
2013-06-13 21:37:20 -05:00
|
|
|
use core::iterator::IteratorUtil;
|
2013-05-30 01:13:35 -05:00
|
|
|
|
|
|
|
#[deriving(Eq)]
|
|
|
|
enum States {
|
|
|
|
Nothing,
|
|
|
|
Percent,
|
|
|
|
SetVar,
|
|
|
|
GetVar,
|
|
|
|
PushParam,
|
|
|
|
CharConstant,
|
|
|
|
CharClose,
|
|
|
|
IntConstant,
|
|
|
|
IfCond,
|
|
|
|
IfBody
|
|
|
|
}
|
|
|
|
|
2013-05-30 18:14:40 -05:00
|
|
|
/// Types of parameters a capability can use
|
2013-05-30 01:13:35 -05:00
|
|
|
pub enum Param {
|
|
|
|
String(~str),
|
|
|
|
Char(char),
|
|
|
|
Number(int)
|
|
|
|
}
|
|
|
|
|
2013-06-13 20:16:25 -05:00
|
|
|
/// Container for static and dynamic variable arrays
|
|
|
|
pub struct Variables {
|
|
|
|
/// Static variables A-Z
|
|
|
|
sta: [Param, ..26],
|
|
|
|
/// Dynamic variables a-z
|
|
|
|
dyn: [Param, ..26]
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Variables {
|
|
|
|
/// Return a new zero-initialized Variables
|
|
|
|
pub fn new() -> Variables {
|
|
|
|
Variables{ sta: [Number(0), ..26], dyn: [Number(0), ..26] }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-30 18:14:40 -05:00
|
|
|
/**
|
|
|
|
Expand a parameterized capability
|
|
|
|
|
|
|
|
# Arguments
|
|
|
|
* `cap` - string to expand
|
|
|
|
* `params` - vector of params for %p1 etc
|
2013-06-13 20:16:25 -05:00
|
|
|
* `vars` - Variables struct for %Pa etc
|
2013-05-30 18:14:40 -05:00
|
|
|
|
2013-06-13 20:16:25 -05:00
|
|
|
To be compatible with ncurses, `vars` should be the same between calls to `expand` for
|
2013-05-30 18:14:40 -05:00
|
|
|
multiple capabilities for the same terminal.
|
|
|
|
*/
|
2013-06-13 21:37:20 -05:00
|
|
|
pub fn expand(cap: &[u8], params: &[Param], vars: &mut Variables)
|
2013-05-30 18:14:40 -05:00
|
|
|
-> Result<~[u8], ~str> {
|
2013-05-30 01:13:35 -05:00
|
|
|
let mut state = Nothing;
|
|
|
|
let mut i = 0;
|
|
|
|
|
2013-06-13 21:37:20 -05:00
|
|
|
// expanded cap will only rarely be larger than the cap itself
|
2013-05-30 01:13:35 -05:00
|
|
|
let mut output = vec::with_capacity(cap.len());
|
|
|
|
|
|
|
|
let mut cur;
|
|
|
|
|
|
|
|
let mut stack: ~[Param] = ~[];
|
|
|
|
|
|
|
|
let mut intstate = ~[];
|
|
|
|
|
2013-06-13 21:37:20 -05:00
|
|
|
// Copy parameters into a local vector for mutability
|
|
|
|
let mut mparams = [Number(0), ..9];
|
|
|
|
for mparams.mut_iter().zip(params.iter()).advance |(dst, &src)| {
|
|
|
|
*dst = src;
|
|
|
|
}
|
|
|
|
|
2013-05-30 01:13:35 -05:00
|
|
|
while i < cap.len() {
|
|
|
|
cur = cap[i] as char;
|
|
|
|
let mut old_state = state;
|
|
|
|
match state {
|
|
|
|
Nothing => {
|
|
|
|
if cur == '%' {
|
|
|
|
state = Percent;
|
|
|
|
} else {
|
|
|
|
output.push(cap[i]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Percent => {
|
|
|
|
match cur {
|
|
|
|
'%' => { output.push(cap[i]); state = Nothing },
|
|
|
|
'c' => match stack.pop() {
|
|
|
|
Char(c) => output.push(c as u8),
|
2013-05-30 18:14:40 -05:00
|
|
|
_ => return Err(~"a non-char was used with %c")
|
2013-05-30 01:13:35 -05:00
|
|
|
},
|
|
|
|
's' => match stack.pop() {
|
2013-06-10 22:10:37 -05:00
|
|
|
String(s) => output.push_all(s.as_bytes()),
|
2013-05-30 18:14:40 -05:00
|
|
|
_ => return Err(~"a non-str was used with %s")
|
2013-05-30 01:13:35 -05:00
|
|
|
},
|
|
|
|
'd' => match stack.pop() {
|
2013-06-10 22:10:37 -05:00
|
|
|
Number(x) => {
|
|
|
|
let s = x.to_str();
|
|
|
|
output.push_all(s.as_bytes())
|
|
|
|
}
|
2013-05-30 18:14:40 -05:00
|
|
|
_ => return Err(~"a non-number was used with %d")
|
2013-05-30 01:13:35 -05:00
|
|
|
},
|
|
|
|
'p' => state = PushParam,
|
|
|
|
'P' => state = SetVar,
|
|
|
|
'g' => state = GetVar,
|
|
|
|
'\'' => state = CharConstant,
|
|
|
|
'{' => state = IntConstant,
|
|
|
|
'l' => match stack.pop() {
|
|
|
|
String(s) => stack.push(Number(s.len() as int)),
|
2013-05-30 18:14:40 -05:00
|
|
|
_ => return Err(~"a non-str was used with %l")
|
2013-05-30 01:13:35 -05:00
|
|
|
},
|
|
|
|
'+' => match (stack.pop(), stack.pop()) {
|
|
|
|
(Number(x), Number(y)) => stack.push(Number(x + y)),
|
2013-05-30 18:14:40 -05:00
|
|
|
(_, _) => return Err(~"non-numbers on stack with +")
|
2013-05-30 01:13:35 -05:00
|
|
|
},
|
|
|
|
'-' => match (stack.pop(), stack.pop()) {
|
|
|
|
(Number(x), Number(y)) => stack.push(Number(x - y)),
|
2013-05-30 18:14:40 -05:00
|
|
|
(_, _) => return Err(~"non-numbers on stack with -")
|
2013-05-30 01:13:35 -05:00
|
|
|
},
|
|
|
|
'*' => match (stack.pop(), stack.pop()) {
|
|
|
|
(Number(x), Number(y)) => stack.push(Number(x * y)),
|
2013-05-30 18:14:40 -05:00
|
|
|
(_, _) => return Err(~"non-numbers on stack with *")
|
2013-05-30 01:13:35 -05:00
|
|
|
},
|
|
|
|
'/' => match (stack.pop(), stack.pop()) {
|
|
|
|
(Number(x), Number(y)) => stack.push(Number(x / y)),
|
2013-05-30 18:14:40 -05:00
|
|
|
(_, _) => return Err(~"non-numbers on stack with /")
|
2013-05-30 01:13:35 -05:00
|
|
|
},
|
|
|
|
'm' => match (stack.pop(), stack.pop()) {
|
|
|
|
(Number(x), Number(y)) => stack.push(Number(x % y)),
|
2013-05-30 18:14:40 -05:00
|
|
|
(_, _) => return Err(~"non-numbers on stack with %")
|
2013-05-30 01:13:35 -05:00
|
|
|
},
|
|
|
|
'&' => match (stack.pop(), stack.pop()) {
|
|
|
|
(Number(x), Number(y)) => stack.push(Number(x & y)),
|
2013-05-30 18:14:40 -05:00
|
|
|
(_, _) => return Err(~"non-numbers on stack with &")
|
2013-05-30 01:13:35 -05:00
|
|
|
},
|
|
|
|
'|' => match (stack.pop(), stack.pop()) {
|
|
|
|
(Number(x), Number(y)) => stack.push(Number(x | y)),
|
2013-05-30 18:14:40 -05:00
|
|
|
(_, _) => return Err(~"non-numbers on stack with |")
|
2013-05-30 01:13:35 -05:00
|
|
|
},
|
2013-06-13 20:42:49 -05:00
|
|
|
'A' => match (stack.pop(), stack.pop()) {
|
2013-06-13 21:36:45 -05:00
|
|
|
(Number(0), Number(_)) => stack.push(Number(0)),
|
|
|
|
(Number(_), Number(0)) => stack.push(Number(0)),
|
|
|
|
(Number(_), Number(_)) => stack.push(Number(1)),
|
|
|
|
_ => return Err(~"non-numbers on stack with logical and")
|
2013-06-13 20:42:49 -05:00
|
|
|
},
|
|
|
|
'O' => match (stack.pop(), stack.pop()) {
|
2013-06-13 21:36:45 -05:00
|
|
|
(Number(0), Number(0)) => stack.push(Number(0)),
|
|
|
|
(Number(_), Number(_)) => stack.push(Number(1)),
|
|
|
|
_ => return Err(~"non-numbers on stack with logical or")
|
2013-06-13 20:42:49 -05:00
|
|
|
},
|
|
|
|
'!' => match stack.pop() {
|
2013-06-13 21:36:45 -05:00
|
|
|
Number(0) => stack.push(Number(1)),
|
|
|
|
Number(_) => stack.push(Number(0)),
|
2013-06-13 20:42:49 -05:00
|
|
|
_ => return Err(~"non-number on stack with logical not")
|
|
|
|
},
|
2013-05-30 01:13:35 -05:00
|
|
|
'~' => match stack.pop() {
|
|
|
|
Number(x) => stack.push(Number(!x)),
|
2013-05-30 18:14:40 -05:00
|
|
|
_ => return Err(~"non-number on stack with %~")
|
2013-05-30 01:13:35 -05:00
|
|
|
},
|
2013-06-13 21:37:20 -05:00
|
|
|
'i' => match (copy mparams[0], copy mparams[1]) {
|
2013-06-13 21:36:45 -05:00
|
|
|
(Number(ref mut x), Number(ref mut y)) => {
|
|
|
|
*x += 1;
|
|
|
|
*y += 1;
|
2013-05-30 01:13:35 -05:00
|
|
|
},
|
2013-05-30 18:14:40 -05:00
|
|
|
(_, _) => return Err(~"first two params not numbers with %i")
|
2013-05-30 01:13:35 -05:00
|
|
|
},
|
2013-05-30 18:14:40 -05:00
|
|
|
'?' => state = return Err(fmt!("if expressions unimplemented (%?)", cap)),
|
|
|
|
_ => return Err(fmt!("unrecognized format option %c", cur))
|
2013-05-30 01:13:35 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
PushParam => {
|
|
|
|
// params are 1-indexed
|
2013-06-13 21:37:20 -05:00
|
|
|
stack.push(copy mparams[char::to_digit(cur, 10).expect("bad param number") - 1]);
|
2013-05-30 01:13:35 -05:00
|
|
|
},
|
|
|
|
SetVar => {
|
|
|
|
if cur >= 'A' && cur <= 'Z' {
|
|
|
|
let idx = (cur as u8) - ('A' as u8);
|
2013-06-13 20:16:25 -05:00
|
|
|
vars.sta[idx] = stack.pop();
|
2013-05-30 01:13:35 -05:00
|
|
|
} else if cur >= 'a' && cur <= 'z' {
|
|
|
|
let idx = (cur as u8) - ('a' as u8);
|
2013-06-13 20:16:25 -05:00
|
|
|
vars.dyn[idx] = stack.pop();
|
2013-05-30 01:13:35 -05:00
|
|
|
} else {
|
2013-05-30 18:14:40 -05:00
|
|
|
return Err(~"bad variable name in %P");
|
2013-05-30 01:13:35 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
GetVar => {
|
|
|
|
if cur >= 'A' && cur <= 'Z' {
|
|
|
|
let idx = (cur as u8) - ('A' as u8);
|
2013-06-13 20:16:25 -05:00
|
|
|
stack.push(copy vars.sta[idx]);
|
2013-05-30 01:13:35 -05:00
|
|
|
} else if cur >= 'a' && cur <= 'z' {
|
|
|
|
let idx = (cur as u8) - ('a' as u8);
|
2013-06-13 20:16:25 -05:00
|
|
|
stack.push(copy vars.dyn[idx]);
|
2013-05-30 01:13:35 -05:00
|
|
|
} else {
|
2013-05-30 18:14:40 -05:00
|
|
|
return Err(~"bad variable name in %g");
|
2013-05-30 01:13:35 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
CharConstant => {
|
|
|
|
stack.push(Char(cur));
|
|
|
|
state = CharClose;
|
|
|
|
},
|
|
|
|
CharClose => {
|
2013-06-13 20:42:49 -05:00
|
|
|
if cur != '\'' {
|
|
|
|
return Err(~"malformed character constant");
|
|
|
|
}
|
2013-05-30 01:13:35 -05:00
|
|
|
},
|
|
|
|
IntConstant => {
|
|
|
|
if cur == '}' {
|
|
|
|
stack.push(Number(int::parse_bytes(intstate, 10).expect("bad int constant")));
|
|
|
|
state = Nothing;
|
|
|
|
}
|
|
|
|
intstate.push(cur as u8);
|
|
|
|
old_state = Nothing;
|
|
|
|
}
|
2013-05-30 18:14:40 -05:00
|
|
|
_ => return Err(~"unimplemented state")
|
2013-05-30 01:13:35 -05:00
|
|
|
}
|
|
|
|
if state == old_state {
|
|
|
|
state = Nothing;
|
|
|
|
}
|
|
|
|
i += 1;
|
|
|
|
}
|
2013-05-30 18:14:40 -05:00
|
|
|
Ok(output)
|
2013-05-30 01:13:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
#[test]
|
|
|
|
fn test_basic_setabf() {
|
|
|
|
let s = bytes!("\\E[48;5;%p1%dm");
|
2013-06-13 20:16:25 -05:00
|
|
|
assert_eq!(expand(s, [Number(1)], &mut Variables::new()).unwrap(), bytes!("\\E[48;5;1m").to_owned());
|
2013-05-30 01:13:35 -05:00
|
|
|
}
|
|
|
|
}
|