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.
|
|
|
|
|
2012-12-23 22:41:37 +00:00
|
|
|
use ast;
|
2013-01-30 17:56:33 +00:00
|
|
|
use codemap::spanned;
|
2013-01-09 03:37:25 +00:00
|
|
|
use codemap::BytePos;
|
2012-12-13 21:05:22 +00:00
|
|
|
use parse::common::*; //resolve bug?
|
2012-12-23 22:41:37 +00:00
|
|
|
use parse::token;
|
2013-02-25 19:11:21 +00:00
|
|
|
use parse::parser::Parser;
|
2012-12-23 22:41:37 +00:00
|
|
|
|
2013-02-11 21:36:24 +00:00
|
|
|
// a parser that can parse attributes.
|
2013-01-29 21:54:06 +00:00
|
|
|
pub trait parser_attr {
|
2013-03-02 21:02:27 +00:00
|
|
|
fn parse_outer_attributes(&self) -> ~[ast::attribute];
|
|
|
|
fn parse_attribute(&self, style: ast::attr_style) -> ast::attribute;
|
|
|
|
fn parse_attribute_naked(
|
|
|
|
&self,
|
|
|
|
style: ast::attr_style,
|
|
|
|
lo: BytePos
|
|
|
|
) -> ast::attribute;
|
|
|
|
fn parse_inner_attrs_and_next(&self) ->
|
2013-02-21 08:16:31 +00:00
|
|
|
(~[ast::attribute], ~[ast::attribute]);
|
2013-03-02 21:02:27 +00:00
|
|
|
fn parse_meta_item(&self) -> @ast::meta_item;
|
|
|
|
fn parse_meta_seq(&self) -> ~[@ast::meta_item];
|
|
|
|
fn parse_optional_meta(&self) -> ~[@ast::meta_item];
|
2012-07-11 22:00:40 +00:00
|
|
|
}
|
|
|
|
|
2013-02-14 19:47:00 +00:00
|
|
|
impl parser_attr for Parser {
|
2012-05-24 20:44:42 +00:00
|
|
|
|
|
|
|
// Parse attributes that appear before an item
|
2013-03-02 21:02:27 +00:00
|
|
|
fn parse_outer_attributes(&self) -> ~[ast::attribute] {
|
2012-06-29 23:26:56 +00:00
|
|
|
let mut attrs: ~[ast::attribute] = ~[];
|
2012-06-30 10:54:54 +00:00
|
|
|
loop {
|
2013-02-22 02:12:13 +00:00
|
|
|
match *self.token {
|
2012-08-04 02:59:04 +00:00
|
|
|
token::POUND => {
|
2012-06-30 10:54:54 +00:00
|
|
|
if self.look_ahead(1u) != token::LBRACKET {
|
|
|
|
break;
|
|
|
|
}
|
2012-07-11 23:49:02 +00:00
|
|
|
attrs += ~[self.parse_attribute(ast::attr_outer)];
|
2012-06-30 10:54:54 +00:00
|
|
|
}
|
2012-08-04 02:59:04 +00:00
|
|
|
token::DOC_COMMENT(s) => {
|
2012-06-30 10:54:54 +00:00
|
|
|
let attr = ::attr::mk_sugared_doc_attr(
|
2013-02-26 14:35:36 +00:00
|
|
|
copy *self.id_to_str(s),
|
|
|
|
self.span.lo,
|
|
|
|
self.span.hi
|
|
|
|
);
|
2012-06-30 10:54:54 +00:00
|
|
|
if attr.node.style != ast::attr_outer {
|
2012-07-14 05:57:48 +00:00
|
|
|
self.fatal(~"expected outer comment");
|
2012-06-30 10:54:54 +00:00
|
|
|
}
|
2012-07-11 23:49:02 +00:00
|
|
|
attrs += ~[attr];
|
2012-06-30 10:54:54 +00:00
|
|
|
self.bump();
|
|
|
|
}
|
2012-08-04 02:59:04 +00:00
|
|
|
_ => break
|
2012-06-30 10:54:54 +00:00
|
|
|
}
|
2012-05-24 20:44:42 +00:00
|
|
|
}
|
2012-08-02 00:30:05 +00:00
|
|
|
return attrs;
|
2012-04-20 03:51:31 +00:00
|
|
|
}
|
|
|
|
|
2013-03-29 19:51:10 +00:00
|
|
|
// matches attribute = # attribute_naked
|
2013-03-02 21:02:27 +00:00
|
|
|
fn parse_attribute(&self, style: ast::attr_style) -> ast::attribute {
|
2012-05-24 20:44:42 +00:00
|
|
|
let lo = self.span.lo;
|
2013-02-24 18:20:24 +00:00
|
|
|
self.expect(&token::POUND);
|
2012-08-02 00:30:05 +00:00
|
|
|
return self.parse_attribute_naked(style, lo);
|
2012-05-24 20:44:42 +00:00
|
|
|
}
|
2012-04-20 03:51:31 +00:00
|
|
|
|
2013-03-29 19:51:10 +00:00
|
|
|
// matches attribute_naked = [ meta_item ]
|
2013-03-02 21:02:27 +00:00
|
|
|
fn parse_attribute_naked(&self, style: ast::attr_style, lo: BytePos) ->
|
2012-05-24 20:44:42 +00:00
|
|
|
ast::attribute {
|
2013-02-24 18:20:24 +00:00
|
|
|
self.expect(&token::LBRACKET);
|
2012-05-24 20:44:42 +00:00
|
|
|
let meta_item = self.parse_meta_item();
|
2013-02-24 18:20:24 +00:00
|
|
|
self.expect(&token::RBRACKET);
|
2013-04-12 05:10:31 +00:00
|
|
|
let hi = self.span.hi;
|
2013-01-14 00:51:48 +00:00
|
|
|
return spanned(lo, hi, ast::attribute_ { style: style,
|
2013-02-25 14:19:44 +00:00
|
|
|
value: meta_item,
|
2013-01-14 00:51:48 +00:00
|
|
|
is_sugared_doc: false });
|
2012-05-24 20:44:42 +00:00
|
|
|
}
|
2012-04-20 03:51:31 +00:00
|
|
|
|
2012-05-24 20:44:42 +00:00
|
|
|
// Parse attributes that appear after the opening of an item, each
|
|
|
|
// terminated by a semicolon. In addition to a vector of inner attributes,
|
|
|
|
// this function also returns a vector that may contain the first outer
|
|
|
|
// attribute of the next item (since we can't know whether the attribute
|
|
|
|
// is an inner attribute of the containing item or an outer attribute of
|
|
|
|
// the first contained item until we see the semi).
|
2013-02-11 21:36:24 +00:00
|
|
|
|
2013-03-29 19:51:10 +00:00
|
|
|
// matches inner_attrs* outer_attr?
|
2013-02-11 21:36:24 +00:00
|
|
|
// you can make the 'next' field an Option, but the result is going to be
|
|
|
|
// more useful as a vector.
|
2013-03-02 21:02:27 +00:00
|
|
|
fn parse_inner_attrs_and_next(&self) ->
|
2013-02-21 08:16:31 +00:00
|
|
|
(~[ast::attribute], ~[ast::attribute]) {
|
2012-06-29 23:26:56 +00:00
|
|
|
let mut inner_attrs: ~[ast::attribute] = ~[];
|
|
|
|
let mut next_outer_attrs: ~[ast::attribute] = ~[];
|
2012-06-30 10:54:54 +00:00
|
|
|
loop {
|
2013-02-22 02:12:13 +00:00
|
|
|
match *self.token {
|
2012-08-04 02:59:04 +00:00
|
|
|
token::POUND => {
|
2012-06-30 10:54:54 +00:00
|
|
|
if self.look_ahead(1u) != token::LBRACKET {
|
|
|
|
// This is an extension
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
let attr = self.parse_attribute(ast::attr_inner);
|
2013-02-22 02:12:13 +00:00
|
|
|
if *self.token == token::SEMI {
|
2012-06-30 10:54:54 +00:00
|
|
|
self.bump();
|
2012-07-11 23:49:02 +00:00
|
|
|
inner_attrs += ~[attr];
|
2012-06-30 10:54:54 +00:00
|
|
|
} else {
|
|
|
|
// It's not really an inner attribute
|
|
|
|
let outer_attr =
|
|
|
|
spanned(attr.span.lo, attr.span.hi,
|
2013-01-14 00:51:48 +00:00
|
|
|
ast::attribute_ { style: ast::attr_outer,
|
|
|
|
value: attr.node.value,
|
|
|
|
is_sugared_doc: false });
|
2012-07-11 23:49:02 +00:00
|
|
|
next_outer_attrs += ~[outer_attr];
|
2012-06-30 10:54:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-08-04 02:59:04 +00:00
|
|
|
token::DOC_COMMENT(s) => {
|
2012-06-30 10:54:54 +00:00
|
|
|
let attr = ::attr::mk_sugared_doc_attr(
|
2013-02-26 14:35:36 +00:00
|
|
|
copy *self.id_to_str(s),
|
|
|
|
self.span.lo,
|
|
|
|
self.span.hi
|
|
|
|
);
|
2012-05-24 20:44:42 +00:00
|
|
|
self.bump();
|
2012-06-30 10:54:54 +00:00
|
|
|
if attr.node.style == ast::attr_inner {
|
2012-07-11 23:49:02 +00:00
|
|
|
inner_attrs += ~[attr];
|
2012-06-30 10:54:54 +00:00
|
|
|
} else {
|
2012-07-11 23:49:02 +00:00
|
|
|
next_outer_attrs += ~[attr];
|
2012-06-30 10:54:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-08-04 02:59:04 +00:00
|
|
|
_ => break
|
2012-05-24 20:44:42 +00:00
|
|
|
}
|
2012-04-20 03:51:31 +00:00
|
|
|
}
|
2013-02-21 08:16:31 +00:00
|
|
|
(inner_attrs, next_outer_attrs)
|
2012-05-24 20:44:42 +00:00
|
|
|
}
|
|
|
|
|
2013-03-29 19:51:10 +00:00
|
|
|
// matches meta_item = IDENT
|
|
|
|
// | IDENT = lit
|
|
|
|
// | IDENT meta_seq
|
2013-03-02 21:02:27 +00:00
|
|
|
fn parse_meta_item(&self) -> @ast::meta_item {
|
2012-05-24 20:44:42 +00:00
|
|
|
let lo = self.span.lo;
|
2013-02-14 15:34:21 +00:00
|
|
|
let name = self.id_to_str(self.parse_ident());
|
2013-02-22 02:12:13 +00:00
|
|
|
match *self.token {
|
2013-02-14 15:34:21 +00:00
|
|
|
token::EQ => {
|
|
|
|
self.bump();
|
|
|
|
let lit = self.parse_lit();
|
2013-04-12 05:10:31 +00:00
|
|
|
let hi = self.span.hi;
|
2013-02-14 15:34:21 +00:00
|
|
|
@spanned(lo, hi, ast::meta_name_value(name, lit))
|
|
|
|
}
|
|
|
|
token::LPAREN => {
|
|
|
|
let inner_items = self.parse_meta_seq();
|
2013-04-12 05:10:31 +00:00
|
|
|
let hi = self.span.hi;
|
2013-02-14 15:34:21 +00:00
|
|
|
@spanned(lo, hi, ast::meta_list(name, inner_items))
|
|
|
|
}
|
|
|
|
_ => {
|
2013-05-02 17:36:24 +00:00
|
|
|
let hi = self.last_span.hi;
|
2013-02-14 15:34:21 +00:00
|
|
|
@spanned(lo, hi, ast::meta_word(name))
|
|
|
|
}
|
2012-04-20 03:51:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-29 19:51:10 +00:00
|
|
|
// matches meta_seq = ( COMMASEP(meta_item) )
|
2013-03-02 21:02:27 +00:00
|
|
|
fn parse_meta_seq(&self) -> ~[@ast::meta_item] {
|
2013-02-26 14:35:36 +00:00
|
|
|
copy self.parse_seq(
|
2013-02-25 02:55:24 +00:00
|
|
|
&token::LPAREN,
|
|
|
|
&token::RPAREN,
|
2013-02-24 23:41:54 +00:00
|
|
|
seq_sep_trailing_disallowed(token::COMMA),
|
|
|
|
|p| p.parse_meta_item()
|
|
|
|
).node
|
2012-04-20 03:51:31 +00:00
|
|
|
}
|
|
|
|
|
2013-03-02 21:02:27 +00:00
|
|
|
fn parse_optional_meta(&self) -> ~[@ast::meta_item] {
|
2013-02-22 02:12:13 +00:00
|
|
|
match *self.token {
|
2013-02-25 04:51:56 +00:00
|
|
|
token::LPAREN => self.parse_meta_seq(),
|
|
|
|
_ => ~[]
|
2012-08-04 02:59:04 +00:00
|
|
|
}
|
2012-05-24 20:44:42 +00:00
|
|
|
}
|
2012-04-20 03:51:31 +00:00
|
|
|
}
|