From bfcee63e75d6feb21cafbdf3887e0efd508b6b2e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 16:52:08 +0200 Subject: Work on expressions grammar --- xtask/src/codegen/gen_syntax.rs | 3 + xtask/src/codegen/rust.ungram | 330 +++++++++++++++++++++------------------- 2 files changed, 176 insertions(+), 157 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs index d9f358513..90f746e96 100644 --- a/xtask/src/codegen/gen_syntax.rs +++ b/xtask/src/codegen/gen_syntax.rs @@ -579,6 +579,9 @@ fn lower_rule(acc: &mut Vec, grammar: &Grammar, label: Option<&String>, r } Rule::Labeled { label: l, rule } => { assert!(label.is_none()); + if l == "op" { + return; + } lower_rule(acc, grammar, Some(l), rule); } Rule::Seq(rules) | Rule::Alt(rules) => { diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 17de36d7a..93195befe 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -115,8 +115,8 @@ Union = RecordFieldList AdtDef = - Struct -| Enum + Enum +| Struct | Union Const = @@ -136,10 +136,10 @@ AssocItemList = '{' Attr* AssocItem* '}' AssocItem = - Fn -| TypeAlias -| Const + Const +| Fn | MacroCall +| TypeAlias Impl = Attr* Visibility? @@ -162,9 +162,9 @@ GenericParamList = '<' (GenericParam (',' GenericParam)* ','?)? '>' GenericParam = - LifetimeParam + ConstParam +| LifetimeParam | TypeParam -| ConstParam TypeParam = Attr* Name (':' TypeBoundList?)? @@ -195,9 +195,9 @@ Attr = '#' '!'? '[' Path ('=' Literal | TokenTree)? ']' Stmt = - LetStmt -| ExprStmt + ExprStmt | Item +| LetStmt LetStmt = Attr* 'let' Pat (':' Type)? @@ -206,189 +206,238 @@ LetStmt = ExprStmt = Attr* Expr ';'? -Type = - ParenType -| TupleType -| NeverType -| PathType -| PointerType -| ArrayType -| SliceType -| ReferenceType -| InferType -| FnPointerType -| ForType -| ImplTraitType -| DynTraitType +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 -ParenType = - '(' Type ')' +Literal = + Attr* 'int_number' -NeverType = - '!' +PathExpr = + Attr* Path -PathType = - Path +BlockExpr = + '{' + Attr* + statements:Stmt* + Expr? + '}' -TupleType = - '(' fields:(Type (',' Type)* ','?)? ')' +RefExpr = + Attr* '&' ('raw' |'mut' | 'const') Expr -PointerType = - '*' ('const' | 'mut') Type +TryExpr = + Attr* Expr '?' -ReferenceType = - '&' 'lifetime'? 'mut'? Type +EffectExpr = + Attr* Label? ('try' | 'unsafe' | 'async') BlockExpr -ArrayType = - '[' Type ';' Expr ']' +PrefixExpr = + Attr* op:('-' | '!' | '*') Expr -SliceType = - '[' Type ']' +BinExpr = + Attr* + Expr + op:( + '||' | '&&' + | '==' | '!=' | '<=' | '>=' | '<' | '>' + | '+' | '*' | '-' | '/' | '%' | '<<' | '>>' | '^' | '|' | '&' + | '=' | '+=' | '/=' | '*=' | '%=' | '>>=' | '<<=' | '-=' | '|=' | '&=' | '^=' + ) + Expr -InferType = - '_' +CastExpr = + Attr* Expr 'as' Type -FnPointerType = - 'const'? 'async'? 'unsafe'? Abi? 'fn' ParamList RetType? +ParenExpr = + Attr* '(' Attr* Expr ')' -ForType = - 'for' GenericParamList Type +ArrayExpr = + Attr* '[' Attr* ( + (Expr (',' Expr)* ','?)? + | Expr ';' Expr + ) ']' -ImplTraitType = - 'impl' TypeBoundList +IndexExpr = + Attr* Expr '[' Expr ']' -DynTraitType = - 'dyn' TypeBoundList +TupleExpr = + Attr* '(' Attr* (Expr (',' Expr)* ','?)? ')' -TypeBoundList = - bounds:(TypeBound ('+' TypeBound)* '+'?) +RecordExpr = + Path RecordExprFieldList -TypeBound = - 'lifetime' -| '?'? Type +RecordExprFieldList = + '{' + Attr* + fields:(RecordExprField (',' RecordExprField)* ','?) + ('..' spread:Expr)? + '}' -TupleExpr = - Attr* '(' Expr* ')' +RecordExprField = + Attr* NameRef (':' Expr)? -ArrayExpr = - Attr* '[' (Expr* | Expr ';' Expr) ']' +CallExpr = + Attr* Expr ArgList -ParenExpr = - Attr* '(' Expr ')' +ArgList = + '(' args:(Expr (',' Expr)* ','?)? ')' -PathExpr = - Path +MethodCallExpr = + Attr* Expr '.' NameRef TypeArgList? ArgList + +FieldExpr = + Attr* Expr '.' NameRef LambdaExpr = Attr* 'static'? 'async'? 'move'? ParamList RetType? body:Expr IfExpr = - Attr* 'if' Condition + Attr* 'if' Condition BlockExpr + ('else' (IfExpr | BlockExpr))? Condition = 'let' Pat '=' Expr | Expr -EffectExpr = - Attr* Label? ('try' | 'unsafe' | 'async') BlockExpr - LoopExpr = Attr* Label? 'loop' - loop_body:BlockExpr? + loop_body:BlockExpr ForExpr = Attr* Label? 'for' Pat 'in' iterable:Expr - loop_body:BlockExpr? + loop_body:BlockExpr WhileExpr = Attr* Label? 'while' Condition loop_body:BlockExpr? -ContinueExpr = - Attr* 'continue' 'lifetime'? +Label = + 'lifetime' BreakExpr = Attr* 'break' 'lifetime'? Expr? -Label = - 'lifetime' - -BlockExpr = - Attr* Label - '{' - statements:Stmt* - Expr? - '}' +ContinueExpr = + Attr* 'continue' 'lifetime'? -ReturnExpr = - Attr* 'return' Expr +RangeExpr = + Attr* Expr? op:('..' | '..=') Expr? -CallExpr = - Attr* Expr ArgList +MatchExpr = + Attr* 'match' Expr MatchArmList -MethodCallExpr = - Attr* Expr '.' NameRef TypeArgList? ArgList +MatchArmList = + '{' + Attr* + arms:MatchArm* + '}' -ArgList = - '(' args:Expr* ')' +MatchArm = + Attr* Pat guard:MatchGuard? '=>' Expr ','? -FieldExpr = - Attr* Expr '.' NameRef +MatchGuard = + 'if' Expr -IndexExpr = - Attr* '[' ']' +ReturnExpr = + Attr* 'return' Expr? AwaitExpr = Attr* Expr '.' 'await' -TryExpr = - Attr* Expr '?' +BoxExpr = + Attr* 'box' Expr -CastExpr = - Attr* Expr 'as' Type +Type = + ArrayType +| DynTraitType +| FnPointerType +| ForType +| ImplTraitType +| InferType +| NeverType +| ParenType +| PathType +| PointerType +| ReferenceType +| SliceType +| TupleType -RefExpr = - Attr* '&' ('raw' | 'mut' | 'const') Expr +ParenType = + '(' Type ')' -PrefixExpr = - Attr* Expr +NeverType = + '!' -BoxExpr = - Attr* 'box' Expr +PathType = + Path -RangeExpr = - Attr* +TupleType = + '(' fields:(Type (',' Type)* ','?)? ')' -BinExpr = - Attr* +PointerType = + '*' ('const' | 'mut') Type -Literal = - 'int_number' +ReferenceType = + '&' 'lifetime'? 'mut'? Type -MatchExpr = - Attr* 'match' Expr MatchArmList +ArrayType = + '[' Type ';' Expr ']' -MatchArmList = - '{' arms:MatchArm* '}' +SliceType = + '[' Type ']' -MatchArm = - Attr* Pat guard:MatchGuard? '=>' Expr +InferType = + '_' -MatchGuard = - 'if' Expr +FnPointerType = + 'const'? 'async'? 'unsafe'? Abi? 'fn' ParamList RetType? -RecordExpr = - Path RecordExprFieldList +ForType = + 'for' GenericParamList Type -RecordExprFieldList = - '{' - fields:RecordExprField* - ('..' spread:Expr)? - '}' +ImplTraitType = + 'impl' TypeBoundList -RecordExprField = - Attr* NameRef (':' Expr)? +DynTraitType = + 'dyn' TypeBoundList + +TypeBoundList = + bounds:(TypeBound ('+' TypeBound)* '+'?) + +TypeBound = + 'lifetime' +| '?'? Type OrPat = Pat* @@ -510,36 +559,3 @@ Pat = | RangePat | LiteralPat | 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 -- cgit v1.2.3