1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 use codemap::{spanned, Spanned, mk_sp, Span};
14 use parse::common::*; //resolve bug?
16 use parse::parser::Parser;
19 /// A parser that can parse attributes.
20 pub trait ParserAttr {
21 fn parse_outer_attributes(&mut self) -> Vec<ast::Attribute>;
22 fn parse_attribute(&mut self, permit_inner: bool) -> ast::Attribute;
23 fn parse_inner_attrs_and_next(&mut self)
24 -> (Vec<ast::Attribute>, Vec<ast::Attribute>);
25 fn parse_meta_item(&mut self) -> P<ast::MetaItem>;
26 fn parse_meta_seq(&mut self) -> Vec<P<ast::MetaItem>>;
27 fn parse_optional_meta(&mut self) -> Vec<P<ast::MetaItem>>;
30 impl<'a> ParserAttr for Parser<'a> {
31 /// Parse attributes that appear before an item
32 fn parse_outer_attributes(&mut self) -> Vec<ast::Attribute> {
33 let mut attrs: Vec<ast::Attribute> = Vec::new();
35 debug!("parse_outer_attributes: self.token={}",
39 attrs.push(self.parse_attribute(false));
41 token::DocComment(s) => {
42 let attr = ::attr::mk_sugared_doc_attr(
44 self.id_to_interned_str(s.ident()),
48 if attr.node.style != ast::AttrOuter {
49 self.fatal("expected outer comment");
60 /// Matches `attribute = # ! [ meta_item ]`
62 /// If permit_inner is true, then a leading `!` indicates an inner
64 fn parse_attribute(&mut self, permit_inner: bool) -> ast::Attribute {
65 debug!("parse_attributes: permit_inner={} self.token={}",
66 permit_inner, self.token);
67 let (span, value, mut style) = match self.token {
69 let lo = self.span.lo;
72 let style = if self.eat(&token::Not) {
76 "an inner attribute is not permitted in \
79 "place inner attribute at the top of the module or block");
86 self.expect(&token::OpenDelim(token::Bracket));
87 let meta_item = self.parse_meta_item();
88 let hi = self.span.hi;
89 self.expect(&token::CloseDelim(token::Bracket));
91 (mk_sp(lo, hi), meta_item, style)
94 let token_str = self.this_token_to_string();
95 self.fatal(format!("expected `#`, found `{}`",
96 token_str).as_slice());
100 if permit_inner && self.eat(&token::Semi) {
101 self.span_warn(span, "this inner attribute syntax is deprecated. \
102 The new syntax is `#![foo]`, with a bang and no semicolon");
103 style = ast::AttrInner;
108 node: ast::Attribute_ {
109 id: attr::mk_attr_id(),
112 is_sugared_doc: false
117 /// Parse attributes that appear after the opening of an item. These should
118 /// be preceded by an exclamation mark, but we accept and warn about one
119 /// terminated by a semicolon. In addition to a vector of inner attributes,
120 /// this function also returns a vector that may contain the first outer
121 /// attribute of the next item (since we can't know whether the attribute
122 /// is an inner attribute of the containing item or an outer attribute of
123 /// the first contained item until we see the semi).
125 /// matches inner_attrs* outer_attr?
126 /// you can make the 'next' field an Option, but the result is going to be
127 /// more useful as a vector.
128 fn parse_inner_attrs_and_next(&mut self)
129 -> (Vec<ast::Attribute> , Vec<ast::Attribute> ) {
130 let mut inner_attrs: Vec<ast::Attribute> = Vec::new();
131 let mut next_outer_attrs: Vec<ast::Attribute> = Vec::new();
133 let attr = match self.token {
135 self.parse_attribute(true)
137 token::DocComment(s) => {
138 // we need to get the position of this token before we bump.
139 let Span { lo, hi, .. } = self.span;
141 attr::mk_sugared_doc_attr(attr::mk_attr_id(),
142 self.id_to_interned_str(s.ident()),
150 if attr.node.style == ast::AttrInner {
151 inner_attrs.push(attr);
153 next_outer_attrs.push(attr);
157 (inner_attrs, next_outer_attrs)
160 /// matches meta_item = IDENT
163 fn parse_meta_item(&mut self) -> P<ast::MetaItem> {
164 let nt_meta = match self.token {
165 token::Interpolated(token::NtMeta(ref e)) => {
179 let lo = self.span.lo;
180 let ident = self.parse_ident();
181 let name = self.id_to_interned_str(ident);
185 let lit = self.parse_lit();
186 // FIXME #623 Non-string meta items are not serialized correctly;
187 // just forbid them for now
189 ast::LitStr(..) => {}
193 "non-string literals are not allowed in meta-items");
196 let hi = self.span.hi;
197 P(spanned(lo, hi, ast::MetaNameValue(name, lit)))
199 token::OpenDelim(token::Paren) => {
200 let inner_items = self.parse_meta_seq();
201 let hi = self.span.hi;
202 P(spanned(lo, hi, ast::MetaList(name, inner_items)))
205 let hi = self.last_span.hi;
206 P(spanned(lo, hi, ast::MetaWord(name)))
211 /// matches meta_seq = ( COMMASEP(meta_item) )
212 fn parse_meta_seq(&mut self) -> Vec<P<ast::MetaItem>> {
213 self.parse_seq(&token::OpenDelim(token::Paren),
214 &token::CloseDelim(token::Paren),
215 seq_sep_trailing_allowed(token::Comma),
216 |p| p.parse_meta_item()).node
219 fn parse_optional_meta(&mut self) -> Vec<P<ast::MetaItem>> {
221 token::OpenDelim(token::Paren) => self.parse_meta_seq(),