diff options
Diffstat (limited to 'crates/syntax/src')
-rw-r--r-- | crates/syntax/src/ast/generated/nodes.rs | 2 | ||||
-rw-r--r-- | crates/syntax/src/lib.rs | 9 | ||||
-rw-r--r-- | crates/syntax/src/tests.rs | 9 |
3 files changed, 18 insertions, 2 deletions
diff --git a/crates/syntax/src/ast/generated/nodes.rs b/crates/syntax/src/ast/generated/nodes.rs index c5b80bffe..92ed2ee9d 100644 --- a/crates/syntax/src/ast/generated/nodes.rs +++ b/crates/syntax/src/ast/generated/nodes.rs | |||
@@ -484,7 +484,7 @@ impl ast::AttrsOwner for BlockExpr {} | |||
484 | impl BlockExpr { | 484 | impl BlockExpr { |
485 | pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['{']) } | 485 | pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['{']) } |
486 | pub fn statements(&self) -> AstChildren<Stmt> { support::children(&self.syntax) } | 486 | pub fn statements(&self) -> AstChildren<Stmt> { support::children(&self.syntax) } |
487 | pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } | 487 | pub fn tail_expr(&self) -> Option<Expr> { support::child(&self.syntax) } |
488 | pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['}']) } | 488 | pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['}']) } |
489 | } | 489 | } |
490 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 490 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
diff --git a/crates/syntax/src/lib.rs b/crates/syntax/src/lib.rs index 4d272f367..ea7482bb1 100644 --- a/crates/syntax/src/lib.rs +++ b/crates/syntax/src/lib.rs | |||
@@ -212,6 +212,13 @@ impl ast::Attr { | |||
212 | } | 212 | } |
213 | } | 213 | } |
214 | 214 | ||
215 | impl ast::Stmt { | ||
216 | /// Returns `text`, parsed as statement, but only if it has no errors. | ||
217 | pub fn parse(text: &str) -> Result<Self, ()> { | ||
218 | parsing::parse_text_fragment(text, parser::FragmentKind::StatementOptionalSemi) | ||
219 | } | ||
220 | } | ||
221 | |||
215 | /// Matches a `SyntaxNode` against an `ast` type. | 222 | /// Matches a `SyntaxNode` against an `ast` type. |
216 | /// | 223 | /// |
217 | /// # Example: | 224 | /// # Example: |
@@ -283,7 +290,7 @@ fn api_walkthrough() { | |||
283 | 290 | ||
284 | // Let's get the `1 + 1` expression! | 291 | // Let's get the `1 + 1` expression! |
285 | let body: ast::BlockExpr = func.body().unwrap(); | 292 | let body: ast::BlockExpr = func.body().unwrap(); |
286 | let expr: ast::Expr = body.expr().unwrap(); | 293 | let expr: ast::Expr = body.tail_expr().unwrap(); |
287 | 294 | ||
288 | // Enums are used to group related ast nodes together, and can be used for | 295 | // Enums are used to group related ast nodes together, and can be used for |
289 | // matching. However, because there are no public fields, it's possible to | 296 | // matching. However, because there are no public fields, it's possible to |
diff --git a/crates/syntax/src/tests.rs b/crates/syntax/src/tests.rs index 8c217dfe0..9d3433c9d 100644 --- a/crates/syntax/src/tests.rs +++ b/crates/syntax/src/tests.rs | |||
@@ -103,6 +103,15 @@ fn type_parser_tests() { | |||
103 | } | 103 | } |
104 | 104 | ||
105 | #[test] | 105 | #[test] |
106 | fn stmt_parser_tests() { | ||
107 | fragment_parser_dir_test( | ||
108 | &["parser/fragments/stmt/ok"], | ||
109 | &["parser/fragments/stmt/err"], | ||
110 | crate::ast::Stmt::parse, | ||
111 | ); | ||
112 | } | ||
113 | |||
114 | #[test] | ||
106 | fn parser_fuzz_tests() { | 115 | fn parser_fuzz_tests() { |
107 | for (_, text) in collect_rust_files(&test_data_dir(), &["parser/fuzz-failures"]) { | 116 | for (_, text) in collect_rust_files(&test_data_dir(), &["parser/fuzz-failures"]) { |
108 | fuzz::check_parser(&text) | 117 | fuzz::check_parser(&text) |