5624: Finalize expressions grammar r=matklad a=matklad

bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-07-31 15:09:31 +00:00 committed by GitHub
commit 7325283c69
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 1545 additions and 1499 deletions

View file

@ -1,7 +1,7 @@
use ra_syntax::{ use ra_syntax::{
ast::{self, AstNode}, ast::{self, AstNode},
SyntaxKind::{ SyntaxKind::{
BLOCK_EXPR, BREAK_EXPR, COMMENT, LAMBDA_EXPR, LOOP_EXPR, MATCH_ARM, PATH_EXPR, RETURN_EXPR, BLOCK_EXPR, BREAK_EXPR, CLOSURE_EXPR, COMMENT, LOOP_EXPR, MATCH_ARM, PATH_EXPR, RETURN_EXPR,
}, },
SyntaxNode, SyntaxNode,
}; };
@ -148,7 +148,7 @@ impl Anchor {
} }
if let Some(parent) = node.parent() { if let Some(parent) = node.parent() {
if parent.kind() == MATCH_ARM || parent.kind() == LAMBDA_EXPR { if parent.kind() == MATCH_ARM || parent.kind() == CLOSURE_EXPR {
return Some(Anchor::WrapInBlock(node)); return Some(Anchor::WrapInBlock(node));
} }
} }

View file

@ -224,9 +224,22 @@ impl ExprCollector<'_> {
self.alloc_expr(Expr::Unsafe { body }, syntax_ptr) self.alloc_expr(Expr::Unsafe { body }, syntax_ptr)
} }
// FIXME: we need to record these effects somewhere... // FIXME: we need to record these effects somewhere...
ast::Effect::Async(_) | ast::Effect::Label(_) => { ast::Effect::Label(label) => match e.block_expr() {
self.collect_block_opt(e.block_expr()) Some(block) => {
let res = self.collect_block(block);
match &mut self.body.exprs[res] {
Expr::Block { label: block_label, .. } => {
*block_label =
label.lifetime_token().map(|t| Name::new_lifetime(&t))
} }
_ => unreachable!(),
}
res
}
None => self.missing_expr(),
},
// FIXME: we need to record these effects somewhere...
ast::Effect::Async(_) => self.collect_block_opt(e.block_expr()),
}, },
ast::Expr::BlockExpr(e) => self.collect_block(e), ast::Expr::BlockExpr(e) => self.collect_block(e),
ast::Expr::LoopExpr(e) => { ast::Expr::LoopExpr(e) => {
@ -460,7 +473,7 @@ impl ExprCollector<'_> {
self.alloc_expr(Expr::Missing, syntax_ptr) self.alloc_expr(Expr::Missing, syntax_ptr)
} }
} }
ast::Expr::LambdaExpr(e) => { ast::Expr::ClosureExpr(e) => {
let mut args = Vec::new(); let mut args = Vec::new();
let mut arg_types = Vec::new(); let mut arg_types = Vec::new();
if let Some(pl) = e.param_list() { if let Some(pl) = e.param_list() {
@ -618,8 +631,7 @@ impl ExprCollector<'_> {
}) })
.collect(); .collect();
let tail = block.expr().map(|e| self.collect_expr(e)); let tail = block.expr().map(|e| self.collect_expr(e));
let label = block.label().and_then(|l| l.lifetime_token()).map(|t| Name::new_lifetime(&t)); self.alloc_expr(Expr::Block { statements, tail, label: None }, syntax_node_ptr)
self.alloc_expr(Expr::Block { statements, tail, label }, syntax_node_ptr)
} }
fn collect_block_items(&mut self, block: &ast::BlockExpr) { fn collect_block_items(&mut self, block: &ast::BlockExpr) {

View file

@ -379,7 +379,7 @@ fn to_fragment_kind(db: &dyn AstDatabase, id: MacroCallId) -> FragmentKind {
FOR_EXPR => FragmentKind::Expr, FOR_EXPR => FragmentKind::Expr,
PATH_EXPR => FragmentKind::Expr, PATH_EXPR => FragmentKind::Expr,
LAMBDA_EXPR => FragmentKind::Expr, CLOSURE_EXPR => FragmentKind::Expr,
CONDITION => FragmentKind::Expr, CONDITION => FragmentKind::Expr,
BREAK_EXPR => FragmentKind::Expr, BREAK_EXPR => FragmentKind::Expr,
RETURN_EXPR => FragmentKind::Expr, RETURN_EXPR => FragmentKind::Expr,

View file

@ -134,7 +134,7 @@ pub(crate) fn is_in_loop_body(element: SyntaxElement) -> bool {
NodeOrToken::Token(token) => token.parent(), NodeOrToken::Token(token) => token.parent(),
}; };
for node in leaf.ancestors() { for node in leaf.ancestors() {
if node.kind() == FN || node.kind() == LAMBDA_EXPR { if node.kind() == FN || node.kind() == CLOSURE_EXPR {
break; break;
} }
let loop_body = match_ast! { let loop_body = match_ast! {

View file

@ -250,7 +250,7 @@ fn lambda_expr(p: &mut Parser) -> CompletedMarker {
p.error("expected expression"); p.error("expected expression");
} }
} }
m.complete(p, LAMBDA_EXPR) m.complete(p, CLOSURE_EXPR)
} }
// test if_expr // test if_expr

View file

@ -173,7 +173,7 @@ pub enum SyntaxKind {
ARRAY_EXPR, ARRAY_EXPR,
PAREN_EXPR, PAREN_EXPR,
PATH_EXPR, PATH_EXPR,
LAMBDA_EXPR, CLOSURE_EXPR,
IF_EXPR, IF_EXPR,
WHILE_EXPR, WHILE_EXPR,
CONDITION, CONDITION,

File diff suppressed because it is too large Load diff

View file

@ -12,7 +12,7 @@ SOURCE_FILE@0..42
L_CURLY@10..11 "{" L_CURLY@10..11 "{"
WHITESPACE@11..16 "\n " WHITESPACE@11..16 "\n "
EXPR_STMT@16..24 EXPR_STMT@16..24
LAMBDA_EXPR@16..24 CLOSURE_EXPR@16..24
PARAM_LIST@16..18 PARAM_LIST@16..18
PIPE@16..17 "|" PIPE@16..17 "|"
PIPE@17..18 "|" PIPE@17..18 "|"

View file

@ -117,7 +117,7 @@ SOURCE_FILE@0..389
WHITESPACE@140..141 WHITESPACE@140..141
EQ@141..142 EQ@141..142
WHITESPACE@142..143 WHITESPACE@142..143
LAMBDA_EXPR@143..389 CLOSURE_EXPR@143..389
PARAM_LIST@143..388 PARAM_LIST@143..388
PIPE@143..144 PIPE@143..144
PARAM@144..159 PARAM@144..159

View file

@ -49,7 +49,7 @@ SOURCE_FILE@0..83
IDENT@48..51 "map" IDENT@48..51 "map"
ARG_LIST@51..57 ARG_LIST@51..57
L_PAREN@51..52 "(" L_PAREN@51..52 "("
LAMBDA_EXPR@52..56 CLOSURE_EXPR@52..56
PARAM_LIST@52..56 PARAM_LIST@52..56
PIPE@52..53 "|" PIPE@52..53 "|"
PARAM@53..55 PARAM@53..55

View file

@ -12,7 +12,7 @@ SOURCE_FILE@0..134
L_CURLY@9..10 "{" L_CURLY@9..10 "{"
WHITESPACE@10..15 "\n " WHITESPACE@10..15 "\n "
EXPR_STMT@15..21 EXPR_STMT@15..21
LAMBDA_EXPR@15..20 CLOSURE_EXPR@15..20
PARAM_LIST@15..17 PARAM_LIST@15..17
PIPE@15..16 "|" PIPE@15..16 "|"
PIPE@16..17 "|" PIPE@16..17 "|"
@ -23,7 +23,7 @@ SOURCE_FILE@0..134
SEMICOLON@20..21 ";" SEMICOLON@20..21 ";"
WHITESPACE@21..26 "\n " WHITESPACE@21..26 "\n "
EXPR_STMT@26..43 EXPR_STMT@26..43
LAMBDA_EXPR@26..42 CLOSURE_EXPR@26..42
PARAM_LIST@26..28 PARAM_LIST@26..28
PIPE@26..27 "|" PIPE@26..27 "|"
PIPE@27..28 "|" PIPE@27..28 "|"
@ -47,7 +47,7 @@ SOURCE_FILE@0..134
SEMICOLON@42..43 ";" SEMICOLON@42..43 ";"
WHITESPACE@43..48 "\n " WHITESPACE@43..48 "\n "
EXPR_STMT@48..54 EXPR_STMT@48..54
LAMBDA_EXPR@48..53 CLOSURE_EXPR@48..53
PARAM_LIST@48..51 PARAM_LIST@48..51
PIPE@48..49 "|" PIPE@48..49 "|"
PARAM@49..50 PARAM@49..50
@ -64,7 +64,7 @@ SOURCE_FILE@0..134
SEMICOLON@53..54 ";" SEMICOLON@53..54 ";"
WHITESPACE@54..59 "\n " WHITESPACE@54..59 "\n "
EXPR_STMT@59..76 EXPR_STMT@59..76
LAMBDA_EXPR@59..75 CLOSURE_EXPR@59..75
MOVE_KW@59..63 "move" MOVE_KW@59..63 "move"
WHITESPACE@63..64 " " WHITESPACE@63..64 " "
PARAM_LIST@64..73 PARAM_LIST@64..73
@ -91,7 +91,7 @@ SOURCE_FILE@0..134
SEMICOLON@75..76 ";" SEMICOLON@75..76 ";"
WHITESPACE@76..81 "\n " WHITESPACE@76..81 "\n "
EXPR_STMT@81..93 EXPR_STMT@81..93
LAMBDA_EXPR@81..92 CLOSURE_EXPR@81..92
ASYNC_KW@81..86 "async" ASYNC_KW@81..86 "async"
WHITESPACE@86..87 " " WHITESPACE@86..87 " "
PARAM_LIST@87..89 PARAM_LIST@87..89
@ -104,7 +104,7 @@ SOURCE_FILE@0..134
SEMICOLON@92..93 ";" SEMICOLON@92..93 ";"
WHITESPACE@93..98 "\n " WHITESPACE@93..98 "\n "
EXPR_STMT@98..109 EXPR_STMT@98..109
LAMBDA_EXPR@98..108 CLOSURE_EXPR@98..108
MOVE_KW@98..102 "move" MOVE_KW@98..102 "move"
WHITESPACE@102..103 " " WHITESPACE@102..103 " "
PARAM_LIST@103..105 PARAM_LIST@103..105
@ -117,7 +117,7 @@ SOURCE_FILE@0..134
SEMICOLON@108..109 ";" SEMICOLON@108..109 ";"
WHITESPACE@109..114 "\n " WHITESPACE@109..114 "\n "
EXPR_STMT@114..131 EXPR_STMT@114..131
LAMBDA_EXPR@114..130 CLOSURE_EXPR@114..130
ASYNC_KW@114..119 "async" ASYNC_KW@114..119 "async"
WHITESPACE@119..120 " " WHITESPACE@119..120 " "
MOVE_KW@120..124 "move" MOVE_KW@120..124 "move"

View file

@ -105,7 +105,7 @@ SOURCE_FILE@0..135
WHITESPACE@117..118 " " WHITESPACE@117..118 " "
EQ@118..119 "=" EQ@118..119 "="
WHITESPACE@119..120 " " WHITESPACE@119..120 " "
LAMBDA_EXPR@120..131 CLOSURE_EXPR@120..131
ATTR@120..127 ATTR@120..127
POUND@120..121 "#" POUND@120..121 "#"
L_BRACK@121..122 "[" L_BRACK@121..122 "["

View file

@ -20,7 +20,7 @@ SOURCE_FILE@0..63
WHITESPACE@22..23 " " WHITESPACE@22..23 " "
EQ@23..24 "=" EQ@23..24 "="
WHITESPACE@24..25 " " WHITESPACE@24..25 " "
LAMBDA_EXPR@25..59 CLOSURE_EXPR@25..59
PARAM_LIST@25..56 PARAM_LIST@25..56
PIPE@25..26 "|" PIPE@25..26 "|"
PARAM@26..29 PARAM@26..29

View file

@ -13,7 +13,7 @@ SOURCE_FILE@0..34
WHITESPACE@11..12 " " WHITESPACE@11..12 " "
EXPR_STMT@12..31 EXPR_STMT@12..31
CALL_EXPR@12..30 CALL_EXPR@12..30
LAMBDA_EXPR@12..28 CLOSURE_EXPR@12..28
PARAM_LIST@12..14 PARAM_LIST@12..14
PIPE@12..13 "|" PIPE@12..13 "|"
PIPE@13..14 "|" PIPE@13..14 "|"

View file

@ -351,7 +351,7 @@ SOURCE_FILE@0..3813
WHITESPACE@766..767 " " WHITESPACE@766..767 " "
BLOCK_EXPR@767..777 BLOCK_EXPR@767..777
L_CURLY@767..768 "{" L_CURLY@767..768 "{"
LAMBDA_EXPR@768..776 CLOSURE_EXPR@768..776
PARAM_LIST@768..770 PARAM_LIST@768..770
PIPE@768..769 "|" PIPE@768..769 "|"
PIPE@769..770 "|" PIPE@769..770 "|"
@ -1628,7 +1628,7 @@ SOURCE_FILE@0..3813
CALL_EXPR@2950..2995 CALL_EXPR@2950..2995
PAREN_EXPR@2950..2971 PAREN_EXPR@2950..2971
L_PAREN@2950..2951 "(" L_PAREN@2950..2951 "("
LAMBDA_EXPR@2951..2970 CLOSURE_EXPR@2951..2970
PARAM_LIST@2951..2968 PARAM_LIST@2951..2968
PIPE@2951..2952 "|" PIPE@2951..2952 "|"
PARAM@2952..2962 PARAM@2952..2962

View file

@ -52,7 +52,7 @@ SOURCE_FILE@0..166
IDENT@134..146 "catch_unwind" IDENT@134..146 "catch_unwind"
ARG_LIST@146..162 ARG_LIST@146..162
L_PAREN@146..147 "(" L_PAREN@146..147 "("
LAMBDA_EXPR@147..161 CLOSURE_EXPR@147..161
MOVE_KW@147..151 "move" MOVE_KW@147..151 "move"
WHITESPACE@151..152 " " WHITESPACE@151..152 " "
PARAM_LIST@152..154 PARAM_LIST@152..154

View file

@ -144,7 +144,7 @@ pub(crate) const KINDS_SRC: KindsSrc = KindsSrc {
"ARRAY_EXPR", "ARRAY_EXPR",
"PAREN_EXPR", "PAREN_EXPR",
"PATH_EXPR", "PATH_EXPR",
"LAMBDA_EXPR", "CLOSURE_EXPR",
"IF_EXPR", "IF_EXPR",
"WHILE_EXPR", "WHILE_EXPR",
"CONDITION", "CONDITION",

View file

@ -579,6 +579,21 @@ fn lower_rule(acc: &mut Vec<Field>, grammar: &Grammar, label: Option<&String>, r
} }
Rule::Labeled { label: l, rule } => { Rule::Labeled { label: l, rule } => {
assert!(label.is_none()); assert!(label.is_none());
let manually_implemented = matches!(
l.as_str(),
"lhs"
| "rhs"
| "then_branch"
| "else_branch"
| "start"
| "end"
| "op"
| "index"
| "base"
);
if manually_implemented {
return;
}
lower_rule(acc, grammar, Some(l), rule); lower_rule(acc, grammar, Some(l), rule);
} }
Rule::Seq(rules) | Rule::Alt(rules) => { Rule::Seq(rules) | Rule::Alt(rules) => {

View file

@ -115,8 +115,8 @@ Union =
RecordFieldList RecordFieldList
AdtDef = AdtDef =
Struct Enum
| Enum | Struct
| Union | Union
Const = Const =
@ -136,10 +136,10 @@ AssocItemList =
'{' Attr* AssocItem* '}' '{' Attr* AssocItem* '}'
AssocItem = AssocItem =
Fn Const
| TypeAlias | Fn
| Const
| MacroCall | MacroCall
| TypeAlias
Impl = Impl =
Attr* Visibility? Attr* Visibility?
@ -162,9 +162,9 @@ GenericParamList =
'<' (GenericParam (',' GenericParam)* ','?)? '>' '<' (GenericParam (',' GenericParam)* ','?)? '>'
GenericParam = GenericParam =
LifetimeParam ConstParam
| LifetimeParam
| TypeParam | TypeParam
| ConstParam
TypeParam = TypeParam =
Attr* Name (':' TypeBoundList?)? Attr* Name (':' TypeBoundList?)?
@ -195,9 +195,9 @@ Attr =
'#' '!'? '[' Path ('=' Literal | TokenTree)? ']' '#' '!'? '[' Path ('=' Literal | TokenTree)? ']'
Stmt = Stmt =
LetStmt ExprStmt
| ExprStmt
| Item | Item
| LetStmt
LetStmt = LetStmt =
Attr* 'let' Pat (':' Type)? Attr* 'let' Pat (':' Type)?
@ -206,20 +206,192 @@ LetStmt =
ExprStmt = ExprStmt =
Attr* Expr ';'? Attr* Expr ';'?
Expr =
ArrayExpr
| AwaitExpr
| BinExpr
| BlockExpr
| BoxExpr
| BreakExpr
| CallExpr
| CastExpr
| ContinueExpr
| EffectExpr
| FieldExpr
| ForExpr
| IfExpr
| IndexExpr
| Label
| ClosureExpr
| Literal
| LoopExpr
| MacroCall
| MatchExpr
| MethodCallExpr
| ParenExpr
| PathExpr
| PrefixExpr
| RangeExpr
| RecordExpr
| RefExpr
| ReturnExpr
| TryExpr
| TupleExpr
| WhileExpr
Literal =
Attr* 'int_number'
PathExpr =
Attr* Path
BlockExpr =
'{'
Attr*
statements:Stmt*
Expr?
'}'
RefExpr =
Attr* '&' ('raw' |'mut' | 'const') Expr
TryExpr =
Attr* Expr '?'
EffectExpr =
Attr* Label? ('try' | 'unsafe' | 'async') BlockExpr
PrefixExpr =
Attr* op:('-' | '!' | '*') Expr
BinExpr =
Attr*
lhs:Expr
op:(
'||' | '&&'
| '==' | '!=' | '<=' | '>=' | '<' | '>'
| '+' | '*' | '-' | '/' | '%' | '<<' | '>>' | '^' | '|' | '&'
| '=' | '+=' | '/=' | '*=' | '%=' | '>>=' | '<<=' | '-=' | '|=' | '&=' | '^='
)
rhs:Expr
CastExpr =
Attr* Expr 'as' Type
ParenExpr =
Attr* '(' Attr* Expr ')'
ArrayExpr =
Attr* '[' Attr* (
(Expr (',' Expr)* ','?)?
| Expr ';' Expr
) ']'
IndexExpr =
Attr* base:Expr '[' index:Expr ']'
TupleExpr =
Attr* '(' Attr* (Expr (',' Expr)* ','?)? ')'
RecordExpr =
Path RecordExprFieldList
RecordExprFieldList =
'{'
Attr*
fields:(RecordExprField (',' RecordExprField)* ','?)
('..' spread:Expr)?
'}'
RecordExprField =
Attr* NameRef (':' Expr)?
CallExpr =
Attr* Expr ArgList
ArgList =
'(' args:(Expr (',' Expr)* ','?)? ')'
MethodCallExpr =
Attr* Expr '.' NameRef TypeArgList? ArgList
FieldExpr =
Attr* Expr '.' NameRef
ClosureExpr =
Attr* 'static'? 'async'? 'move'? ParamList RetType?
body:Expr
IfExpr =
Attr* 'if' Condition then_branch:BlockExpr
('else' else_branch:(IfExpr | BlockExpr))?
Condition =
'let' Pat '=' Expr
| Expr
LoopExpr =
Attr* Label? 'loop'
loop_body:BlockExpr
ForExpr =
Attr* Label? 'for' Pat 'in' iterable:Expr
loop_body:BlockExpr
WhileExpr =
Attr* Label? 'while' Condition
loop_body:BlockExpr?
Label =
'lifetime'
BreakExpr =
Attr* 'break' 'lifetime'? Expr?
ContinueExpr =
Attr* 'continue' 'lifetime'?
RangeExpr =
Attr* start:Expr? op:('..' | '..=') end:Expr?
MatchExpr =
Attr* 'match' Expr MatchArmList
MatchArmList =
'{'
Attr*
arms:MatchArm*
'}'
MatchArm =
Attr* Pat guard:MatchGuard? '=>' Expr ','?
MatchGuard =
'if' Expr
ReturnExpr =
Attr* 'return' Expr?
AwaitExpr =
Attr* Expr '.' 'await'
BoxExpr =
Attr* 'box' Expr
Type = Type =
ParenType ArrayType
| TupleType | DynTraitType
| NeverType
| PathType
| PointerType
| ArrayType
| SliceType
| ReferenceType
| InferType
| FnPointerType | FnPointerType
| ForType | ForType
| ImplTraitType | ImplTraitType
| DynTraitType | InferType
| NeverType
| ParenType
| PathType
| PointerType
| ReferenceType
| SliceType
| TupleType
ParenType = ParenType =
'(' Type ')' '(' Type ')'
@ -267,129 +439,6 @@ TypeBound =
'lifetime' 'lifetime'
| '?'? Type | '?'? Type
TupleExpr =
Attr* '(' Expr* ')'
ArrayExpr =
Attr* '[' (Expr* | Expr ';' Expr) ']'
ParenExpr =
Attr* '(' Expr ')'
PathExpr =
Path
LambdaExpr =
Attr* 'static'? 'async'? 'move'? ParamList RetType?
body:Expr
IfExpr =
Attr* 'if' Condition
Condition =
'let' Pat '=' Expr
| Expr
EffectExpr =
Attr* Label? ('try' | 'unsafe' | 'async') BlockExpr
LoopExpr =
Attr* Label? 'loop'
loop_body:BlockExpr?
ForExpr =
Attr* Label? 'for' Pat 'in' iterable:Expr
loop_body:BlockExpr?
WhileExpr =
Attr* Label? 'while' Condition
loop_body:BlockExpr?
ContinueExpr =
Attr* 'continue' 'lifetime'?
BreakExpr =
Attr* 'break' 'lifetime'? Expr?
Label =
'lifetime'
BlockExpr =
Attr* Label
'{'
statements:Stmt*
Expr?
'}'
ReturnExpr =
Attr* 'return' Expr
CallExpr =
Attr* Expr ArgList
MethodCallExpr =
Attr* Expr '.' NameRef TypeArgList? ArgList
ArgList =
'(' args:Expr* ')'
FieldExpr =
Attr* Expr '.' NameRef
IndexExpr =
Attr* '[' ']'
AwaitExpr =
Attr* Expr '.' 'await'
TryExpr =
Attr* Expr '?'
CastExpr =
Attr* Expr 'as' Type
RefExpr =
Attr* '&' ('raw' | 'mut' | 'const') Expr
PrefixExpr =
Attr* Expr
BoxExpr =
Attr* 'box' Expr
RangeExpr =
Attr*
BinExpr =
Attr*
Literal =
'int_number'
MatchExpr =
Attr* 'match' Expr MatchArmList
MatchArmList =
'{' arms:MatchArm* '}'
MatchArm =
Attr* Pat guard:MatchGuard? '=>' Expr
MatchGuard =
'if' Expr
RecordExpr =
Path RecordExprFieldList
RecordExprFieldList =
'{'
fields:RecordExprField*
('..' spread:Expr)?
'}'
RecordExprField =
Attr* NameRef (':' Expr)?
OrPat = OrPat =
Pat* Pat*
@ -510,36 +559,3 @@ Pat =
| RangePat | RangePat
| LiteralPat | LiteralPat
| MacroPat | MacroPat
Expr =
TupleExpr
| ArrayExpr
| ParenExpr
| PathExpr
| LambdaExpr
| IfExpr
| LoopExpr
| ForExpr
| WhileExpr
| ContinueExpr
| BreakExpr
| Label
| BlockExpr
| ReturnExpr
| MatchExpr
| RecordExpr
| CallExpr
| IndexExpr
| MethodCallExpr
| FieldExpr
| AwaitExpr
| TryExpr
| EffectExpr
| CastExpr
| RefExpr
| PrefixExpr
| RangeExpr
| BinExpr
| Literal
| MacroCall
| BoxExpr