From 85cd3524e28443836658615fe40599bf10a96943 Mon Sep 17 00:00:00 2001 From: Daiki Ihara Date: Thu, 14 Jan 2021 00:01:50 +0900 Subject: Add support for yiled keyword --- crates/syntax/src/ast/generated/nodes.rs | 34 +++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) (limited to 'crates/syntax') diff --git a/crates/syntax/src/ast/generated/nodes.rs b/crates/syntax/src/ast/generated/nodes.rs index 92ed2ee9d..9c96d3d07 100644 --- a/crates/syntax/src/ast/generated/nodes.rs +++ b/crates/syntax/src/ast/generated/nodes.rs @@ -931,6 +931,15 @@ impl WhileExpr { pub fn condition(&self) -> Option { support::child(&self.syntax) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct YieldExpr { + pub(crate) syntax: SyntaxNode, +} +impl ast::AttrsOwner for YieldExpr {} +impl YieldExpr { + pub fn yield_token(&self) -> Option { support::token(&self.syntax, T![yield]) } + pub fn expr(&self) -> Option { support::child(&self.syntax) } +} +#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Label { pub(crate) syntax: SyntaxNode, } @@ -1334,6 +1343,7 @@ pub enum Expr { TryExpr(TryExpr), TupleExpr(TupleExpr), WhileExpr(WhileExpr), + YieldExpr(YieldExpr), } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum Item { @@ -2386,6 +2396,17 @@ impl AstNode for WhileExpr { } fn syntax(&self) -> &SyntaxNode { &self.syntax } } +impl AstNode for YieldExpr { + fn can_cast(kind: SyntaxKind) -> bool { kind == YIELD_EXPR } + fn cast(syntax: SyntaxNode) -> Option { + if Self::can_cast(syntax.kind()) { + Some(Self { syntax }) + } else { + None + } + } + fn syntax(&self) -> &SyntaxNode { &self.syntax } +} impl AstNode for Label { fn can_cast(kind: SyntaxKind) -> bool { kind == LABEL } fn cast(syntax: SyntaxNode) -> Option { @@ -3028,6 +3049,9 @@ impl From for Expr { impl From for Expr { fn from(node: WhileExpr) -> Expr { Expr::WhileExpr(node) } } +impl From for Expr { + fn from(node: YieldExpr) -> Expr { Expr::YieldExpr(node) } +} impl AstNode for Expr { fn can_cast(kind: SyntaxKind) -> bool { match kind { @@ -3035,7 +3059,8 @@ impl AstNode for Expr { | CAST_EXPR | CLOSURE_EXPR | CONTINUE_EXPR | EFFECT_EXPR | FIELD_EXPR | FOR_EXPR | IF_EXPR | INDEX_EXPR | LITERAL | LOOP_EXPR | MACRO_CALL | MATCH_EXPR | METHOD_CALL_EXPR | PAREN_EXPR | PATH_EXPR | PREFIX_EXPR | RANGE_EXPR - | RECORD_EXPR | REF_EXPR | RETURN_EXPR | TRY_EXPR | TUPLE_EXPR | WHILE_EXPR => true, + | RECORD_EXPR | REF_EXPR | RETURN_EXPR | TRY_EXPR | TUPLE_EXPR | WHILE_EXPR + | YIELD_EXPR => true, _ => false, } } @@ -3071,6 +3096,7 @@ impl AstNode for Expr { TRY_EXPR => Expr::TryExpr(TryExpr { syntax }), TUPLE_EXPR => Expr::TupleExpr(TupleExpr { syntax }), WHILE_EXPR => Expr::WhileExpr(WhileExpr { syntax }), + YIELD_EXPR => Expr::YieldExpr(YieldExpr { syntax }), _ => return None, }; Some(res) @@ -3107,6 +3133,7 @@ impl AstNode for Expr { Expr::TryExpr(it) => &it.syntax, Expr::TupleExpr(it) => &it.syntax, Expr::WhileExpr(it) => &it.syntax, + Expr::YieldExpr(it) => &it.syntax, } } } @@ -3983,6 +4010,11 @@ impl std::fmt::Display for WhileExpr { std::fmt::Display::fmt(self.syntax(), f) } } +impl std::fmt::Display for YieldExpr { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + std::fmt::Display::fmt(self.syntax(), f) + } +} impl std::fmt::Display for Label { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { std::fmt::Display::fmt(self.syntax(), f) -- cgit v1.2.3 From 138514bea244253f2c0007976921f4475c9d2cd5 Mon Sep 17 00:00:00 2001 From: Daiki Ihara Date: Thu, 14 Jan 2021 00:02:03 +0900 Subject: Add test for yield_expr --- .../parser/inline/ok/0159_yield_expr.rast | 28 ++++++++++++++++++++++ .../test_data/parser/inline/ok/0159_yield_expr.rs | 4 ++++ 2 files changed, 32 insertions(+) create mode 100644 crates/syntax/test_data/parser/inline/ok/0159_yield_expr.rast create mode 100644 crates/syntax/test_data/parser/inline/ok/0159_yield_expr.rs (limited to 'crates/syntax') diff --git a/crates/syntax/test_data/parser/inline/ok/0159_yield_expr.rast b/crates/syntax/test_data/parser/inline/ok/0159_yield_expr.rast new file mode 100644 index 000000000..05fc90743 --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0159_yield_expr.rast @@ -0,0 +1,28 @@ +SOURCE_FILE@0..37 + FN@0..36 + 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_EXPR@9..36 + L_CURLY@9..10 "{" + WHITESPACE@10..15 "\n " + EXPR_STMT@15..21 + YIELD_EXPR@15..20 + YIELD_KW@15..20 "yield" + SEMICOLON@20..21 ";" + WHITESPACE@21..26 "\n " + EXPR_STMT@26..34 + YIELD_EXPR@26..33 + YIELD_KW@26..31 "yield" + WHITESPACE@31..32 " " + LITERAL@32..33 + INT_NUMBER@32..33 "1" + SEMICOLON@33..34 ";" + WHITESPACE@34..35 "\n" + R_CURLY@35..36 "}" + WHITESPACE@36..37 "\n" diff --git a/crates/syntax/test_data/parser/inline/ok/0159_yield_expr.rs b/crates/syntax/test_data/parser/inline/ok/0159_yield_expr.rs new file mode 100644 index 000000000..596e221f7 --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0159_yield_expr.rs @@ -0,0 +1,4 @@ +fn foo() { + yield; + yield 1; +} -- cgit v1.2.3