about summary refs log tree commit diff
path: root/src/libsyntax/parse
diff options
context:
space:
mode:
authorJorge Aparicio <japaricious@gmail.com>2016-02-28 17:38:48 -0500
committerJorge Aparicio <japaricious@gmail.com>2016-03-07 14:39:39 -0500
commit210dd611aa1bd80ed2f4e663b3c4b87b3cea069a (patch)
treead6c6e804d47524cce05caaf60d22ac8d62d2935 /src/libsyntax/parse
parentc116ae35cf49b55bd8d82e31f1ba030cf7e63867 (diff)
downloadrust-210dd611aa1bd80ed2f4e663b3c4b87b3cea069a.tar.gz
rust-210dd611aa1bd80ed2f4e663b3c4b87b3cea069a.zip
implement the `?` operator
The `?` postfix operator is sugar equivalent to the try! macro, but is more amenable to chaining:
`File::open("foo")?.metadata()?.is_dir()`.

`?` is accepted on any *expression* that can return a `Result`, e.g. `x()?`, `y!()?`, `{z}?`,
`(w)?`, etc. And binds more tightly than unary operators, e.g. `!x?` is parsed as `!(x?)`.

cc #31436
Diffstat (limited to 'src/libsyntax/parse')
-rw-r--r--src/libsyntax/parse/parser.rs7
1 files changed, 6 insertions, 1 deletions
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index d9714cc1e25..53b53415429 100644
--- a/src/libsyntax/parse/parser.rs
+++ b/src/libsyntax/parse/parser.rs
@@ -2534,6 +2534,12 @@ impl<'a> Parser<'a> {
         let mut e = e0;
         let mut hi;
         loop {
+            // expr?
+            while self.eat(&token::Question) {
+                let hi = self.span.hi;
+                e = self.mk_expr(lo, hi, ExprKind::Try(e), None);
+            }
+
             // expr.f
             if self.eat(&token::Dot) {
                 match self.token {
@@ -2907,7 +2913,6 @@ impl<'a> Parser<'a> {
             }
         };
 
-
         if self.expr_is_complete(&lhs) {
             // Semi-statement forms are odd. See https://github.com/rust-lang/rust/issues/29071
             return Ok(lhs);