diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-06-06 13:58:28 +0100 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-06-06 13:58:28 +0100 |
commit | ba3148159974b4fa7a2e37ade3a79ba5eeb4e760 (patch) | |
tree | b5b9eb1151c6fdbfcbeeaaf4dfd2b2db015a6266 /crates/ra_syntax | |
parent | b79e6294a68fd41f0a3dbd9eb907dfe99646d77e (diff) | |
parent | 505b8d873ffe422358bb4ff47dc82d6c76d0772e (diff) |
Merge #1380
1380: [#1083] Try block syntax r=matklad a=andreytkachenko
The `try` block syntax
Co-authored-by: Andrey Tkachenko <[email protected]>
Diffstat (limited to 'crates/ra_syntax')
-rw-r--r-- | crates/ra_syntax/src/ast/generated.rs | 37 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/traits.rs | 6 | ||||
-rw-r--r-- | crates/ra_syntax/src/grammar.ron | 6 | ||||
-rw-r--r-- | crates/ra_syntax/tests/data/parser/inline/ok/0130_try_block_expr.rs | 3 | ||||
-rw-r--r-- | crates/ra_syntax/tests/data/parser/inline/ok/0130_try_block_expr.txt | 31 |
5 files changed, 83 insertions, 0 deletions
diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index e73fe22e9..1d888e709 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs | |||
@@ -713,6 +713,7 @@ pub enum ExprKind<'a> { | |||
713 | MethodCallExpr(&'a MethodCallExpr), | 713 | MethodCallExpr(&'a MethodCallExpr), |
714 | FieldExpr(&'a FieldExpr), | 714 | FieldExpr(&'a FieldExpr), |
715 | TryExpr(&'a TryExpr), | 715 | TryExpr(&'a TryExpr), |
716 | TryBlockExpr(&'a TryBlockExpr), | ||
716 | CastExpr(&'a CastExpr), | 717 | CastExpr(&'a CastExpr), |
717 | RefExpr(&'a RefExpr), | 718 | RefExpr(&'a RefExpr), |
718 | PrefixExpr(&'a PrefixExpr), | 719 | PrefixExpr(&'a PrefixExpr), |
@@ -826,6 +827,11 @@ impl<'a> From<&'a TryExpr> for &'a Expr { | |||
826 | Expr::cast(&n.syntax).unwrap() | 827 | Expr::cast(&n.syntax).unwrap() |
827 | } | 828 | } |
828 | } | 829 | } |
830 | impl<'a> From<&'a TryBlockExpr> for &'a Expr { | ||
831 | fn from(n: &'a TryBlockExpr) -> &'a Expr { | ||
832 | Expr::cast(&n.syntax).unwrap() | ||
833 | } | ||
834 | } | ||
829 | impl<'a> From<&'a CastExpr> for &'a Expr { | 835 | impl<'a> From<&'a CastExpr> for &'a Expr { |
830 | fn from(n: &'a CastExpr) -> &'a Expr { | 836 | fn from(n: &'a CastExpr) -> &'a Expr { |
831 | Expr::cast(&n.syntax).unwrap() | 837 | Expr::cast(&n.syntax).unwrap() |
@@ -887,6 +893,7 @@ impl AstNode for Expr { | |||
887 | | METHOD_CALL_EXPR | 893 | | METHOD_CALL_EXPR |
888 | | FIELD_EXPR | 894 | | FIELD_EXPR |
889 | | TRY_EXPR | 895 | | TRY_EXPR |
896 | | TRY_BLOCK_EXPR | ||
890 | | CAST_EXPR | 897 | | CAST_EXPR |
891 | | REF_EXPR | 898 | | REF_EXPR |
892 | | PREFIX_EXPR | 899 | | PREFIX_EXPR |
@@ -929,6 +936,7 @@ impl Expr { | |||
929 | METHOD_CALL_EXPR => ExprKind::MethodCallExpr(MethodCallExpr::cast(&self.syntax).unwrap()), | 936 | METHOD_CALL_EXPR => ExprKind::MethodCallExpr(MethodCallExpr::cast(&self.syntax).unwrap()), |
930 | FIELD_EXPR => ExprKind::FieldExpr(FieldExpr::cast(&self.syntax).unwrap()), | 937 | FIELD_EXPR => ExprKind::FieldExpr(FieldExpr::cast(&self.syntax).unwrap()), |
931 | TRY_EXPR => ExprKind::TryExpr(TryExpr::cast(&self.syntax).unwrap()), | 938 | TRY_EXPR => ExprKind::TryExpr(TryExpr::cast(&self.syntax).unwrap()), |
939 | TRY_BLOCK_EXPR => ExprKind::TryBlockExpr(TryBlockExpr::cast(&self.syntax).unwrap()), | ||
932 | CAST_EXPR => ExprKind::CastExpr(CastExpr::cast(&self.syntax).unwrap()), | 940 | CAST_EXPR => ExprKind::CastExpr(CastExpr::cast(&self.syntax).unwrap()), |
933 | REF_EXPR => ExprKind::RefExpr(RefExpr::cast(&self.syntax).unwrap()), | 941 | REF_EXPR => ExprKind::RefExpr(RefExpr::cast(&self.syntax).unwrap()), |
934 | PREFIX_EXPR => ExprKind::PrefixExpr(PrefixExpr::cast(&self.syntax).unwrap()), | 942 | PREFIX_EXPR => ExprKind::PrefixExpr(PrefixExpr::cast(&self.syntax).unwrap()), |
@@ -3672,6 +3680,35 @@ impl TraitDef { | |||
3672 | } | 3680 | } |
3673 | } | 3681 | } |
3674 | 3682 | ||
3683 | // TryBlockExpr | ||
3684 | #[derive(Debug, PartialEq, Eq, Hash)] | ||
3685 | #[repr(transparent)] | ||
3686 | pub struct TryBlockExpr { | ||
3687 | pub(crate) syntax: SyntaxNode, | ||
3688 | } | ||
3689 | unsafe impl TransparentNewType for TryBlockExpr { | ||
3690 | type Repr = rowan::SyntaxNode; | ||
3691 | } | ||
3692 | |||
3693 | impl AstNode for TryBlockExpr { | ||
3694 | fn cast(syntax: &SyntaxNode) -> Option<&Self> { | ||
3695 | match syntax.kind() { | ||
3696 | TRY_BLOCK_EXPR => Some(TryBlockExpr::from_repr(syntax.into_repr())), | ||
3697 | _ => None, | ||
3698 | } | ||
3699 | } | ||
3700 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
3701 | } | ||
3702 | |||
3703 | impl ToOwned for TryBlockExpr { | ||
3704 | type Owned = TreeArc<TryBlockExpr>; | ||
3705 | fn to_owned(&self) -> TreeArc<TryBlockExpr> { TreeArc::cast(self.syntax.to_owned()) } | ||
3706 | } | ||
3707 | |||
3708 | |||
3709 | impl ast::TryBlockBodyOwner for TryBlockExpr {} | ||
3710 | impl TryBlockExpr {} | ||
3711 | |||
3675 | // TryExpr | 3712 | // TryExpr |
3676 | #[derive(Debug, PartialEq, Eq, Hash)] | 3713 | #[derive(Debug, PartialEq, Eq, Hash)] |
3677 | #[repr(transparent)] | 3714 | #[repr(transparent)] |
diff --git a/crates/ra_syntax/src/ast/traits.rs b/crates/ra_syntax/src/ast/traits.rs index 1c90cf148..433485400 100644 --- a/crates/ra_syntax/src/ast/traits.rs +++ b/crates/ra_syntax/src/ast/traits.rs | |||
@@ -33,6 +33,12 @@ pub trait LoopBodyOwner: AstNode { | |||
33 | } | 33 | } |
34 | } | 34 | } |
35 | 35 | ||
36 | pub trait TryBlockBodyOwner: AstNode { | ||
37 | fn try_body(&self) -> Option<&ast::Block> { | ||
38 | child_opt(self) | ||
39 | } | ||
40 | } | ||
41 | |||
36 | pub trait ArgListOwner: AstNode { | 42 | pub trait ArgListOwner: AstNode { |
37 | fn arg_list(&self) -> Option<&ast::ArgList> { | 43 | fn arg_list(&self) -> Option<&ast::ArgList> { |
38 | child_opt(self) | 44 | child_opt(self) |
diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron index b8665bbc8..1c2714307 100644 --- a/crates/ra_syntax/src/grammar.ron +++ b/crates/ra_syntax/src/grammar.ron | |||
@@ -95,6 +95,7 @@ Grammar( | |||
95 | "let", | 95 | "let", |
96 | "move", | 96 | "move", |
97 | "return", | 97 | "return", |
98 | "try", | ||
98 | ], | 99 | ], |
99 | contextual_keywords: [ | 100 | contextual_keywords: [ |
100 | "auto", | 101 | "auto", |
@@ -189,6 +190,7 @@ Grammar( | |||
189 | "STRUCT_LIT", | 190 | "STRUCT_LIT", |
190 | "NAMED_FIELD_LIST", | 191 | "NAMED_FIELD_LIST", |
191 | "NAMED_FIELD", | 192 | "NAMED_FIELD", |
193 | "TRY_BLOCK_EXPR", | ||
192 | 194 | ||
193 | // postfix | 195 | // postfix |
194 | "CALL_EXPR", | 196 | "CALL_EXPR", |
@@ -417,6 +419,9 @@ Grammar( | |||
417 | "LoopExpr": ( | 419 | "LoopExpr": ( |
418 | traits: ["LoopBodyOwner"], | 420 | traits: ["LoopBodyOwner"], |
419 | ), | 421 | ), |
422 | "TryBlockExpr": ( | ||
423 | traits: ["TryBlockBodyOwner"], | ||
424 | ), | ||
420 | "ForExpr": ( | 425 | "ForExpr": ( |
421 | traits: ["LoopBodyOwner"], | 426 | traits: ["LoopBodyOwner"], |
422 | options: [ | 427 | options: [ |
@@ -499,6 +504,7 @@ Grammar( | |||
499 | "MethodCallExpr", | 504 | "MethodCallExpr", |
500 | "FieldExpr", | 505 | "FieldExpr", |
501 | "TryExpr", | 506 | "TryExpr", |
507 | "TryBlockExpr", | ||
502 | "CastExpr", | 508 | "CastExpr", |
503 | "RefExpr", | 509 | "RefExpr", |
504 | "PrefixExpr", | 510 | "PrefixExpr", |
diff --git a/crates/ra_syntax/tests/data/parser/inline/ok/0130_try_block_expr.rs b/crates/ra_syntax/tests/data/parser/inline/ok/0130_try_block_expr.rs new file mode 100644 index 000000000..0f1b41eb6 --- /dev/null +++ b/crates/ra_syntax/tests/data/parser/inline/ok/0130_try_block_expr.rs | |||
@@ -0,0 +1,3 @@ | |||
1 | fn foo() { | ||
2 | let _ = try {}; | ||
3 | } | ||
diff --git a/crates/ra_syntax/tests/data/parser/inline/ok/0130_try_block_expr.txt b/crates/ra_syntax/tests/data/parser/inline/ok/0130_try_block_expr.txt new file mode 100644 index 000000000..53f49b9b5 --- /dev/null +++ b/crates/ra_syntax/tests/data/parser/inline/ok/0130_try_block_expr.txt | |||
@@ -0,0 +1,31 @@ | |||
1 | SOURCE_FILE@[0; 33) | ||
2 | FN_DEF@[0; 32) | ||
3 | FN_KW@[0; 2) "fn" | ||
4 | WHITESPACE@[2; 3) " " | ||
5 | NAME@[3; 6) | ||
6 | IDENT@[3; 6) "foo" | ||
7 | PARAM_LIST@[6; 8) | ||
8 | L_PAREN@[6; 7) "(" | ||
9 | R_PAREN@[7; 8) ")" | ||
10 | WHITESPACE@[8; 9) " " | ||
11 | BLOCK@[9; 32) | ||
12 | L_CURLY@[9; 10) "{" | ||
13 | WHITESPACE@[10; 15) "\n " | ||
14 | LET_STMT@[15; 30) | ||
15 | LET_KW@[15; 18) "let" | ||
16 | WHITESPACE@[18; 19) " " | ||
17 | PLACEHOLDER_PAT@[19; 20) | ||
18 | UNDERSCORE@[19; 20) "_" | ||
19 | WHITESPACE@[20; 21) " " | ||
20 | EQ@[21; 22) "=" | ||
21 | WHITESPACE@[22; 23) " " | ||
22 | TRY_EXPR@[23; 29) | ||
23 | TRY_KW@[23; 26) "try" | ||
24 | WHITESPACE@[26; 27) " " | ||
25 | BLOCK@[27; 29) | ||
26 | L_CURLY@[27; 28) "{" | ||
27 | R_CURLY@[28; 29) "}" | ||
28 | SEMI@[29; 30) ";" | ||
29 | WHITESPACE@[30; 31) "\n" | ||
30 | R_CURLY@[31; 32) "}" | ||
31 | WHITESPACE@[32; 33) "\n" | ||