3 use crate::parse::{SeqSep, PResult};
4 use crate::parse::token::{self, Nonterminal, DelimToken};
5 use crate::parse::parser::{Parser, TokenType, PathStyle};
6 use crate::tokenstream::{TokenStream, TokenTree};
7 use crate::source_map::Span;
10 use smallvec::smallvec;
13 enum InnerAttributeParsePolicy<'a> {
15 NotPermitted { reason: &'a str, saw_doc_comment: bool, prev_attr_sp: Option<Span> },
18 const DEFAULT_UNEXPECTED_INNER_ATTR_ERR_MSG: &str = "an inner attribute is not \
19 permitted in this context";
22 crate fn parse_arg_attributes(&mut self) -> PResult<'a, Vec<ast::Attribute>> {
23 let attrs = self.parse_outer_attributes()?;
24 self.sess.gated_spans.param_attrs.borrow_mut()
25 .extend(attrs.iter().map(|a| a.span));
29 /// Parse attributes that appear before an item
30 crate fn parse_outer_attributes(&mut self) -> PResult<'a, Vec<ast::Attribute>> {
31 let mut attrs: Vec<ast::Attribute> = Vec::new();
32 let mut just_parsed_doc_comment = false;
34 debug!("parse_outer_attributes: self.token={:?}", self.token);
35 match self.token.kind {
37 let inner_error_reason = if just_parsed_doc_comment {
38 "an inner attribute is not permitted following an outer doc comment"
39 } else if !attrs.is_empty() {
40 "an inner attribute is not permitted following an outer attribute"
42 DEFAULT_UNEXPECTED_INNER_ATTR_ERR_MSG
44 let inner_parse_policy =
45 InnerAttributeParsePolicy::NotPermitted {
46 reason: inner_error_reason,
47 saw_doc_comment: just_parsed_doc_comment,
48 prev_attr_sp: attrs.last().and_then(|a| Some(a.span))
50 let attr = self.parse_attribute_with_inner_parse_policy(inner_parse_policy)?;
52 just_parsed_doc_comment = false;
54 token::DocComment(s) => {
55 let attr = attr::mk_sugared_doc_attr(s, self.token.span);
56 if attr.style != ast::AttrStyle::Outer {
57 let mut err = self.fatal("expected outer doc comment");
58 err.note("inner doc comments like this (starting with \
59 `//!` or `/*!`) can only appear before items");
64 just_parsed_doc_comment = true;
72 /// Matches `attribute = # ! [ meta_item ]`
74 /// If permit_inner is true, then a leading `!` indicates an inner
76 pub fn parse_attribute(&mut self, permit_inner: bool) -> PResult<'a, ast::Attribute> {
77 debug!("parse_attribute: permit_inner={:?} self.token={:?}",
80 let inner_parse_policy = if permit_inner {
81 InnerAttributeParsePolicy::Permitted
83 InnerAttributeParsePolicy::NotPermitted {
84 reason: DEFAULT_UNEXPECTED_INNER_ATTR_ERR_MSG,
85 saw_doc_comment: false,
89 self.parse_attribute_with_inner_parse_policy(inner_parse_policy)
92 /// The same as `parse_attribute`, except it takes in an `InnerAttributeParsePolicy`
93 /// that prescribes how to handle inner attributes.
94 fn parse_attribute_with_inner_parse_policy(&mut self,
95 inner_parse_policy: InnerAttributeParsePolicy<'_>)
96 -> PResult<'a, ast::Attribute> {
97 debug!("parse_attribute_with_inner_parse_policy: inner_parse_policy={:?} self.token={:?}",
100 let (span, path, tokens, style) = match self.token.kind {
102 let lo = self.token.span;
105 if let InnerAttributeParsePolicy::Permitted = inner_parse_policy {
106 self.expected_tokens.push(TokenType::Token(token::Not));
109 let style = if self.token == token::Not {
111 ast::AttrStyle::Inner
113 ast::AttrStyle::Outer
116 self.expect(&token::OpenDelim(token::Bracket))?;
117 let (path, tokens) = self.parse_meta_item_unrestricted()?;
118 self.expect(&token::CloseDelim(token::Bracket))?;
119 let hi = self.prev_span;
121 let attr_sp = lo.to(hi);
123 // Emit error if inner attribute is encountered and not permitted
124 if style == ast::AttrStyle::Inner {
125 if let InnerAttributeParsePolicy::NotPermitted { reason,
126 saw_doc_comment, prev_attr_sp } = inner_parse_policy {
127 let prev_attr_note = if saw_doc_comment {
128 "previous doc comment"
130 "previous outer attribute"
133 let mut diagnostic = self
135 .struct_span_err(attr_sp, reason);
137 if let Some(prev_attr_sp) = prev_attr_sp {
139 .span_label(attr_sp, "not permitted following an outer attibute")
140 .span_label(prev_attr_sp, prev_attr_note);
144 .note("inner attributes, like `#![no_std]`, annotate the item \
145 enclosing them, and are usually found at the beginning of \
146 source files. Outer attributes, like `#[test]`, annotate the \
147 item following them.")
152 (attr_sp, path, tokens, style)
155 let token_str = self.this_token_to_string();
156 return Err(self.fatal(&format!("expected `#`, found `{}`", token_str)));
161 id: attr::mk_attr_id(),
165 is_sugared_doc: false,
170 /// Parse an inner part of attribute - path and following tokens.
171 /// The tokens must be either a delimited token stream, or empty token stream,
172 /// or the "legacy" key-value form.
173 /// PATH `(` TOKEN_STREAM `)`
174 /// PATH `[` TOKEN_STREAM `]`
175 /// PATH `{` TOKEN_STREAM `}`
177 /// PATH `=` TOKEN_TREE
178 /// The delimiters or `=` are still put into the resulting token stream.
179 crate fn parse_meta_item_unrestricted(&mut self) -> PResult<'a, (ast::Path, TokenStream)> {
180 let meta = match self.token.kind {
181 token::Interpolated(ref nt) => match **nt {
182 Nonterminal::NtMeta(ref meta) => Some(meta.clone()),
187 Ok(if let Some(meta) = meta {
189 (meta.path, meta.node.tokens(meta.span))
191 let path = self.parse_path(PathStyle::Mod)?;
192 let tokens = if self.check(&token::OpenDelim(DelimToken::Paren)) ||
193 self.check(&token::OpenDelim(DelimToken::Bracket)) ||
194 self.check(&token::OpenDelim(DelimToken::Brace)) {
195 self.parse_token_tree().into()
196 } else if self.eat(&token::Eq) {
197 let eq = TokenTree::token(token::Eq, self.prev_span);
198 let mut is_interpolated_expr = false;
199 if let token::Interpolated(nt) = &self.token.kind {
200 if let token::NtExpr(..) = **nt {
201 is_interpolated_expr = true;
204 let tokens = if is_interpolated_expr {
205 // We need to accept arbitrary interpolated expressions to continue
206 // supporting things like `doc = $expr` that work on stable.
207 // Non-literal interpolated expressions are rejected after expansion.
208 self.parse_token_tree().into()
210 self.parse_unsuffixed_lit()?.tokens()
212 TokenStream::from_streams(smallvec![eq.into(), tokens])
220 /// Parse attributes that appear after the opening of an item. These should
221 /// be preceded by an exclamation mark, but we accept and warn about one
222 /// terminated by a semicolon.
224 /// matches inner_attrs*
225 crate fn parse_inner_attributes(&mut self) -> PResult<'a, Vec<ast::Attribute>> {
226 let mut attrs: Vec<ast::Attribute> = vec![];
228 match self.token.kind {
230 // Don't even try to parse if it's not an inner attribute.
231 if !self.look_ahead(1, |t| t == &token::Not) {
235 let attr = self.parse_attribute(true)?;
236 assert_eq!(attr.style, ast::AttrStyle::Inner);
239 token::DocComment(s) => {
240 // we need to get the position of this token before we bump.
241 let attr = attr::mk_sugared_doc_attr(s, self.token.span);
242 if attr.style == ast::AttrStyle::Inner {
255 fn parse_unsuffixed_lit(&mut self) -> PResult<'a, ast::Lit> {
256 let lit = self.parse_lit()?;
257 debug!("checking if {:?} is unusuffixed", lit);
259 if !lit.node.is_unsuffixed() {
260 let msg = "suffixed literals are not allowed in attributes";
261 self.diagnostic().struct_span_err(lit.span, msg)
262 .help("instead of using a suffixed literal \
263 (1u8, 1.0f32, etc.), use an unsuffixed version \
271 /// Per RFC#1559, matches the following grammar:
273 /// meta_item : IDENT ( '=' UNSUFFIXED_LIT | '(' meta_item_inner? ')' )? ;
274 /// meta_item_inner : (meta_item | UNSUFFIXED_LIT) (',' meta_item_inner)? ;
275 pub fn parse_meta_item(&mut self) -> PResult<'a, ast::MetaItem> {
276 let nt_meta = match self.token.kind {
277 token::Interpolated(ref nt) => match **nt {
278 token::NtMeta(ref e) => Some(e.clone()),
284 if let Some(meta) = nt_meta {
289 let lo = self.token.span;
290 let path = self.parse_path(PathStyle::Mod)?;
291 let node = self.parse_meta_item_kind()?;
292 let span = lo.to(self.prev_span);
293 Ok(ast::MetaItem { path, node, span })
296 crate fn parse_meta_item_kind(&mut self) -> PResult<'a, ast::MetaItemKind> {
297 Ok(if self.eat(&token::Eq) {
298 ast::MetaItemKind::NameValue(self.parse_unsuffixed_lit()?)
299 } else if self.eat(&token::OpenDelim(token::Paren)) {
300 ast::MetaItemKind::List(self.parse_meta_seq()?)
302 ast::MetaItemKind::Word
306 /// matches meta_item_inner : (meta_item | UNSUFFIXED_LIT) ;
307 fn parse_meta_item_inner(&mut self) -> PResult<'a, ast::NestedMetaItem> {
308 match self.parse_unsuffixed_lit() {
310 return Ok(ast::NestedMetaItem::Literal(lit))
312 Err(ref mut err) => self.diagnostic().cancel(err)
315 match self.parse_meta_item() {
317 return Ok(ast::NestedMetaItem::MetaItem(mi))
319 Err(ref mut err) => self.diagnostic().cancel(err)
322 let found = self.this_token_to_string();
323 let msg = format!("expected unsuffixed literal or identifier, found `{}`", found);
324 Err(self.diagnostic().struct_span_err(self.token.span, &msg))
327 /// matches meta_seq = ( COMMASEP(meta_item_inner) )
328 fn parse_meta_seq(&mut self) -> PResult<'a, Vec<ast::NestedMetaItem>> {
329 self.parse_seq_to_end(&token::CloseDelim(token::Paren),
330 SeqSep::trailing_allowed(token::Comma),
331 |p: &mut Parser<'a>| p.parse_meta_item_inner())