]> git.proxmox.com Git - rustc.git/blob - src/libsyntax/parse/attr.rs
New upstream version 1.37.0+dfsg1
[rustc.git] / src / libsyntax / parse / attr.rs
1 use crate::attr;
2 use crate::ast;
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
8 use log::debug;
9 use smallvec::smallvec;
10
11 #[derive(Debug)]
12 enum InnerAttributeParsePolicy<'a> {
13 Permitted,
14 NotPermitted { reason: &'a str },
15 }
16
17 const DEFAULT_UNEXPECTED_INNER_ATTR_ERR_MSG: &str = "an inner attribute is not \
18 permitted in this context";
19
20 impl<'a> Parser<'a> {
21 crate fn parse_arg_attributes(&mut self) -> PResult<'a, Vec<ast::Attribute>> {
22 let attrs = self.parse_outer_attributes()?;
23 attrs.iter().for_each(|a|
24 self.sess.param_attr_spans.borrow_mut().push(a.span)
25 );
26 Ok(attrs)
27 }
28
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;
33 loop {
34 debug!("parse_outer_attributes: self.token={:?}", self.token);
35 match self.token.kind {
36 token::Pound => {
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"
41 } else {
42 DEFAULT_UNEXPECTED_INNER_ATTR_ERR_MSG
43 };
44 let inner_parse_policy =
45 InnerAttributeParsePolicy::NotPermitted { reason: inner_error_reason };
46 let attr = self.parse_attribute_with_inner_parse_policy(inner_parse_policy)?;
47 attrs.push(attr);
48 just_parsed_doc_comment = false;
49 }
50 token::DocComment(s) => {
51 let attr = attr::mk_sugared_doc_attr(attr::mk_attr_id(), s, self.token.span);
52 if attr.style != ast::AttrStyle::Outer {
53 let mut err = self.fatal("expected outer doc comment");
54 err.note("inner doc comments like this (starting with \
55 `//!` or `/*!`) can only appear before items");
56 return Err(err);
57 }
58 attrs.push(attr);
59 self.bump();
60 just_parsed_doc_comment = true;
61 }
62 _ => break,
63 }
64 }
65 Ok(attrs)
66 }
67
68 /// Matches `attribute = # ! [ meta_item ]`
69 ///
70 /// If permit_inner is true, then a leading `!` indicates an inner
71 /// attribute
72 pub fn parse_attribute(&mut self, permit_inner: bool) -> PResult<'a, ast::Attribute> {
73 debug!("parse_attribute: permit_inner={:?} self.token={:?}",
74 permit_inner,
75 self.token);
76 let inner_parse_policy = if permit_inner {
77 InnerAttributeParsePolicy::Permitted
78 } else {
79 InnerAttributeParsePolicy::NotPermitted
80 { reason: DEFAULT_UNEXPECTED_INNER_ATTR_ERR_MSG }
81 };
82 self.parse_attribute_with_inner_parse_policy(inner_parse_policy)
83 }
84
85 /// The same as `parse_attribute`, except it takes in an `InnerAttributeParsePolicy`
86 /// that prescribes how to handle inner attributes.
87 fn parse_attribute_with_inner_parse_policy(&mut self,
88 inner_parse_policy: InnerAttributeParsePolicy<'_>)
89 -> PResult<'a, ast::Attribute> {
90 debug!("parse_attribute_with_inner_parse_policy: inner_parse_policy={:?} self.token={:?}",
91 inner_parse_policy,
92 self.token);
93 let (span, path, tokens, style) = match self.token.kind {
94 token::Pound => {
95 let lo = self.token.span;
96 self.bump();
97
98 if let InnerAttributeParsePolicy::Permitted = inner_parse_policy {
99 self.expected_tokens.push(TokenType::Token(token::Not));
100 }
101 let style = if self.token == token::Not {
102 self.bump();
103 if let InnerAttributeParsePolicy::NotPermitted { reason } = inner_parse_policy
104 {
105 let span = self.token.span;
106 self.diagnostic()
107 .struct_span_err(span, reason)
108 .note("inner attributes, like `#![no_std]`, annotate the item \
109 enclosing them, and are usually found at the beginning of \
110 source files. Outer attributes, like `#[test]`, annotate the \
111 item following them.")
112 .emit()
113 }
114 ast::AttrStyle::Inner
115 } else {
116 ast::AttrStyle::Outer
117 };
118
119 self.expect(&token::OpenDelim(token::Bracket))?;
120 let (path, tokens) = self.parse_meta_item_unrestricted()?;
121 self.expect(&token::CloseDelim(token::Bracket))?;
122 let hi = self.prev_span;
123
124 (lo.to(hi), path, tokens, style)
125 }
126 _ => {
127 let token_str = self.this_token_to_string();
128 return Err(self.fatal(&format!("expected `#`, found `{}`", token_str)));
129 }
130 };
131
132 Ok(ast::Attribute {
133 id: attr::mk_attr_id(),
134 style,
135 path,
136 tokens,
137 is_sugared_doc: false,
138 span,
139 })
140 }
141
142 /// Parse an inner part of attribute - path and following tokens.
143 /// The tokens must be either a delimited token stream, or empty token stream,
144 /// or the "legacy" key-value form.
145 /// PATH `(` TOKEN_STREAM `)`
146 /// PATH `[` TOKEN_STREAM `]`
147 /// PATH `{` TOKEN_STREAM `}`
148 /// PATH
149 /// PATH `=` TOKEN_TREE
150 /// The delimiters or `=` are still put into the resulting token stream.
151 crate fn parse_meta_item_unrestricted(&mut self) -> PResult<'a, (ast::Path, TokenStream)> {
152 let meta = match self.token.kind {
153 token::Interpolated(ref nt) => match **nt {
154 Nonterminal::NtMeta(ref meta) => Some(meta.clone()),
155 _ => None,
156 },
157 _ => None,
158 };
159 Ok(if let Some(meta) = meta {
160 self.bump();
161 (meta.path, meta.node.tokens(meta.span))
162 } else {
163 let path = self.parse_path(PathStyle::Mod)?;
164 let tokens = if self.check(&token::OpenDelim(DelimToken::Paren)) ||
165 self.check(&token::OpenDelim(DelimToken::Bracket)) ||
166 self.check(&token::OpenDelim(DelimToken::Brace)) {
167 self.parse_token_tree().into()
168 } else if self.eat(&token::Eq) {
169 let eq = TokenTree::token(token::Eq, self.prev_span);
170 let mut is_interpolated_expr = false;
171 if let token::Interpolated(nt) = &self.token.kind {
172 if let token::NtExpr(..) = **nt {
173 is_interpolated_expr = true;
174 }
175 }
176 let tokens = if is_interpolated_expr {
177 // We need to accept arbitrary interpolated expressions to continue
178 // supporting things like `doc = $expr` that work on stable.
179 // Non-literal interpolated expressions are rejected after expansion.
180 self.parse_token_tree().into()
181 } else {
182 self.parse_unsuffixed_lit()?.tokens()
183 };
184 TokenStream::from_streams(smallvec![eq.into(), tokens])
185 } else {
186 TokenStream::empty()
187 };
188 (path, tokens)
189 })
190 }
191
192 /// Parse attributes that appear after the opening of an item. These should
193 /// be preceded by an exclamation mark, but we accept and warn about one
194 /// terminated by a semicolon.
195
196 /// matches inner_attrs*
197 crate fn parse_inner_attributes(&mut self) -> PResult<'a, Vec<ast::Attribute>> {
198 let mut attrs: Vec<ast::Attribute> = vec![];
199 loop {
200 match self.token.kind {
201 token::Pound => {
202 // Don't even try to parse if it's not an inner attribute.
203 if !self.look_ahead(1, |t| t == &token::Not) {
204 break;
205 }
206
207 let attr = self.parse_attribute(true)?;
208 assert_eq!(attr.style, ast::AttrStyle::Inner);
209 attrs.push(attr);
210 }
211 token::DocComment(s) => {
212 // we need to get the position of this token before we bump.
213 let attr = attr::mk_sugared_doc_attr(attr::mk_attr_id(), s, self.token.span);
214 if attr.style == ast::AttrStyle::Inner {
215 attrs.push(attr);
216 self.bump();
217 } else {
218 break;
219 }
220 }
221 _ => break,
222 }
223 }
224 Ok(attrs)
225 }
226
227 fn parse_unsuffixed_lit(&mut self) -> PResult<'a, ast::Lit> {
228 let lit = self.parse_lit()?;
229 debug!("Checking if {:?} is unusuffixed.", lit);
230
231 if !lit.node.is_unsuffixed() {
232 let msg = "suffixed literals are not allowed in attributes";
233 self.diagnostic().struct_span_err(lit.span, msg)
234 .help("instead of using a suffixed literal \
235 (1u8, 1.0f32, etc.), use an unsuffixed version \
236 (1, 1.0, etc.).")
237 .emit()
238 }
239
240 Ok(lit)
241 }
242
243 /// Per RFC#1559, matches the following grammar:
244 ///
245 /// meta_item : IDENT ( '=' UNSUFFIXED_LIT | '(' meta_item_inner? ')' )? ;
246 /// meta_item_inner : (meta_item | UNSUFFIXED_LIT) (',' meta_item_inner)? ;
247 pub fn parse_meta_item(&mut self) -> PResult<'a, ast::MetaItem> {
248 let nt_meta = match self.token.kind {
249 token::Interpolated(ref nt) => match **nt {
250 token::NtMeta(ref e) => Some(e.clone()),
251 _ => None,
252 },
253 _ => None,
254 };
255
256 if let Some(meta) = nt_meta {
257 self.bump();
258 return Ok(meta);
259 }
260
261 let lo = self.token.span;
262 let path = self.parse_path(PathStyle::Mod)?;
263 let node = self.parse_meta_item_kind()?;
264 let span = lo.to(self.prev_span);
265 Ok(ast::MetaItem { path, node, span })
266 }
267
268 crate fn parse_meta_item_kind(&mut self) -> PResult<'a, ast::MetaItemKind> {
269 Ok(if self.eat(&token::Eq) {
270 ast::MetaItemKind::NameValue(self.parse_unsuffixed_lit()?)
271 } else if self.eat(&token::OpenDelim(token::Paren)) {
272 ast::MetaItemKind::List(self.parse_meta_seq()?)
273 } else {
274 ast::MetaItemKind::Word
275 })
276 }
277
278 /// matches meta_item_inner : (meta_item | UNSUFFIXED_LIT) ;
279 fn parse_meta_item_inner(&mut self) -> PResult<'a, ast::NestedMetaItem> {
280 match self.parse_unsuffixed_lit() {
281 Ok(lit) => {
282 return Ok(ast::NestedMetaItem::Literal(lit))
283 }
284 Err(ref mut err) => self.diagnostic().cancel(err)
285 }
286
287 match self.parse_meta_item() {
288 Ok(mi) => {
289 return Ok(ast::NestedMetaItem::MetaItem(mi))
290 }
291 Err(ref mut err) => self.diagnostic().cancel(err)
292 }
293
294 let found = self.this_token_to_string();
295 let msg = format!("expected unsuffixed literal or identifier, found `{}`", found);
296 Err(self.diagnostic().struct_span_err(self.token.span, &msg))
297 }
298
299 /// matches meta_seq = ( COMMASEP(meta_item_inner) )
300 fn parse_meta_seq(&mut self) -> PResult<'a, Vec<ast::NestedMetaItem>> {
301 self.parse_seq_to_end(&token::CloseDelim(token::Paren),
302 SeqSep::trailing_allowed(token::Comma),
303 |p: &mut Parser<'a>| p.parse_meta_item_inner())
304 }
305 }