parser,syntax: Add separate parser for stmt with optional semicolon

Adjusting `grammar::fragments::stmt` to Optional or Yes will break
original functionality and tests.
This commit is contained in:
Marijn Suijten 2020-11-17 20:02:46 +01:00
parent cc081b7e1c
commit 42da26e959
15 changed files with 72 additions and 5 deletions

View file

@ -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);
}

View file

@ -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,

View file

@ -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<Self, ()> {
parsing::parse_text_fragment(text, parser::FragmentKind::Statement)
parsing::parse_text_fragment(text, parser::FragmentKind::StatementOptionalSemi)
}
}

View file

@ -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 ";"

View file

@ -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 ";"

View file

@ -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 ";"

View file

@ -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 ";"