Parse for<'a> closure syntax

This commit is contained in:
Lukas Wirth 2022-04-10 17:50:14 +02:00
parent e691ae0ab2
commit d8341c5b92
12 changed files with 248 additions and 161 deletions

View file

@ -71,16 +71,8 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar
let done = match p.current() {
T!['('] => tuple_expr(p),
T!['['] => array_expr(p),
T![|] => closure_expr(p),
T![static] | T![async] | T![move] if la == T![|] => closure_expr(p),
T![static] | T![async] if la == T![move] && p.nth(2) == T![|] => closure_expr(p),
T![static] if la == T![async] && p.nth(2) == T![|] => closure_expr(p),
T![static] if la == T![async] && p.nth(2) == T![move] && p.nth(3) == T![|] => {
closure_expr(p)
}
T![if] => if_expr(p),
T![let] => let_expr(p),
T![_] => {
// test destructuring_assignment_wildcard_pat
// fn foo() {
@ -91,12 +83,16 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar
p.bump(T![_]);
m.complete(p, UNDERSCORE_EXPR)
}
T![loop] => loop_expr(p, None),
T![box] => box_expr(p, None),
T![for] => for_expr(p, None),
T![while] => while_expr(p, None),
T![try] => try_block_expr(p, None),
T![match] => match_expr(p),
T![return] => return_expr(p),
T![yield] => yield_expr(p),
T![continue] => continue_expr(p),
T![break] => break_expr(p, r),
LIFETIME_IDENT if la == T![:] => {
let m = p.start();
label(p);
@ -121,30 +117,24 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar
}
}
}
T![async] if la == T!['{'] || (la == T![move] && p.nth(2) == T!['{']) => {
// test effect_blocks
// fn f() { unsafe { } }
// fn f() { const { } }
// fn f() { async { } }
// fn f() { async move { } }
T![const] | T![unsafe] | T![async] if la == T!['{'] => {
let m = p.start();
p.bump_any();
stmt_list(p);
m.complete(p, BLOCK_EXPR)
}
T![async] if la == T![move] && p.nth(2) == T!['{'] => {
let m = p.start();
p.bump(T![async]);
p.eat(T![move]);
stmt_list(p);
m.complete(p, BLOCK_EXPR)
}
T![match] => match_expr(p),
// test unsafe_block
// fn f() { unsafe { } }
T![unsafe] if la == T!['{'] => {
let m = p.start();
p.bump(T![unsafe]);
stmt_list(p);
m.complete(p, BLOCK_EXPR)
}
// test const_block
// fn f() { const { } }
T![const] if la == T!['{'] => {
let m = p.start();
p.bump(T![const]);
stmt_list(p);
m.complete(p, BLOCK_EXPR)
}
T!['{'] => {
// test for_range_from
// fn foo() {
@ -156,10 +146,11 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar
stmt_list(p);
m.complete(p, BLOCK_EXPR)
}
T![return] => return_expr(p),
T![yield] => yield_expr(p),
T![continue] => continue_expr(p),
T![break] => break_expr(p, r),
T![static] | T![async] | T![move] | T![|] => closure_expr(p),
T![for] if la == T![<] => closure_expr(p),
T![for] => for_expr(p, None),
_ => {
p.err_recover("expected expression", EXPR_RECOVERY_SET);
return None;
@ -254,25 +245,30 @@ fn array_expr(p: &mut Parser) -> CompletedMarker {
// static move || {};
// static async || {};
// static async move || {};
// for<'a> || {};
// for<'a> move || {};
// }
fn closure_expr(p: &mut Parser) -> CompletedMarker {
assert!(
p.at(T![|])
|| (p.at(T![move]) && p.nth(1) == T![|])
|| (p.at(T![async]) && p.nth(1) == T![|])
|| (p.at(T![async]) && p.nth(1) == T![move] && p.nth(2) == T![|])
|| (p.at(T![static]) && p.nth(1) == T![|])
|| (p.at(T![static]) && p.nth(1) == T![move] && p.nth(2) == T![|])
|| (p.at(T![static]) && p.nth(1) == T![async] && p.nth(2) == T![|])
|| (p.at(T![static])
&& p.nth(1) == T![async]
&& p.nth(2) == T![move]
&& p.nth(3) == T![|])
);
assert!(match p.current() {
T![static] | T![async] | T![move] | T![|] => true,
T![for] => p.nth(1) == T![<],
_ => false,
});
let m = p.start();
if p.at(T![for]) {
types::for_binder(p);
}
p.eat(T![static]);
p.eat(T![async]);
p.eat(T![move]);
if !p.at(T![|]) {
p.error("expected `|`");
return m.complete(p, CLOSURE_EXPR);
}
params::param_list_closure(p);
if opt_ret_type(p) {
// test lambda_ret_block

View file

@ -182,45 +182,44 @@ SOURCE_FILE
WHITESPACE " "
TUPLE_EXPR
L_PAREN "("
FOR_EXPR
CLOSURE_EXPR
FOR_KW "for"
PATH_PAT
PATH
PATH_SEGMENT
L_ANGLE "<"
ERROR
LIFETIME_IDENT "'a"
R_ANGLE ">"
WHITESPACE " "
GENERIC_PARAM_LIST
L_ANGLE "<"
LIFETIME_PARAM
LIFETIME
LIFETIME_IDENT "'a"
R_ANGLE ">"
WHITESPACE " "
BIN_EXPR
BIN_EXPR
BIN_EXPR
BIN_EXPR
BIN_EXPR
PATH_EXPR
PATH
PATH_SEGMENT
NAME_REF
IDENT "Trait"
L_ANGLE "<"
ERROR
LIFETIME_IDENT "'a"
R_ANGLE ">"
ERROR
R_PAREN ")"
WHITESPACE " "
PLUS "+"
WHITESPACE " "
PAREN_EXPR
L_PAREN "("
PATH_EXPR
PATH
PATH_SEGMENT
NAME_REF
IDENT "Copy"
IDENT "Trait"
L_ANGLE "<"
ERROR
LIFETIME_IDENT "'a"
R_ANGLE ">"
ERROR
R_PAREN ")"
R_ANGLE ">"
ERROR
SEMICOLON ";"
WHITESPACE " "
PLUS "+"
WHITESPACE " "
PAREN_EXPR
L_PAREN "("
PATH_EXPR
PATH
PATH_SEGMENT
NAME_REF
IDENT "Copy"
R_PAREN ")"
R_ANGLE ">"
ERROR
SEMICOLON ";"
WHITESPACE "\n "
LET_EXPR
LET_KW "let"
@ -240,49 +239,48 @@ SOURCE_FILE
L_ANGLE "<"
TUPLE_EXPR
L_PAREN "("
FOR_EXPR
CLOSURE_EXPR
FOR_KW "for"
PATH_PAT
PATH
PATH_SEGMENT
L_ANGLE "<"
ERROR
LIFETIME_IDENT "'a"
R_ANGLE ">"
WHITESPACE " "
GENERIC_PARAM_LIST
L_ANGLE "<"
LIFETIME_PARAM
LIFETIME
LIFETIME_IDENT "'a"
R_ANGLE ">"
WHITESPACE " "
BIN_EXPR
BIN_EXPR
BIN_EXPR
BIN_EXPR
BIN_EXPR
PATH_EXPR
PATH
PATH_SEGMENT
NAME_REF
IDENT "Trait"
L_ANGLE "<"
ERROR
LIFETIME_IDENT "'a"
R_ANGLE ">"
ERROR
R_PAREN ")"
WHITESPACE " "
PLUS "+"
WHITESPACE " "
PAREN_EXPR
L_PAREN "("
PATH_EXPR
PATH
PATH_SEGMENT
NAME_REF
IDENT "Copy"
IDENT "Trait"
L_ANGLE "<"
ERROR
LIFETIME_IDENT "'a"
R_ANGLE ">"
ERROR
R_PAREN ")"
WHITESPACE " "
PLUS "+"
WHITESPACE " "
PAREN_EXPR
L_PAREN "("
ERROR
QUESTION "?"
PATH_EXPR
PATH
PATH_SEGMENT
NAME_REF
IDENT "Copy"
R_PAREN ")"
WHITESPACE " "
PLUS "+"
WHITESPACE " "
PAREN_EXPR
L_PAREN "("
ERROR
QUESTION "?"
PATH_EXPR
PATH
PATH_SEGMENT
@ -307,23 +305,21 @@ error 141: expected SEMICOLON
error 146: expected SEMICOLON
error 146: expected expression
error 148: expected expression
error 155: expected type
error 158: expected IN_KW
error 158: expected `|`
error 158: expected COMMA
error 165: expected expression
error 168: expected expression
error 179: expected expression
error 180: expected a block
error 180: expected COMMA
error 190: expected EQ
error 190: expected expression
error 191: expected COMMA
error 201: expected type
error 204: expected IN_KW
error 204: expected `|`
error 204: expected COMMA
error 211: expected expression
error 214: expected expression
error 228: expected expression
error 229: expected R_PAREN
error 229: expected a block
error 229: expected COMMA
error 236: expected expression
error 237: expected COMMA

View file

@ -199,6 +199,48 @@ SOURCE_FILE
L_CURLY "{"
R_CURLY "}"
SEMICOLON ";"
WHITESPACE "\n "
EXPR_STMT
CLOSURE_EXPR
FOR_KW "for"
GENERIC_PARAM_LIST
L_ANGLE "<"
LIFETIME_PARAM
LIFETIME
LIFETIME_IDENT "'a"
R_ANGLE ">"
WHITESPACE " "
PARAM_LIST
PIPE "|"
PIPE "|"
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
R_CURLY "}"
SEMICOLON ";"
WHITESPACE "\n "
EXPR_STMT
CLOSURE_EXPR
FOR_KW "for"
GENERIC_PARAM_LIST
L_ANGLE "<"
LIFETIME_PARAM
LIFETIME
LIFETIME_IDENT "'a"
R_ANGLE ">"
WHITESPACE " "
MOVE_KW "move"
WHITESPACE " "
PARAM_LIST
PIPE "|"
PIPE "|"
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
R_CURLY "}"
SEMICOLON ";"
WHITESPACE "\n"
R_CURLY "}"
WHITESPACE "\n"

View file

@ -10,4 +10,6 @@ fn foo() {
static move || {};
static async || {};
static async move || {};
for<'a> || {};
for<'a> move || {};
}

View file

@ -1,24 +0,0 @@
SOURCE_FILE
FN
FN_KW "fn"
WHITESPACE " "
NAME
IDENT "f"
PARAM_LIST
L_PAREN "("
R_PAREN ")"
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
WHITESPACE " "
BLOCK_EXPR
CONST_KW "const"
WHITESPACE " "
STMT_LIST
L_CURLY "{"
WHITESPACE " "
R_CURLY "}"
WHITESPACE " "
R_CURLY "}"
WHITESPACE "\n"

View file

@ -1 +0,0 @@
fn f() { const { } }

View file

@ -1,24 +0,0 @@
SOURCE_FILE
FN
FN_KW "fn"
WHITESPACE " "
NAME
IDENT "f"
PARAM_LIST
L_PAREN "("
R_PAREN ")"
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
WHITESPACE " "
BLOCK_EXPR
UNSAFE_KW "unsafe"
WHITESPACE " "
STMT_LIST
L_CURLY "{"
WHITESPACE " "
R_CURLY "}"
WHITESPACE " "
R_CURLY "}"
WHITESPACE "\n"

View file

@ -1 +0,0 @@
fn f() { unsafe { } }

View file

@ -0,0 +1,95 @@
SOURCE_FILE
FN
FN_KW "fn"
WHITESPACE " "
NAME
IDENT "f"
PARAM_LIST
L_PAREN "("
R_PAREN ")"
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
WHITESPACE " "
BLOCK_EXPR
UNSAFE_KW "unsafe"
WHITESPACE " "
STMT_LIST
L_CURLY "{"
WHITESPACE " "
R_CURLY "}"
WHITESPACE " "
R_CURLY "}"
WHITESPACE "\n"
FN
FN_KW "fn"
WHITESPACE " "
NAME
IDENT "f"
PARAM_LIST
L_PAREN "("
R_PAREN ")"
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
WHITESPACE " "
BLOCK_EXPR
CONST_KW "const"
WHITESPACE " "
STMT_LIST
L_CURLY "{"
WHITESPACE " "
R_CURLY "}"
WHITESPACE " "
R_CURLY "}"
WHITESPACE "\n"
FN
FN_KW "fn"
WHITESPACE " "
NAME
IDENT "f"
PARAM_LIST
L_PAREN "("
R_PAREN ")"
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
WHITESPACE " "
BLOCK_EXPR
ASYNC_KW "async"
WHITESPACE " "
STMT_LIST
L_CURLY "{"
WHITESPACE " "
R_CURLY "}"
WHITESPACE " "
R_CURLY "}"
WHITESPACE "\n"
FN
FN_KW "fn"
WHITESPACE " "
NAME
IDENT "f"
PARAM_LIST
L_PAREN "("
R_PAREN ")"
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
WHITESPACE " "
BLOCK_EXPR
ASYNC_KW "async"
WHITESPACE " "
MOVE_KW "move"
WHITESPACE " "
STMT_LIST
L_CURLY "{"
WHITESPACE " "
R_CURLY "}"
WHITESPACE " "
R_CURLY "}"
WHITESPACE "\n"

View file

@ -0,0 +1,4 @@
fn f() { unsafe { } }
fn f() { const { } }
fn f() { async { } }
fn f() { async move { } }

View file

@ -449,7 +449,7 @@ FieldExpr =
Attr* Expr '.' NameRef
ClosureExpr =
Attr* 'static'? 'async'? 'move'? ParamList RetType?
Attr* ('for' GenericParamList)? 'static'? 'async'? 'move'? ParamList RetType?
body:Expr
IfExpr =

View file

@ -837,6 +837,8 @@ pub struct ClosureExpr {
}
impl ast::HasAttrs for ClosureExpr {}
impl ClosureExpr {
pub fn for_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![for]) }
pub fn generic_param_list(&self) -> Option<GenericParamList> { support::child(&self.syntax) }
pub fn static_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![static]) }
pub fn async_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![async]) }
pub fn move_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![move]) }