From 42da26e9597443812da5a59b3fa48bdcbf619666 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Tue, 17 Nov 2020 20:02:46 +0100 Subject: parser,syntax: Add separate parser for stmt with optional semicolon Adjusting `grammar::fragments::stmt` to Optional or Yes will break original functionality and tests. --- crates/parser/src/grammar.rs | 4 ++++ crates/parser/src/lib.rs | 2 ++ crates/syntax/src/lib.rs | 2 +- .../parser/fragments/stmt/err/0000_fn_call.rast | 1 - .../parser/fragments/stmt/err/0000_fn_call.rs | 1 - .../parser/fragments/stmt/err/0000_let_stmt.rast | 1 - .../parser/fragments/stmt/err/0000_let_stmt.rs | 1 - .../fragments/stmt/err/0000_macro_let_stmt.rast | 1 - .../fragments/stmt/err/0000_macro_let_stmt.rs | 1 - .../stmt/err/0000_macro_unterminated_let_stmt.rast | 1 - .../stmt/err/0000_macro_unterminated_let_stmt.rs | 1 - .../parser/fragments/stmt/ok/0000_fn_call.rast | 11 +++++++++++ .../parser/fragments/stmt/ok/0000_fn_call.rs | 1 + .../parser/fragments/stmt/ok/0000_let_stmt.rast | 12 ++++++++++++ .../parser/fragments/stmt/ok/0000_let_stmt.rs | 1 + .../fragments/stmt/ok/0000_macro_let_stmt.rast | 21 +++++++++++++++++++++ .../parser/fragments/stmt/ok/0000_macro_let_stmt.rs | 1 + .../stmt/ok/0000_macro_unterminated_let_stmt.rast | 21 +++++++++++++++++++++ .../stmt/ok/0000_macro_unterminated_let_stmt.rs | 1 + 19 files changed, 76 insertions(+), 9 deletions(-) delete mode 100644 crates/syntax/test_data/parser/fragments/stmt/err/0000_fn_call.rast delete mode 100644 crates/syntax/test_data/parser/fragments/stmt/err/0000_fn_call.rs delete mode 100644 crates/syntax/test_data/parser/fragments/stmt/err/0000_let_stmt.rast delete mode 100644 crates/syntax/test_data/parser/fragments/stmt/err/0000_let_stmt.rs delete mode 100644 crates/syntax/test_data/parser/fragments/stmt/err/0000_macro_let_stmt.rast delete mode 100644 crates/syntax/test_data/parser/fragments/stmt/err/0000_macro_let_stmt.rs delete mode 100644 crates/syntax/test_data/parser/fragments/stmt/err/0000_macro_unterminated_let_stmt.rast delete mode 100644 crates/syntax/test_data/parser/fragments/stmt/err/0000_macro_unterminated_let_stmt.rs create mode 100644 crates/syntax/test_data/parser/fragments/stmt/ok/0000_fn_call.rast create mode 100644 crates/syntax/test_data/parser/fragments/stmt/ok/0000_fn_call.rs create mode 100644 crates/syntax/test_data/parser/fragments/stmt/ok/0000_let_stmt.rast create mode 100644 crates/syntax/test_data/parser/fragments/stmt/ok/0000_let_stmt.rs create mode 100644 crates/syntax/test_data/parser/fragments/stmt/ok/0000_macro_let_stmt.rast create mode 100644 crates/syntax/test_data/parser/fragments/stmt/ok/0000_macro_let_stmt.rs create mode 100644 crates/syntax/test_data/parser/fragments/stmt/ok/0000_macro_unterminated_let_stmt.rast create mode 100644 crates/syntax/test_data/parser/fragments/stmt/ok/0000_macro_unterminated_let_stmt.rs diff --git a/crates/parser/src/grammar.rs b/crates/parser/src/grammar.rs index 63cc90027..bb9ffea8b 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 811e740f9..9dfe63028 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 51ae3da07..da151e328 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 5df7507e2..000000000 --- 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_fn_call.rs b/crates/syntax/test_data/parser/fragments/stmt/err/0000_fn_call.rs deleted file mode 100644 index a280f9a5c..000000000 --- a/crates/syntax/test_data/parser/fragments/stmt/err/0000_fn_call.rs +++ /dev/null @@ -1 +0,0 @@ -foo(); 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 5df7507e2..000000000 --- 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_let_stmt.rs b/crates/syntax/test_data/parser/fragments/stmt/err/0000_let_stmt.rs deleted file mode 100644 index de8a7f1fc..000000000 --- a/crates/syntax/test_data/parser/fragments/stmt/err/0000_let_stmt.rs +++ /dev/null @@ -1 +0,0 @@ -let x = 10; 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 5df7507e2..000000000 --- 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_let_stmt.rs b/crates/syntax/test_data/parser/fragments/stmt/err/0000_macro_let_stmt.rs deleted file mode 100644 index 075f30159..000000000 --- a/crates/syntax/test_data/parser/fragments/stmt/err/0000_macro_let_stmt.rs +++ /dev/null @@ -1 +0,0 @@ -m1!{ let a = 0; }; 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 5df7507e2..000000000 --- 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/err/0000_macro_unterminated_let_stmt.rs b/crates/syntax/test_data/parser/fragments/stmt/err/0000_macro_unterminated_let_stmt.rs deleted file mode 100644 index 075f30159..000000000 --- a/crates/syntax/test_data/parser/fragments/stmt/err/0000_macro_unterminated_let_stmt.rs +++ /dev/null @@ -1 +0,0 @@ -m1!{ let a = 0; }; 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 000000000..8c186da93 --- /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/ok/0000_fn_call.rs b/crates/syntax/test_data/parser/fragments/stmt/ok/0000_fn_call.rs new file mode 100644 index 000000000..a280f9a5c --- /dev/null +++ b/crates/syntax/test_data/parser/fragments/stmt/ok/0000_fn_call.rs @@ -0,0 +1 @@ +foo(); 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 000000000..8ab38da21 --- /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/ok/0000_let_stmt.rs b/crates/syntax/test_data/parser/fragments/stmt/ok/0000_let_stmt.rs new file mode 100644 index 000000000..de8a7f1fc --- /dev/null +++ b/crates/syntax/test_data/parser/fragments/stmt/ok/0000_let_stmt.rs @@ -0,0 +1 @@ +let x = 10; 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 000000000..81d6df29a --- /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/ok/0000_macro_let_stmt.rs b/crates/syntax/test_data/parser/fragments/stmt/ok/0000_macro_let_stmt.rs new file mode 100644 index 000000000..075f30159 --- /dev/null +++ b/crates/syntax/test_data/parser/fragments/stmt/ok/0000_macro_let_stmt.rs @@ -0,0 +1 @@ +m1!{ let a = 0; }; 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 000000000..81d6df29a --- /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/ok/0000_macro_unterminated_let_stmt.rs b/crates/syntax/test_data/parser/fragments/stmt/ok/0000_macro_unterminated_let_stmt.rs new file mode 100644 index 000000000..075f30159 --- /dev/null +++ b/crates/syntax/test_data/parser/fragments/stmt/ok/0000_macro_unterminated_let_stmt.rs @@ -0,0 +1 @@ +m1!{ let a = 0; }; -- cgit v1.2.3