about summary refs log tree commit diff
path: root/src/libsyntax/parse/attr.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2015-04-06 22:08:01 +0000
committerbors <bors@rust-lang.org>2015-04-06 22:08:01 +0000
commitb49a5ef003fedcbb0d78aebda62ba30dfdd17a20 (patch)
treed99b438e04f810e098c79b634ed6d730d2dbcb4a /src/libsyntax/parse/attr.rs
parentaab8669ddad0432ef7279cc7f7b0b20d32785314 (diff)
parente3427c3c341fcd15cbac783bf8dad7276422c97a (diff)
downloadrust-b49a5ef003fedcbb0d78aebda62ba30dfdd17a20.tar.gz
rust-b49a5ef003fedcbb0d78aebda62ba30dfdd17a20.zip
Auto merge of #23857 - phildawes:libsyntax_nopanic, r=nikomatsakis
Hello! 

I've been working towards a libsyntax without panics. See:
http://internals.rust-lang.org/t/changing-libsyntax-to-use-result-instead-of-panic/1670

This patch changes the internals of parser.rs to use Result<> rather than panicing. It keeps the following old-style panicing functions as a facade:
parse_expr, parse_item, parse_pat, parse_arm, parse_ty, parse_stmt

I left these functions because I wasn't sure what to do about the quote_* macros or how many syntax-extensions would break if these and quoting macros returned Result.

The gyst of the rest of the patch is:

 - Functions in parse/parser.rs return PResult<> rather than panicing
 - Other functions in libsyntax call panic! explicitly if they rely on panicing behaviour.
 - I added a macro 'panictry!()' to act as scaffolding for callers while converting panicing functions. (This does the same as 'unwrap()' but is easier to grep for and turn into try!()).

Am I on the right track?  I'd quite like to get something merged soon as keeping this rebased in the face of libsyntax changes is a lot of work. Please let me know what changes you'd like to see to make this happen.

Thanks!, Phil
Diffstat (limited to 'src/libsyntax/parse/attr.rs')
-rw-r--r--src/libsyntax/parse/attr.rs30
1 files changed, 15 insertions, 15 deletions
diff --git a/src/libsyntax/parse/attr.rs b/src/libsyntax/parse/attr.rs
index a5dd4f22224..18588c59357 100644
--- a/src/libsyntax/parse/attr.rs
+++ b/src/libsyntax/parse/attr.rs
@@ -45,10 +45,10 @@ impl<'a> ParserAttr for Parser<'a> {
                     self.span.hi
                 );
                 if attr.node.style != ast::AttrOuter {
-                  self.fatal("expected outer comment");
+                  panic!(self.fatal("expected outer comment"));
                 }
                 attrs.push(attr);
-                self.bump();
+                panictry!(self.bump());
               }
               _ => break
             }
@@ -66,11 +66,11 @@ impl<'a> ParserAttr for Parser<'a> {
         let (span, value, mut style) = match self.token {
             token::Pound => {
                 let lo = self.span.lo;
-                self.bump();
+                panictry!(self.bump());
 
                 if permit_inner { self.expected_tokens.push(TokenType::Token(token::Not)); }
                 let style = if self.token == token::Not {
-                    self.bump();
+                    panictry!(self.bump());
                     if !permit_inner {
                         let span = self.span;
                         self.span_err(span,
@@ -84,21 +84,21 @@ impl<'a> ParserAttr for Parser<'a> {
                     ast::AttrOuter
                 };
 
-                self.expect(&token::OpenDelim(token::Bracket));
+                panictry!(self.expect(&token::OpenDelim(token::Bracket)));
                 let meta_item = self.parse_meta_item();
                 let hi = self.span.hi;
-                self.expect(&token::CloseDelim(token::Bracket));
+                panictry!(self.expect(&token::CloseDelim(token::Bracket)));
 
                 (mk_sp(lo, hi), meta_item, style)
             }
             _ => {
                 let token_str = self.this_token_to_string();
-                self.fatal(&format!("expected `#`, found `{}`", token_str));
+                panic!(self.fatal(&format!("expected `#`, found `{}`", token_str)));
             }
         };
 
         if permit_inner && self.token == token::Semi {
-            self.bump();
+            panictry!(self.bump());
             self.span_warn(span, "this inner attribute syntax is deprecated. \
                            The new syntax is `#![foo]`, with a bang and no semicolon");
             style = ast::AttrInner;
@@ -142,7 +142,7 @@ impl<'a> ParserAttr for Parser<'a> {
                                                          lo, hi);
                     if attr.node.style == ast::AttrInner {
                         attrs.push(attr);
-                        self.bump();
+                        panictry!(self.bump());
                     } else {
                         break;
                     }
@@ -166,19 +166,19 @@ impl<'a> ParserAttr for Parser<'a> {
 
         match nt_meta {
             Some(meta) => {
-                self.bump();
+                panictry!(self.bump());
                 return meta;
             }
             None => {}
         }
 
         let lo = self.span.lo;
-        let ident = self.parse_ident();
+        let ident = panictry!(self.parse_ident());
         let name = self.id_to_interned_str(ident);
         match self.token {
             token::Eq => {
-                self.bump();
-                let lit = self.parse_lit();
+                panictry!(self.bump());
+                let lit = panictry!(self.parse_lit());
                 // FIXME #623 Non-string meta items are not serialized correctly;
                 // just forbid them for now
                 match lit.node {
@@ -206,10 +206,10 @@ impl<'a> ParserAttr for Parser<'a> {
 
     /// matches meta_seq = ( COMMASEP(meta_item) )
     fn parse_meta_seq(&mut self) -> Vec<P<ast::MetaItem>> {
-        self.parse_seq(&token::OpenDelim(token::Paren),
+        panictry!(self.parse_seq(&token::OpenDelim(token::Paren),
                        &token::CloseDelim(token::Paren),
                        seq_sep_trailing_allowed(token::Comma),
-                       |p| p.parse_meta_item()).node
+                       |p| Ok(p.parse_meta_item()))).node
     }
 
     fn parse_optional_meta(&mut self) -> Vec<P<ast::MetaItem>> {