Repository: rust-analyzer/rust-analyzer
Pull request #1558 (2019-07-20 08:46:37)

Support .await syntax
- [x] Add `await` keyword to grammar, parser for await_expr
- [ ] Infer type for await_expr


Patch:
diff --git a/crates/ra_parser/src/grammar/expressions.rs b/crates/ra_parser/src/grammar/expressions.rs
index 298030cb99..3e49e70c72 100644
--- a/crates/ra_parser/src/grammar/expressions.rs
+++ b/crates/ra_parser/src/grammar/expressions.rs
@@ -393,6 +393,18 @@ fn postfix_expr(
             T![.] if p.nth(1) == IDENT && (p.nth(2) == T!['('] || p.nth(2) == T![::]) => {
                 method_call_expr(p, lhs)
             }
+            T![.] if p.nth(1) == AWAIT_KW => {
+                // test await_expr
+                // fn foo() {
+                //     x.await;
+                //     x.0.await;
+                //     x.0().await?.hello();
+                // }
+                let m = lhs.precede(p);
+                p.bump();
+                p.bump();
+                m.complete(p, AWAIT_EXPR)
+            }
             T![.] => field_expr(p, lhs),
             // test postfix_range
             // fn foo() { let x = 1..; }
diff --git a/crates/ra_parser/src/syntax_kind/generated.rs b/crates/ra_parser/src/syntax_kind/generated.rs
index 374fd5aec5..4b5abb516c 100644
--- a/crates/ra_parser/src/syntax_kind/generated.rs
+++ b/crates/ra_parser/src/syntax_kind/generated.rs
@@ -105,6 +105,7 @@ pub enum SyntaxKind {
     RETURN_KW,
     TRY_KW,
     BOX_KW,
+    AWAIT_KW,
     AUTO_KW,
     DEFAULT_KW,
     EXISTENTIAL_KW,
@@ -193,6 +194,7 @@ pub enum SyntaxKind {
     INDEX_EXPR,
     METHOD_CALL_EXPR,
     FIELD_EXPR,
+    AWAIT_EXPR,
     TRY_EXPR,
     CAST_EXPR,
     REF_EXPR,
@@ -338,6 +340,7 @@ macro_rules! T {
     (return) => { $crate::SyntaxKind::RETURN_KW };
     (try) => { $crate::SyntaxKind::TRY_KW };
     (box) => { $crate::SyntaxKind::BOX_KW };
+    (await) => { $crate::SyntaxKind::AWAIT_KW };
     (auto) => { $crate::SyntaxKind::AUTO_KW };
     (default) => { $crate::SyntaxKind::DEFAULT_KW };
     (existential) => { $crate::SyntaxKind::EXISTENTIAL_KW };
@@ -398,6 +401,7 @@ impl SyntaxKind {
             | RETURN_KW
             | TRY_KW
             | BOX_KW
+            | AWAIT_KW
             | AUTO_KW
             | DEFAULT_KW
             | EXISTENTIAL_KW
@@ -572,6 +576,7 @@ impl SyntaxKind {
             RETURN_KW => &SyntaxInfo { name: "RETURN_KW" },
             TRY_KW => &SyntaxInfo { name: "TRY_KW" },
             BOX_KW => &SyntaxInfo { name: "BOX_KW" },
+            AWAIT_KW => &SyntaxInfo { name: "AWAIT_KW" },
             AUTO_KW => &SyntaxInfo { name: "AUTO_KW" },
             DEFAULT_KW => &SyntaxInfo { name: "DEFAULT_KW" },
             EXISTENTIAL_KW => &SyntaxInfo { name: "EXISTENTIAL_KW" },
@@ -660,6 +665,7 @@ impl SyntaxKind {
             INDEX_EXPR => &SyntaxInfo { name: "INDEX_EXPR" },
             METHOD_CALL_EXPR => &SyntaxInfo { name: "METHOD_CALL_EXPR" },
             FIELD_EXPR => &SyntaxInfo { name: "FIELD_EXPR" },
+            AWAIT_EXPR => &SyntaxInfo { name: "AWAIT_EXPR" },
             TRY_EXPR => &SyntaxInfo { name: "TRY_EXPR" },
             CAST_EXPR => &SyntaxInfo { name: "CAST_EXPR" },
             REF_EXPR => &SyntaxInfo { name: "REF_EXPR" },
@@ -752,6 +758,7 @@ impl SyntaxKind {
             "return" => RETURN_KW,
             "try" => TRY_KW,
             "box" => BOX_KW,
+            "await" => AWAIT_KW,
             _ => return None,
         };
         Some(kw)
diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron
index 6c075a700f..55494239e4 100644
--- a/crates/ra_syntax/src/grammar.ron
+++ b/crates/ra_syntax/src/grammar.ron
@@ -97,6 +97,7 @@ Grammar(
         "return",
         "try",
         "box",
+        "await"
     ],
     contextual_keywords: [
         "auto",
@@ -200,6 +201,7 @@ Grammar(
         "INDEX_EXPR",
         "METHOD_CALL_EXPR",
         "FIELD_EXPR",
+        "AWAIT_EXPR",
         "TRY_EXPR",
         "CAST_EXPR",
 


Test patch:
diff --git a/crates/ra_syntax/tests/data/parser/inline/ok/0137_await_expr.rs b/crates/ra_syntax/tests/data/parser/inline/ok/0137_await_expr.rs
new file mode 100644
index 0000000000..d2ba89ca60
--- /dev/null
+++ b/crates/ra_syntax/tests/data/parser/inline/ok/0137_await_expr.rs
@@ -0,0 +1,5 @@
+fn foo() {
+    x.await;
+    x.0.await;
+    x.0().await?.hello();
+}
diff --git a/crates/ra_syntax/tests/data/parser/inline/ok/0137_await_expr.txt b/crates/ra_syntax/tests/data/parser/inline/ok/0137_await_expr.txt
new file mode 100644
index 0000000000..99bd76ace3
--- /dev/null
+++ b/crates/ra_syntax/tests/data/parser/inline/ok/0137_await_expr.txt
@@ -0,0 +1,67 @@
+SOURCE_FILE@[0; 67)
+  FN_DEF@[0; 66)
+    FN_KW@[0; 2) "fn"
+    WHITESPACE@[2; 3) " "
+    NAME@[3; 6)
+      IDENT@[3; 6) "foo"
+    PARAM_LIST@[6; 8)
+      L_PAREN@[6; 7) "("
+      R_PAREN@[7; 8) ")"
+    WHITESPACE@[8; 9) " "
+    BLOCK@[9; 66)
+      L_CURLY@[9; 10) "{"
+      WHITESPACE@[10; 15) "\n    "
+      EXPR_STMT@[15; 23)
+        AWAIT_EXPR@[15; 22)
+          PATH_EXPR@[15; 16)
+            PATH@[15; 16)
+              PATH_SEGMENT@[15; 16)
+                NAME_REF@[15; 16)
+                  IDENT@[15; 16) "x"
+          DOT@[16; 17) "."
+          AWAIT_KW@[17; 22) "await"
+        SEMI@[22; 23) ";"
+      WHITESPACE@[23; 28) "\n    "
+      EXPR_STMT@[28; 38)
+        AWAIT_EXPR@[28; 37)
+          FIELD_EXPR@[28; 31)
+            PATH_EXPR@[28; 29)
+              PATH@[28; 29)
+                PATH_SEGMENT@[28; 29)
+                  NAME_REF@[28; 29)
+                    IDENT@[28; 29) "x"
+            DOT@[29; 30) "."
+            INT_NUMBER@[30; 31) "0"
+          DOT@[31; 32) "."
+          AWAIT_KW@[32; 37) "await"
+        SEMI@[37; 38) ";"
+      WHITESPACE@[38; 43) "\n    "
+      EXPR_STMT@[43; 64)
+        METHOD_CALL_EXPR@[43; 63)
+          TRY_EXPR@[43; 55)
+            AWAIT_EXPR@[43; 54)
+              CALL_EXPR@[43; 48)
+                FIELD_EXPR@[43; 46)
+                  PATH_EXPR@[43; 44)
+                    PATH@[43; 44)
+                      PATH_SEGMENT@[43; 44)
+                        NAME_REF@[43; 44)
+                          IDENT@[43; 44) "x"
+                  DOT@[44; 45) "."
+                  INT_NUMBER@[45; 46) "0"
+                ARG_LIST@[46; 48)
+                  L_PAREN@[46; 47) "("
+                  R_PAREN@[47; 48) ")"
+              DOT@[48; 49) "."
+              AWAIT_KW@[49; 54) "await"
+            QUESTION@[54; 55) "?"
+          DOT@[55; 56) "."
+          NAME_REF@[56; 61)
+            IDENT@[56; 61) "hello"
+          ARG_LIST@[61; 63)
+            L_PAREN@[61; 62) "("
+            R_PAREN@[62; 63) ")"
+        SEMI@[63; 64) ";"
+      WHITESPACE@[64; 65) "\n"
+      R_CURLY@[65; 66) "}"
+  WHITESPACE@[66; 67) "\n"