diff --git a/crates/parser/src/grammar.rs b/crates/parser/src/grammar.rs index 63cc900272..bb9ffea8be 100644 --- a/crates/parser/src/grammar.rs +++ b/crates/parser/src/grammar.rs @@ -66,6 +66,10 @@ pub(crate) mod fragments { expressions::stmt(p, expressions::StmtWithSemi::No) } + pub(crate) fn stmt_optional_semi(p: &mut Parser) { + expressions::stmt(p, expressions::StmtWithSemi::Optional) + } + pub(crate) fn opt_visibility(p: &mut Parser) { let _ = super::opt_visibility(p); } diff --git a/crates/parser/src/lib.rs b/crates/parser/src/lib.rs index 811e740f98..9dfe63028c 100644 --- a/crates/parser/src/lib.rs +++ b/crates/parser/src/lib.rs @@ -88,6 +88,7 @@ pub enum FragmentKind { Path, Expr, Statement, + StatementOptionalSemi, Type, Pattern, Item, @@ -118,6 +119,7 @@ pub fn parse_fragment( FragmentKind::Visibility => grammar::fragments::opt_visibility, FragmentKind::MetaItem => grammar::fragments::meta_item, FragmentKind::Statement => grammar::fragments::stmt, + FragmentKind::StatementOptionalSemi => grammar::fragments::stmt_optional_semi, FragmentKind::Items => grammar::fragments::macro_items, FragmentKind::Statements => grammar::fragments::macro_stmts, FragmentKind::Attr => grammar::fragments::attr, diff --git a/crates/syntax/src/lib.rs b/crates/syntax/src/lib.rs index 51ae3da07e..da151e3289 100644 --- a/crates/syntax/src/lib.rs +++ b/crates/syntax/src/lib.rs @@ -215,7 +215,7 @@ impl ast::Attr { impl ast::Stmt { /// Returns `text`, parsed as statement, but only if it has no errors. pub fn parse(text: &str) -> Result { - parsing::parse_text_fragment(text, parser::FragmentKind::Statement) + parsing::parse_text_fragment(text, parser::FragmentKind::StatementOptionalSemi) } } diff --git a/crates/syntax/test_data/parser/fragments/stmt/err/0000_fn_call.rast b/crates/syntax/test_data/parser/fragments/stmt/err/0000_fn_call.rast deleted file mode 100644 index 5df7507e2d..0000000000 --- a/crates/syntax/test_data/parser/fragments/stmt/err/0000_fn_call.rast +++ /dev/null @@ -1 +0,0 @@ -ERROR diff --git a/crates/syntax/test_data/parser/fragments/stmt/err/0000_let_stmt.rast b/crates/syntax/test_data/parser/fragments/stmt/err/0000_let_stmt.rast deleted file mode 100644 index 5df7507e2d..0000000000 --- a/crates/syntax/test_data/parser/fragments/stmt/err/0000_let_stmt.rast +++ /dev/null @@ -1 +0,0 @@ -ERROR diff --git a/crates/syntax/test_data/parser/fragments/stmt/err/0000_macro_let_stmt.rast b/crates/syntax/test_data/parser/fragments/stmt/err/0000_macro_let_stmt.rast deleted file mode 100644 index 5df7507e2d..0000000000 --- a/crates/syntax/test_data/parser/fragments/stmt/err/0000_macro_let_stmt.rast +++ /dev/null @@ -1 +0,0 @@ -ERROR diff --git a/crates/syntax/test_data/parser/fragments/stmt/err/0000_macro_unterminated_let_stmt.rast b/crates/syntax/test_data/parser/fragments/stmt/err/0000_macro_unterminated_let_stmt.rast deleted file mode 100644 index 5df7507e2d..0000000000 --- a/crates/syntax/test_data/parser/fragments/stmt/err/0000_macro_unterminated_let_stmt.rast +++ /dev/null @@ -1 +0,0 @@ -ERROR diff --git a/crates/syntax/test_data/parser/fragments/stmt/ok/0000_fn_call.rast b/crates/syntax/test_data/parser/fragments/stmt/ok/0000_fn_call.rast new file mode 100644 index 0000000000..8c186da93e --- /dev/null +++ b/crates/syntax/test_data/parser/fragments/stmt/ok/0000_fn_call.rast @@ -0,0 +1,11 @@ +EXPR_STMT@0..6 + CALL_EXPR@0..5 + PATH_EXPR@0..3 + PATH@0..3 + PATH_SEGMENT@0..3 + NAME_REF@0..3 + IDENT@0..3 "foo" + ARG_LIST@3..5 + L_PAREN@3..4 "(" + R_PAREN@4..5 ")" + SEMICOLON@5..6 ";" diff --git a/crates/syntax/test_data/parser/fragments/stmt/err/0000_fn_call.rs b/crates/syntax/test_data/parser/fragments/stmt/ok/0000_fn_call.rs similarity index 100% rename from crates/syntax/test_data/parser/fragments/stmt/err/0000_fn_call.rs rename to crates/syntax/test_data/parser/fragments/stmt/ok/0000_fn_call.rs diff --git a/crates/syntax/test_data/parser/fragments/stmt/ok/0000_let_stmt.rast b/crates/syntax/test_data/parser/fragments/stmt/ok/0000_let_stmt.rast new file mode 100644 index 0000000000..8ab38da21c --- /dev/null +++ b/crates/syntax/test_data/parser/fragments/stmt/ok/0000_let_stmt.rast @@ -0,0 +1,12 @@ +LET_STMT@0..11 + LET_KW@0..3 "let" + WHITESPACE@3..4 " " + IDENT_PAT@4..5 + NAME@4..5 + IDENT@4..5 "x" + WHITESPACE@5..6 " " + EQ@6..7 "=" + WHITESPACE@7..8 " " + LITERAL@8..10 + INT_NUMBER@8..10 "10" + SEMICOLON@10..11 ";" diff --git a/crates/syntax/test_data/parser/fragments/stmt/err/0000_let_stmt.rs b/crates/syntax/test_data/parser/fragments/stmt/ok/0000_let_stmt.rs similarity index 100% rename from crates/syntax/test_data/parser/fragments/stmt/err/0000_let_stmt.rs rename to crates/syntax/test_data/parser/fragments/stmt/ok/0000_let_stmt.rs diff --git a/crates/syntax/test_data/parser/fragments/stmt/ok/0000_macro_let_stmt.rast b/crates/syntax/test_data/parser/fragments/stmt/ok/0000_macro_let_stmt.rast new file mode 100644 index 0000000000..81d6df29a5 --- /dev/null +++ b/crates/syntax/test_data/parser/fragments/stmt/ok/0000_macro_let_stmt.rast @@ -0,0 +1,21 @@ +EXPR_STMT@0..18 + MACRO_CALL@0..17 + PATH@0..2 + PATH_SEGMENT@0..2 + NAME_REF@0..2 + IDENT@0..2 "m1" + BANG@2..3 "!" + TOKEN_TREE@3..17 + L_CURLY@3..4 "{" + WHITESPACE@4..5 " " + LET_KW@5..8 "let" + WHITESPACE@8..9 " " + IDENT@9..10 "a" + WHITESPACE@10..11 " " + EQ@11..12 "=" + WHITESPACE@12..13 " " + INT_NUMBER@13..14 "0" + SEMICOLON@14..15 ";" + WHITESPACE@15..16 " " + R_CURLY@16..17 "}" + SEMICOLON@17..18 ";" diff --git a/crates/syntax/test_data/parser/fragments/stmt/err/0000_macro_let_stmt.rs b/crates/syntax/test_data/parser/fragments/stmt/ok/0000_macro_let_stmt.rs similarity index 100% rename from crates/syntax/test_data/parser/fragments/stmt/err/0000_macro_let_stmt.rs rename to crates/syntax/test_data/parser/fragments/stmt/ok/0000_macro_let_stmt.rs diff --git a/crates/syntax/test_data/parser/fragments/stmt/ok/0000_macro_unterminated_let_stmt.rast b/crates/syntax/test_data/parser/fragments/stmt/ok/0000_macro_unterminated_let_stmt.rast new file mode 100644 index 0000000000..81d6df29a5 --- /dev/null +++ b/crates/syntax/test_data/parser/fragments/stmt/ok/0000_macro_unterminated_let_stmt.rast @@ -0,0 +1,21 @@ +EXPR_STMT@0..18 + MACRO_CALL@0..17 + PATH@0..2 + PATH_SEGMENT@0..2 + NAME_REF@0..2 + IDENT@0..2 "m1" + BANG@2..3 "!" + TOKEN_TREE@3..17 + L_CURLY@3..4 "{" + WHITESPACE@4..5 " " + LET_KW@5..8 "let" + WHITESPACE@8..9 " " + IDENT@9..10 "a" + WHITESPACE@10..11 " " + EQ@11..12 "=" + WHITESPACE@12..13 " " + INT_NUMBER@13..14 "0" + SEMICOLON@14..15 ";" + WHITESPACE@15..16 " " + R_CURLY@16..17 "}" + SEMICOLON@17..18 ";" diff --git a/crates/syntax/test_data/parser/fragments/stmt/err/0000_macro_unterminated_let_stmt.rs b/crates/syntax/test_data/parser/fragments/stmt/ok/0000_macro_unterminated_let_stmt.rs similarity index 100% rename from crates/syntax/test_data/parser/fragments/stmt/err/0000_macro_unterminated_let_stmt.rs rename to crates/syntax/test_data/parser/fragments/stmt/ok/0000_macro_unterminated_let_stmt.rs