Re-structure control flow a bit + modernization

This commit is contained in:
Alex Crichton 2015-06-07 22:38:36 -07:00
parent 8487b63c97
commit 6580b77a20

View file

@ -9,7 +9,7 @@ macro_rules! try {
($e:expr) => (match $e { Some(s) => s, None => return None }) ($e:expr) => (match $e { Some(s) => s, None => return None })
} }
/* /*
* We redefine Array, Table and Value, because we need to keep track of * We redefine Array, Table and Value, because we need to keep track of
* encountered table definitions, eg when parsing: * encountered table definitions, eg when parsing:
* [a] * [a]
@ -56,7 +56,7 @@ impl Value {
Value::Table(..) => "table", Value::Table(..) => "table",
} }
} }
fn as_table<'a>(&'a self) -> Option<&'a TomlTable> { fn as_table<'a>(&'a self) -> Option<&'a TomlTable> {
match *self { Value::Table(ref s) => Some(s), _ => None } match *self { Value::Table(ref s) => Some(s), _ => None }
} }
@ -254,9 +254,8 @@ impl<'a> Parser<'a> {
let mut keys = Vec::new(); let mut keys = Vec::new();
loop { loop {
self.ws(); self.ws();
match self.key_name() { if let Some(s) = self.key_name() {
Some(s) => keys.push(s), keys.push(s);
None => {}
} }
self.ws(); self.ws();
if self.eat(']') { if self.eat(']') {
@ -293,18 +292,13 @@ impl<'a> Parser<'a> {
self.finish_string(start, false) self.finish_string(start, false)
} else { } else {
let mut ret = String::new(); let mut ret = String::new();
loop { while let Some((_, ch)) = self.cur.clone().next() {
match self.cur.clone().next() { match ch {
Some((_, ch)) => { 'a' ... 'z' |
match ch { 'A' ... 'Z' |
'a' ... 'z' | '0' ... '9' |
'A' ... 'Z' | '_' | '-' => { self.cur.next(); ret.push(ch) }
'0' ... '9' | _ => break,
'_' | '-' => { self.cur.next(); ret.push(ch) }
_ => break,
}
}
None => break
} }
} }
Some(ret) Some(ret)
@ -423,9 +417,8 @@ impl<'a> Parser<'a> {
return Some(ret) return Some(ret)
} }
Some((pos, '\\')) => { Some((pos, '\\')) => {
match escape(self, pos, multiline) { if let Some(c) = escape(self, pos, multiline) {
Some(c) => ret.push(c), ret.push(c);
None => {}
} }
} }
Some((pos, ch)) if ch < '\u{1f}' => { Some((pos, ch)) if ch < '\u{1f}' => {
@ -470,32 +463,26 @@ impl<'a> Parser<'a> {
} else { } else {
"invalid" "invalid"
}; };
match u32::from_str_radix(num, 16).ok() { if let Some(n) = u32::from_str_radix(num, 16).ok() {
Some(n) => { if let Some(c) = char::from_u32(n) {
match char::from_u32(n) { me.cur.by_ref().skip(len - 1).next();
Some(c) => { return Some(c)
me.cur.by_ref().skip(len - 1).next(); } else {
return Some(c)
}
None => {
me.errors.push(ParserError {
lo: pos + 1,
hi: pos + 5,
desc: format!("codepoint `{:x}` is \
not a valid unicode \
codepoint", n),
})
}
}
}
None => {
me.errors.push(ParserError { me.errors.push(ParserError {
lo: pos, lo: pos + 1,
hi: pos + 1, hi: pos + 5,
desc: format!("expected {} hex digits \ desc: format!("codepoint `{:x}` is \
after a `{}` escape", len, c), not a valid unicode \
codepoint", n),
}) })
} }
} else {
me.errors.push(ParserError {
lo: pos,
hi: pos + 1,
desc: format!("expected {} hex digits \
after a `{}` escape", len, c),
})
} }
None None
} }
@ -832,10 +819,7 @@ impl<'a> Parser<'a> {
if tmp.0.contains_key(part) { if tmp.0.contains_key(part) {
match *tmp.0.get_mut(part).unwrap() { match *tmp.0.get_mut(part).unwrap() {
Value::Table(ref mut table) => { Value::Table(ref mut table) => cur = table,
cur = table;
continue
}
Value::Array(ref mut array) => { Value::Array(ref mut array) => {
match array.last_mut() { match array.last_mut() {
Some(&mut Value::Table(ref mut table)) => cur = table, Some(&mut Value::Table(ref mut table)) => cur = table,
@ -849,7 +833,6 @@ impl<'a> Parser<'a> {
return None return None
} }
} }
continue
} }
_ => { _ => {
self.errors.push(ParserError { self.errors.push(ParserError {
@ -861,6 +844,7 @@ impl<'a> Parser<'a> {
return None return None
} }
} }
continue
} }
// Initialize an empty table as part of this sub-key // Initialize an empty table as part of this sub-key
@ -922,11 +906,10 @@ impl<'a> Parser<'a> {
Some(pair) => pair, Some(pair) => pair,
None => return, None => return,
}; };
let key = format!("{}", key); if !into.0.contains_key(key) {
if !into.0.contains_key(&key) { into.0.insert(key.to_owned(), Value::Array(Vec::new()));
into.0.insert(key.clone(), Value::Array(Vec::new()));
} }
match *into.0.get_mut(&key).unwrap() { match *into.0.get_mut(key).unwrap() {
Value::Array(ref mut vec) => { Value::Array(ref mut vec) => {
match vec.first() { match vec.first() {
Some(ref v) if !v.same_type(&value) => { Some(ref v) if !v.same_type(&value) => {