2012-04-19 22:51:31 -05:00
|
|
|
import either::{either, left, right};
|
2012-04-19 23:24:42 -05:00
|
|
|
import ast_util::spanned;
|
2012-06-27 17:29:35 -05:00
|
|
|
import common::*; //resolve bug?
|
2012-04-19 22:51:31 -05:00
|
|
|
|
|
|
|
export attr_or_ext;
|
2012-05-24 15:44:42 -05:00
|
|
|
export parser_attr;
|
2012-04-19 22:51:31 -05:00
|
|
|
|
|
|
|
// A type to distingush between the parsing of item attributes or syntax
|
|
|
|
// extensions, which both begin with token.POUND
|
2012-06-29 18:26:56 -05:00
|
|
|
type attr_or_ext = option<either<~[ast::attribute], @ast::expr>>;
|
2012-04-19 22:51:31 -05:00
|
|
|
|
2012-07-11 17:00:40 -05:00
|
|
|
trait parser_attr {
|
|
|
|
fn parse_outer_attrs_or_ext(first_item_attrs: ~[ast::attribute])
|
|
|
|
-> attr_or_ext;
|
|
|
|
fn parse_outer_attributes() -> ~[ast::attribute];
|
|
|
|
fn parse_attribute(style: ast::attr_style) -> ast::attribute;
|
|
|
|
fn parse_attribute_naked(style: ast::attr_style, lo: uint) ->
|
|
|
|
ast::attribute;
|
|
|
|
fn parse_inner_attrs_and_next() ->
|
|
|
|
{inner: ~[ast::attribute], next: ~[ast::attribute]};
|
|
|
|
fn parse_meta_item() -> @ast::meta_item;
|
|
|
|
fn parse_meta_seq() -> ~[@ast::meta_item];
|
|
|
|
fn parse_optional_meta() -> ~[@ast::meta_item];
|
|
|
|
}
|
|
|
|
|
|
|
|
impl parser_attr of parser_attr for parser {
|
2012-05-24 15:44:42 -05:00
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn parse_outer_attrs_or_ext(first_item_attrs: ~[ast::attribute])
|
2012-05-24 15:44:42 -05:00
|
|
|
-> attr_or_ext
|
|
|
|
{
|
|
|
|
let expect_item_next = vec::is_not_empty(first_item_attrs);
|
2012-06-30 05:54:54 -05:00
|
|
|
alt self.token {
|
|
|
|
token::POUND {
|
2012-05-24 15:44:42 -05:00
|
|
|
let lo = self.span.lo;
|
|
|
|
if self.look_ahead(1u) == token::LBRACKET {
|
|
|
|
self.bump();
|
|
|
|
let first_attr =
|
|
|
|
self.parse_attribute_naked(ast::attr_outer, lo);
|
2012-06-29 18:26:56 -05:00
|
|
|
ret some(left(vec::append(~[first_attr],
|
2012-06-28 01:09:51 -05:00
|
|
|
self.parse_outer_attributes())));
|
2012-05-24 15:44:42 -05:00
|
|
|
} else if !(self.look_ahead(1u) == token::LT
|
|
|
|
|| self.look_ahead(1u) == token::LBRACKET
|
2012-05-21 12:45:56 -05:00
|
|
|
|| self.look_ahead(1u) == token::POUND
|
2012-05-24 15:44:42 -05:00
|
|
|
|| expect_item_next) {
|
|
|
|
self.bump();
|
|
|
|
ret some(right(self.parse_syntax_ext_naked(lo)));
|
|
|
|
} else { ret none; }
|
2012-06-30 05:54:54 -05:00
|
|
|
}
|
|
|
|
token::DOC_COMMENT(_) {
|
|
|
|
ret some(left(self.parse_outer_attributes()));
|
|
|
|
}
|
|
|
|
_ {
|
|
|
|
ret none;
|
|
|
|
}
|
|
|
|
}
|
2012-05-24 15:44:42 -05:00
|
|
|
}
|
2012-04-19 22:51:31 -05:00
|
|
|
|
2012-05-24 15:44:42 -05:00
|
|
|
// Parse attributes that appear before an item
|
2012-06-29 18:26:56 -05:00
|
|
|
fn parse_outer_attributes() -> ~[ast::attribute] {
|
|
|
|
let mut attrs: ~[ast::attribute] = ~[];
|
2012-06-30 05:54:54 -05:00
|
|
|
loop {
|
|
|
|
alt copy self.token {
|
|
|
|
token::POUND {
|
|
|
|
if self.look_ahead(1u) != token::LBRACKET {
|
|
|
|
break;
|
|
|
|
}
|
2012-07-11 18:49:02 -05:00
|
|
|
attrs += ~[self.parse_attribute(ast::attr_outer)];
|
2012-06-30 05:54:54 -05:00
|
|
|
}
|
|
|
|
token::DOC_COMMENT(s) {
|
|
|
|
let attr = ::attr::mk_sugared_doc_attr(
|
|
|
|
*self.get_str(s), self.span.lo, self.span.hi);
|
|
|
|
if attr.node.style != ast::attr_outer {
|
2012-07-14 00:57:48 -05:00
|
|
|
self.fatal(~"expected outer comment");
|
2012-06-30 05:54:54 -05:00
|
|
|
}
|
2012-07-11 18:49:02 -05:00
|
|
|
attrs += ~[attr];
|
2012-06-30 05:54:54 -05:00
|
|
|
self.bump();
|
|
|
|
}
|
|
|
|
_ {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-05-24 15:44:42 -05:00
|
|
|
}
|
|
|
|
ret attrs;
|
2012-04-19 22:51:31 -05:00
|
|
|
}
|
|
|
|
|
2012-05-24 15:44:42 -05:00
|
|
|
fn parse_attribute(style: ast::attr_style) -> ast::attribute {
|
|
|
|
let lo = self.span.lo;
|
|
|
|
self.expect(token::POUND);
|
|
|
|
ret self.parse_attribute_naked(style, lo);
|
|
|
|
}
|
2012-04-19 22:51:31 -05:00
|
|
|
|
2012-05-24 15:44:42 -05:00
|
|
|
fn parse_attribute_naked(style: ast::attr_style, lo: uint) ->
|
|
|
|
ast::attribute {
|
|
|
|
self.expect(token::LBRACKET);
|
|
|
|
let meta_item = self.parse_meta_item();
|
|
|
|
self.expect(token::RBRACKET);
|
|
|
|
let mut hi = self.span.hi;
|
2012-06-30 05:54:54 -05:00
|
|
|
ret spanned(lo, hi, {style: style, value: *meta_item,
|
|
|
|
is_sugared_doc: false});
|
2012-05-24 15:44:42 -05:00
|
|
|
}
|
2012-04-19 22:51:31 -05:00
|
|
|
|
2012-05-24 15:44:42 -05: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).
|
|
|
|
fn parse_inner_attrs_and_next() ->
|
2012-06-29 18:26:56 -05:00
|
|
|
{inner: ~[ast::attribute], next: ~[ast::attribute]} {
|
|
|
|
let mut inner_attrs: ~[ast::attribute] = ~[];
|
|
|
|
let mut next_outer_attrs: ~[ast::attribute] = ~[];
|
2012-06-30 05:54:54 -05:00
|
|
|
loop {
|
|
|
|
alt copy self.token {
|
|
|
|
token::POUND {
|
|
|
|
if self.look_ahead(1u) != token::LBRACKET {
|
|
|
|
// This is an extension
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
let attr = self.parse_attribute(ast::attr_inner);
|
|
|
|
if self.token == token::SEMI {
|
|
|
|
self.bump();
|
2012-07-11 18:49:02 -05:00
|
|
|
inner_attrs += ~[attr];
|
2012-06-30 05:54:54 -05:00
|
|
|
} else {
|
|
|
|
// It's not really an inner attribute
|
|
|
|
let outer_attr =
|
|
|
|
spanned(attr.span.lo, attr.span.hi,
|
|
|
|
{style: ast::attr_outer, value: attr.node.value,
|
|
|
|
is_sugared_doc: false});
|
2012-07-11 18:49:02 -05:00
|
|
|
next_outer_attrs += ~[outer_attr];
|
2012-06-30 05:54:54 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
token::DOC_COMMENT(s) {
|
|
|
|
let attr = ::attr::mk_sugared_doc_attr(
|
|
|
|
*self.get_str(s), self.span.lo, self.span.hi);
|
2012-05-24 15:44:42 -05:00
|
|
|
self.bump();
|
2012-06-30 05:54:54 -05:00
|
|
|
if attr.node.style == ast::attr_inner {
|
2012-07-11 18:49:02 -05:00
|
|
|
inner_attrs += ~[attr];
|
2012-06-30 05:54:54 -05:00
|
|
|
} else {
|
2012-07-11 18:49:02 -05:00
|
|
|
next_outer_attrs += ~[attr];
|
2012-06-30 05:54:54 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ {
|
2012-05-24 15:44:42 -05:00
|
|
|
break;
|
2012-06-30 05:54:54 -05:00
|
|
|
}
|
2012-05-24 15:44:42 -05:00
|
|
|
}
|
2012-04-19 22:51:31 -05:00
|
|
|
}
|
2012-05-24 15:44:42 -05:00
|
|
|
ret {inner: inner_attrs, next: next_outer_attrs};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_meta_item() -> @ast::meta_item {
|
|
|
|
let lo = self.span.lo;
|
|
|
|
let ident = self.parse_ident();
|
|
|
|
alt self.token {
|
|
|
|
token::EQ {
|
|
|
|
self.bump();
|
|
|
|
let lit = self.parse_lit();
|
|
|
|
let mut hi = self.span.hi;
|
|
|
|
ret @spanned(lo, hi, ast::meta_name_value(ident, lit));
|
|
|
|
}
|
|
|
|
token::LPAREN {
|
|
|
|
let inner_items = self.parse_meta_seq();
|
|
|
|
let mut hi = self.span.hi;
|
|
|
|
ret @spanned(lo, hi, ast::meta_list(ident, inner_items));
|
|
|
|
}
|
|
|
|
_ {
|
|
|
|
let mut hi = self.span.hi;
|
|
|
|
ret @spanned(lo, hi, ast::meta_word(ident));
|
|
|
|
}
|
2012-04-19 22:51:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn parse_meta_seq() -> ~[@ast::meta_item] {
|
2012-05-24 15:44:42 -05:00
|
|
|
ret self.parse_seq(token::LPAREN, token::RPAREN,
|
2012-06-12 12:17:36 -05:00
|
|
|
seq_sep_trailing_disallowed(token::COMMA),
|
2012-06-30 18:19:07 -05:00
|
|
|
|p| p.parse_meta_item()).node;
|
2012-04-19 22:51:31 -05:00
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn parse_optional_meta() -> ~[@ast::meta_item] {
|
2012-05-24 15:44:42 -05:00
|
|
|
alt self.token { token::LPAREN { ret self.parse_meta_seq(); }
|
2012-06-29 18:26:56 -05:00
|
|
|
_ { ret ~[]; } }
|
2012-05-24 15:44:42 -05:00
|
|
|
}
|
2012-04-19 22:51:31 -05:00
|
|
|
}
|
|
|
|
|
2012-05-24 15:44:42 -05:00
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|
|
|
|
//
|