always wrap block into an expression

This commit is contained in:
Aleksey Kladov 2019-09-02 19:33:02 +03:00
parent a8397deab9
commit 3c2dea7f55
5 changed files with 19 additions and 14 deletions

View file

@ -144,7 +144,7 @@ pub(crate) fn reparser(
parent: Option<SyntaxKind>,
) -> Option<fn(&mut Parser)> {
let res = match node {
BLOCK => expressions::block,
BLOCK => expressions::naked_block,
RECORD_FIELD_DEF_LIST => items::record_field_def_list,
RECORD_FIELD_LIST => items::record_field_list,
ENUM_VARIANT_LIST => items::enum_variant_list,

View file

@ -40,6 +40,11 @@ pub(crate) fn block(p: &mut Parser) {
p.error("expected a block");
return;
}
atom::block_expr(p, None);
}
pub(crate) fn naked_block(p: &mut Parser) {
assert!(p.at(T!['{']));
let m = p.start();
p.bump();
expr_block_contents(p);

View file

@ -463,10 +463,10 @@ fn match_guard(p: &mut Parser) -> CompletedMarker {
// unsafe {};
// 'label: {};
// }
fn block_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
pub(super) fn block_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
assert!(p.at(T!['{']));
let m = m.unwrap_or_else(|| p.start());
block(p);
naked_block(p);
m.complete(p, BLOCK_EXPR)
}

View file

@ -97,7 +97,7 @@ pub(crate) fn validate(root: &SyntaxNode) -> Vec<SyntaxError> {
for node in root.descendants() {
let _ = visitor_ctx(&mut errors)
.visit::<ast::Literal, _>(validate_literal)
.visit::<ast::Block, _>(block::validate_block_node)
.visit::<ast::BlockExpr, _>(block::validate_block_expr)
.visit::<ast::FieldExpr, _>(|it, errors| validate_numeric_name(it.name_ref(), errors))
.visit::<ast::RecordField, _>(|it, errors| validate_numeric_name(it.name_ref(), errors))
.accept(&node);

View file

@ -5,18 +5,18 @@ use crate::{
SyntaxKind::*,
};
pub(crate) fn validate_block_node(node: ast::Block, errors: &mut Vec<SyntaxError>) {
if let Some(parent) = node.syntax().parent() {
pub(crate) fn validate_block_expr(expr: ast::BlockExpr, errors: &mut Vec<SyntaxError>) {
if let Some(parent) = expr.syntax().parent() {
match parent.kind() {
FN_DEF => return,
BLOCK_EXPR => match parent.parent().map(|v| v.kind()) {
Some(EXPR_STMT) | Some(BLOCK) => return,
_ => {}
},
FN_DEF | EXPR_STMT | BLOCK => return,
_ => {}
}
}
errors.extend(
node.attrs().map(|attr| SyntaxError::new(InvalidBlockAttr, attr.syntax().text_range())),
)
if let Some(block) = expr.block() {
errors.extend(
block
.attrs()
.map(|attr| SyntaxError::new(InvalidBlockAttr, attr.syntax().text_range())),
)
}
}