Skip to content

Commit 84a91b8

Browse files
committed
syntax: Tidy up parsing the new attribute syntax
1 parent 4e00cf6 commit 84a91b8

File tree

14 files changed

+56
-54
lines changed

14 files changed

+56
-54
lines changed

src/compiletest/compiletest.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#[feature(phase)];
1313

1414
#[allow(non_camel_case_types)];
15+
#[allow(deprecated_owned_vector)]; // NOTE: remove after stage0
1516
#[deny(warnings)];
1617

1718
extern crate test;

src/libarena/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
html_root_url = "http://static.rust-lang.org/doc/master")];
2525
#[allow(missing_doc)];
2626
#[feature(managed_boxes)];
27+
#[allow(deprecated_owned_vector)]; // NOTE: remove after stage0
2728

2829
extern crate collections;
2930

src/libgreen/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@
174174
// NB this does *not* include globs, please keep it that way.
175175
#[feature(macro_rules, phase)];
176176
#[allow(visible_private_types)];
177+
#[allow(deprecated_owned_vector)]; // NOTE: remove after stage0
177178

178179
#[cfg(test)] #[phase(syntax, link)] extern crate log;
179180
extern crate rand;

src/librustuv/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ via `close` and `delete` methods.
4242
#[feature(macro_rules)];
4343
#[deny(unused_result, unused_must_use)];
4444
#[allow(visible_private_types)];
45+
#[allow(deprecated_owned_vector)]; // NOTE: remove after stage0
4546

4647
#[cfg(test)] extern crate green;
4748

src/libsyntax/parse/attr.rs

+18-28
Original file line numberDiff line numberDiff line change
@@ -58,41 +58,40 @@ impl<'a> ParserAttr for Parser<'a> {
5858
return attrs;
5959
}
6060

61-
// matches attribute = # [ meta_item ]
61+
// matches attribute = # ! [ meta_item ]
6262
//
63-
// if permit_inner is true, then a trailing `;` indicates an inner
63+
// if permit_inner is true, then a leading `!` indicates an inner
6464
// attribute
6565
fn parse_attribute(&mut self, permit_inner: bool) -> ast::Attribute {
6666
debug!("parse_attributes: permit_inner={:?} self.token={:?}",
6767
permit_inner, self.token);
68-
let mut warned = false;
69-
let (span, value) = match self.token {
68+
let (span, value, mut style) = match self.token {
7069
INTERPOLATED(token::NtAttr(attr)) => {
7170
assert!(attr.node.style == ast::AttrOuter);
7271
self.bump();
73-
(attr.span, attr.node.value)
72+
(attr.span, attr.node.value, ast::AttrOuter)
7473
}
7574
token::POUND => {
7675
let lo = self.span.lo;
7776
self.bump();
7877

79-
if self.eat(&token::NOT) {
78+
let style = if self.eat(&token::NOT) {
8079
if !permit_inner {
81-
self.fatal("an inner attribute was not permitted in this context.");
80+
self.span_err(self.span,
81+
"an inner attribute is not permitted in \
82+
this context");
8283
}
84+
ast::AttrInner
8385
} else {
84-
warned = true;
85-
// NOTE: uncomment this after a stage0 snap
86-
//self.warn("The syntax for inner attributes have changed.
87-
// Use `#![lang(foo)]` instead.");
88-
}
86+
ast::AttrOuter
87+
};
8988

9089
self.expect(&token::LBRACKET);
9190
let meta_item = self.parse_meta_item();
9291
self.expect(&token::RBRACKET);
9392

9493
let hi = self.span.hi;
95-
(mk_sp(lo, hi), meta_item)
94+
(mk_sp(lo, hi), meta_item, style)
9695
}
9796
_ => {
9897
let token_str = self.this_token_to_str();
@@ -101,21 +100,12 @@ impl<'a> ParserAttr for Parser<'a> {
101100
}
102101
};
103102

104-
let style = if permit_inner {
105-
106-
if self.eat(&token::SEMI) {
107-
// Only warn the user once if the syntax is the old one.
108-
if !warned {
109-
// NOTE: uncomment this after a stage0 snap
110-
//self.warn("This uses the old attribute syntax. Semicolons
111-
// are not longer required.");
112-
}
113-
}
114-
115-
ast::AttrInner
116-
} else {
117-
ast::AttrOuter
118-
};
103+
if permit_inner && self.eat(&token::SEMI) {
104+
// NOTE: uncomment this after a stage0 snap
105+
//self.warn("This uses the old attribute syntax. Semicolons
106+
// are not longer required.");
107+
style = ast::AttrInner;
108+
}
119109

120110
return Spanned {
121111
span: span,

src/libsyntax/parse/comments.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use ast;
1212
use codemap::{BytePos, CharPos, CodeMap, Pos};
1313
use diagnostic;
1414
use parse::lexer::{is_whitespace, with_str_from, Reader};
15-
use parse::lexer::{StringReader, bump, peek, is_eof, nextch_is, TokenAndSpan};
15+
use parse::lexer::{StringReader, bump, is_eof, nextch_is, TokenAndSpan};
1616
use parse::lexer::{is_line_non_doc_comment, is_block_non_doc_comment};
1717
use parse::lexer;
1818
use parse::token;
@@ -319,7 +319,9 @@ fn read_block_comment(rdr: &StringReader,
319319
fn peeking_at_comment(rdr: &StringReader) -> bool {
320320
return (rdr.curr_is('/') && nextch_is(rdr, '/')) ||
321321
(rdr.curr_is('/') && nextch_is(rdr, '*')) ||
322-
(rdr.curr_is('#') && nextch_is(rdr, '!'));
322+
// consider shebangs comments, but not inner attributes
323+
(rdr.curr_is('#') && nextch_is(rdr, '!') &&
324+
!lexer::nextnextch_is(rdr, '['));
323325
}
324326

325327
fn consume_comment(rdr: &StringReader,
@@ -331,11 +333,7 @@ fn consume_comment(rdr: &StringReader,
331333
} else if rdr.curr_is('/') && nextch_is(rdr, '*') {
332334
read_block_comment(rdr, code_to_the_left, comments);
333335
} else if rdr.curr_is('#') && nextch_is(rdr, '!') {
334-
// Make sure the following token is **not** the beginning
335-
// of an inner attribute, which starts with the same syntax.
336-
if peek(rdr, 2).unwrap() != '[' {
337-
read_shebang_comment(rdr, code_to_the_left, comments);
338-
}
336+
read_shebang_comment(rdr, code_to_the_left, comments);
339337
} else { fail!(); }
340338
debug!("<<< consume comment");
341339
}

src/libsyntax/parse/lexer.rs

+18-12
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ use parse::token::{str_to_ident};
1818

1919
use std::cell::{Cell, RefCell};
2020
use std::char;
21-
use std::rc::Rc;
2221
use std::mem::replace;
2322
use std::num::from_str_radix;
23+
use std::rc::Rc;
24+
use std::str;
2425

2526
pub use ext::tt::transcribe::{TtReader, new_tt_reader};
2627

@@ -272,16 +273,6 @@ pub fn bump(rdr: &StringReader) {
272273
}
273274
}
274275

275-
// EFFECT: Peek 'n' characters ahead.
276-
pub fn peek(rdr: &StringReader, n: uint) -> Option<char> {
277-
let offset = byte_offset(rdr, rdr.pos.get()).to_uint() + (n - 1);
278-
if offset < (rdr.filemap.src).len() {
279-
Some(rdr.filemap.src.char_at(offset))
280-
} else {
281-
None
282-
}
283-
}
284-
285276
pub fn is_eof(rdr: &StringReader) -> bool {
286277
rdr.curr.get().is_none()
287278
}
@@ -298,6 +289,21 @@ pub fn nextch_is(rdr: &StringReader, c: char) -> bool {
298289
nextch(rdr) == Some(c)
299290
}
300291

292+
pub fn nextnextch(rdr: &StringReader) -> Option<char> {
293+
let offset = byte_offset(rdr, rdr.pos.get()).to_uint();
294+
let s = rdr.filemap.deref().src.as_slice();
295+
if offset >= s.len() { return None }
296+
let str::CharRange { next, .. } = s.char_range_at(offset);
297+
if next < s.len() {
298+
Some(s.char_at(next))
299+
} else {
300+
None
301+
}
302+
}
303+
pub fn nextnextch_is(rdr: &StringReader, c: char) -> bool {
304+
nextnextch(rdr) == Some(c)
305+
}
306+
301307
fn hex_digit_val(c: Option<char>) -> int {
302308
let d = c.unwrap_or('\x00');
303309

@@ -384,7 +390,7 @@ fn consume_any_line_comment(rdr: &StringReader)
384390
if nextch_is(rdr, '!') {
385391

386392
// Parse an inner attribute.
387-
if peek(rdr, 2).unwrap() == '[' {
393+
if nextnextch_is(rdr, '[') {
388394
return None;
389395
}
390396

src/libtest/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
html_root_url = "http://static.rust-lang.org/doc/master")];
3434

3535
#[feature(asm, macro_rules)];
36+
#[allow(deprecated_owned_vector)]; // NOTE: remove after stage0
3637

3738
extern crate collections;
3839
extern crate getopts;

src/test/compile-fail/attr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010

1111
fn main() {}
1212

13-
#![lang(foo)] //~ ERROR An inner attribute was not permitted in this context.
14-
fn foo() {}
13+
#![lang(foo)] //~ ERROR an inner attribute is not permitted in this context
14+
fn foo() {}

src/test/compile-fail/column-offset-1-based.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
# //~ ERROR 11:1: 11:2 error: expected item
11+
# //~ ERROR 11:1: 11:2 error: expected `[` but found `<eof>`

src/test/compile-fail/issue-1655.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// error-pattern:expected item
11+
// error-pattern:expected `[` but found `~`
1212
mod blade_runner {
1313
#~[doc(
1414
brief = "Blade Runner is probably the best movie ever",

src/test/run-pass/attr-mix-new.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ mod foo {
1313
#![feature(globs)]
1414
}
1515

16-
fn main() {}
16+
pub fn main() {}

src/test/run-pass/attr-shebang.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![allow(unknown_features)]
22
#![feature(bogus)]
3-
fn main() { }
4-
// ignore-license
3+
pub fn main() { }
4+
// ignore-license
5+
// ignore-fast

src/test/run-pass/attr.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// ignore-fast
12+
1113
#[main]
1214
fn foo() {
1315
}

0 commit comments

Comments
 (0)