Skip to content

Commit 4e00cf6

Browse files
thehydroimpulsealexcrichton
authored andcommitted
Added new attribute syntax with backward compatibility.
Signed-off-by: Daniel Fagnan <[email protected]>
1 parent 6eae7df commit 4e00cf6

File tree

7 files changed

+98
-11
lines changed

7 files changed

+98
-11
lines changed

src/libsyntax/parse/attr.rs

Lines changed: 27 additions & 9 deletions
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) => {
@@ -68,6 +65,7 @@ impl<'a> ParserAttr for Parser<'a> {
6865
fn parse_attribute(&mut self, permit_inner: bool) -> ast::Attribute {
6966
debug!("parse_attributes: permit_inner={:?} self.token={:?}",
7067
permit_inner, self.token);
68+
let mut warned = false;
7169
let (span, value) = match self.token {
7270
INTERPOLATED(token::NtAttr(attr)) => {
7371
assert!(attr.node.style == ast::AttrOuter);
@@ -77,9 +75,22 @@ impl<'a> ParserAttr for Parser<'a> {
7775
token::POUND => {
7876
let lo = self.span.lo;
7977
self.bump();
78+
79+
if self.eat(&token::NOT) {
80+
if !permit_inner {
81+
self.fatal("an inner attribute was not permitted in this context.");
82+
}
83+
} 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+
}
89+
8090
self.expect(&token::LBRACKET);
8191
let meta_item = self.parse_meta_item();
8292
self.expect(&token::RBRACKET);
93+
8394
let hi = self.span.hi;
8495
(mk_sp(lo, hi), meta_item)
8596
}
@@ -89,12 +100,23 @@ impl<'a> ParserAttr for Parser<'a> {
89100
token_str));
90101
}
91102
};
92-
let style = if permit_inner && self.token == token::SEMI {
93-
self.bump();
103+
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+
94115
ast::AttrInner
95116
} else {
96117
ast::AttrOuter
97118
};
119+
98120
return Spanned {
99121
span: span,
100122
node: ast::Attribute_ {
@@ -125,10 +147,6 @@ impl<'a> ParserAttr for Parser<'a> {
125147
self.parse_attribute(true)
126148
}
127149
token::POUND => {
128-
if self.look_ahead(1, |t| *t != token::LBRACKET) {
129-
// This is an extension
130-
break;
131-
}
132150
self.parse_attribute(true)
133151
}
134152
token::DOC_COMMENT(s) => {

src/libsyntax/parse/comments.rs

Lines changed: 6 additions & 2 deletions
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, is_eof, nextch_is, TokenAndSpan};
15+
use parse::lexer::{StringReader, bump, peek, 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;
@@ -331,7 +331,11 @@ fn consume_comment(rdr: &StringReader,
331331
} else if rdr.curr_is('/') && nextch_is(rdr, '*') {
332332
read_block_comment(rdr, code_to_the_left, comments);
333333
} else if rdr.curr_is('#') && nextch_is(rdr, '!') {
334-
read_shebang_comment(rdr, code_to_the_left, comments);
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+
}
335339
} else { fail!(); }
336340
debug!("<<< consume comment");
337341
}

src/libsyntax/parse/lexer.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,21 @@ pub fn bump(rdr: &StringReader) {
271271
rdr.curr.set(None);
272272
}
273273
}
274+
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+
274285
pub fn is_eof(rdr: &StringReader) -> bool {
275286
rdr.curr.get().is_none()
276287
}
288+
277289
pub fn nextch(rdr: &StringReader) -> Option<char> {
278290
let offset = byte_offset(rdr, rdr.pos.get()).to_uint();
279291
if offset < rdr.filemap.deref().src.len() {
@@ -370,6 +382,12 @@ fn consume_any_line_comment(rdr: &StringReader)
370382
}
371383
} else if rdr.curr_is('#') {
372384
if nextch_is(rdr, '!') {
385+
386+
// Parse an inner attribute.
387+
if peek(rdr, 2).unwrap() == '[' {
388+
return None;
389+
}
390+
373391
// I guess this is the only way to figure out if
374392
// we're at the beginning of the file...
375393
let cmap = CodeMap::new();

src/test/compile-fail/attr.rs

Lines changed: 14 additions & 0 deletions
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 was not permitted in this context.
14+
fn foo() {}

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

Lines changed: 16 additions & 0 deletions
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+
fn main() {}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#![allow(unknown_features)]
2+
#![feature(bogus)]
3+
fn main() { }
4+
// ignore-license

src/test/run-pass/attr.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
#[main]
12+
fn foo() {
13+
}

0 commit comments

Comments
 (0)