Skip to content

Commit 739f22f

Browse files
committed
auto merge of #13037 : alexcrichton/rust/attr-syntax, r=brson
This will require a snapshot to finish, but these commits update the parser to parse attributes of the form `#![...]` Thanks to @thehydroimpulse for all the initial work! cc #2569
2 parents 6eae7df + 84a91b8 commit 739f22f

File tree

14 files changed

+111
-22
lines changed

14 files changed

+111
-22
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

+26-18
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ impl<'a> ParserAttr for Parser<'a> {
3838
attrs.push(self.parse_attribute(false));
3939
}
4040
token::POUND => {
41-
if self.look_ahead(1, |t| *t != token::LBRACKET) {
42-
break;
43-
}
4441
attrs.push(self.parse_attribute(false));
4542
}
4643
token::DOC_COMMENT(s) => {
@@ -61,40 +58,55 @@ impl<'a> ParserAttr for Parser<'a> {
6158
return attrs;
6259
}
6360

64-
// matches attribute = # [ meta_item ]
61+
// matches attribute = # ! [ meta_item ]
6562
//
66-
// if permit_inner is true, then a trailing `;` indicates an inner
63+
// if permit_inner is true, then a leading `!` indicates an inner
6764
// attribute
6865
fn parse_attribute(&mut self, permit_inner: bool) -> ast::Attribute {
6966
debug!("parse_attributes: permit_inner={:?} self.token={:?}",
7067
permit_inner, self.token);
71-
let (span, value) = match self.token {
68+
let (span, value, mut style) = match self.token {
7269
INTERPOLATED(token::NtAttr(attr)) => {
7370
assert!(attr.node.style == ast::AttrOuter);
7471
self.bump();
75-
(attr.span, attr.node.value)
72+
(attr.span, attr.node.value, ast::AttrOuter)
7673
}
7774
token::POUND => {
7875
let lo = self.span.lo;
7976
self.bump();
77+
78+
let style = if self.eat(&token::NOT) {
79+
if !permit_inner {
80+
self.span_err(self.span,
81+
"an inner attribute is not permitted in \
82+
this context");
83+
}
84+
ast::AttrInner
85+
} else {
86+
ast::AttrOuter
87+
};
88+
8089
self.expect(&token::LBRACKET);
8190
let meta_item = self.parse_meta_item();
8291
self.expect(&token::RBRACKET);
92+
8393
let hi = self.span.hi;
84-
(mk_sp(lo, hi), meta_item)
94+
(mk_sp(lo, hi), meta_item, style)
8595
}
8696
_ => {
8797
let token_str = self.this_token_to_str();
8898
self.fatal(format!("expected `\\#` but found `{}`",
8999
token_str));
90100
}
91101
};
92-
let style = if permit_inner && self.token == token::SEMI {
93-
self.bump();
94-
ast::AttrInner
95-
} else {
96-
ast::AttrOuter
97-
};
102+
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+
}
109+
98110
return Spanned {
99111
span: span,
100112
node: ast::Attribute_ {
@@ -125,10 +137,6 @@ impl<'a> ParserAttr for Parser<'a> {
125137
self.parse_attribute(true)
126138
}
127139
token::POUND => {
128-
if self.look_ahead(1, |t| *t != token::LBRACKET) {
129-
// This is an extension
130-
break;
131-
}
132140
self.parse_attribute(true)
133141
}
134142
token::DOC_COMMENT(s) => {

src/libsyntax/parse/comments.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -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,

src/libsyntax/parse/lexer.rs

+25-1
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

@@ -271,9 +272,11 @@ pub fn bump(rdr: &StringReader) {
271272
rdr.curr.set(None);
272273
}
273274
}
275+
274276
pub fn is_eof(rdr: &StringReader) -> bool {
275277
rdr.curr.get().is_none()
276278
}
279+
277280
pub fn nextch(rdr: &StringReader) -> Option<char> {
278281
let offset = byte_offset(rdr, rdr.pos.get()).to_uint();
279282
if offset < rdr.filemap.deref().src.len() {
@@ -286,6 +289,21 @@ pub fn nextch_is(rdr: &StringReader, c: char) -> bool {
286289
nextch(rdr) == Some(c)
287290
}
288291

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+
289307
fn hex_digit_val(c: Option<char>) -> int {
290308
let d = c.unwrap_or('\x00');
291309

@@ -370,6 +388,12 @@ fn consume_any_line_comment(rdr: &StringReader)
370388
}
371389
} else if rdr.curr_is('#') {
372390
if nextch_is(rdr, '!') {
391+
392+
// Parse an inner attribute.
393+
if nextnextch_is(rdr, '[') {
394+
return None;
395+
}
396+
373397
// I guess this is the only way to figure out if
374398
// we're at the beginning of the file...
375399
let cmap = CodeMap::new();

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

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {}
12+
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

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[foo(bar)]
12+
mod foo {
13+
#![feature(globs)]
14+
}
15+
16+
pub fn main() {}

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

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

src/test/run-pass/attr.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// ignore-fast
12+
13+
#[main]
14+
fn foo() {
15+
}

0 commit comments

Comments
 (0)