@@ -110,6 +110,7 @@ type arg_or_capture_item = Either<arg, ()>;
110
110
type item_info = ( ident , item_ , Option < ~[ attribute ] > ) ;
111
111
112
112
pub enum item_or_view_item {
113
+ // indicates a failure to parse any kind of item:
113
114
iovi_none,
114
115
iovi_item( @item) ,
115
116
iovi_foreign_item( @foreign_item ) ,
@@ -2666,7 +2667,8 @@ pub impl Parser {
2666
2667
_ => None
2667
2668
}
2668
2669
}
2669
- _ => fail ! ( )
2670
+ _ => self . bug (
2671
+ ~"is_ident ( ) said this would be an identifier")
2670
2672
} ;
2671
2673
2672
2674
match maybe_bound {
@@ -3204,9 +3206,12 @@ pub impl Parser {
3204
3206
self . eat_keyword ( ~"static ")
3205
3207
}
3206
3208
3209
+ // given a termination token and a vector of already-parsed
3210
+ // attributes (of length 0 or 1), parse all of the items in a module
3207
3211
fn parse_mod_items ( term : token:: Token ,
3208
3212
+first_item_attrs : ~[ attribute ] ) -> _mod {
3209
- // Shouldn't be any view items since we've already parsed an item attr
3213
+ // parse all of the items up to closing or an attribute.
3214
+ // view items are legal here.
3210
3215
let ParsedItemsAndViewItems {
3211
3216
attrs_remaining : attrs_remaining,
3212
3217
view_items : view_items,
@@ -3217,6 +3222,9 @@ pub impl Parser {
3217
3222
true ) ;
3218
3223
let mut items: ~[ @item] = starting_items;
3219
3224
3225
+ // looks like this code depends on the invariant that
3226
+ // outer attributes can't occur on view items (or macros
3227
+ // invocations?)
3220
3228
let mut first = true ;
3221
3229
while self . token != term {
3222
3230
let mut attrs = self . parse_outer_attributes ( ) ;
@@ -3751,6 +3759,8 @@ pub impl Parser {
3751
3759
}
3752
3760
}
3753
3761
3762
+ // parse one of the items or view items allowed by the
3763
+ // flags; on failure, return iovi_none.
3754
3764
fn parse_item_or_view_item ( +attrs : ~[ attribute ] , items_allowed : bool ,
3755
3765
foreign_items_allowed : bool ,
3756
3766
macros_allowed : bool )
@@ -3770,14 +3780,17 @@ pub impl Parser {
3770
3780
}
3771
3781
3772
3782
if items_allowed && self . eat_keyword ( ~"const ") {
3783
+ // CONST ITEM
3773
3784
let ( ident, item_, extra_attrs) = self . parse_item_const ( ) ;
3774
3785
return iovi_item ( self . mk_item ( lo, self . last_span . hi , ident, item_,
3775
3786
visibility,
3776
3787
maybe_append ( attrs, extra_attrs) ) ) ;
3777
3788
} else if foreign_items_allowed && self . is_keyword ( ~"const ") {
3789
+ // FOREIGN CONST ITEM
3778
3790
let item = self . parse_item_foreign_const ( visibility, attrs) ;
3779
3791
return iovi_foreign_item ( item) ;
3780
3792
} else if items_allowed &&
3793
+ // FUNCTION ITEM (not sure about lookahead condition...)
3781
3794
self . is_keyword ( ~"fn ") &&
3782
3795
!self . fn_expr_lookahead ( self . look_ahead ( 1 u) ) {
3783
3796
self . bump ( ) ;
@@ -3786,6 +3799,7 @@ pub impl Parser {
3786
3799
visibility,
3787
3800
maybe_append ( attrs, extra_attrs) ) ) ;
3788
3801
} else if items_allowed && self . eat_keyword ( ~"pure") {
3802
+ // PURE FUNCTION ITEM
3789
3803
self . expect_keyword ( ~"fn ") ;
3790
3804
let ( ident, item_, extra_attrs) = self . parse_item_fn ( pure_fn) ;
3791
3805
return iovi_item ( self . mk_item ( lo, self . last_span . hi , ident, item_,
@@ -3794,10 +3808,12 @@ pub impl Parser {
3794
3808
} else if foreign_items_allowed &&
3795
3809
( self . is_keyword ( ~"fn ") || self . is_keyword ( ~"pure") ||
3796
3810
self . is_keyword ( ~"unsafe ") ) {
3811
+ // FOREIGN FUNCTION ITEM (no items allowed)
3797
3812
let item = self . parse_item_foreign_fn ( attrs) ;
3798
3813
return iovi_foreign_item ( item) ;
3799
3814
} else if items_allowed && self . is_keyword ( ~"unsafe ")
3800
3815
&& self . look_ahead ( 1 u) != token:: LBRACE {
3816
+ // UNSAFE FUNCTION ITEM (where items are allowed)
3801
3817
self . bump ( ) ;
3802
3818
self . expect_keyword ( ~"fn ") ;
3803
3819
let ( ident, item_, extra_attrs) = self . parse_item_fn ( unsafe_fn) ;
@@ -3806,46 +3822,55 @@ pub impl Parser {
3806
3822
maybe_append ( attrs, extra_attrs) ) ) ;
3807
3823
} else if self . eat_keyword ( ~"extern ") {
3808
3824
if items_allowed && self.eat_keyword(~" fn ") {
3825
+ // EXTERN FUNCTION ITEM
3809
3826
let ( ident, item_, extra_attrs) =
3810
3827
self . parse_item_fn ( extern_fn) ;
3811
3828
return iovi_item ( self . mk_item ( lo, self . last_span . hi , ident,
3812
3829
item_, visibility,
3813
3830
maybe_append ( attrs,
3814
3831
extra_attrs) ) ) ;
3815
3832
}
3833
+ // EXTERN MODULE ITEM
3816
3834
return self . parse_item_foreign_mod ( lo, visibility, attrs,
3817
3835
items_allowed) ;
3818
3836
} else if items_allowed && self . eat_keyword ( ~"mod ") {
3837
+ // MODULE ITEM
3819
3838
let ( ident, item_, extra_attrs) = self . parse_item_mod ( attrs) ;
3820
3839
return iovi_item ( self . mk_item ( lo, self . last_span . hi , ident, item_,
3821
3840
visibility,
3822
3841
maybe_append ( attrs, extra_attrs) ) ) ;
3823
3842
} else if items_allowed && self . eat_keyword ( ~"type ") {
3843
+ // TYPE ITEM
3824
3844
let ( ident, item_, extra_attrs) = self . parse_item_type ( ) ;
3825
3845
return iovi_item ( self . mk_item ( lo, self . last_span . hi , ident, item_,
3826
3846
visibility,
3827
3847
maybe_append ( attrs, extra_attrs) ) ) ;
3828
3848
} else if items_allowed && self . eat_keyword ( ~"enum ") {
3849
+ // ENUM ITEM
3829
3850
let ( ident, item_, extra_attrs) = self . parse_item_enum ( ) ;
3830
3851
return iovi_item ( self . mk_item ( lo, self . last_span . hi , ident, item_,
3831
3852
visibility,
3832
3853
maybe_append ( attrs, extra_attrs) ) ) ;
3833
3854
} else if items_allowed && self . eat_keyword ( ~"trait ") {
3855
+ // TRAIT ITEM
3834
3856
let ( ident, item_, extra_attrs) = self . parse_item_trait ( ) ;
3835
3857
return iovi_item ( self . mk_item ( lo, self . last_span . hi , ident, item_,
3836
3858
visibility,
3837
3859
maybe_append ( attrs, extra_attrs) ) ) ;
3838
3860
} else if items_allowed && self . eat_keyword ( ~"impl ") {
3861
+ // IMPL ITEM
3839
3862
let ( ident, item_, extra_attrs) = self . parse_item_impl ( ) ;
3840
3863
return iovi_item ( self . mk_item ( lo, self . last_span . hi , ident, item_,
3841
3864
visibility,
3842
3865
maybe_append ( attrs, extra_attrs) ) ) ;
3843
3866
} else if items_allowed && self . eat_keyword ( ~"struct ") {
3867
+ // STRUCT ITEM
3844
3868
let ( ident, item_, extra_attrs) = self . parse_item_struct ( ) ;
3845
3869
return iovi_item ( self . mk_item ( lo, self . last_span . hi , ident, item_,
3846
3870
visibility,
3847
3871
maybe_append ( attrs, extra_attrs) ) ) ;
3848
3872
} else if self . eat_keyword ( ~"use ") {
3873
+ // USE ITEM
3849
3874
let view_item = self . parse_use ( ) ;
3850
3875
self . expect ( token:: SEMI ) ;
3851
3876
return iovi_view_item ( @ast:: view_item {
@@ -3859,6 +3884,7 @@ pub impl Parser {
3859
3884
&& ( is_plain_ident ( self . look_ahead ( 2 ) )
3860
3885
|| self . look_ahead ( 2 ) == token:: LPAREN
3861
3886
|| self . look_ahead ( 2 ) == token:: LBRACE ) {
3887
+ // MACRO INVOCATION ITEM
3862
3888
if attrs. len ( ) > 0 {
3863
3889
self . fatal ( ~"attrs on macros are not yet supported") ;
3864
3890
}
@@ -3875,6 +3901,7 @@ pub impl Parser {
3875
3901
} else {
3876
3902
token:: special_idents:: invalid // no special identifier
3877
3903
} ;
3904
+ // eat a matched-delimiter token tree:
3878
3905
let tts = match self . token {
3879
3906
token:: LPAREN | token:: LBRACE => {
3880
3907
let ket = token:: flip_delimiter ( copy self . token ) ;
@@ -3884,6 +3911,7 @@ pub impl Parser {
3884
3911
}
3885
3912
_ => self . fatal ( ~"expected open delimiter")
3886
3913
} ;
3914
+ // single-variant-enum... :
3887
3915
let m = ast:: mac_invoc_tt ( pth, tts) ;
3888
3916
let m: ast:: mac = codemap:: spanned { node : m,
3889
3917
span : mk_sp ( self . span . lo ,
@@ -3892,6 +3920,7 @@ pub impl Parser {
3892
3920
return iovi_item ( self . mk_item ( lo, self . last_span . hi , id, item_,
3893
3921
visibility, attrs) ) ;
3894
3922
} else {
3923
+ // FAILURE TO PARSE ITEM
3895
3924
if visibility != inherited {
3896
3925
let mut s = ~"unmatched visibility `";
3897
3926
s += if visibility == public { ~"pub " } else { ~"priv" } ;
@@ -4030,6 +4059,7 @@ pub impl Parser {
4030
4059
self.token_is_keyword(~" mod", next_tok) )
4031
4060
}
4032
4061
4062
+ // parse a view item.
4033
4063
fn parse_view_item ( +attrs : ~[ attribute ] , vis : visibility ) -> @view_item {
4034
4064
let lo = self . span . lo ;
4035
4065
let node = if self . eat_keyword ( ~"use ") {
@@ -4040,7 +4070,7 @@ pub impl Parser {
4040
4070
let metadata = self . parse_optional_meta ( ) ;
4041
4071
view_item_extern_mod ( ident, metadata, self . get_id ( ) )
4042
4072
} else {
4043
- fail! ( ) ;
4073
+ self . bug ( ~"expected view item" ) ;
4044
4074
} ;
4045
4075
self . expect ( token:: SEMI ) ;
4046
4076
@ast:: view_item { node: node,
@@ -4049,6 +4079,8 @@ pub impl Parser {
4049
4079
span: mk_sp ( lo, self . last_span . hi ) }
4050
4080
}
4051
4081
4082
+ // Parses a sequence of items. Stops when it finds program
4083
+ // text that can't be parsed as an item
4052
4084
fn parse_items_and_view_items( +first_item_attrs: ~[ attribute] ,
4053
4085
mode: view_item_parse_mode,
4054
4086
macros_allowed: bool )
@@ -4114,8 +4146,11 @@ pub impl Parser {
4114
4146
// Parses a source module as a crate
4115
4147
fn parse_crate_mod( _cfg: crate_cfg) -> @crate {
4116
4148
let lo = self . span . lo ;
4149
+ // parse the crate's inner attrs, maybe (oops) one
4150
+ // of the attrs of an item:
4117
4151
let ( inner, next) = self . parse_inner_attrs_and_next ( ) ;
4118
4152
let first_item_outer_attrs = next;
4153
+ // parse the items inside the crate:
4119
4154
let m = self . parse_mod_items ( token:: EOF , first_item_outer_attrs) ;
4120
4155
@spanned ( lo, self . span . lo ,
4121
4156
ast:: crate_ { module : m,
0 commit comments