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}
;
9 use smallvec
::smallvec
;
12 enum InnerAttributeParsePolicy
<'a
> {
14 NotPermitted { reason: &'a str }
,
17 const DEFAULT_UNEXPECTED_INNER_ATTR_ERR_MSG
: &str = "an inner attribute is not \
18 permitted in this context";
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
)
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 { reason: inner_error_reason }
;
46 let attr
= self.parse_attribute_with_inner_parse_policy(inner_parse_policy
)?
;
48 just_parsed_doc_comment
= false;
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");
60 just_parsed_doc_comment
= true;
68 /// Matches `attribute = # ! [ meta_item ]`
70 /// If permit_inner is true, then a leading `!` indicates an inner
72 pub fn parse_attribute(&mut self, permit_inner
: bool
) -> PResult
<'a
, ast
::Attribute
> {
73 debug
!("parse_attribute: permit_inner={:?} self.token={:?}",
76 let inner_parse_policy
= if permit_inner
{
77 InnerAttributeParsePolicy
::Permitted
79 InnerAttributeParsePolicy
::NotPermitted
80 { reason: DEFAULT_UNEXPECTED_INNER_ATTR_ERR_MSG }
82 self.parse_attribute_with_inner_parse_policy(inner_parse_policy
)
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={:?}",
93 let (span
, path
, tokens
, style
) = match self.token
.kind
{
95 let lo
= self.token
.span
;
98 if let InnerAttributeParsePolicy
::Permitted
= inner_parse_policy
{
99 self.expected_tokens
.push(TokenType
::Token(token
::Not
));
101 let style
= if self.token
== token
::Not
{
103 if let InnerAttributeParsePolicy
::NotPermitted { reason }
= inner_parse_policy
105 let span
= self.token
.span
;
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.")
114 ast
::AttrStyle
::Inner
116 ast
::AttrStyle
::Outer
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
;
124 (lo
.to(hi
), path
, tokens
, style
)
127 let token_str
= self.this_token_to_string();
128 return Err(self.fatal(&format
!("expected `#`, found `{}`", token_str
)));
133 id
: attr
::mk_attr_id(),
137 is_sugared_doc
: false,
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 `}`
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()),
159 Ok(if let Some(meta
) = meta
{
161 (meta
.path
, meta
.node
.tokens(meta
.span
))
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;
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()
182 self.parse_unsuffixed_lit()?
.tokens()
184 TokenStream
::from_streams(smallvec
![eq
.into(), tokens
])
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.
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
![];
200 match self.token
.kind
{
202 // Don't even try to parse if it's not an inner attribute.
203 if !self.look_ahead(1, |t
| t
== &token
::Not
) {
207 let attr
= self.parse_attribute(true)?
;
208 assert_eq
!(attr
.style
, ast
::AttrStyle
::Inner
);
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
{
227 fn parse_unsuffixed_lit(&mut self) -> PResult
<'a
, ast
::Lit
> {
228 let lit
= self.parse_lit()?
;
229 debug
!("Checking if {:?} is unusuffixed.", lit
);
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 \
243 /// Per RFC#1559, matches the following grammar:
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()),
256 if let Some(meta
) = nt_meta
{
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 }
)
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()?
)
274 ast
::MetaItemKind
::Word
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() {
282 return Ok(ast
::NestedMetaItem
::Literal(lit
))
284 Err(ref mut err
) => self.diagnostic().cancel(err
)
287 match self.parse_meta_item() {
289 return Ok(ast
::NestedMetaItem
::MetaItem(mi
))
291 Err(ref mut err
) => self.diagnostic().cancel(err
)
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
))
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())