diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2015-01-27 22:52:32 -0800 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2015-01-30 08:52:44 -0800 |
| commit | 0cdde6e5e015ee6f6d9381ab624a312af7c9b069 (patch) | |
| tree | af04df6cb8bb0ce50036333bae380602d42fabe2 /src/libsyntax/parse | |
| parent | 1a51eb9cca3ae5f815825096de4dfbdc9267f735 (diff) | |
| download | rust-0cdde6e5e015ee6f6d9381ab624a312af7c9b069.tar.gz rust-0cdde6e5e015ee6f6d9381ab624a312af7c9b069.zip | |
std: Stabilize FromStr and parse
This commits adds an associated type to the `FromStr` trait representing an error payload for parses which do not succeed. The previous return value, `Option<Self>` did not allow for this form of payload. After the associated type was added, the following attributes were applied: * `FromStr` is now stable * `FromStr::Err` is now stable * `FromStr::from_str` is now stable * `StrExt::parse` is now stable * `FromStr for bool` is now stable * `FromStr for $float` is now stable * `FromStr for $integral` is now stable * Errors returned from stable `FromStr` implementations are stable * Errors implement `Display` and `Error` (both impl blocks being `#[stable]`) Closes #15138
Diffstat (limited to 'src/libsyntax/parse')
| -rw-r--r-- | src/libsyntax/parse/lexer/mod.rs | 4 | ||||
| -rw-r--r-- | src/libsyntax/parse/mod.rs | 8 | ||||
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 4 |
3 files changed, 8 insertions, 8 deletions
diff --git a/src/libsyntax/parse/lexer/mod.rs b/src/libsyntax/parse/lexer/mod.rs index 493a97c24cf..2cf6058a433 100644 --- a/src/libsyntax/parse/lexer/mod.rs +++ b/src/libsyntax/parse/lexer/mod.rs @@ -613,7 +613,7 @@ impl<'a> StringReader<'a> { // find the integer representing the name self.scan_digits(base); let encoded_name : u32 = self.with_str_from(start_bpos, |s| { - num::from_str_radix(s, 10).unwrap_or_else(|| { + num::from_str_radix(s, 10).ok().unwrap_or_else(|| { panic!("expected digits representing a name, got {:?}, {}, range [{:?},{:?}]", s, whence, start_bpos, self.last_pos); }) @@ -631,7 +631,7 @@ impl<'a> StringReader<'a> { let start_bpos = self.last_pos; self.scan_digits(base); let encoded_ctxt : ast::SyntaxContext = self.with_str_from(start_bpos, |s| { - num::from_str_radix(s, 10).unwrap_or_else(|| { + num::from_str_radix(s, 10).ok().unwrap_or_else(|| { panic!("expected digits representing a ctxt, got {:?}, {}", s, whence); }) }); diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index 1c146968fd5..e7be876edbb 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -401,7 +401,7 @@ pub fn char_lit(lit: &str) -> (char, isize) { let msg2 = &msg[]; fn esc(len: usize, lit: &str) -> Option<(char, isize)> { - num::from_str_radix(&lit[2..len], 16) + num::from_str_radix(&lit[2..len], 16).ok() .and_then(char::from_u32) .map(|x| (x, len as isize)) } @@ -410,7 +410,7 @@ pub fn char_lit(lit: &str) -> (char, isize) { if lit.as_bytes()[2] == b'{' { let idx = lit.find('}').expect(msg2); let subslice = &lit[3..idx]; - num::from_str_radix(subslice, 16) + num::from_str_radix(subslice, 16).ok() .and_then(char::from_u32) .map(|x| (x, subslice.chars().count() as isize + 4)) } else { @@ -583,7 +583,7 @@ pub fn byte_lit(lit: &str) -> (u8, usize) { b'\'' => b'\'', b'0' => b'\0', _ => { - match ::std::num::from_str_radix::<u64>(&lit[2..4], 16) { + match ::std::num::from_str_radix::<u64>(&lit[2..4], 16).ok() { Some(c) => if c > 0xFF { panic!(err(2)) @@ -732,7 +732,7 @@ pub fn integer_lit(s: &str, suffix: Option<&str>, sd: &SpanHandler, sp: Span) -> debug!("integer_lit: the type is {:?}, base {:?}, the new string is {:?}, the original \ string was {:?}, the original suffix was {:?}", ty, base, s, orig, suffix); - let res: u64 = match ::std::num::from_str_radix(s, base) { + let res: u64 = match ::std::num::from_str_radix(s, base).ok() { Some(r) => r, None => { sd.span_err(sp, "int literal is too large"); 0 } }; diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index c4224db8e18..d99095eeba3 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2459,7 +2459,7 @@ impl<'a> Parser<'a> { hi = self.span.hi; self.bump(); - let index = n.as_str().parse::<usize>(); + let index = n.as_str().parse::<usize>().ok(); match index { Some(n) => { let id = spanned(dot, hi, n); @@ -2479,7 +2479,7 @@ impl<'a> Parser<'a> { self.span_err(last_span, &format!("unexpected token: `{}`", n.as_str())[]); if fstr.chars().all(|x| "0123456789.".contains_char(x)) { - let float = match fstr.parse::<f64>() { + let float = match fstr.parse::<f64>().ok() { Some(f) => f, None => continue, }; |
