From b2bcb7229a4bce0c9459807552d071eb2b2c9a0e Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Sat, 28 Mar 2015 21:58:51 +0000 Subject: Work towards a non-panicing parser (libsyntax) - Functions in parser.rs return PResult<> rather than panicing - Other functions in libsyntax call panic! explicitly for now if they rely on panicing behaviour. - 'panictry!' macro added as scaffolding while converting panicing functions. (This does the same as 'unwrap()' but is easier to grep for and turn into try!()) - Leaves panicing wrappers for the following functions so that the quote_* macros behave the same: - parse_expr, parse_item, parse_pat, parse_arm, parse_ty, parse_stmt --- src/libsyntax/ext/format.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'src/libsyntax/ext/format.rs') diff --git a/src/libsyntax/ext/format.rs b/src/libsyntax/ext/format.rs index 2fe77bf7a54..fe3a05cfc44 100644 --- a/src/libsyntax/ext/format.rs +++ b/src/libsyntax/ext/format.rs @@ -24,6 +24,17 @@ use ptr::P; use std::collections::HashMap; use std::iter::repeat; +macro_rules! panictry { + ($e:expr) => ({ + use std::result::Result::{Ok, Err}; + + match $e { + Ok(e) => e, + Err(e) => { panic!(e); } + } + }) +} + #[derive(PartialEq)] enum ArgumentType { Known(String), @@ -92,7 +103,7 @@ fn parse_args(ecx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) let fmtstr = p.parse_expr(); let mut named = false; while p.token != token::Eof { - if !p.eat(&token::Comma) { + if !panictry!(p.eat(&token::Comma)) { ecx.span_err(sp, "expected token: `,`"); return None; } @@ -101,7 +112,7 @@ fn parse_args(ecx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) named = true; let ident = match p.token { token::Ident(i, _) => { - p.bump(); + panictry!(p.bump()); i } _ if named => { @@ -120,7 +131,7 @@ fn parse_args(ecx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) let interned_name = token::get_ident(ident); let name = &interned_name[..]; - p.expect(&token::Eq); + panictry!(p.expect(&token::Eq)); let e = p.parse_expr(); match names.get(name) { None => {} -- cgit 1.4.1-3-g733a5 From e3427c3c341fcd15cbac783bf8dad7276422c97a Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Sat, 4 Apr 2015 21:47:40 +0100 Subject: Add comments suggested by Niko --- src/libsyntax/diagnostic.rs | 6 +++--- src/libsyntax/ext/asm.rs | 11 ----------- src/libsyntax/ext/format.rs | 11 ----------- src/libsyntax/lib.rs | 8 ++++++-- 4 files changed, 9 insertions(+), 27 deletions(-) (limited to 'src/libsyntax/ext/format.rs') diff --git a/src/libsyntax/diagnostic.rs b/src/libsyntax/diagnostic.rs index c746445e77e..8fe23a3c8e8 100644 --- a/src/libsyntax/diagnostic.rs +++ b/src/libsyntax/diagnostic.rs @@ -68,9 +68,9 @@ pub trait Emitter { sp: RenderSpan, msg: &str, lvl: Level); } -/// This structure is used to signify that a task has panicked with a fatal error -/// from the diagnostics. You can use this with the `Any` trait to figure out -/// how a rustc task died (if so desired). +/// Used as a return value to signify a fatal error occurred. (It is also +/// used as the argument to panic at the moment, but that will eventually +/// not be true.) #[derive(Copy, Clone)] #[must_use] pub struct FatalError; diff --git a/src/libsyntax/ext/asm.rs b/src/libsyntax/ext/asm.rs index 3513f6d2bb9..f2b45d89f73 100644 --- a/src/libsyntax/ext/asm.rs +++ b/src/libsyntax/ext/asm.rs @@ -23,17 +23,6 @@ use parse::token::InternedString; use parse::token; use ptr::P; -macro_rules! panictry { - ($e:expr) => ({ - use std::result::Result::{Ok, Err}; - - match $e { - Ok(e) => e, - Err(e) => panic!(e), - } - }) -} - enum State { Asm, Outputs, diff --git a/src/libsyntax/ext/format.rs b/src/libsyntax/ext/format.rs index fe3a05cfc44..1d99a475b32 100644 --- a/src/libsyntax/ext/format.rs +++ b/src/libsyntax/ext/format.rs @@ -24,17 +24,6 @@ use ptr::P; use std::collections::HashMap; use std::iter::repeat; -macro_rules! panictry { - ($e:expr) => ({ - use std::result::Result::{Ok, Err}; - - match $e { - Ok(e) => e, - Err(e) => { panic!(e); } - } - }) -} - #[derive(PartialEq)] enum ArgumentType { Known(String), diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index a8b6dae06b9..bf95daf8755 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -50,13 +50,17 @@ extern crate libc; extern crate serialize as rustc_serialize; // used by deriving +// A variant of 'try!' that panics on Err(FatalError). This is used as a +// crutch on the way towards a non-panic!-prone parser. It should be used +// for fatal parsing errors; eventually we plan to convert all code using +// panictry to just use normal try macro_rules! panictry { ($e:expr) => ({ use std::result::Result::{Ok, Err}; - + use diagnostic::FatalError; match $e { Ok(e) => e, - Err(e) => panic!(e) + Err(FatalError) => panic!(FatalError) } }) } -- cgit 1.4.1-3-g733a5