#![deny(warnings)]
extern crate unicode_xid;
pub use self::Piece::*;
pub use self::Position::*;
pub use self::Alignment::*;
pub use self::Flag::*;
pub use self::Count::*;
use std::str;
use std::string;
use std::iter;
use self::unicode_xid::UnicodeXID;
#[derive(Copy, Clone, PartialEq)]
pub enum Piece<'a> {
String(&'a str),
NextArgument(Argument<'a>),
}
#[derive(Copy, Clone, PartialEq)]
pub struct Argument<'a> {
pub position: Position<'a>,
pub format: FormatSpec<'a>,
}
#[derive(Copy, Clone, PartialEq)]
pub struct FormatSpec<'a> {
pub fill: Option<char>,
pub align: Alignment,
pub flags: u32,
pub precision: Count<'a>,
pub width: Count<'a>,
pub ty: &'a str,
}
#[derive(Copy, Clone, PartialEq)]
pub enum Position<'a> {
ArgumentIs(usize),
ArgumentNamed(&'a str),
}
#[derive(Copy, Clone, PartialEq)]
pub enum Alignment {
AlignLeft,
AlignRight,
AlignCenter,
AlignUnknown,
}
#[derive(Copy, Clone, PartialEq)]
pub enum Flag {
FlagSignPlus,
FlagSignMinus,
FlagAlternate,
FlagSignAwareZeroPad,
}
#[derive(Copy, Clone, PartialEq)]
pub enum Count<'a> {
CountIs(usize),
CountIsName(&'a str),
CountIsParam(usize),
CountImplied,
}
pub struct Parser<'a> {
input: &'a str,
cur: iter::Peekable<str::CharIndices<'a>>,
pub errors: Vec<(string::String, Option<string::String>)>,
curarg: usize,
}
impl<'a> Iterator for Parser<'a> {
type Item = Piece<'a>;
fn next(&mut self) -> Option<Piece<'a>> {
if let Some(&(pos, c)) = self.cur.peek() {
match c {
'{' => {
self.cur.next();
if self.consume('{') {
Some(String(self.string(pos + 1)))
} else {
let ret = Some(NextArgument(self.argument()));
self.must_consume('}');
ret
}
}
'}' => {
self.cur.next();
if self.consume('}') {
Some(String(self.string(pos + 1)))
} else {
self.err_with_note("unmatched `}` found",
"if you intended to print `}`, \
you can escape it using `}}`");
None
}
}
_ => Some(String(self.string(pos))),
}
} else {
None
}
}
}
impl<'a> Parser<'a> {
pub fn new(s: &'a str) -> Parser<'a> {
Parser {
input: s,
cur: s.char_indices().peekable(),
errors: vec![],
curarg: 0,
}
}
fn err(&mut self, msg: &str) {
self.errors.push((msg.to_owned(), None));
}
fn err_with_note(&mut self, msg: &str, note: &str) {
self.errors.push((msg.to_owned(), Some(note.to_owned())));
}
fn consume(&mut self, c: char) -> bool {
if let Some(&(_, maybe)) = self.cur.peek() {
if c == maybe {
self.cur.next();
true
} else {
false
}
} else {
false
}
}
fn must_consume(&mut self, c: char) {
self.ws();
if let Some(&(_, maybe)) = self.cur.peek() {
if c == maybe {
self.cur.next();
} else {
self.err(&format!("expected `{:?}`, found `{:?}`", c, maybe));
}
} else {
let msg = &format!("expected `{:?}` but string was terminated", c);
if c == '}' {
self.err_with_note(msg,
"if you intended to print `{`, you can escape it using `{{`");
} else {
self.err(msg);
}
}
}
fn ws(&mut self) {
while let Some(&(_, c)) = self.cur.peek() {
if c.is_whitespace() {
self.cur.next();
} else {
break;
}
}
}
fn string(&mut self, start: usize) -> &'a str {
while let Some(&(pos, c)) = self.cur.peek() {
match c {
'{' | '}' => {
return &self.input[start..pos];
}
_ => {
self.cur.next();
}
}
}
&self.input[start..self.input.len()]
}
fn argument(&mut self) -> Argument<'a> {
let pos = self.position();
let format = self.format();
let pos = match pos {
Some(position) => position,
None => {
let i = self.curarg;
self.curarg += 1;
ArgumentIs(i)
}
};
Argument {
position: pos,
format: format,
}
}
fn position(&mut self) -> Option<Position<'a>> {
if let Some(i) = self.integer() {
Some(ArgumentIs(i))
} else {
match self.cur.peek() {
Some(&(_, c)) if c.is_alphabetic() => Some(ArgumentNamed(self.word())),
_ => None,
}
}
}
fn format(&mut self) -> FormatSpec<'a> {
let mut spec = FormatSpec {
fill: None,
align: AlignUnknown,
flags: 0,
precision: CountImplied,
width: CountImplied,
ty: &self.input[..0],
};
if !self.consume(':') {
return spec;
}
if let Some(&(_, c)) = self.cur.peek() {
match self.cur.clone().skip(1).next() {
Some((_, '>')) | Some((_, '<')) | Some((_, '^')) => {
spec.fill = Some(c);
self.cur.next();
}
_ => {}
}
}
if self.consume('<') {
spec.align = AlignLeft;
} else if self.consume('>') {
spec.align = AlignRight;
} else if self.consume('^') {
spec.align = AlignCenter;
}
if self.consume('+') {
spec.flags |= 1 << (FlagSignPlus as u32);
} else if self.consume('-') {
spec.flags |= 1 << (FlagSignMinus as u32);
}
if self.consume('#') {
spec.flags |= 1 << (FlagAlternate as u32);
}
let mut havewidth = false;
if self.consume('0') {
if self.consume('$') {
spec.width = CountIsParam(0);
havewidth = true;
} else {
spec.flags |= 1 << (FlagSignAwareZeroPad as u32);
}
}
if !havewidth {
spec.width = self.count();
}
if self.consume('.') {
if self.consume('*') {
let i = self.curarg;
self.curarg += 1;
spec.precision = CountIsParam(i);
} else {
spec.precision = self.count();
}
}
if self.consume('?') {
spec.ty = "?";
} else {
spec.ty = self.word();
}
spec
}
fn count(&mut self) -> Count<'a> {
if let Some(i) = self.integer() {
if self.consume('$') {
CountIsParam(i)
} else {
CountIs(i)
}
} else {
let tmp = self.cur.clone();
let word = self.word();
if word.is_empty() {
self.cur = tmp;
CountImplied
} else {
if self.consume('$') {
CountIsName(word)
} else {
self.cur = tmp;
CountImplied
}
}
}
}
fn word(&mut self) -> &'a str {
let start = match self.cur.peek() {
Some(&(pos, c)) if UnicodeXID::is_xid_start(c) => {
self.cur.next();
pos
}
_ => {
return &self.input[..0];
}
};
while let Some(&(pos, c)) = self.cur.peek() {
if UnicodeXID::is_xid_continue(c) {
self.cur.next();
} else {
return &self.input[start..pos];
}
}
&self.input[start..self.input.len()]
}
fn integer(&mut self) -> Option<usize> {
let mut cur = 0;
let mut found = false;
while let Some(&(_, c)) = self.cur.peek() {
if let Some(i) = c.to_digit(10) {
cur = cur * 10 + i as usize;
found = true;
self.cur.next();
} else {
break;
}
}
if found {
Some(cur)
} else {
None
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn same(fmt: &'static str, p: &[Piece<'static>]) {
let parser = Parser::new(fmt);
assert!(parser.collect::<Vec<Piece<'static>>>() == p);
}
fn fmtdflt() -> FormatSpec<'static> {
return FormatSpec {
fill: None,
align: AlignUnknown,
flags: 0,
precision: CountImplied,
width: CountImplied,
ty: "",
};
}
fn musterr(s: &str) {
let mut p = Parser::new(s);
p.next();
assert!(!p.errors.is_empty());
}
#[test]
fn simple() {
same("asdf", &[String("asdf")]);
same("a{{b", &[String("a"), String("{b")]);
same("a}}b", &[String("a"), String("}b")]);
same("a}}", &[String("a"), String("}")]);
same("}}", &[String("}")]);
same("\\}}", &[String("\\"), String("}")]);
}
#[test]
fn invalid01() {
musterr("{")
}
#[test]
fn invalid02() {
musterr("}")
}
#[test]
fn invalid04() {
musterr("{3a}")
}
#[test]
fn invalid05() {
musterr("{:|}")
}
#[test]
fn invalid06() {
musterr("{:>>>}")
}
#[test]
fn format_nothing() {
same("{}",
&[NextArgument(Argument {
position: ArgumentIs(0),
format: fmtdflt(),
})]);
}
#[test]
fn format_position() {
same("{3}",
&[NextArgument(Argument {
position: ArgumentIs(3),
format: fmtdflt(),
})]);
}
#[test]
fn format_position_nothing_else() {
same("{3:}",
&[NextArgument(Argument {
position: ArgumentIs(3),
format: fmtdflt(),
})]);
}
#[test]
fn format_type() {
same("{3:a}",
&[NextArgument(Argument {
position: ArgumentIs(3),
format: FormatSpec {
fill: None,
align: AlignUnknown,
flags: 0,
precision: CountImplied,
width: CountImplied,
ty: "a",
},
})]);
}
#[test]
fn format_align_fill() {
same("{3:>}",
&[NextArgument(Argument {
position: ArgumentIs(3),
format: FormatSpec {
fill: None,
align: AlignRight,
flags: 0,
precision: CountImplied,
width: CountImplied,
ty: "",
},
})]);
same("{3:0<}",
&[NextArgument(Argument {
position: ArgumentIs(3),
format: FormatSpec {
fill: Some('0'),
align: AlignLeft,
flags: 0,
precision: CountImplied,
width: CountImplied,
ty: "",
},
})]);
same("{3:*<abcd}",
&[NextArgument(Argument {
position: ArgumentIs(3),
format: FormatSpec {
fill: Some('*'),
align: AlignLeft,
flags: 0,
precision: CountImplied,
width: CountImplied,
ty: "abcd",
},
})]);
}
#[test]
fn format_counts() {
same("{:10s}",
&[NextArgument(Argument {
position: ArgumentIs(0),
format: FormatSpec {
fill: None,
align: AlignUnknown,
flags: 0,
precision: CountImplied,
width: CountIs(10),
ty: "s",
},
})]);
same("{:10$.10s}",
&[NextArgument(Argument {
position: ArgumentIs(0),
format: FormatSpec {
fill: None,
align: AlignUnknown,
flags: 0,
precision: CountIs(10),
width: CountIsParam(10),
ty: "s",
},
})]);
same("{:.*s}",
&[NextArgument(Argument {
position: ArgumentIs(1),
format: FormatSpec {
fill: None,
align: AlignUnknown,
flags: 0,
precision: CountIsParam(0),
width: CountImplied,
ty: "s",
},
})]);
same("{:.10$s}",
&[NextArgument(Argument {
position: ArgumentIs(0),
format: FormatSpec {
fill: None,
align: AlignUnknown,
flags: 0,
precision: CountIsParam(10),
width: CountImplied,
ty: "s",
},
})]);
same("{:a$.b$s}",
&[NextArgument(Argument {
position: ArgumentIs(0),
format: FormatSpec {
fill: None,
align: AlignUnknown,
flags: 0,
precision: CountIsName("b"),
width: CountIsName("a"),
ty: "s",
},
})]);
}
#[test]
fn format_flags() {
same("{:-}",
&[NextArgument(Argument {
position: ArgumentIs(0),
format: FormatSpec {
fill: None,
align: AlignUnknown,
flags: (1 << FlagSignMinus as u32),
precision: CountImplied,
width: CountImplied,
ty: "",
},
})]);
same("{:+#}",
&[NextArgument(Argument {
position: ArgumentIs(0),
format: FormatSpec {
fill: None,
align: AlignUnknown,
flags: (1 << FlagSignPlus as u32) | (1 << FlagAlternate as u32),
precision: CountImplied,
width: CountImplied,
ty: "",
},
})]);
}
#[test]
fn format_mixture() {
same("abcd {3:a} efg",
&[String("abcd "),
NextArgument(Argument {
position: ArgumentIs(3),
format: FormatSpec {
fill: None,
align: AlignUnknown,
flags: 0,
precision: CountImplied,
width: CountImplied,
ty: "a",
},
}),
String(" efg")]);
}
}