@@ -110,6 +110,7 @@ type arg_or_capture_item = Either<arg, ()>;
110110type item_info = ( ident , item_ , Option < ~[ attribute ] > ) ;
111111
112112pub enum item_or_view_item {
113+ // indicates a failure to parse any kind of item:
113114 iovi_none,
114115 iovi_item( @item) ,
115116 iovi_foreign_item( @foreign_item ) ,
@@ -2666,7 +2667,8 @@ pub impl Parser {
26662667 _ => None
26672668 }
26682669 }
2669- _ => fail ! ( )
2670+ _ => self . bug (
2671+ ~"is_ident ( ) said this would be an identifier")
26702672 } ;
26712673
26722674 match maybe_bound {
@@ -3204,9 +3206,12 @@ pub impl Parser {
32043206 self . eat_keyword ( ~"static ")
32053207 }
32063208
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
32073211 fn parse_mod_items ( term : token:: Token ,
32083212 +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.
32103215 let ParsedItemsAndViewItems {
32113216 attrs_remaining : attrs_remaining,
32123217 view_items : view_items,
@@ -3217,6 +3222,9 @@ pub impl Parser {
32173222 true ) ;
32183223 let mut items: ~[ @item] = starting_items;
32193224
3225+ // looks like this code depends on the invariant that
3226+ // outer attributes can't occur on view items (or macros
3227+ // invocations?)
32203228 let mut first = true ;
32213229 while self . token != term {
32223230 let mut attrs = self . parse_outer_attributes ( ) ;
@@ -3751,6 +3759,8 @@ pub impl Parser {
37513759 }
37523760 }
37533761
3762+ // parse one of the items or view items allowed by the
3763+ // flags; on failure, return iovi_none.
37543764 fn parse_item_or_view_item ( +attrs : ~[ attribute ] , items_allowed : bool ,
37553765 foreign_items_allowed : bool ,
37563766 macros_allowed : bool )
@@ -3770,14 +3780,17 @@ pub impl Parser {
37703780 }
37713781
37723782 if items_allowed && self . eat_keyword ( ~"const ") {
3783+ // CONST ITEM
37733784 let ( ident, item_, extra_attrs) = self . parse_item_const ( ) ;
37743785 return iovi_item ( self . mk_item ( lo, self . last_span . hi , ident, item_,
37753786 visibility,
37763787 maybe_append ( attrs, extra_attrs) ) ) ;
37773788 } else if foreign_items_allowed && self . is_keyword ( ~"const ") {
3789+ // FOREIGN CONST ITEM
37783790 let item = self . parse_item_foreign_const ( visibility, attrs) ;
37793791 return iovi_foreign_item ( item) ;
37803792 } else if items_allowed &&
3793+ // FUNCTION ITEM (not sure about lookahead condition...)
37813794 self . is_keyword ( ~"fn ") &&
37823795 !self . fn_expr_lookahead ( self . look_ahead ( 1 u) ) {
37833796 self . bump ( ) ;
@@ -3786,6 +3799,7 @@ pub impl Parser {
37863799 visibility,
37873800 maybe_append ( attrs, extra_attrs) ) ) ;
37883801 } else if items_allowed && self . eat_keyword ( ~"pure") {
3802+ // PURE FUNCTION ITEM
37893803 self . expect_keyword ( ~"fn ") ;
37903804 let ( ident, item_, extra_attrs) = self . parse_item_fn ( pure_fn) ;
37913805 return iovi_item ( self . mk_item ( lo, self . last_span . hi , ident, item_,
@@ -3794,10 +3808,12 @@ pub impl Parser {
37943808 } else if foreign_items_allowed &&
37953809 ( self . is_keyword ( ~"fn ") || self . is_keyword ( ~"pure") ||
37963810 self . is_keyword ( ~"unsafe ") ) {
3811+ // FOREIGN FUNCTION ITEM (no items allowed)
37973812 let item = self . parse_item_foreign_fn ( attrs) ;
37983813 return iovi_foreign_item ( item) ;
37993814 } else if items_allowed && self . is_keyword ( ~"unsafe ")
38003815 && self . look_ahead ( 1 u) != token:: LBRACE {
3816+ // UNSAFE FUNCTION ITEM (where items are allowed)
38013817 self . bump ( ) ;
38023818 self . expect_keyword ( ~"fn ") ;
38033819 let ( ident, item_, extra_attrs) = self . parse_item_fn ( unsafe_fn) ;
@@ -3806,46 +3822,55 @@ pub impl Parser {
38063822 maybe_append ( attrs, extra_attrs) ) ) ;
38073823 } else if self . eat_keyword ( ~"extern ") {
38083824 if items_allowed && self.eat_keyword(~" fn ") {
3825+ // EXTERN FUNCTION ITEM
38093826 let ( ident, item_, extra_attrs) =
38103827 self . parse_item_fn ( extern_fn) ;
38113828 return iovi_item ( self . mk_item ( lo, self . last_span . hi , ident,
38123829 item_, visibility,
38133830 maybe_append ( attrs,
38143831 extra_attrs) ) ) ;
38153832 }
3833+ // EXTERN MODULE ITEM
38163834 return self . parse_item_foreign_mod ( lo, visibility, attrs,
38173835 items_allowed) ;
38183836 } else if items_allowed && self . eat_keyword ( ~"mod ") {
3837+ // MODULE ITEM
38193838 let ( ident, item_, extra_attrs) = self . parse_item_mod ( attrs) ;
38203839 return iovi_item ( self . mk_item ( lo, self . last_span . hi , ident, item_,
38213840 visibility,
38223841 maybe_append ( attrs, extra_attrs) ) ) ;
38233842 } else if items_allowed && self . eat_keyword ( ~"type ") {
3843+ // TYPE ITEM
38243844 let ( ident, item_, extra_attrs) = self . parse_item_type ( ) ;
38253845 return iovi_item ( self . mk_item ( lo, self . last_span . hi , ident, item_,
38263846 visibility,
38273847 maybe_append ( attrs, extra_attrs) ) ) ;
38283848 } else if items_allowed && self . eat_keyword ( ~"enum ") {
3849+ // ENUM ITEM
38293850 let ( ident, item_, extra_attrs) = self . parse_item_enum ( ) ;
38303851 return iovi_item ( self . mk_item ( lo, self . last_span . hi , ident, item_,
38313852 visibility,
38323853 maybe_append ( attrs, extra_attrs) ) ) ;
38333854 } else if items_allowed && self . eat_keyword ( ~"trait ") {
3855+ // TRAIT ITEM
38343856 let ( ident, item_, extra_attrs) = self . parse_item_trait ( ) ;
38353857 return iovi_item ( self . mk_item ( lo, self . last_span . hi , ident, item_,
38363858 visibility,
38373859 maybe_append ( attrs, extra_attrs) ) ) ;
38383860 } else if items_allowed && self . eat_keyword ( ~"impl ") {
3861+ // IMPL ITEM
38393862 let ( ident, item_, extra_attrs) = self . parse_item_impl ( ) ;
38403863 return iovi_item ( self . mk_item ( lo, self . last_span . hi , ident, item_,
38413864 visibility,
38423865 maybe_append ( attrs, extra_attrs) ) ) ;
38433866 } else if items_allowed && self . eat_keyword ( ~"struct ") {
3867+ // STRUCT ITEM
38443868 let ( ident, item_, extra_attrs) = self . parse_item_struct ( ) ;
38453869 return iovi_item ( self . mk_item ( lo, self . last_span . hi , ident, item_,
38463870 visibility,
38473871 maybe_append ( attrs, extra_attrs) ) ) ;
38483872 } else if self . eat_keyword ( ~"use ") {
3873+ // USE ITEM
38493874 let view_item = self . parse_use ( ) ;
38503875 self . expect ( token:: SEMI ) ;
38513876 return iovi_view_item ( @ast:: view_item {
@@ -3859,6 +3884,7 @@ pub impl Parser {
38593884 && ( is_plain_ident ( self . look_ahead ( 2 ) )
38603885 || self . look_ahead ( 2 ) == token:: LPAREN
38613886 || self . look_ahead ( 2 ) == token:: LBRACE ) {
3887+ // MACRO INVOCATION ITEM
38623888 if attrs. len ( ) > 0 {
38633889 self . fatal ( ~"attrs on macros are not yet supported") ;
38643890 }
@@ -3875,6 +3901,7 @@ pub impl Parser {
38753901 } else {
38763902 token:: special_idents:: invalid // no special identifier
38773903 } ;
3904+ // eat a matched-delimiter token tree:
38783905 let tts = match self . token {
38793906 token:: LPAREN | token:: LBRACE => {
38803907 let ket = token:: flip_delimiter ( copy self . token ) ;
@@ -3884,6 +3911,7 @@ pub impl Parser {
38843911 }
38853912 _ => self . fatal ( ~"expected open delimiter")
38863913 } ;
3914+ // single-variant-enum... :
38873915 let m = ast:: mac_invoc_tt ( pth, tts) ;
38883916 let m: ast:: mac = codemap:: spanned { node : m,
38893917 span : mk_sp ( self . span . lo ,
@@ -3892,6 +3920,7 @@ pub impl Parser {
38923920 return iovi_item ( self . mk_item ( lo, self . last_span . hi , id, item_,
38933921 visibility, attrs) ) ;
38943922 } else {
3923+ // FAILURE TO PARSE ITEM
38953924 if visibility != inherited {
38963925 let mut s = ~"unmatched visibility `";
38973926 s += if visibility == public { ~"pub " } else { ~"priv" } ;
@@ -4030,6 +4059,7 @@ pub impl Parser {
40304059 self.token_is_keyword(~" mod", next_tok) )
40314060 }
40324061
4062+ // parse a view item.
40334063 fn parse_view_item ( +attrs : ~[ attribute ] , vis : visibility ) -> @view_item {
40344064 let lo = self . span . lo ;
40354065 let node = if self . eat_keyword ( ~"use ") {
@@ -4040,7 +4070,7 @@ pub impl Parser {
40404070 let metadata = self . parse_optional_meta ( ) ;
40414071 view_item_extern_mod ( ident, metadata, self . get_id ( ) )
40424072 } else {
4043- fail! ( ) ;
4073+ self . bug ( ~"expected view item" ) ;
40444074 } ;
40454075 self . expect ( token:: SEMI ) ;
40464076 @ast:: view_item { node: node,
@@ -4049,6 +4079,8 @@ pub impl Parser {
40494079 span: mk_sp ( lo, self . last_span . hi ) }
40504080 }
40514081
4082+ // Parses a sequence of items. Stops when it finds program
4083+ // text that can't be parsed as an item
40524084 fn parse_items_and_view_items( +first_item_attrs: ~[ attribute] ,
40534085 mode: view_item_parse_mode,
40544086 macros_allowed: bool )
@@ -4114,8 +4146,11 @@ pub impl Parser {
41144146 // Parses a source module as a crate
41154147 fn parse_crate_mod( _cfg: crate_cfg) -> @crate {
41164148 let lo = self . span . lo ;
4149+ // parse the crate's inner attrs, maybe (oops) one
4150+ // of the attrs of an item:
41174151 let ( inner, next) = self . parse_inner_attrs_and_next ( ) ;
41184152 let first_item_outer_attrs = next;
4153+ // parse the items inside the crate:
41194154 let m = self . parse_mod_items ( token:: EOF , first_item_outer_attrs) ;
41204155 @spanned ( lo, self . span . lo ,
41214156 ast:: crate_ { module : m,
0 commit comments