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 ++++++ 3 files changed, 49 insertions(+) (limited to 'crates/ra_syntax/src') 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", -- cgit v1.2.3