2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-05-20 02:07:24 -05:00
|
|
|
use attr;
|
2012-12-23 16:41:37 -06:00
|
|
|
use ast;
|
2014-05-22 07:55:48 -05:00
|
|
|
use codemap::{spanned, Spanned, mk_sp, Span};
|
2012-12-13 15:05:22 -06:00
|
|
|
use parse::common::*; //resolve bug?
|
2012-12-23 16:41:37 -06:00
|
|
|
use parse::token;
|
2015-01-15 21:04:28 -06:00
|
|
|
use parse::parser::{Parser, TokenType};
|
2014-09-13 11:06:01 -05:00
|
|
|
use ptr::P;
|
2014-05-16 02:16:13 -05:00
|
|
|
|
2014-06-09 15:12:30 -05:00
|
|
|
/// A parser that can parse attributes.
|
2014-01-09 07:05:33 -06:00
|
|
|
pub trait ParserAttr {
|
2014-05-16 02:16:13 -05:00
|
|
|
fn parse_outer_attributes(&mut self) -> Vec<ast::Attribute>;
|
2015-03-13 04:34:51 -05:00
|
|
|
fn parse_inner_attributes(&mut self) -> Vec<ast::Attribute>;
|
2013-12-30 16:04:00 -06:00
|
|
|
fn parse_attribute(&mut self, permit_inner: bool) -> ast::Attribute;
|
2014-09-13 11:06:01 -05:00
|
|
|
fn parse_meta_item(&mut self) -> P<ast::MetaItem>;
|
|
|
|
fn parse_meta_seq(&mut self) -> Vec<P<ast::MetaItem>>;
|
|
|
|
fn parse_optional_meta(&mut self) -> Vec<P<ast::MetaItem>>;
|
2012-07-11 17:00:40 -05:00
|
|
|
}
|
|
|
|
|
2014-03-09 09:54:34 -05:00
|
|
|
impl<'a> ParserAttr for Parser<'a> {
|
2014-06-09 15:12:30 -05:00
|
|
|
/// Parse attributes that appear before an item
|
2014-02-28 15:09:09 -06:00
|
|
|
fn parse_outer_attributes(&mut self) -> Vec<ast::Attribute> {
|
|
|
|
let mut attrs: Vec<ast::Attribute> = Vec::new();
|
2012-06-30 05:54:54 -05:00
|
|
|
loop {
|
2014-12-20 02:09:35 -06:00
|
|
|
debug!("parse_outer_attributes: self.token={:?}",
|
2013-08-08 12:28:06 -05:00
|
|
|
self.token);
|
2013-12-30 17:09:41 -06:00
|
|
|
match self.token {
|
2014-10-27 03:22:52 -05:00
|
|
|
token::Pound => {
|
2013-08-08 12:28:06 -05:00
|
|
|
attrs.push(self.parse_attribute(false));
|
2012-06-30 05:54:54 -05:00
|
|
|
}
|
2014-10-27 03:22:52 -05:00
|
|
|
token::DocComment(s) => {
|
2012-06-30 05:54:54 -05:00
|
|
|
let attr = ::attr::mk_sugared_doc_attr(
|
2014-05-20 02:07:24 -05:00
|
|
|
attr::mk_attr_id(),
|
2014-07-06 03:17:59 -05:00
|
|
|
self.id_to_interned_str(s.ident()),
|
2013-02-26 08:35:36 -06:00
|
|
|
self.span.lo,
|
|
|
|
self.span.hi
|
|
|
|
);
|
2013-07-19 06:51:37 -05:00
|
|
|
if attr.node.style != ast::AttrOuter {
|
2013-05-19 00:07:44 -05:00
|
|
|
self.fatal("expected outer comment");
|
2012-06-30 05:54:54 -05:00
|
|
|
}
|
2013-06-11 21:13:42 -05:00
|
|
|
attrs.push(attr);
|
2012-06-30 05:54:54 -05:00
|
|
|
self.bump();
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => break
|
2012-06-30 05:54:54 -05:00
|
|
|
}
|
2012-05-24 15:44:42 -05:00
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return attrs;
|
2012-04-19 22:51:31 -05:00
|
|
|
}
|
|
|
|
|
2014-06-09 15:12:30 -05:00
|
|
|
/// Matches `attribute = # ! [ meta_item ]`
|
|
|
|
///
|
|
|
|
/// If permit_inner is true, then a leading `!` indicates an inner
|
|
|
|
/// attribute
|
2013-12-30 16:04:00 -06:00
|
|
|
fn parse_attribute(&mut self, permit_inner: bool) -> ast::Attribute {
|
2014-12-20 02:09:35 -06:00
|
|
|
debug!("parse_attributes: permit_inner={:?} self.token={:?}",
|
2013-08-08 12:28:06 -05:00
|
|
|
permit_inner, self.token);
|
2014-03-20 13:21:17 -05:00
|
|
|
let (span, value, mut style) = match self.token {
|
2014-10-27 03:22:52 -05:00
|
|
|
token::Pound => {
|
2013-08-08 12:28:06 -05:00
|
|
|
let lo = self.span.lo;
|
|
|
|
self.bump();
|
2014-02-24 20:42:40 -06:00
|
|
|
|
2015-01-15 21:04:28 -06:00
|
|
|
if permit_inner { self.expected_tokens.push(TokenType::Token(token::Not)); }
|
|
|
|
let style = if self.token == token::Not {
|
|
|
|
self.bump();
|
2014-02-24 20:42:40 -06:00
|
|
|
if !permit_inner {
|
2014-06-13 22:48:09 -05:00
|
|
|
let span = self.span;
|
|
|
|
self.span_err(span,
|
2014-03-20 13:21:17 -05:00
|
|
|
"an inner attribute is not permitted in \
|
|
|
|
this context");
|
2015-02-24 08:07:54 -06:00
|
|
|
self.fileline_help(span,
|
2014-11-08 03:23:27 -06:00
|
|
|
"place inner attribute at the top of the module or block");
|
2014-02-24 20:42:40 -06:00
|
|
|
}
|
2014-03-20 13:21:17 -05:00
|
|
|
ast::AttrInner
|
2014-02-24 20:42:40 -06:00
|
|
|
} else {
|
2014-03-20 13:21:17 -05:00
|
|
|
ast::AttrOuter
|
|
|
|
};
|
2014-02-24 20:42:40 -06:00
|
|
|
|
2014-10-29 05:37:54 -05:00
|
|
|
self.expect(&token::OpenDelim(token::Bracket));
|
2013-08-08 12:28:06 -05:00
|
|
|
let meta_item = self.parse_meta_item();
|
2014-07-11 17:26:26 -05:00
|
|
|
let hi = self.span.hi;
|
2014-10-29 05:37:54 -05:00
|
|
|
self.expect(&token::CloseDelim(token::Bracket));
|
2014-02-24 20:42:40 -06:00
|
|
|
|
2014-03-20 13:21:17 -05:00
|
|
|
(mk_sp(lo, hi), meta_item, style)
|
2013-08-08 12:28:06 -05:00
|
|
|
}
|
2014-05-28 11:24:28 -05:00
|
|
|
_ => {
|
2014-06-21 05:39:03 -05:00
|
|
|
let token_str = self.this_token_to_string();
|
2015-02-20 13:08:14 -06:00
|
|
|
self.fatal(&format!("expected `#`, found `{}`", token_str));
|
2014-05-28 11:24:28 -05:00
|
|
|
}
|
2013-08-08 12:28:06 -05:00
|
|
|
};
|
2014-02-24 20:42:40 -06:00
|
|
|
|
2015-01-15 21:04:28 -06:00
|
|
|
if permit_inner && self.token == token::Semi {
|
|
|
|
self.bump();
|
2014-03-21 20:05:05 -05:00
|
|
|
self.span_warn(span, "this inner attribute syntax is deprecated. \
|
2014-11-28 00:01:41 -06:00
|
|
|
The new syntax is `#![foo]`, with a bang and no semicolon");
|
2014-03-20 13:21:17 -05:00
|
|
|
style = ast::AttrInner;
|
|
|
|
}
|
2014-02-24 20:42:40 -06:00
|
|
|
|
2013-08-31 11:13:04 -05:00
|
|
|
return Spanned {
|
2013-08-08 12:28:06 -05:00
|
|
|
span: span,
|
|
|
|
node: ast::Attribute_ {
|
2014-05-20 02:07:24 -05:00
|
|
|
id: attr::mk_attr_id(),
|
2013-08-08 12:28:06 -05:00
|
|
|
style: style,
|
|
|
|
value: value,
|
|
|
|
is_sugared_doc: false
|
|
|
|
}
|
|
|
|
};
|
2012-05-24 15:44:42 -05:00
|
|
|
}
|
2012-04-19 22:51:31 -05:00
|
|
|
|
2014-06-09 15:12:30 -05:00
|
|
|
/// Parse attributes that appear after the opening of an item. These should
|
|
|
|
/// be preceded by an exclamation mark, but we accept and warn about one
|
2015-03-13 04:34:51 -05:00
|
|
|
/// terminated by a semicolon.
|
2014-06-09 15:12:30 -05:00
|
|
|
|
2015-03-13 04:34:51 -05:00
|
|
|
/// matches inner_attrs*
|
|
|
|
fn parse_inner_attributes(&mut self) -> Vec<ast::Attribute> {
|
|
|
|
let mut attrs: Vec<ast::Attribute> = vec![];
|
2012-06-30 05:54:54 -05:00
|
|
|
loop {
|
2015-03-13 04:34:51 -05:00
|
|
|
match self.token {
|
2014-10-27 03:22:52 -05:00
|
|
|
token::Pound => {
|
2015-03-13 04:34:51 -05:00
|
|
|
// Don't even try to parse if it's not an inner attribute.
|
|
|
|
if !self.look_ahead(1, |t| t == &token::Not) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
let attr = self.parse_attribute(true);
|
|
|
|
assert!(attr.node.style == ast::AttrInner);
|
|
|
|
attrs.push(attr);
|
2013-08-08 12:28:06 -05:00
|
|
|
}
|
2014-10-27 03:22:52 -05:00
|
|
|
token::DocComment(s) => {
|
2014-05-22 07:55:48 -05:00
|
|
|
// we need to get the position of this token before we bump.
|
|
|
|
let Span { lo, hi, .. } = self.span;
|
2015-03-13 04:34:51 -05:00
|
|
|
let attr = attr::mk_sugared_doc_attr(attr::mk_attr_id(),
|
|
|
|
self.id_to_interned_str(s.ident()),
|
|
|
|
lo, hi);
|
|
|
|
if attr.node.style == ast::AttrInner {
|
|
|
|
attrs.push(attr);
|
|
|
|
self.bump();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2012-06-30 05:54:54 -05:00
|
|
|
}
|
2015-03-13 04:34:51 -05:00
|
|
|
_ => break
|
2012-05-24 15:44:42 -05:00
|
|
|
}
|
2012-04-19 22:51:31 -05:00
|
|
|
}
|
2015-03-13 04:34:51 -05:00
|
|
|
attrs
|
2012-05-24 15:44:42 -05:00
|
|
|
}
|
|
|
|
|
2014-06-09 15:12:30 -05:00
|
|
|
/// matches meta_item = IDENT
|
|
|
|
/// | IDENT = lit
|
|
|
|
/// | IDENT meta_seq
|
2014-09-13 11:06:01 -05:00
|
|
|
fn parse_meta_item(&mut self) -> P<ast::MetaItem> {
|
|
|
|
let nt_meta = match self.token {
|
2014-10-27 03:22:52 -05:00
|
|
|
token::Interpolated(token::NtMeta(ref e)) => {
|
2014-09-13 11:06:01 -05:00
|
|
|
Some(e.clone())
|
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
};
|
|
|
|
|
|
|
|
match nt_meta {
|
|
|
|
Some(meta) => {
|
2014-03-26 18:14:07 -05:00
|
|
|
self.bump();
|
2014-09-13 11:06:01 -05:00
|
|
|
return meta;
|
2014-03-26 18:14:07 -05:00
|
|
|
}
|
2014-09-13 11:06:01 -05:00
|
|
|
None => {}
|
2014-03-26 18:14:07 -05:00
|
|
|
}
|
|
|
|
|
2012-05-24 15:44:42 -05:00
|
|
|
let lo = self.span.lo;
|
2013-12-30 16:04:00 -06:00
|
|
|
let ident = self.parse_ident();
|
2014-01-08 12:35:15 -06:00
|
|
|
let name = self.id_to_interned_str(ident);
|
2013-12-30 17:09:41 -06:00
|
|
|
match self.token {
|
2014-10-27 03:22:52 -05:00
|
|
|
token::Eq => {
|
2013-02-14 09:34:21 -06:00
|
|
|
self.bump();
|
|
|
|
let lit = self.parse_lit();
|
2013-10-29 19:47:11 -05:00
|
|
|
// FIXME #623 Non-string meta items are not serialized correctly;
|
|
|
|
// just forbid them for now
|
|
|
|
match lit.node {
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::LitStr(..) => {}
|
2013-10-29 19:47:11 -05:00
|
|
|
_ => {
|
|
|
|
self.span_err(
|
|
|
|
lit.span,
|
|
|
|
"non-string literals are not allowed in meta-items");
|
|
|
|
}
|
|
|
|
}
|
2013-04-12 00:10:31 -05:00
|
|
|
let hi = self.span.hi;
|
2014-09-13 11:06:01 -05:00
|
|
|
P(spanned(lo, hi, ast::MetaNameValue(name, lit)))
|
2013-02-14 09:34:21 -06:00
|
|
|
}
|
2014-10-29 05:37:54 -05:00
|
|
|
token::OpenDelim(token::Paren) => {
|
2013-02-14 09:34:21 -06:00
|
|
|
let inner_items = self.parse_meta_seq();
|
2013-04-12 00:10:31 -05:00
|
|
|
let hi = self.span.hi;
|
2014-09-13 11:06:01 -05:00
|
|
|
P(spanned(lo, hi, ast::MetaList(name, inner_items)))
|
2013-02-14 09:34:21 -06:00
|
|
|
}
|
|
|
|
_ => {
|
2013-05-02 12:36:24 -05:00
|
|
|
let hi = self.last_span.hi;
|
2014-09-13 11:06:01 -05:00
|
|
|
P(spanned(lo, hi, ast::MetaWord(name)))
|
2013-02-14 09:34:21 -06:00
|
|
|
}
|
2012-04-19 22:51:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 15:12:30 -05:00
|
|
|
/// matches meta_seq = ( COMMASEP(meta_item) )
|
2014-09-13 11:06:01 -05:00
|
|
|
fn parse_meta_seq(&mut self) -> Vec<P<ast::MetaItem>> {
|
2014-10-29 05:37:54 -05:00
|
|
|
self.parse_seq(&token::OpenDelim(token::Paren),
|
|
|
|
&token::CloseDelim(token::Paren),
|
2014-11-29 22:39:50 -06:00
|
|
|
seq_sep_trailing_allowed(token::Comma),
|
2013-07-02 14:47:32 -05:00
|
|
|
|p| p.parse_meta_item()).node
|
2012-04-19 22:51:31 -05:00
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn parse_optional_meta(&mut self) -> Vec<P<ast::MetaItem>> {
|
2013-12-30 17:09:41 -06:00
|
|
|
match self.token {
|
2014-10-29 05:37:54 -05:00
|
|
|
token::OpenDelim(token::Paren) => self.parse_meta_seq(),
|
2014-02-28 15:09:09 -06:00
|
|
|
_ => Vec::new()
|
2012-08-03 21:59:04 -05:00
|
|
|
}
|
2012-05-24 15:44:42 -05:00
|
|
|
}
|
2012-04-19 22:51:31 -05:00
|
|
|
}
|