Work on expressions grammar

This commit is contained in:
Aleksey Kladov 2020-07-31 16:52:08 +02:00
parent d4d986c7f8
commit bfcee63e75
4 changed files with 1516 additions and 1475 deletions

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) => {
@ -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) {

File diff suppressed because it is too large Load diff

View file

@ -579,6 +579,9 @@ 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());
if l == "op" {
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
| LambdaExpr
| 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*
Expr
op:(
'||' | '&&'
| '==' | '!=' | '<=' | '>=' | '<' | '>'
| '+' | '*' | '-' | '/' | '%' | '<<' | '>>' | '^' | '|' | '&'
| '=' | '+=' | '/=' | '*=' | '%=' | '>>=' | '<<=' | '-=' | '|=' | '&=' | '^='
)
Expr
CastExpr =
Attr* Expr 'as' Type
ParenExpr =
Attr* '(' Attr* Expr ')'
ArrayExpr =
Attr* '[' Attr* (
(Expr (',' Expr)* ','?)?
| Expr ';' Expr
) ']'
IndexExpr =
Attr* Expr '[' 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
LambdaExpr =
Attr* 'static'? 'async'? 'move'? ParamList RetType?
body:Expr
IfExpr =
Attr* 'if' Condition BlockExpr
('else' (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* Expr? op:('..' | '..=') 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