@@ -866,7 +866,10 @@ impl Parser {
866
866
867
867
*/
868
868
869
- let opt_abis = self . parse_opt_abis ( ) ;
869
+ let opt_abis = if self . eat_keyword ( keywords:: Extern ) {
870
+ self . parse_opt_abis ( )
871
+ } else { None } ;
872
+
870
873
let abis = opt_abis. unwrap_or ( AbiSet :: Rust ( ) ) ;
871
874
let purity = self . parse_unsafety ( ) ;
872
875
self . expect_keyword ( keywords:: Fn ) ;
@@ -4308,91 +4311,78 @@ impl Parser {
4308
4311
}
4309
4312
}
4310
4313
4311
- // parse extern foo; or extern mod foo { ... } or extern { ... }
4312
- fn parse_item_foreign_mod( & mut self ,
4313
- lo: BytePos ,
4314
- opt_abis: Option < AbiSet > ,
4315
- visibility: Visibility ,
4316
- attrs: ~[ Attribute ] ,
4317
- items_allowed: bool )
4318
- -> ItemOrViewItem {
4319
- let mut must_be_named_mod = false ;
4320
- if self . is_keyword ( keywords:: Mod ) {
4321
- must_be_named_mod = true ;
4322
- self . expect_keyword ( keywords:: Mod ) ;
4323
- } else if self . token != token:: LBRACE {
4324
- let token_str = self . this_token_to_str ( ) ;
4325
- self . span_fatal ( self . span ,
4326
- format ! ( "expected `\\ {` or `mod` but found `{}`" ,
4327
- token_str) )
4328
- }
4314
+ /// Parse extern crate links
4315
+ ///
4316
+ /// # Example
4317
+ ///
4318
+ /// extern crate extra;
4319
+ /// extern crate foo = "bar";
4320
+ fn parse_item_extern_crate( & mut self ,
4321
+ lo: BytePos ,
4322
+ visibility: Visibility ,
4323
+ attrs: ~[ Attribute ] )
4324
+ -> ItemOrViewItem {
4329
4325
4330
- let ( named , maybe_path, ident) = match self . token {
4326
+ let ( maybe_path, ident) = match self . token {
4331
4327
token : : IDENT ( ..) => {
4332
4328
let the_ident = self . parse_ident( ) ;
4329
+ self . expect_one_of( & [ ] , & [ token:: EQ , token:: SEMI ] ) ;
4333
4330
let path = if self . token == token:: EQ {
4334
4331
self. bump( ) ;
4335
4332
Some ( self . parse_str( ) )
4336
- }
4337
- else { None } ;
4338
- ( true , path, the_ident)
4333
+ } else { None} ;
4334
+
4335
+ self . expect( & token:: SEMI ) ;
4336
+ ( path, the_ident)
4339
4337
}
4340
4338
_ => {
4341
- if must_be_named_mod {
4342
- let token_str = self . this_token_to_str ( ) ;
4343
- self . span_fatal ( self . span ,
4344
- format ! ( "expected foreign module name but \
4345
- found `{}`",
4346
- token_str) )
4347
- }
4348
-
4349
- ( false , None ,
4350
- special_idents:: clownshoes_foreign_mod)
4339
+ let token_str = self . this_token_to_str ( ) ;
4340
+ self . span_fatal ( self . span ,
4341
+ format ! ( "expected extern crate name but found `{}`" ,
4342
+ token_str) ) ;
4351
4343
}
4352
4344
} ;
4353
4345
4354
- // extern mod foo { ... } or extern { ... }
4355
- if items_allowed && self . eat ( & token:: LBRACE ) {
4356
- // `extern mod foo { ... }` is obsolete.
4357
- if named {
4358
- self . obsolete ( self . last_span , ObsoleteNamedExternModule ) ;
4359
- }
4360
-
4361
- let abis = opt_abis. unwrap_or ( AbiSet :: C ( ) ) ;
4346
+ IoviViewItem ( ast:: ViewItem {
4347
+ node : ViewItemExternMod ( ident, maybe_path, ast:: DUMMY_NODE_ID ) ,
4348
+ attrs : attrs,
4349
+ vis : visibility,
4350
+ span : mk_sp ( lo, self . last_span . hi )
4351
+ } )
4352
+ }
4362
4353
4363
- let ( inner, next) = self . parse_inner_attrs_and_next ( ) ;
4364
- let m = self . parse_foreign_mod_items ( abis, next) ;
4365
- self . expect ( & token:: RBRACE ) ;
4354
+ /// Parse `extern` for foreign ABIs
4355
+ /// modules.
4356
+ ///
4357
+ /// `extern` is expected to have been
4358
+ /// consumed before calling this method
4359
+ ///
4360
+ /// # Examples:
4361
+ ///
4362
+ /// extern "C" {}
4363
+ /// extern {}
4364
+ fn parse_item_foreign_mod ( & mut self ,
4365
+ lo : BytePos ,
4366
+ opt_abis : Option < AbiSet > ,
4367
+ visibility : Visibility ,
4368
+ attrs : ~[ Attribute ] )
4369
+ -> ItemOrViewItem {
4366
4370
4367
- let item = self . mk_item ( lo,
4368
- self . last_span . hi ,
4369
- ident,
4370
- ItemForeignMod ( m) ,
4371
- visibility,
4372
- maybe_append ( attrs, Some ( inner) ) ) ;
4373
- return IoviItem ( item) ;
4374
- }
4371
+ self . expect ( & token:: LBRACE ) ;
4375
4372
4376
- if opt_abis. is_some ( ) {
4377
- self . span_err ( self . span , "an ABI may not be specified here" ) ;
4378
- }
4373
+ let abis = opt_abis. unwrap_or ( AbiSet :: C ( ) ) ;
4379
4374
4375
+ let ( inner, next) = self . parse_inner_attrs_and_next ( ) ;
4376
+ let m = self . parse_foreign_mod_items ( abis, next) ;
4377
+ self . expect ( & token:: RBRACE ) ;
4380
4378
4381
- if self . token == token:: LPAREN {
4382
- // `extern mod foo (name = "bar"[,vers = "version"]) is obsolete,
4383
- // `extern mod foo = "bar#[version]";` should be used.
4384
- // Parse obsolete options to avoid wired parser errors
4385
- self . parse_optional_meta ( ) ;
4386
- self . obsolete ( self . span , ObsoleteExternModAttributesInParens ) ;
4387
- }
4388
- // extern mod foo;
4389
- self . expect ( & token:: SEMI ) ;
4390
- IoviViewItem ( ast:: ViewItem {
4391
- node : ViewItemExternMod ( ident, maybe_path, ast:: DUMMY_NODE_ID ) ,
4392
- attrs : attrs,
4393
- vis : visibility,
4394
- span : mk_sp ( lo, self . last_span . hi )
4395
- } )
4379
+ let item = self . mk_item ( lo,
4380
+ self . last_span . hi ,
4381
+ special_idents:: clownshoes_foreign_mod,
4382
+ ItemForeignMod ( m) ,
4383
+ visibility,
4384
+ maybe_append ( attrs, Some ( inner) ) ) ;
4385
+ return IoviItem ( item) ;
4396
4386
}
4397
4387
4398
4388
// parse type Foo = Bar;
@@ -4504,10 +4494,6 @@ impl Parser {
4504
4494
// Parses a string as an ABI spec on an extern type or module. Consumes
4505
4495
// the `extern` keyword, if one is found.
4506
4496
fn parse_opt_abis ( & mut self ) -> Option < AbiSet > {
4507
- if !self . eat_keyword ( keywords:: Extern ) {
4508
- return None
4509
- }
4510
-
4511
4497
match self . token {
4512
4498
token:: LIT_STR ( s)
4513
4499
| token:: LIT_STR_RAW ( s, _) => {
@@ -4585,7 +4571,20 @@ impl Parser {
4585
4571
} ) ;
4586
4572
}
4587
4573
// either a view item or an item:
4588
- if self . is_keyword ( keywords:: Extern ) {
4574
+ if self . eat_keyword ( keywords:: Extern ) {
4575
+ let next_is_mod = self . eat_keyword ( keywords:: Mod ) ;
4576
+
4577
+ if next_is_mod || self . eat_keyword ( keywords:: Crate ) {
4578
+ // NOTE(flaper87): Uncomment this when this changes gets into stage0
4579
+ //
4580
+ // if next_is_mod {
4581
+ // self.span_err(self.span,
4582
+ // format!("`extern mod` is obsolete, use `extern crate` instead \
4583
+ // to refer to external crates."))
4584
+ // }
4585
+ return self . parse_item_extern_crate ( lo, visibility, attrs) ;
4586
+ }
4587
+
4589
4588
let opt_abis = self . parse_opt_abis ( ) ;
4590
4589
4591
4590
if self . eat_keyword ( keywords:: Fn ) {
@@ -4600,12 +4599,15 @@ impl Parser {
4600
4599
visibility,
4601
4600
maybe_append ( attrs, extra_attrs) ) ;
4602
4601
return IoviItem ( item) ;
4603
- } else {
4604
- // EXTERN MODULE ITEM (IoviViewItem)
4605
- return self . parse_item_foreign_mod ( lo, opt_abis, visibility, attrs,
4606
- true ) ;
4602
+ } else if self . token == token:: LBRACE {
4603
+ return self . parse_item_foreign_mod ( lo, opt_abis, visibility, attrs) ;
4607
4604
}
4605
+
4606
+ let token_str = self . this_token_to_str ( ) ;
4607
+ self . span_fatal ( self . span ,
4608
+ format ! ( "expected `\\ {` or `fn` but found `{}`" , token_str) ) ;
4608
4609
}
4610
+
4609
4611
// the rest are all guaranteed to be items:
4610
4612
if self . is_keyword ( keywords:: Static ) {
4611
4613
// STATIC ITEM
0 commit comments