2012-12-04 00:48:01 +00: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 07:07:24 +00:00
|
|
|
use attr;
|
2012-12-23 22:41:37 +00:00
|
|
|
use ast;
|
2016-06-21 22:08:13 +00:00
|
|
|
use syntax_pos::{mk_sp, Span};
|
|
|
|
use codemap::{spanned, Spanned};
|
2016-02-23 04:24:42 +00:00
|
|
|
use parse::common::SeqSep;
|
2015-10-24 02:02:38 +00:00
|
|
|
use parse::PResult;
|
2012-12-23 22:41:37 +00:00
|
|
|
use parse::token;
|
2015-01-16 03:04:28 +00:00
|
|
|
use parse::parser::{Parser, TokenType};
|
2014-09-13 16:06:01 +00:00
|
|
|
use ptr::P;
|
2014-05-16 07:16:13 +00:00
|
|
|
|
2015-10-24 01:37:21 +00:00
|
|
|
impl<'a> Parser<'a> {
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parse attributes that appear before an item
|
2015-12-20 21:00:43 +00:00
|
|
|
pub fn parse_outer_attributes(&mut self) -> PResult<'a, Vec<ast::Attribute>> {
|
2014-02-28 21:09:09 +00:00
|
|
|
let mut attrs: Vec<ast::Attribute> = Vec::new();
|
2012-06-30 10:54:54 +00:00
|
|
|
loop {
|
2016-01-27 19:42:26 +00:00
|
|
|
debug!("parse_outer_attributes: self.token={:?}", self.token);
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2016-01-27 19:42:26 +00:00
|
|
|
token::Pound => {
|
2016-03-23 03:01:37 +00:00
|
|
|
attrs.push(self.parse_attribute(false)?);
|
2016-01-27 19:42:26 +00:00
|
|
|
}
|
|
|
|
token::DocComment(s) => {
|
|
|
|
let attr = ::attr::mk_sugared_doc_attr(
|
2014-05-20 07:07:24 +00:00
|
|
|
attr::mk_attr_id(),
|
2015-09-24 20:05:02 +00:00
|
|
|
self.id_to_interned_str(ast::Ident::with_empty_ctxt(s)),
|
2013-02-26 14:35:36 +00:00
|
|
|
self.span.lo,
|
|
|
|
self.span.hi
|
|
|
|
);
|
2016-01-27 19:42:26 +00:00
|
|
|
if attr.node.style != ast::AttrStyle::Outer {
|
2016-05-02 05:27:43 +00:00
|
|
|
let mut err = self.fatal("expected outer doc comment");
|
|
|
|
err.note("inner doc comments like this (starting with \
|
|
|
|
`//!` or `/*!`) can only appear before items");
|
|
|
|
return Err(err);
|
2016-01-27 19:42:26 +00:00
|
|
|
}
|
|
|
|
attrs.push(attr);
|
|
|
|
self.bump();
|
2012-06-30 10:54:54 +00:00
|
|
|
}
|
2016-01-27 19:42:26 +00:00
|
|
|
_ => break,
|
2012-06-30 10:54:54 +00:00
|
|
|
}
|
2012-05-24 20:44:42 +00:00
|
|
|
}
|
2015-10-24 02:02:38 +00:00
|
|
|
return Ok(attrs);
|
2012-04-20 03:51:31 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Matches `attribute = # ! [ meta_item ]`
|
|
|
|
///
|
|
|
|
/// If permit_inner is true, then a leading `!` indicates an inner
|
|
|
|
/// attribute
|
2015-12-20 21:00:43 +00:00
|
|
|
pub fn parse_attribute(&mut self, permit_inner: bool) -> PResult<'a, ast::Attribute> {
|
2014-12-20 08:09:35 +00:00
|
|
|
debug!("parse_attributes: permit_inner={:?} self.token={:?}",
|
2016-01-27 19:42:26 +00:00
|
|
|
permit_inner,
|
|
|
|
self.token);
|
2014-03-20 18:21:17 +00:00
|
|
|
let (span, value, mut style) = match self.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Pound => {
|
2013-08-08 17:28:06 +00:00
|
|
|
let lo = self.span.lo;
|
2015-12-30 23:11:53 +00:00
|
|
|
self.bump();
|
2014-02-25 02:42:40 +00:00
|
|
|
|
2016-01-27 19:42:26 +00:00
|
|
|
if permit_inner {
|
|
|
|
self.expected_tokens.push(TokenType::Token(token::Not));
|
|
|
|
}
|
2015-01-16 03:04:28 +00:00
|
|
|
let style = if self.token == token::Not {
|
2015-12-30 23:11:53 +00:00
|
|
|
self.bump();
|
2014-02-25 02:42:40 +00:00
|
|
|
if !permit_inner {
|
2014-06-14 03:48:09 +00:00
|
|
|
let span = self.span;
|
2016-01-27 19:42:26 +00:00
|
|
|
self.diagnostic()
|
|
|
|
.struct_span_err(span,
|
|
|
|
"an inner attribute is not permitted in this context")
|
2016-04-20 18:49:16 +00:00
|
|
|
.help("place inner attribute at the top of the module or \
|
|
|
|
block")
|
2016-01-27 19:42:26 +00:00
|
|
|
.emit()
|
2014-02-25 02:42:40 +00:00
|
|
|
}
|
2015-10-01 16:03:34 +00:00
|
|
|
ast::AttrStyle::Inner
|
2014-02-25 02:42:40 +00:00
|
|
|
} else {
|
2015-10-01 16:03:34 +00:00
|
|
|
ast::AttrStyle::Outer
|
2014-03-20 18:21:17 +00:00
|
|
|
};
|
2014-02-25 02:42:40 +00:00
|
|
|
|
2016-03-23 03:01:37 +00:00
|
|
|
self.expect(&token::OpenDelim(token::Bracket))?;
|
|
|
|
let meta_item = self.parse_meta_item()?;
|
2014-07-11 22:26:26 +00:00
|
|
|
let hi = self.span.hi;
|
2016-03-23 03:01:37 +00:00
|
|
|
self.expect(&token::CloseDelim(token::Bracket))?;
|
2014-02-25 02:42:40 +00:00
|
|
|
|
2014-03-20 18:21:17 +00:00
|
|
|
(mk_sp(lo, hi), meta_item, style)
|
2013-08-08 17:28:06 +00:00
|
|
|
}
|
2014-05-28 16:24:28 +00:00
|
|
|
_ => {
|
2014-06-21 10:39:03 +00:00
|
|
|
let token_str = self.this_token_to_string();
|
2015-10-24 02:02:38 +00:00
|
|
|
return Err(self.fatal(&format!("expected `#`, found `{}`", token_str)));
|
2014-05-28 16:24:28 +00:00
|
|
|
}
|
2013-08-08 17:28:06 +00:00
|
|
|
};
|
2014-02-25 02:42:40 +00:00
|
|
|
|
2015-01-16 03:04:28 +00:00
|
|
|
if permit_inner && self.token == token::Semi {
|
2015-12-30 23:11:53 +00:00
|
|
|
self.bump();
|
2016-01-27 19:42:26 +00:00
|
|
|
self.span_warn(span,
|
|
|
|
"this inner attribute syntax is deprecated. The new syntax is \
|
|
|
|
`#![foo]`, with a bang and no semicolon");
|
2015-10-01 16:03:34 +00:00
|
|
|
style = ast::AttrStyle::Inner;
|
2014-03-20 18:21:17 +00:00
|
|
|
}
|
2014-02-25 02:42:40 +00:00
|
|
|
|
2015-10-24 02:02:38 +00:00
|
|
|
Ok(Spanned {
|
2013-08-08 17:28:06 +00:00
|
|
|
span: span,
|
|
|
|
node: ast::Attribute_ {
|
2014-05-20 07:07:24 +00:00
|
|
|
id: attr::mk_attr_id(),
|
2013-08-08 17:28:06 +00:00
|
|
|
style: style,
|
|
|
|
value: value,
|
2016-01-27 19:42:26 +00:00
|
|
|
is_sugared_doc: false,
|
|
|
|
},
|
2015-10-24 02:02:38 +00:00
|
|
|
})
|
2012-05-24 20:44:42 +00:00
|
|
|
}
|
2012-04-20 03:51:31 +00:00
|
|
|
|
2014-06-09 20:12:30 +00: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 09:34:51 +00:00
|
|
|
/// terminated by a semicolon.
|
2014-06-09 20:12:30 +00:00
|
|
|
|
2015-03-13 09:34:51 +00:00
|
|
|
/// matches inner_attrs*
|
2015-12-20 21:00:43 +00:00
|
|
|
pub fn parse_inner_attributes(&mut self) -> PResult<'a, Vec<ast::Attribute>> {
|
2015-03-13 09:34:51 +00:00
|
|
|
let mut attrs: Vec<ast::Attribute> = vec![];
|
2012-06-30 10:54:54 +00:00
|
|
|
loop {
|
2015-03-13 09:34:51 +00:00
|
|
|
match self.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Pound => {
|
2015-03-13 09:34:51 +00:00
|
|
|
// Don't even try to parse if it's not an inner attribute.
|
|
|
|
if !self.look_ahead(1, |t| t == &token::Not) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-03-23 03:01:37 +00:00
|
|
|
let attr = self.parse_attribute(true)?;
|
2015-10-01 16:03:34 +00:00
|
|
|
assert!(attr.node.style == ast::AttrStyle::Inner);
|
2015-03-13 09:34:51 +00:00
|
|
|
attrs.push(attr);
|
2013-08-08 17:28:06 +00:00
|
|
|
}
|
2014-10-27 08:22:52 +00:00
|
|
|
token::DocComment(s) => {
|
2014-05-22 12:55:48 +00:00
|
|
|
// we need to get the position of this token before we bump.
|
|
|
|
let Span { lo, hi, .. } = self.span;
|
2015-09-24 20:05:02 +00:00
|
|
|
let str = self.id_to_interned_str(ast::Ident::with_empty_ctxt(s));
|
|
|
|
let attr = attr::mk_sugared_doc_attr(attr::mk_attr_id(), str, lo, hi);
|
2015-10-01 16:03:34 +00:00
|
|
|
if attr.node.style == ast::AttrStyle::Inner {
|
2015-03-13 09:34:51 +00:00
|
|
|
attrs.push(attr);
|
2015-12-30 23:11:53 +00:00
|
|
|
self.bump();
|
2015-03-13 09:34:51 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2012-06-30 10:54:54 +00:00
|
|
|
}
|
2016-01-27 19:42:26 +00:00
|
|
|
_ => break,
|
2012-05-24 20:44:42 +00:00
|
|
|
}
|
2012-04-20 03:51:31 +00:00
|
|
|
}
|
2015-10-24 02:02:38 +00:00
|
|
|
Ok(attrs)
|
2012-05-24 20:44:42 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// matches meta_item = IDENT
|
|
|
|
/// | IDENT = lit
|
|
|
|
/// | IDENT meta_seq
|
2015-12-20 21:00:43 +00:00
|
|
|
pub fn parse_meta_item(&mut self) -> PResult<'a, P<ast::MetaItem>> {
|
2014-09-13 16:06:01 +00:00
|
|
|
let nt_meta = match self.token {
|
2016-01-27 19:42:26 +00:00
|
|
|
token::Interpolated(token::NtMeta(ref e)) => Some(e.clone()),
|
|
|
|
_ => None,
|
2014-09-13 16:06:01 +00:00
|
|
|
};
|
|
|
|
|
2016-07-03 21:38:37 +00:00
|
|
|
if let Some(meta) = nt_meta {
|
|
|
|
self.bump();
|
|
|
|
return Ok(meta);
|
2014-03-26 23:14:07 +00:00
|
|
|
}
|
|
|
|
|
2012-05-24 20:44:42 +00:00
|
|
|
let lo = self.span.lo;
|
2016-03-23 03:01:37 +00:00
|
|
|
let ident = self.parse_ident()?;
|
2014-01-08 18:35:15 +00:00
|
|
|
let name = self.id_to_interned_str(ident);
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Eq => {
|
2015-12-30 23:11:53 +00:00
|
|
|
self.bump();
|
2016-03-23 03:01:37 +00:00
|
|
|
let lit = self.parse_lit()?;
|
2013-10-30 00:47:11 +00:00
|
|
|
// FIXME #623 Non-string meta items are not serialized correctly;
|
|
|
|
// just forbid them for now
|
|
|
|
match lit.node {
|
2016-02-08 16:06:20 +00:00
|
|
|
ast::LitKind::Str(..) => {}
|
2013-10-30 00:47:11 +00:00
|
|
|
_ => {
|
2016-01-27 19:42:26 +00:00
|
|
|
self.span_err(lit.span,
|
|
|
|
"non-string literals are not allowed in meta-items");
|
2013-10-30 00:47:11 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-12 05:10:31 +00:00
|
|
|
let hi = self.span.hi;
|
2016-02-09 11:05:20 +00:00
|
|
|
Ok(P(spanned(lo, hi, ast::MetaItemKind::NameValue(name, lit))))
|
2013-02-14 15:34:21 +00:00
|
|
|
}
|
2014-10-29 10:37:54 +00:00
|
|
|
token::OpenDelim(token::Paren) => {
|
2016-03-23 03:01:37 +00:00
|
|
|
let inner_items = self.parse_meta_seq()?;
|
2013-04-12 05:10:31 +00:00
|
|
|
let hi = self.span.hi;
|
2016-02-09 11:05:20 +00:00
|
|
|
Ok(P(spanned(lo, hi, ast::MetaItemKind::List(name, inner_items))))
|
2013-02-14 15:34:21 +00:00
|
|
|
}
|
|
|
|
_ => {
|
2013-05-02 17:36:24 +00:00
|
|
|
let hi = self.last_span.hi;
|
2016-02-09 11:05:20 +00:00
|
|
|
Ok(P(spanned(lo, hi, ast::MetaItemKind::Word(name))))
|
2013-02-14 15:34:21 +00:00
|
|
|
}
|
2012-04-20 03:51:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// matches meta_seq = ( COMMASEP(meta_item) )
|
2015-12-20 21:00:43 +00:00
|
|
|
fn parse_meta_seq(&mut self) -> PResult<'a, Vec<P<ast::MetaItem>>> {
|
2015-10-24 02:02:38 +00:00
|
|
|
self.parse_unspanned_seq(&token::OpenDelim(token::Paren),
|
|
|
|
&token::CloseDelim(token::Paren),
|
2016-02-23 04:24:42 +00:00
|
|
|
SeqSep::trailing_allowed(token::Comma),
|
2015-12-20 21:00:43 +00:00
|
|
|
|p: &mut Parser<'a>| p.parse_meta_item())
|
2012-04-20 03:51:31 +00:00
|
|
|
}
|
|
|
|
}
|