@@ -864,6 +864,7 @@ gen_expression(lhs,rhs,rhs2) :: { Expr Span }
864
864
: ntExpr { $1 }
865
865
| lit_expr { $1 }
866
866
| ' [' sep_byT(expr,' ,' ) ']' { Vec [] $2 ($1 # $>) }
867
+ | ' [' inner_attrs sep_byT (expr,' ,' ) ']' { Vec (toList $2 ) $3 ($1 # $>) }
867
868
| ' [' expr ' ;' expr ' ]' { Repeat [] $2 $4 ($1 # $>) }
868
869
| expr_mac { MacExpr [] $1 (spanOf $1 ) }
869
870
| expr_path %prec PATH { PathExpr [] Nothing $1 (spanOf $1 ) }
@@ -1000,24 +1001,26 @@ lit_expr :: { Expr Span }
1000
1001
-- one to omit the separating ';' after 'if', 'match', 'loop', 'for', 'while'"
1001
1002
block_expr :: { Expr Span }
1002
1003
: block_like_expr { $1 }
1003
- | block { BlockExpr [] $ 1 (spanOf $ 1 ) }
1004
+ | inner_attrs_block { let (as,b) = $ 1 in BlockExpr as b (spanOf b ) }
1004
1005
1005
1006
-- Any expression ending in a ' { ... }' block except a block itself.
1006
1007
block_like_expr :: { Expr Span }
1007
1008
: if_expr { $1 }
1008
- | loop block { Loop [] $2 Nothing ($1 # $>) }
1009
- | lifetime ' :' loop block { Loop [] $4 (Just $1 ) ($1 # $>) }
1010
- | for pat in nostruct_expr block { ForLoop [] $2 $4 $5 Nothing ($1 # $>) }
1011
- | lifetime ' :' for pat in nostruct_expr block { ForLoop [] $4 $6 $7 (Just $1 ) ($1 # $>) }
1012
- | while nostruct_expr block { While [] $2 $3 Nothing ($1 # $>) }
1013
- | lifetime ' :' while nostruct_expr block { While [] $4 $5 (Just $1 ) ($1 # $>) }
1014
- | while let pat ' =' nostruct_expr block { WhileLet [] $3 $5 $6 Nothing ($1 # $>) }
1015
- | lifetime ' :' while let pat ' =' nostruct_expr block { WhileLet [] $5 $7 $8 (Just $1 ) ($1 # $>) }
1016
- | match nostruct_expr ' {' ' }' { Match [] $2 [] ($1 # $>) }
1017
- | match nostruct_expr ' {' arms ' }' { Match [] $2 $4 ($1 # $>) }
1009
+ | loop inner_attrs_block { let (as,b) = $> in Loop as b Nothing ($1 # b) }
1010
+ | lifetime ' :' loop inner_attrs_block { let (as,b) = $> in Loop as b (Just $1 ) ($1 # b) }
1011
+ | for pat in nostruct_expr inner_attrs_block { let (as,b) = $> in ForLoop as $2 $4 b Nothing ($1 # b) }
1012
+ | lifetime ' :' for pat in nostruct_expr inner_attrs_block { let (as,b) = $> in ForLoop as $4 $6 b (Just $1 ) ($1 # b) }
1013
+ | while nostruct_expr inner_attrs_block { let (as,b) = $> in While as $2 b Nothing ($1 # b) }
1014
+ | lifetime ' :' while nostruct_expr inner_attrs_block { let (as,b) = $> in While as $4 b (Just $1 ) ($1 # b) }
1015
+ | while let pat ' =' nostruct_expr inner_attrs_block { let (as,b) = $> in WhileLet as $3 $5 b Nothing ($1 # b) }
1016
+ | lifetime ' :' while let pat ' =' nostruct_expr inner_attrs_block { let (as,b) = $> in WhileLet as $5 $7 b (Just $1 ) ($1 # b) }
1017
+ | match nostruct_expr ' {' ' }' { Match [] $2 [] ($1 # $>) }
1018
+ | match nostruct_expr ' {' inner_attrs ' }' { Match (toList $4 ) $2 [] ($1 # $>) }
1019
+ | match nostruct_expr ' {' arms ' }' { Match [] $2 $4 ($1 # $>) }
1020
+ | match nostruct_expr ' {' inner_attrs arms ' }' { Match (toList $4 ) $2 $5 ($1 # $>) }
1018
1021
| expr_path ' !' ' {' many(token_tree) ' }' { MacExpr [] (Mac $1 $4 ($1 # $>)) ($1 # $>) }
1019
- | unsafe block { BlockExpr [] $ 2 { rules = Unsafe } ($1 # $> ) }
1020
- | do catch block { Catch [] $ 3 ($1 # $> ) }
1022
+ | unsafe inner_attrs_block { let (as,b) = $> in BlockExpr as b { rules = Unsafe } ($1 # b ) }
1023
+ | do catch inner_attrs_block { let (as,b) = $> in Catch as b ($1 # b ) }
1021
1024
1022
1025
-- ' if' expressions are a bit special since they can have an arbitrary number of ' else if' chains.
1023
1026
if_expr :: { Expr Span }
@@ -1053,18 +1056,21 @@ expr_arms :: { (Expr Span, [Arm Span]) }
1053
1056
| paren_expr comma_arms { ($1 , $2 ) }
1054
1057
| struct_expr comma_arms { ($1 , $2 ) }
1055
1058
| block_like_expr comma_arms { ($1 , $2 ) }
1056
- | block comma_arms { (BlockExpr [] $ 1 (spanOf $ 1 ), $2 ) }
1057
- | block arms { (BlockExpr [] $ 1 (spanOf $ 1 ), $2 ) }
1059
+ | inner_attrs_block comma_arms { let (as,b) = $ 1 in (BlockExpr as b (spanOf b ), $2 ) }
1060
+ | inner_attrs_block arms { let (as,b) = $ 1 in (BlockExpr as b (spanOf b ), $2 ) }
1058
1061
1059
1062
1060
1063
-- As per https:// github.com/rust-lang/rust/issues/15701 (as of March 10 2017), the only way to have
1061
1064
-- attributes on expressions should be with inner attributes on a paren expression.
1062
1065
paren_expr :: { Expr Span }
1063
- : ' (' ' )' { TupExpr [] [] ($1 # $2 ) }
1064
- | ' (' expr ' )' { ParenExpr [] $2 ($1 # $3 ) }
1065
- | ' (' inner_attrs expr ' )' { ParenExpr (toList $2 ) $3 ($1 # $4 ) }
1066
- | ' (' expr ' ,' ' )' { TupExpr [] [$2 ] ($1 # $4 ) }
1067
- | ' (' expr ' ,' sep_by1T(expr,' ,' ) ' )' { TupExpr [] ($2 : toList $4 ) ($1 # $5 ) }
1066
+ : ' (' ' )' { TupExpr [] [] ($1 # $>) }
1067
+ | ' (' inner_attrs ' )' { TupExpr (toList $2 ) [] ($1 # $>) }
1068
+ | ' (' expr ' )' { ParenExpr [] $2 ($1 # $>) }
1069
+ | ' (' inner_attrs expr ' )' { ParenExpr (toList $2 ) $3 ($1 # $>) }
1070
+ | ' (' expr ' ,' ' )' { TupExpr [] [$2 ] ($1 # $>) }
1071
+ | ' (' inner_attrs expr ' ,' ' )' { TupExpr (toList $2 ) [$3 ] ($1 # $>) }
1072
+ | ' (' expr ' ,' sep_by1T(expr,' ,' ) ' )' { TupExpr [] ($2 : toList $4 ) ($1 # $>) }
1073
+ | ' (' inner_attrs expr ' ,' sep_by1T(expr,' ,' ) ' )' { TupExpr (toList $2 ) ($3 : toList $5 ) ($1 # $>) }
1068
1074
1069
1075
1070
1076
-- Closure ending in blocks
@@ -1087,9 +1093,12 @@ arg :: { Arg Span }
1087
1093
1088
1094
-- Struct expression literal
1089
1095
struct_expr :: { Expr Span }
1090
- : expr_path ' {' ' ..' expr ' }' { Struct [] $1 [] (Just $4 ) ($1 # $>) }
1091
- | expr_path ' {' sep_by1(field,' ,' ) ' ,' ' ..' expr ' }' { Struct [] $1 (toList $3 ) (Just $6 ) ($1 # $>) }
1092
- | expr_path ' {' sep_byT(field,' ,' ) ' }' { Struct [] $1 $3 Nothing ($1 # $>) }
1096
+ : expr_path ' {' ' ..' expr ' }' { Struct [] $1 [] (Just $4 ) ($1 # $>) }
1097
+ | expr_path ' {' inner_attrs ' ..' expr ' }' { Struct (toList $3 ) $1 [] (Just $5 ) ($1 # $>) }
1098
+ | expr_path ' {' sep_by1(field,' ,' ) ' ,' ' ..' expr ' }' { Struct [] $1 (toList $3 ) (Just $6 ) ($1 # $>) }
1099
+ | expr_path ' {' inner_attrs sep_by1 (field,' ,' ) ',' '..' expr '}' { Struct (toList $3) $1 (toList $4) (Just $7) ($1 # $>) }
1100
+ | expr_path ' {' sep_byT(field,' ,' ) ' }' { Struct [] $1 $3 Nothing ($1 # $>) }
1101
+ | expr_path ' {' inner_attrs sep_byT(field,' ,' ) ' }' { Struct (toList $3) $1 $4 Nothing ($1 # $>) }
1093
1102
1094
1103
field :: { Field Span }
1095
1104
: ident ' :' expr { Field (unspan $1) (Just $3) ($1 # $3) }
@@ -1126,14 +1135,14 @@ initializer :: { Maybe (Expr Span) }
1126
1135
1127
1136
1128
1137
block :: { Block Span }
1129
- : ntBlock { $1 }
1130
- | ' {' ' }' { Block [] Normal ($1 # $>) }
1131
- | ' {' stmts_possibly_no_semi ' }' { Block [ s | Just s <- $2 ] Normal ($1 # $>) }
1138
+ : ntBlock { $1 }
1139
+ | ' {' ' }' { Block [] Normal ($1 # $>) }
1140
+ | ' {' stmts_possibly_no_semi ' }' { Block [ s | Just s <- $2 ] Normal ($1 # $>) }
1132
1141
1133
1142
inner_attrs_block :: { ([Attribute Span], Block Span) }
1134
- : block { ([], $1 ) }
1135
- | ' {' inner_attrs ' }' { (toList $2 , Block [] Normal ($1 # $>)) }
1136
- | ' {' inner_attrs stmts_possibly_no_semi ' }' { (toList $2 , Block [ s | Just s <- $3 ] Normal ($1 # $>)) }
1143
+ : block { ([], $1) }
1144
+ | ' {' inner_attrs ' }' { (toList $2, Block [] Normal ($1 # $>)) }
1145
+ | ' {' inner_attrs stmts_possibly_no_semi ' }' { (toList $2, Block [ s | Just s <- $3 ] Normal ($1 # $>)) }
1137
1146
1138
1147
1139
1148
-----------
@@ -1143,12 +1152,20 @@ inner_attrs_block :: { ([Attribute Span], Block Span) }
1143
1152
item :: { Item Span }
1144
1153
: ntItem { $1 }
1145
1154
| stmt_item { $1 }
1146
- | expr_path ' !' ident ' [' many(token_tree) ' ]' ' ;' { Item (unspan $3 ) [] (MacItem (Mac $1 $5 ($1 # $>))) InheritedV ($1 # $>) }
1155
+ | expr_path ' !' ident ' [' many(token_tree) ' ]' ' ;' { macroItem (unspan $3) (Mac $1 $5 ($1 # $>)) ($1 # $>) }
1156
+ | expr_path ' !' ' [' many(token_tree) ' ]' ' ;' { macroItem "" (Mac $1 $4 ($1 # $>)) ($1 # $>) }
1157
+ | expr_path ' !' ident ' (' many(token_tree) ' )' ' ;' { macroItem (unspan $3) (Mac $1 $5 ($1 # $>)) ($1 # $>) }
1158
+ | expr_path ' !' ' (' many(token_tree) ' )' ' ;' { macroItem "" (Mac $1 $4 ($1 # $>)) ($1 # $>) }
1159
+ | expr_path ' !' ident ' {' many(token_tree) ' }' { macroItem (unspan $3) (Mac $1 $5 ($1 # $>)) ($1 # $>) }
1160
+ | expr_path ' !' ' {' many(token_tree) ' }' { macroItem "" (Mac $1 $4 ($1 # $>)) ($1 # $>) }
1161
+
1162
+ {- | expr_path ' !' ident ' [' many(token_tree) ' ]' ' ;' { Item (unspan $3) [] (MacItem (Mac $1 $5 ($1 # $>))) InheritedV ($1 # $>) }
1147
1163
| expr_path ' !' ' [' many(token_tree) ' ]' ' ;' { Item "" [] (MacItem (Mac $1 $4 ($1 # $>))) InheritedV ($1 # $>) }
1148
1164
| expr_path ' !' ident ' (' many(token_tree) ' )' ' ;' { Item (unspan $3) [] (MacItem (Mac $1 $5 ($1 # $>))) InheritedV ($1 # $>) }
1149
1165
| expr_path ' !' ' (' many(token_tree) ' )' ' ;' { Item "" [] (MacItem (Mac $1 $4 ($1 # $>))) InheritedV ($1 # $>) }
1150
1166
| expr_path ' !' ident ' {' many(token_tree) ' }' { Item (unspan $3) [] (MacItem (Mac $1 $5 ($1 # $>))) InheritedV ($1 # $>) }
1151
1167
| expr_path ' !' ' {' many(token_tree) ' }' { Item "" [] (MacItem (Mac $1 $4 ($1 # $>))) InheritedV ($1 # $>) }
1168
+ -}
1152
1169
1153
1170
mod_item :: { Item Span }
1154
1171
: many(outer_attribute) vis item { let Item i a n _ _ = $3 in Item i ($1 ++ a) n (unspan $2) ($1 # $2 # $3) }
@@ -1487,6 +1504,7 @@ token :: { Spanned Token }
1487
1504
-- Weak keywords, have special meaning only in specific contexts.
1488
1505
| default { $1 }
1489
1506
| union { $1 }
1507
+ | catch { $1 }
1490
1508
-- Comments
1491
1509
| outerDoc { $1 }
1492
1510
| innerDoc { $1 }
@@ -1543,6 +1561,11 @@ toStmt (MacExpr a m s) hasSemi isBlock | hasSemi = MacStmt m SemicolonMac a
1543
1561
| isBlock = MacStmt m BracesMac a
1544
1562
toStmt e hasSemi _ = (if hasSemi then Semi else NoSemi) e
1545
1563
1564
+ -- | Make a macro item, which may be a ' MacroDef'
1565
+ macroItem :: Ident -> Mac Span -> Span -> Item Span
1566
+ macroItem i (Mac (Path False (("macro_rules", NoParameters _) :| []) _) tts _) x = Item i [] (MacroDef tts) InheritedV x
1567
+ macroItem i mac x = Item i [] (MacItem mac) InheritedV x
1568
+
1546
1569
-- | Add attributes to an expression
1547
1570
addAttrs :: [Attribute Span] -> Expr Span -> Expr Span
1548
1571
addAttrs as (Box as' e s) = Box (as ++ as' ) e s
@@ -1565,6 +1588,7 @@ addAttrs as (Loop as' b l s) = Loop (as ++ as') b l s
1565
1588
addAttrs as (Match as' e a s) = Match (as ++ as' ) e a s
1566
1589
addAttrs as (Closure as' c f e s) = Closure (as ++ as' ) c f e s
1567
1590
addAttrs as (BlockExpr as' b s) = BlockExpr (as ++ as' ) b s
1591
+ addAttrs as (Catch as' b s) = Catch (as ++ as' ) b s
1568
1592
addAttrs as (Assign as' e1 e2 s) = Assign (as ++ as' ) e1 e2 s
1569
1593
addAttrs as (AssignOp as' b e1 e2 s) = AssignOp (as ++ as' ) b e1 e2 s
1570
1594
addAttrs as (FieldAccess as' e i s) = FieldAccess (as ++ as' ) e i s
@@ -1573,7 +1597,7 @@ addAttrs as (Index as' e1 e2 s) = Index (as ++ as') e1 e2 s
1573
1597
addAttrs as (Range as' e1 e2 r s) = Range (as ++ as' ) e1 e2 r s
1574
1598
addAttrs as (PathExpr as' q p s) = PathExpr (as ++ as' ) q p s
1575
1599
addAttrs as (AddrOf as' m e s) = AddrOf (as ++ as' ) m e s
1576
- addAttrs as (Break as' l e s) = Break (as ++ as' ) l e s
1600
+ addAttrs as (Break as' l e s) = Break (as ++ as' ) l e s
1577
1601
addAttrs as (Continue as' l s) = Continue (as ++ as' ) l s
1578
1602
addAttrs as (Ret as' e s) = Ret (as ++ as' ) e s
1579
1603
addAttrs as (InlineAsmExpr as' a s) = InlineAsmExpr (as ++ as' ) a s
0 commit comments