mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
parent
8f65dbfcfa
commit
33c6d3fd78
@ -17,6 +17,7 @@ use codemap;
|
|||||||
use parse::lexer::*; //resolve bug?
|
use parse::lexer::*; //resolve bug?
|
||||||
use parse::ParseSess;
|
use parse::ParseSess;
|
||||||
use parse::parser::Parser;
|
use parse::parser::Parser;
|
||||||
|
use parse::attr::parser_attr;
|
||||||
use parse::token::{Token, EOF, to_str, nonterminal, get_ident_interner, ident_to_str};
|
use parse::token::{Token, EOF, to_str, nonterminal, get_ident_interner, ident_to_str};
|
||||||
use parse::token;
|
use parse::token;
|
||||||
|
|
||||||
@ -430,6 +431,7 @@ pub fn parse_nt(p: &Parser, name: &str) -> nonterminal {
|
|||||||
+ token::to_str(get_ident_interner(), p.token))
|
+ token::to_str(get_ident_interner(), p.token))
|
||||||
},
|
},
|
||||||
"path" => token::nt_path(p.parse_path_with_tps(false)),
|
"path" => token::nt_path(p.parse_path_with_tps(false)),
|
||||||
|
"attr" => token::nt_attr(@p.parse_attribute(false)),
|
||||||
"tt" => {
|
"tt" => {
|
||||||
*p.quote_depth += 1u; //but in theory, non-quoted tts might be useful
|
*p.quote_depth += 1u; //but in theory, non-quoted tts might be useful
|
||||||
let res = token::nt_tt(@p.parse_token_tree());
|
let res = token::nt_tt(@p.parse_token_tree());
|
||||||
|
@ -9,21 +9,17 @@
|
|||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
use ast;
|
use ast;
|
||||||
use codemap::spanned;
|
use codemap::{spanned, mk_sp};
|
||||||
use codemap::BytePos;
|
use codemap::BytePos;
|
||||||
use parse::common::*; //resolve bug?
|
use parse::common::*; //resolve bug?
|
||||||
use parse::token;
|
use parse::token;
|
||||||
use parse::parser::Parser;
|
use parse::parser::Parser;
|
||||||
|
use parse::token::INTERPOLATED;
|
||||||
|
|
||||||
// a parser that can parse attributes.
|
// a parser that can parse attributes.
|
||||||
pub trait parser_attr {
|
pub trait parser_attr {
|
||||||
fn parse_outer_attributes(&self) -> ~[ast::Attribute];
|
fn parse_outer_attributes(&self) -> ~[ast::Attribute];
|
||||||
fn parse_attribute(&self, style: ast::AttrStyle) -> ast::Attribute;
|
fn parse_attribute(&self, permit_inner: bool) -> ast::Attribute;
|
||||||
fn parse_attribute_naked(
|
|
||||||
&self,
|
|
||||||
style: ast::AttrStyle,
|
|
||||||
lo: BytePos
|
|
||||||
) -> ast::Attribute;
|
|
||||||
fn parse_inner_attrs_and_next(&self) ->
|
fn parse_inner_attrs_and_next(&self) ->
|
||||||
(~[ast::Attribute], ~[ast::Attribute]);
|
(~[ast::Attribute], ~[ast::Attribute]);
|
||||||
fn parse_meta_item(&self) -> @ast::MetaItem;
|
fn parse_meta_item(&self) -> @ast::MetaItem;
|
||||||
@ -37,12 +33,17 @@ impl parser_attr for Parser {
|
|||||||
fn parse_outer_attributes(&self) -> ~[ast::Attribute] {
|
fn parse_outer_attributes(&self) -> ~[ast::Attribute] {
|
||||||
let mut attrs: ~[ast::Attribute] = ~[];
|
let mut attrs: ~[ast::Attribute] = ~[];
|
||||||
loop {
|
loop {
|
||||||
|
debug!("parse_outer_attributes: self.token=%?",
|
||||||
|
self.token);
|
||||||
match *self.token {
|
match *self.token {
|
||||||
|
token::INTERPOLATED(token::nt_attr(*)) => {
|
||||||
|
attrs.push(self.parse_attribute(false));
|
||||||
|
}
|
||||||
token::POUND => {
|
token::POUND => {
|
||||||
if self.look_ahead(1, |t| *t != token::LBRACKET) {
|
if self.look_ahead(1, |t| *t != token::LBRACKET) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
attrs.push(self.parse_attribute(ast::AttrOuter));
|
attrs.push(self.parse_attribute(false));
|
||||||
}
|
}
|
||||||
token::DOC_COMMENT(s) => {
|
token::DOC_COMMENT(s) => {
|
||||||
let attr = ::attr::mk_sugared_doc_attr(
|
let attr = ::attr::mk_sugared_doc_attr(
|
||||||
@ -62,23 +63,49 @@ impl parser_attr for Parser {
|
|||||||
return attrs;
|
return attrs;
|
||||||
}
|
}
|
||||||
|
|
||||||
// matches attribute = # attribute_naked
|
// matches attribute = # [ meta_item ]
|
||||||
fn parse_attribute(&self, style: ast::AttrStyle) -> ast::Attribute {
|
//
|
||||||
let lo = self.span.lo;
|
// if permit_inner is true, then a trailing `;` indicates an inner
|
||||||
self.expect(&token::POUND);
|
// attribute
|
||||||
return self.parse_attribute_naked(style, lo);
|
fn parse_attribute(&self, permit_inner: bool) -> ast::Attribute {
|
||||||
|
debug!("parse_attributes: permit_inner=%? self.token=%?",
|
||||||
|
permit_inner, self.token);
|
||||||
|
let (span, value) = match *self.token {
|
||||||
|
INTERPOLATED(token::nt_attr(attr)) => {
|
||||||
|
assert!(attr.node.style == ast::AttrOuter);
|
||||||
|
self.bump();
|
||||||
|
(attr.span, attr.node.value)
|
||||||
|
}
|
||||||
|
token::POUND => {
|
||||||
|
let lo = self.span.lo;
|
||||||
|
self.bump();
|
||||||
|
self.expect(&token::LBRACKET);
|
||||||
|
let meta_item = self.parse_meta_item();
|
||||||
|
self.expect(&token::RBRACKET);
|
||||||
|
let hi = self.span.hi;
|
||||||
|
(mk_sp(lo, hi), meta_item)
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
self.fatal(fmt!("expected `#` but found `%s`",
|
||||||
|
self.this_token_to_str()));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let style = if permit_inner && *self.token == token::SEMI {
|
||||||
|
self.bump();
|
||||||
|
ast::AttrInner
|
||||||
|
} else {
|
||||||
|
ast::AttrOuter
|
||||||
|
};
|
||||||
|
return spanned {
|
||||||
|
span: span,
|
||||||
|
node: ast::Attribute_ {
|
||||||
|
style: style,
|
||||||
|
value: value,
|
||||||
|
is_sugared_doc: false
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// matches attribute_naked = [ meta_item ]
|
|
||||||
fn parse_attribute_naked(&self, style: ast::AttrStyle, lo: BytePos) ->
|
|
||||||
ast::Attribute {
|
|
||||||
self.expect(&token::LBRACKET);
|
|
||||||
let meta_item = self.parse_meta_item();
|
|
||||||
self.expect(&token::RBRACKET);
|
|
||||||
let hi = self.span.hi;
|
|
||||||
return spanned(lo, hi, ast::Attribute_ { style: style,
|
|
||||||
value: meta_item, is_sugared_doc: false }); }
|
|
||||||
|
|
||||||
// Parse attributes that appear after the opening of an item, each
|
// Parse attributes that appear after the opening of an item, each
|
||||||
// terminated by a semicolon. In addition to a vector of inner attributes,
|
// terminated by a semicolon. In addition to a vector of inner attributes,
|
||||||
// this function also returns a vector that may contain the first outer
|
// this function also returns a vector that may contain the first outer
|
||||||
@ -89,47 +116,37 @@ impl parser_attr for Parser {
|
|||||||
// matches inner_attrs* outer_attr?
|
// matches inner_attrs* outer_attr?
|
||||||
// you can make the 'next' field an Option, but the result is going to be
|
// you can make the 'next' field an Option, but the result is going to be
|
||||||
// more useful as a vector.
|
// more useful as a vector.
|
||||||
fn parse_inner_attrs_and_next(&self) ->
|
fn parse_inner_attrs_and_next(&self)
|
||||||
(~[ast::Attribute], ~[ast::Attribute]) {
|
-> (~[ast::Attribute], ~[ast::Attribute]) {
|
||||||
let mut inner_attrs: ~[ast::Attribute] = ~[];
|
let mut inner_attrs: ~[ast::Attribute] = ~[];
|
||||||
let mut next_outer_attrs: ~[ast::Attribute] = ~[];
|
let mut next_outer_attrs: ~[ast::Attribute] = ~[];
|
||||||
loop {
|
loop {
|
||||||
match *self.token {
|
let attr = match *self.token {
|
||||||
token::POUND => {
|
token::INTERPOLATED(token::nt_attr(*)) => {
|
||||||
if self.look_ahead(1, |t| *t != token::LBRACKET) {
|
self.parse_attribute(true)
|
||||||
// This is an extension
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
let attr = self.parse_attribute(ast::AttrInner);
|
token::POUND => {
|
||||||
if *self.token == token::SEMI {
|
if self.look_ahead(1, |t| *t != token::LBRACKET) {
|
||||||
|
// This is an extension
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
self.parse_attribute(true)
|
||||||
|
}
|
||||||
|
token::DOC_COMMENT(s) => {
|
||||||
self.bump();
|
self.bump();
|
||||||
inner_attrs.push(attr);
|
::attr::mk_sugared_doc_attr(self.id_to_str(s),
|
||||||
} else {
|
self.span.lo,
|
||||||
// It's not really an inner attribute
|
self.span.hi)
|
||||||
let outer_attr =
|
}
|
||||||
spanned(attr.span.lo, attr.span.hi,
|
_ => {
|
||||||
ast::Attribute_ { style: ast::AttrOuter,
|
|
||||||
value: attr.node.value,
|
|
||||||
is_sugared_doc: false });
|
|
||||||
next_outer_attrs.push(outer_attr);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
token::DOC_COMMENT(s) => {
|
if attr.node.style == ast::AttrInner {
|
||||||
let attr = ::attr::mk_sugared_doc_attr(
|
inner_attrs.push(attr);
|
||||||
self.id_to_str(s),
|
} else {
|
||||||
self.span.lo,
|
next_outer_attrs.push(attr);
|
||||||
self.span.hi
|
break;
|
||||||
);
|
|
||||||
self.bump();
|
|
||||||
if attr.node.style == ast::AttrInner {
|
|
||||||
inner_attrs.push(attr);
|
|
||||||
} else {
|
|
||||||
next_outer_attrs.push(attr);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(inner_attrs, next_outer_attrs)
|
(inner_attrs, next_outer_attrs)
|
||||||
|
@ -129,7 +129,11 @@ impl reader for StringReader {
|
|||||||
|
|
||||||
impl reader for TtReader {
|
impl reader for TtReader {
|
||||||
fn is_eof(@mut self) -> bool { self.cur_tok == token::EOF }
|
fn is_eof(@mut self) -> bool { self.cur_tok == token::EOF }
|
||||||
fn next_token(@mut self) -> TokenAndSpan { tt_next_token(self) }
|
fn next_token(@mut self) -> TokenAndSpan {
|
||||||
|
let r = tt_next_token(self);
|
||||||
|
debug!("TtReader: r=%?", r);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
fn fatal(@mut self, m: ~str) -> ! {
|
fn fatal(@mut self, m: ~str) -> ! {
|
||||||
self.sp_diag.span_fatal(self.cur_span, m);
|
self.sp_diag.span_fatal(self.cur_span, m);
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,8 @@
|
|||||||
// option. This file may not be copied, modified, or distributed
|
// option. This file may not be copied, modified, or distributed
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
|
#[macro_escape];
|
||||||
|
|
||||||
use abi;
|
use abi;
|
||||||
use abi::AbiSet;
|
use abi::AbiSet;
|
||||||
use ast::{Sigil, BorrowedSigil, ManagedSigil, OwnedSigil};
|
use ast::{Sigil, BorrowedSigil, ManagedSigil, OwnedSigil};
|
||||||
@ -4452,7 +4454,17 @@ impl Parser {
|
|||||||
attrs: ~[Attribute],
|
attrs: ~[Attribute],
|
||||||
macros_allowed: bool)
|
macros_allowed: bool)
|
||||||
-> item_or_view_item {
|
-> item_or_view_item {
|
||||||
maybe_whole!(iovi self, nt_item);
|
match *self.token {
|
||||||
|
INTERPOLATED(token::nt_item(item)) => {
|
||||||
|
self.bump();
|
||||||
|
let new_attrs = vec::append(attrs, item.attrs);
|
||||||
|
return iovi_item(@ast::item {
|
||||||
|
attrs: new_attrs,
|
||||||
|
..(*item).clone()});
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
|
||||||
let lo = self.span.lo;
|
let lo = self.span.lo;
|
||||||
|
|
||||||
let visibility = self.parse_non_priv_visibility();
|
let visibility = self.parse_non_priv_visibility();
|
||||||
|
@ -105,6 +105,7 @@ pub enum nonterminal {
|
|||||||
nt_expr(@ast::expr),
|
nt_expr(@ast::expr),
|
||||||
nt_ty( ast::Ty),
|
nt_ty( ast::Ty),
|
||||||
nt_ident(ast::ident, bool),
|
nt_ident(ast::ident, bool),
|
||||||
|
nt_attr(@ast::Attribute), // #[foo]
|
||||||
nt_path( ast::Path),
|
nt_path( ast::Path),
|
||||||
nt_tt( @ast::token_tree), //needs @ed to break a circularity
|
nt_tt( @ast::token_tree), //needs @ed to break a circularity
|
||||||
nt_matchers(~[ast::matcher])
|
nt_matchers(~[ast::matcher])
|
||||||
@ -205,6 +206,7 @@ pub fn to_str(input: @ident_interner, t: &Token) -> ~str {
|
|||||||
INTERPOLATED(ref nt) => {
|
INTERPOLATED(ref nt) => {
|
||||||
match nt {
|
match nt {
|
||||||
&nt_expr(e) => ::print::pprust::expr_to_str(e, input),
|
&nt_expr(e) => ::print::pprust::expr_to_str(e, input),
|
||||||
|
&nt_attr(e) => ::print::pprust::attribute_to_str(e, input),
|
||||||
_ => {
|
_ => {
|
||||||
~"an interpolated " +
|
~"an interpolated " +
|
||||||
match (*nt) {
|
match (*nt) {
|
||||||
@ -212,6 +214,7 @@ pub fn to_str(input: @ident_interner, t: &Token) -> ~str {
|
|||||||
nt_block(*) => ~"block",
|
nt_block(*) => ~"block",
|
||||||
nt_stmt(*) => ~"statement",
|
nt_stmt(*) => ~"statement",
|
||||||
nt_pat(*) => ~"pattern",
|
nt_pat(*) => ~"pattern",
|
||||||
|
nt_attr(*) => fail!("should have been handled"),
|
||||||
nt_expr(*) => fail!("should have been handled above"),
|
nt_expr(*) => fail!("should have been handled above"),
|
||||||
nt_ty(*) => ~"type",
|
nt_ty(*) => ~"type",
|
||||||
nt_ident(*) => ~"identifier",
|
nt_ident(*) => ~"identifier",
|
||||||
|
21
src/test/compile-fail/macro-inner-attributes.rs
Normal file
21
src/test/compile-fail/macro-inner-attributes.rs
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
macro_rules! test ( ($nm:ident,
|
||||||
|
$a:attr,
|
||||||
|
$i:item) => (mod $nm { $a; $i }); )
|
||||||
|
|
||||||
|
test!(a,
|
||||||
|
#[cfg(qux)],
|
||||||
|
pub fn bar() { })
|
||||||
|
|
||||||
|
test!(b,
|
||||||
|
#[cfg(not(qux))],
|
||||||
|
pub fn bar() { })
|
||||||
|
|
||||||
|
#[qux]
|
||||||
|
fn main() {
|
||||||
|
a::bar();
|
||||||
|
//~^ ERROR use of undeclared module `a`
|
||||||
|
//~^^ ERROR unresolved name
|
||||||
|
//~^^^ ERROR unresolved name `a::bar`
|
||||||
|
b::bar();
|
||||||
|
}
|
||||||
|
|
19
src/test/compile-fail/macro-outer-attributes.rs
Normal file
19
src/test/compile-fail/macro-outer-attributes.rs
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
macro_rules! test ( ($nm:ident,
|
||||||
|
$a:attr,
|
||||||
|
$i:item) => (mod $nm { $a $i }); )
|
||||||
|
|
||||||
|
test!(a,
|
||||||
|
#[cfg(qux)],
|
||||||
|
pub fn bar() { })
|
||||||
|
|
||||||
|
test!(b,
|
||||||
|
#[cfg(not(qux))],
|
||||||
|
pub fn bar() { })
|
||||||
|
|
||||||
|
// test1!(#[bar])
|
||||||
|
#[qux]
|
||||||
|
fn main() {
|
||||||
|
a::bar(); //~ ERROR unresolved name `a::bar`
|
||||||
|
b::bar();
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user