@@ -124,12 +124,13 @@ type item_info = (ident, item_, Option<~[attribute]>);
124
124
enum item_or_view_item {
125
125
iovi_none,
126
126
iovi_item( @item) ,
127
+ iovi_foreign_item( @foreign_item ) ,
127
128
iovi_view_item( @view_item )
128
129
}
129
130
130
131
enum view_item_parse_mode {
131
132
VIEW_ITEMS_AND_ITEMS_ALLOWED ,
132
- VIEW_ITEMS_ALLOWED ,
133
+ VIEW_ITEMS_AND_FOREIGN_ITEMS_ALLOWED ,
133
134
IMPORTS_AND_ITEMS_ALLOWED
134
135
}
135
136
@@ -2184,7 +2185,7 @@ impl Parser {
2184
2185
2185
2186
let item_attrs = vec:: append ( first_item_attrs, item_attrs) ;
2186
2187
2187
- match self . parse_item_or_view_item ( item_attrs, true ) {
2188
+ match self . parse_item_or_view_item ( item_attrs, true , false ) {
2188
2189
iovi_item( i) => {
2189
2190
let mut hi = i. span . hi ;
2190
2191
let decl = @spanned ( lo, hi, decl_item ( i) ) ;
@@ -2194,6 +2195,9 @@ impl Parser {
2194
2195
self . span_fatal ( vi. span , ~"view items must be declared at \
2195
2196
the top of the block") ;
2196
2197
}
2198
+ iovi_foreign_item( _) => {
2199
+ self . fatal ( ~"foreign items are not allowed here") ;
2200
+ }
2197
2201
iovi_none( ) => { /* fallthrough */ }
2198
2202
}
2199
2203
@@ -2259,7 +2263,7 @@ impl Parser {
2259
2263
let mut stmts = ~[ ] ;
2260
2264
let mut expr = None ;
2261
2265
2262
- let { attrs_remaining, view_items, items: items } =
2266
+ let { attrs_remaining, view_items, items: items , _ } =
2263
2267
self . parse_items_and_view_items ( first_item_attrs,
2264
2268
IMPORTS_AND_ITEMS_ALLOWED ) ;
2265
2269
@@ -2844,7 +2848,7 @@ impl Parser {
2844
2848
fn parse_mod_items ( term : token:: Token ,
2845
2849
+first_item_attrs : ~[ attribute ] ) -> _mod {
2846
2850
// Shouldn't be any view items since we've already parsed an item attr
2847
- let { attrs_remaining, view_items, items: starting_items } =
2851
+ let { attrs_remaining, view_items, items: starting_items , _ } =
2848
2852
self . parse_items_and_view_items ( first_item_attrs,
2849
2853
VIEW_ITEMS_AND_ITEMS_ALLOWED ) ;
2850
2854
let mut items: ~[ @item] = move starting_items;
@@ -2858,7 +2862,7 @@ impl Parser {
2858
2862
}
2859
2863
debug ! ( "parse_mod_items: parse_item_or_view_item(attrs=%?)" ,
2860
2864
attrs) ;
2861
- match self . parse_item_or_view_item ( attrs, true ) {
2865
+ match self . parse_item_or_view_item ( attrs, true , false ) {
2862
2866
iovi_item( item) => items. push ( item) ,
2863
2867
iovi_view_item( view_item) => {
2864
2868
self . span_fatal ( view_item. span , ~"view items must be \
@@ -2958,11 +2962,11 @@ impl Parser {
2958
2962
+first_item_attrs : ~[ attribute ] ) ->
2959
2963
foreign_mod {
2960
2964
// Shouldn't be any view items since we've already parsed an item attr
2961
- let { attrs_remaining, view_items, items: _ } =
2965
+ let { attrs_remaining, view_items, items: _ , foreign_items } =
2962
2966
self . parse_items_and_view_items ( first_item_attrs,
2963
- VIEW_ITEMS_ALLOWED ) ;
2967
+ VIEW_ITEMS_AND_FOREIGN_ITEMS_ALLOWED ) ;
2964
2968
2965
- let mut items: ~[ @foreign_item ] = ~ [ ] ;
2969
+ let mut items: ~[ @foreign_item ] = move foreign_items ;
2966
2970
let mut initial_attrs = attrs_remaining;
2967
2971
while self . token != token:: RBRACE {
2968
2972
let attrs = vec:: append ( initial_attrs,
@@ -2971,7 +2975,7 @@ impl Parser {
2971
2975
items. push ( self . parse_foreign_item ( attrs) ) ;
2972
2976
}
2973
2977
return { sort: sort, view_items: view_items,
2974
- items: items} ;
2978
+ items: items} ;
2975
2979
}
2976
2980
2977
2981
fn parse_item_foreign_mod ( lo : uint ,
@@ -3229,8 +3233,11 @@ impl Parser {
3229
3233
}
3230
3234
}
3231
3235
3232
- fn parse_item_or_view_item ( +attrs : ~[ attribute ] , items_allowed : bool )
3236
+ fn parse_item_or_view_item ( +attrs : ~[ attribute ] , items_allowed : bool ,
3237
+ foreign_items_allowed : bool )
3233
3238
-> item_or_view_item {
3239
+ assert items_allowed != foreign_items_allowed;
3240
+
3234
3241
maybe_whole ! ( iovi self , nt_item) ;
3235
3242
let lo = self . span . lo ;
3236
3243
@@ -3248,6 +3255,9 @@ impl Parser {
3248
3255
return iovi_item ( self . mk_item ( lo, self . last_span . hi , ident, item_,
3249
3256
visibility,
3250
3257
maybe_append ( attrs, extra_attrs) ) ) ;
3258
+ } else if foreign_items_allowed && self . is_keyword ( ~"const ") {
3259
+ let item = self . parse_item_foreign_const ( visibility, attrs) ;
3260
+ return iovi_foreign_item ( item) ;
3251
3261
} else if items_allowed &&
3252
3262
self . is_keyword ( ~"fn ") &&
3253
3263
!self . fn_expr_lookahead ( self . look_ahead ( 1 u) ) {
@@ -3262,6 +3272,10 @@ impl Parser {
3262
3272
return iovi_item ( self . mk_item ( lo, self . last_span . hi , ident, item_,
3263
3273
visibility,
3264
3274
maybe_append ( attrs, extra_attrs) ) ) ;
3275
+ } else if foreign_items_allowed &&
3276
+ ( self . is_keyword ( ~"fn ") || self . is_keyword ( ~"pure") ) {
3277
+ let item = self . parse_item_foreign_fn ( visibility, attrs) ;
3278
+ return iovi_foreign_item ( item) ;
3265
3279
} else if items_allowed && self . is_keyword ( ~"unsafe ")
3266
3280
&& self . look_ahead ( 1 u) != token:: LBRACE {
3267
3281
self . bump ( ) ;
@@ -3348,16 +3362,24 @@ impl Parser {
3348
3362
return iovi_item ( self . mk_item ( lo, self . last_span . hi , id, item_,
3349
3363
visibility, attrs) ) ;
3350
3364
} else {
3365
+ if visibility != inherited {
3366
+ let mut s = ~"unmatched visibility `";
3367
+ s += if visibility == public { ~"pub " } else { ~"priv" } ;
3368
+ s += ~"`";
3369
+ self . span_fatal ( copy self . last_span , s) ;
3370
+ }
3351
3371
return iovi_none;
3352
3372
} ;
3353
3373
}
3354
3374
3355
3375
fn parse_item ( +attrs : ~[ attribute ] ) -> Option < @ast:: item > {
3356
- match self . parse_item_or_view_item ( attrs, true ) {
3376
+ match self . parse_item_or_view_item ( attrs, true , false ) {
3357
3377
iovi_none =>
3358
3378
None ,
3359
3379
iovi_view_item( _) =>
3360
3380
self . fatal ( ~"view items are not allowed here") ,
3381
+ iovi_foreign_item( _) =>
3382
+ self . fatal ( ~"foreign items are not allowed here") ,
3361
3383
iovi_item( item) =>
3362
3384
Some ( item)
3363
3385
}
@@ -3492,28 +3514,35 @@ impl Parser {
3492
3514
mode : view_item_parse_mode )
3493
3515
-> { attrs_remaining : ~[ attribute ] ,
3494
3516
view_items : ~[ @view_item ] ,
3495
- items : ~[ @item] } {
3517
+ items : ~[ @item] ,
3518
+ foreign_items : ~[ @foreign_item ] } {
3496
3519
let mut attrs = vec:: append ( first_item_attrs ,
3497
3520
self . parse_outer_attributes ( ) ) ;
3498
3521
3499
- let items_allowed;
3500
- match mode {
3501
- VIEW_ITEMS_AND_ITEMS_ALLOWED | IMPORTS_AND_ITEMS_ALLOWED =>
3502
- items_allowed = true ,
3503
- VIEW_ITEMS_ALLOWED =>
3504
- items_allowed = false
3505
- }
3522
+ let items_allowed = match mode {
3523
+ VIEW_ITEMS_AND_ITEMS_ALLOWED | IMPORTS_AND_ITEMS_ALLOWED => true ,
3524
+ VIEW_ITEMS_AND_FOREIGN_ITEMS_ALLOWED => false
3525
+ } ;
3506
3526
3507
- let ( view_items, items) = ( DVec ( ) , DVec ( ) ) ;
3527
+ let restricted_to_imports = match mode {
3528
+ IMPORTS_AND_ITEMS_ALLOWED => true ,
3529
+ VIEW_ITEMS_AND_ITEMS_ALLOWED |
3530
+ VIEW_ITEMS_AND_FOREIGN_ITEMS_ALLOWED => false
3531
+ } ;
3532
+
3533
+ let foreign_items_allowed = match mode {
3534
+ VIEW_ITEMS_AND_FOREIGN_ITEMS_ALLOWED => true ,
3535
+ VIEW_ITEMS_AND_ITEMS_ALLOWED | IMPORTS_AND_ITEMS_ALLOWED => false
3536
+ } ;
3537
+
3538
+ let ( view_items, items, foreign_items) = ( DVec ( ) , DVec ( ) , DVec ( ) ) ;
3508
3539
loop {
3509
- match self . parse_item_or_view_item ( attrs, items_allowed) {
3540
+ match self . parse_item_or_view_item ( attrs, items_allowed,
3541
+ foreign_items_allowed) {
3510
3542
iovi_none =>
3511
3543
break ,
3512
3544
iovi_view_item( view_item) => {
3513
- match mode {
3514
- VIEW_ITEMS_AND_ITEMS_ALLOWED |
3515
- VIEW_ITEMS_ALLOWED => { }
3516
- IMPORTS_AND_ITEMS_ALLOWED =>
3545
+ if restricted_to_imports {
3517
3546
match view_item. node {
3518
3547
view_item_import( _) => { }
3519
3548
view_item_export( _) | view_item_use( * ) =>
@@ -3528,13 +3557,18 @@ impl Parser {
3528
3557
assert items_allowed;
3529
3558
items. push ( item)
3530
3559
}
3560
+ iovi_foreign_item( foreign_item) => {
3561
+ assert foreign_items_allowed;
3562
+ foreign_items. push ( foreign_item) ;
3563
+ }
3531
3564
}
3532
3565
attrs = self . parse_outer_attributes ( ) ;
3533
3566
}
3534
3567
3535
3568
{ attrs_remaining: attrs,
3536
3569
view_items: dvec:: unwrap ( move view_items) ,
3537
- items: dvec:: unwrap ( move items) }
3570
+ items: dvec:: unwrap ( move items) ,
3571
+ foreign_items: dvec:: unwrap ( move foreign_items) }
3538
3572
}
3539
3573
3540
3574
// Parses a source module as a crate
0 commit comments