This commit is contained in:
Aleksey Kladov 2021-12-29 19:18:34 +03:00
parent 8234a85d15
commit f5cfc0504e
2 changed files with 19 additions and 20 deletions

View file

@ -59,7 +59,7 @@ pub(crate) mod entry {
} }
pub(crate) fn stmt(p: &mut Parser) { pub(crate) fn stmt(p: &mut Parser) {
expressions::stmt(p, expressions::StmtWithSemi::No); expressions::stmt(p, expressions::Semicolon::Forbidden);
} }
pub(crate) fn pat(p: &mut Parser) { pub(crate) fn pat(p: &mut Parser) {
@ -103,7 +103,7 @@ pub(crate) mod entry {
continue; continue;
} }
expressions::stmt(p, expressions::StmtWithSemi::Optional); expressions::stmt(p, expressions::Semicolon::Optional);
} }
m.complete(p, MACRO_STMTS); m.complete(p, MACRO_STMTS);

View file

@ -6,10 +6,10 @@ pub(crate) use self::atom::{block_expr, match_arm_list};
pub(super) use self::atom::{literal, LITERAL_FIRST}; pub(super) use self::atom::{literal, LITERAL_FIRST};
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]
pub(super) enum StmtWithSemi { pub(super) enum Semicolon {
Yes, Required,
No,
Optional, Optional,
Forbidden,
} }
const EXPR_FIRST: TokenSet = LHS_FIRST; const EXPR_FIRST: TokenSet = LHS_FIRST;
@ -29,7 +29,7 @@ fn expr_no_struct(p: &mut Parser) {
expr_bp(p, None, r, 1); expr_bp(p, None, r, 1);
} }
pub(super) fn stmt(p: &mut Parser, with_semi: StmtWithSemi) { pub(super) fn stmt(p: &mut Parser, semicolon: Semicolon) {
let m = p.start(); let m = p.start();
// test attr_on_expr_stmt // test attr_on_expr_stmt
// fn foo() { // fn foo() {
@ -41,7 +41,7 @@ pub(super) fn stmt(p: &mut Parser, with_semi: StmtWithSemi) {
attributes::outer_attrs(p); attributes::outer_attrs(p);
if p.at(T![let]) { if p.at(T![let]) {
let_stmt(p, m, with_semi); let_stmt(p, m, semicolon);
return; return;
} }
@ -53,7 +53,7 @@ pub(super) fn stmt(p: &mut Parser, with_semi: StmtWithSemi) {
}; };
if let Some((cm, blocklike)) = expr_stmt(p, Some(m)) { if let Some((cm, blocklike)) = expr_stmt(p, Some(m)) {
if !(p.at(T!['}']) || (with_semi != StmtWithSemi::Yes && p.at(EOF))) { if !(p.at(T!['}']) || (semicolon != Semicolon::Required && p.at(EOF))) {
// test no_semi_after_block // test no_semi_after_block
// fn foo() { // fn foo() {
// if true {} // if true {}
@ -69,27 +69,26 @@ pub(super) fn stmt(p: &mut Parser, with_semi: StmtWithSemi) {
// test!{} // test!{}
// } // }
let m = cm.precede(p); let m = cm.precede(p);
match with_semi { match semicolon {
StmtWithSemi::No => (), Semicolon::Required => {
StmtWithSemi::Optional => {
p.eat(T![;]);
}
StmtWithSemi::Yes => {
if blocklike.is_block() { if blocklike.is_block() {
p.eat(T![;]); p.eat(T![;]);
} else { } else {
p.expect(T![;]); p.expect(T![;]);
} }
} }
Semicolon::Optional => {
p.eat(T![;]);
}
Semicolon::Forbidden => (),
} }
m.complete(p, EXPR_STMT); m.complete(p, EXPR_STMT);
} }
} }
// test let_stmt // test let_stmt
// fn f() { let x: i32 = 92; } // fn f() { let x: i32 = 92; }
fn let_stmt(p: &mut Parser, m: Marker, with_semi: StmtWithSemi) { fn let_stmt(p: &mut Parser, m: Marker, with_semi: Semicolon) {
p.bump(T![let]); p.bump(T![let]);
patterns::pattern(p); patterns::pattern(p);
if p.at(T![:]) { if p.at(T![:]) {
@ -114,11 +113,11 @@ pub(super) fn stmt(p: &mut Parser, with_semi: StmtWithSemi) {
} }
match with_semi { match with_semi {
StmtWithSemi::No => (), Semicolon::Forbidden => (),
StmtWithSemi::Optional => { Semicolon::Optional => {
p.eat(T![;]); p.eat(T![;]);
} }
StmtWithSemi::Yes => { Semicolon::Required => {
p.expect(T![;]); p.expect(T![;]);
} }
} }
@ -150,7 +149,7 @@ pub(super) fn expr_block_contents(p: &mut Parser) {
continue; continue;
} }
stmt(p, StmtWithSemi::Yes); stmt(p, Semicolon::Required);
} }
} }