From 281c9eeaff8eac4e666089f80f67cf684e1d001b Mon Sep 17 00:00:00 2001 From: Andrey Tkachenko Date: Thu, 6 Jun 2019 15:36:16 +0400 Subject: [#1083] Try block syntax --- crates/ra_syntax/src/ast/generated.rs | 37 ++++++++++++++++++++++ crates/ra_syntax/src/ast/traits.rs | 6 ++++ crates/ra_syntax/src/grammar.ron | 6 ++++ .../tests/data/parser/ok/0051_try_block.rs | 5 +++ .../tests/data/parser/ok/0051_try_block.txt | 32 +++++++++++++++++++ 5 files changed, 86 insertions(+) create mode 100644 crates/ra_syntax/tests/data/parser/ok/0051_try_block.rs create mode 100644 crates/ra_syntax/tests/data/parser/ok/0051_try_block.txt (limited to 'crates/ra_syntax') 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> { MethodCallExpr(&'a MethodCallExpr), FieldExpr(&'a FieldExpr), TryExpr(&'a TryExpr), + TryBlockExpr(&'a TryBlockExpr), CastExpr(&'a CastExpr), RefExpr(&'a RefExpr), PrefixExpr(&'a PrefixExpr), @@ -826,6 +827,11 @@ impl<'a> From<&'a TryExpr> for &'a Expr { Expr::cast(&n.syntax).unwrap() } } +impl<'a> From<&'a TryBlockExpr> for &'a Expr { + fn from(n: &'a TryBlockExpr) -> &'a Expr { + Expr::cast(&n.syntax).unwrap() + } +} impl<'a> From<&'a CastExpr> for &'a Expr { fn from(n: &'a CastExpr) -> &'a Expr { Expr::cast(&n.syntax).unwrap() @@ -887,6 +893,7 @@ impl AstNode for Expr { | METHOD_CALL_EXPR | FIELD_EXPR | TRY_EXPR + | TRY_BLOCK_EXPR | CAST_EXPR | REF_EXPR | PREFIX_EXPR @@ -929,6 +936,7 @@ impl Expr { METHOD_CALL_EXPR => ExprKind::MethodCallExpr(MethodCallExpr::cast(&self.syntax).unwrap()), FIELD_EXPR => ExprKind::FieldExpr(FieldExpr::cast(&self.syntax).unwrap()), TRY_EXPR => ExprKind::TryExpr(TryExpr::cast(&self.syntax).unwrap()), + TRY_BLOCK_EXPR => ExprKind::TryBlockExpr(TryBlockExpr::cast(&self.syntax).unwrap()), CAST_EXPR => ExprKind::CastExpr(CastExpr::cast(&self.syntax).unwrap()), REF_EXPR => ExprKind::RefExpr(RefExpr::cast(&self.syntax).unwrap()), PREFIX_EXPR => ExprKind::PrefixExpr(PrefixExpr::cast(&self.syntax).unwrap()), @@ -3672,6 +3680,35 @@ impl TraitDef { } } +// TryBlockExpr +#[derive(Debug, PartialEq, Eq, Hash)] +#[repr(transparent)] +pub struct TryBlockExpr { + pub(crate) syntax: SyntaxNode, +} +unsafe impl TransparentNewType for TryBlockExpr { + type Repr = rowan::SyntaxNode; +} + +impl AstNode for TryBlockExpr { + fn cast(syntax: &SyntaxNode) -> Option<&Self> { + match syntax.kind() { + TRY_BLOCK_EXPR => Some(TryBlockExpr::from_repr(syntax.into_repr())), + _ => None, + } + } + fn syntax(&self) -> &SyntaxNode { &self.syntax } +} + +impl ToOwned for TryBlockExpr { + type Owned = TreeArc; + fn to_owned(&self) -> TreeArc { TreeArc::cast(self.syntax.to_owned()) } +} + + +impl ast::TryBlockBodyOwner for TryBlockExpr {} +impl TryBlockExpr {} + // TryExpr #[derive(Debug, PartialEq, Eq, Hash)] #[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 { } } +pub trait TryBlockBodyOwner: AstNode { + fn try_body(&self) -> Option<&ast::Block> { + child_opt(self) + } +} + pub trait ArgListOwner: AstNode { fn arg_list(&self) -> Option<&ast::ArgList> { 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( "let", "move", "return", + "try", ], contextual_keywords: [ "auto", @@ -189,6 +190,7 @@ Grammar( "STRUCT_LIT", "NAMED_FIELD_LIST", "NAMED_FIELD", + "TRY_BLOCK_EXPR", // postfix "CALL_EXPR", @@ -417,6 +419,9 @@ Grammar( "LoopExpr": ( traits: ["LoopBodyOwner"], ), + "TryBlockExpr": ( + traits: ["TryBlockBodyOwner"], + ), "ForExpr": ( traits: ["LoopBodyOwner"], options: [ @@ -499,6 +504,7 @@ Grammar( "MethodCallExpr", "FieldExpr", "TryExpr", + "TryBlockExpr", "CastExpr", "RefExpr", "PrefixExpr", diff --git a/crates/ra_syntax/tests/data/parser/ok/0051_try_block.rs b/crates/ra_syntax/tests/data/parser/ok/0051_try_block.rs new file mode 100644 index 000000000..8cc11ddcc --- /dev/null +++ b/crates/ra_syntax/tests/data/parser/ok/0051_try_block.rs @@ -0,0 +1,5 @@ +fn main() { + let res = try { + + }; +} diff --git a/crates/ra_syntax/tests/data/parser/ok/0051_try_block.txt b/crates/ra_syntax/tests/data/parser/ok/0051_try_block.txt new file mode 100644 index 000000000..f94eb3bcd --- /dev/null +++ b/crates/ra_syntax/tests/data/parser/ok/0051_try_block.txt @@ -0,0 +1,32 @@ +SOURCE_FILE@[0; 41) + FN_DEF@[0; 41) + FN_KW@[0; 2) "fn" + WHITESPACE@[2; 3) " " + NAME@[3; 7) + IDENT@[3; 7) "main" + PARAM_LIST@[7; 9) + L_PAREN@[7; 8) "(" + R_PAREN@[8; 9) ")" + WHITESPACE@[9; 10) " " + BLOCK@[10; 41) + L_CURLY@[10; 11) "{" + WHITESPACE@[11; 16) "\n " + LET_STMT@[16; 39) + LET_KW@[16; 19) "let" + WHITESPACE@[19; 20) " " + BIND_PAT@[20; 23) + NAME@[20; 23) + IDENT@[20; 23) "res" + WHITESPACE@[23; 24) " " + EQ@[24; 25) "=" + WHITESPACE@[25; 26) " " + TRY_EXPR@[26; 38) + TRY_KW@[26; 29) "try" + WHITESPACE@[29; 30) " " + BLOCK@[30; 38) + L_CURLY@[30; 31) "{" + WHITESPACE@[31; 37) "\n\n " + R_CURLY@[37; 38) "}" + SEMI@[38; 39) ";" + WHITESPACE@[39; 40) "\n" + R_CURLY@[40; 41) "}" \ No newline at end of file -- cgit v1.2.3