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;
|
2017-03-15 00:22:48 +00:00
|
|
|
use codemap::respan;
|
2018-05-31 22:53:30 +00:00
|
|
|
use parse::{SeqSep, PResult};
|
2017-03-08 23:13:35 +00:00
|
|
|
use parse::token::{self, Nonterminal};
|
|
|
|
use parse::parser::{Parser, TokenType, PathStyle};
|
|
|
|
use tokenstream::TokenStream;
|
2014-05-16 07:16:13 +00:00
|
|
|
|
2018-03-20 22:58:25 +00:00
|
|
|
#[derive(Debug)]
|
2016-07-06 04:35:12 +00:00
|
|
|
enum InnerAttributeParsePolicy<'a> {
|
|
|
|
Permitted,
|
|
|
|
NotPermitted { reason: &'a str },
|
|
|
|
}
|
|
|
|
|
|
|
|
const DEFAULT_UNEXPECTED_INNER_ATTR_ERR_MSG: &'static str = "an inner attribute is not \
|
|
|
|
permitted in this context";
|
|
|
|
|
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
|
2018-05-31 22:53:30 +00:00
|
|
|
crate 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();
|
2016-07-06 04:35:12 +00:00
|
|
|
let mut just_parsed_doc_comment = false;
|
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-07-06 04:35:12 +00:00
|
|
|
let inner_error_reason = if just_parsed_doc_comment {
|
|
|
|
"an inner attribute is not permitted following an outer doc comment"
|
|
|
|
} else if !attrs.is_empty() {
|
|
|
|
"an inner attribute is not permitted following an outer attribute"
|
|
|
|
} else {
|
|
|
|
DEFAULT_UNEXPECTED_INNER_ATTR_ERR_MSG
|
|
|
|
};
|
|
|
|
let inner_parse_policy =
|
|
|
|
InnerAttributeParsePolicy::NotPermitted { reason: inner_error_reason };
|
|
|
|
attrs.push(self.parse_attribute_with_inner_parse_policy(inner_parse_policy)?);
|
|
|
|
just_parsed_doc_comment = false;
|
2016-01-27 19:42:26 +00:00
|
|
|
}
|
|
|
|
token::DocComment(s) => {
|
2017-03-15 00:22:48 +00:00
|
|
|
let attr = attr::mk_sugared_doc_attr(attr::mk_attr_id(), s, self.span);
|
2016-11-14 12:00:25 +00:00
|
|
|
if attr.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();
|
2016-07-06 04:35:12 +00:00
|
|
|
just_parsed_doc_comment = true;
|
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
|
|
|
}
|
2017-05-12 18:05:39 +00:00
|
|
|
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> {
|
2016-07-06 04:35:12 +00:00
|
|
|
debug!("parse_attribute: permit_inner={:?} self.token={:?}",
|
2016-01-27 19:42:26 +00:00
|
|
|
permit_inner,
|
|
|
|
self.token);
|
2016-07-06 04:35:12 +00:00
|
|
|
let inner_parse_policy = if permit_inner {
|
|
|
|
InnerAttributeParsePolicy::Permitted
|
|
|
|
} else {
|
|
|
|
InnerAttributeParsePolicy::NotPermitted
|
|
|
|
{ reason: DEFAULT_UNEXPECTED_INNER_ATTR_ERR_MSG }
|
|
|
|
};
|
|
|
|
self.parse_attribute_with_inner_parse_policy(inner_parse_policy)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The same as `parse_attribute`, except it takes in an `InnerAttributeParsePolicy`
|
|
|
|
/// that prescribes how to handle inner attributes.
|
|
|
|
fn parse_attribute_with_inner_parse_policy(&mut self,
|
|
|
|
inner_parse_policy: InnerAttributeParsePolicy)
|
|
|
|
-> PResult<'a, ast::Attribute> {
|
|
|
|
debug!("parse_attribute_with_inner_parse_policy: inner_parse_policy={:?} self.token={:?}",
|
|
|
|
inner_parse_policy,
|
|
|
|
self.token);
|
2018-03-17 05:46:45 +00:00
|
|
|
let (span, path, tokens, style) = match self.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Pound => {
|
2017-03-15 00:22:48 +00:00
|
|
|
let lo = self.span;
|
2015-12-30 23:11:53 +00:00
|
|
|
self.bump();
|
2014-02-25 02:42:40 +00:00
|
|
|
|
2018-03-20 22:58:25 +00:00
|
|
|
if let InnerAttributeParsePolicy::Permitted = inner_parse_policy {
|
2016-01-27 19:42:26 +00:00
|
|
|
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();
|
2016-07-06 04:35:12 +00:00
|
|
|
if let InnerAttributeParsePolicy::NotPermitted { reason } = inner_parse_policy
|
|
|
|
{
|
2014-06-14 03:48:09 +00:00
|
|
|
let span = self.span;
|
2016-01-27 19:42:26 +00:00
|
|
|
self.diagnostic()
|
2016-07-06 04:35:12 +00:00
|
|
|
.struct_span_err(span, reason)
|
2017-10-30 23:59:34 +00:00
|
|
|
.note("inner attributes, like `#![no_std]`, annotate the item \
|
|
|
|
enclosing them, and are usually found at the beginning of \
|
|
|
|
source files. Outer attributes, like `#[test]`, annotate the \
|
|
|
|
item following them.")
|
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))?;
|
2017-03-08 23:13:35 +00:00
|
|
|
let (path, tokens) = self.parse_path_and_tokens()?;
|
2016-03-23 03:01:37 +00:00
|
|
|
self.expect(&token::CloseDelim(token::Bracket))?;
|
2017-03-15 00:22:48 +00:00
|
|
|
let hi = self.prev_span;
|
2014-02-25 02:42:40 +00:00
|
|
|
|
2017-03-15 00:22:48 +00:00
|
|
|
(lo.to(hi), path, tokens, 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
|
|
|
|
2016-11-14 12:00:25 +00:00
|
|
|
Ok(ast::Attribute {
|
|
|
|
id: attr::mk_attr_id(),
|
2017-08-07 05:54:09 +00:00
|
|
|
style,
|
|
|
|
path,
|
|
|
|
tokens,
|
2016-11-14 12:00:25 +00:00
|
|
|
is_sugared_doc: false,
|
2017-08-07 05:54:09 +00:00
|
|
|
span,
|
2015-10-24 02:02:38 +00:00
|
|
|
})
|
2012-05-24 20:44:42 +00:00
|
|
|
}
|
2012-04-20 03:51:31 +00:00
|
|
|
|
2018-05-31 22:53:30 +00:00
|
|
|
crate fn parse_path_and_tokens(&mut self) -> PResult<'a, (ast::Path, TokenStream)> {
|
2017-03-08 23:13:35 +00:00
|
|
|
let meta = match self.token {
|
2017-03-29 01:55:01 +00:00
|
|
|
token::Interpolated(ref nt) => match nt.0 {
|
2017-03-08 23:13:35 +00:00
|
|
|
Nonterminal::NtMeta(ref meta) => Some(meta.clone()),
|
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
Ok(if let Some(meta) = meta {
|
|
|
|
self.bump();
|
2018-04-17 13:33:39 +00:00
|
|
|
(meta.ident, meta.node.tokens(meta.span))
|
2017-03-08 23:13:35 +00:00
|
|
|
} else {
|
|
|
|
(self.parse_path(PathStyle::Mod)?, self.parse_tokens())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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*
|
2018-05-31 22:53:30 +00:00
|
|
|
crate 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)?;
|
2017-05-12 18:05:39 +00:00
|
|
|
assert_eq!(attr.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.
|
2017-03-15 00:22:48 +00:00
|
|
|
let attr = attr::mk_sugared_doc_attr(attr::mk_attr_id(), s, self.span);
|
2016-11-14 12:00:25 +00:00
|
|
|
if attr.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
|
|
|
}
|
|
|
|
|
2016-08-20 01:58:14 +00:00
|
|
|
fn parse_unsuffixed_lit(&mut self) -> PResult<'a, ast::Lit> {
|
|
|
|
let lit = self.parse_lit()?;
|
|
|
|
debug!("Checking if {:?} is unusuffixed.", lit);
|
|
|
|
|
|
|
|
if !lit.node.is_unsuffixed() {
|
|
|
|
let msg = "suffixed literals are not allowed in attributes";
|
|
|
|
self.diagnostic().struct_span_err(lit.span, msg)
|
|
|
|
.help("instead of using a suffixed literal \
|
|
|
|
(1u8, 1.0f32, etc.), use an unsuffixed version \
|
|
|
|
(1, 1.0, etc.).")
|
|
|
|
.emit()
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(lit)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Per RFC#1559, matches the following grammar:
|
|
|
|
///
|
|
|
|
/// meta_item : IDENT ( '=' UNSUFFIXED_LIT | '(' meta_item_inner? ')' )? ;
|
|
|
|
/// meta_item_inner : (meta_item | UNSUFFIXED_LIT) (',' meta_item_inner)? ;
|
2016-11-15 10:17:24 +00:00
|
|
|
pub fn parse_meta_item(&mut self) -> PResult<'a, ast::MetaItem> {
|
2014-09-13 16:06:01 +00:00
|
|
|
let nt_meta = match self.token {
|
2017-03-29 01:55:01 +00:00
|
|
|
token::Interpolated(ref nt) => match nt.0 {
|
2016-11-02 03:03:55 +00:00
|
|
|
token::NtMeta(ref e) => Some(e.clone()),
|
|
|
|
_ => None,
|
|
|
|
},
|
2016-01-27 19:42:26 +00:00
|
|
|
_ => 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
|
|
|
}
|
|
|
|
|
2017-03-15 00:22:48 +00:00
|
|
|
let lo = self.span;
|
2018-04-17 13:33:39 +00:00
|
|
|
let ident = self.parse_path(PathStyle::Mod)?;
|
2017-03-03 09:23:59 +00:00
|
|
|
let node = self.parse_meta_item_kind()?;
|
2018-01-30 05:30:39 +00:00
|
|
|
let span = lo.to(self.prev_span);
|
2018-04-17 13:33:39 +00:00
|
|
|
Ok(ast::MetaItem { ident, node, span })
|
2017-03-03 09:23:59 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 22:53:30 +00:00
|
|
|
crate fn parse_meta_item_kind(&mut self) -> PResult<'a, ast::MetaItemKind> {
|
2017-03-03 09:23:59 +00:00
|
|
|
Ok(if self.eat(&token::Eq) {
|
2016-11-15 07:37:10 +00:00
|
|
|
ast::MetaItemKind::NameValue(self.parse_unsuffixed_lit()?)
|
2017-08-16 14:40:06 +00:00
|
|
|
} else if self.eat(&token::OpenDelim(token::Paren)) {
|
2016-11-15 07:37:10 +00:00
|
|
|
ast::MetaItemKind::List(self.parse_meta_seq()?)
|
|
|
|
} else {
|
|
|
|
ast::MetaItemKind::Word
|
2017-03-03 09:23:59 +00:00
|
|
|
})
|
2012-04-20 03:51:31 +00:00
|
|
|
}
|
|
|
|
|
2016-08-20 01:58:14 +00:00
|
|
|
/// matches meta_item_inner : (meta_item | UNSUFFIXED_LIT) ;
|
|
|
|
fn parse_meta_item_inner(&mut self) -> PResult<'a, ast::NestedMetaItem> {
|
2017-03-15 00:22:48 +00:00
|
|
|
let lo = self.span;
|
2016-08-20 01:58:14 +00:00
|
|
|
|
|
|
|
match self.parse_unsuffixed_lit() {
|
|
|
|
Ok(lit) => {
|
2017-03-15 00:22:48 +00:00
|
|
|
return Ok(respan(lo.to(self.prev_span), ast::NestedMetaItemKind::Literal(lit)))
|
2016-08-20 01:58:14 +00:00
|
|
|
}
|
|
|
|
Err(ref mut err) => self.diagnostic().cancel(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
match self.parse_meta_item() {
|
|
|
|
Ok(mi) => {
|
2017-03-15 00:22:48 +00:00
|
|
|
return Ok(respan(lo.to(self.prev_span), ast::NestedMetaItemKind::MetaItem(mi)))
|
2016-08-20 01:58:14 +00:00
|
|
|
}
|
|
|
|
Err(ref mut err) => self.diagnostic().cancel(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
let found = self.this_token_to_string();
|
|
|
|
let msg = format!("expected unsuffixed literal or identifier, found {}", found);
|
2017-03-15 00:22:48 +00:00
|
|
|
Err(self.diagnostic().struct_span_err(lo, &msg))
|
2016-08-20 01:58:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// matches meta_seq = ( COMMASEP(meta_item_inner) )
|
|
|
|
fn parse_meta_seq(&mut self) -> PResult<'a, Vec<ast::NestedMetaItem>> {
|
2017-08-16 14:40:06 +00:00
|
|
|
self.parse_seq_to_end(&token::CloseDelim(token::Paren),
|
|
|
|
SeqSep::trailing_allowed(token::Comma),
|
|
|
|
|p: &mut Parser<'a>| p.parse_meta_item_inner())
|
2012-04-20 03:51:31 +00:00
|
|
|
}
|
|
|
|
}
|