rust/src/parser/grammar/attributes.rs

80 lines
1.7 KiB
Rust
Raw Normal View History

2018-01-07 19:46:10 +01:00
use super::*;
pub(super) fn inner_attributes(p: &mut Parser) {
2018-02-11 11:13:06 +01:00
while p.current() == POUND && p.nth(1) == EXCL {
2018-01-20 22:31:29 +01:00
attribute(p, true)
}
2018-01-07 19:46:10 +01:00
}
2018-01-11 21:01:12 +01:00
pub(super) fn outer_attributes(p: &mut Parser) {
2018-01-20 22:31:29 +01:00
while p.at(POUND) {
attribute(p, false)
}
2018-01-07 19:46:10 +01:00
}
2018-01-28 00:31:23 +01:00
fn attribute(p: &mut Parser, inner: bool) {
2018-01-20 22:31:29 +01:00
let attr = p.start();
assert!(p.at(POUND));
p.bump();
if inner {
assert!(p.at(EXCL));
2018-01-20 19:49:58 +01:00
p.bump();
2018-01-08 20:40:14 +01:00
}
2018-01-20 22:31:29 +01:00
if p.expect(L_BRACK) {
meta_item(p);
p.expect(R_BRACK);
}
attr.complete(p, ATTR);
2018-01-07 19:46:10 +01:00
}
2018-01-20 22:31:29 +01:00
fn meta_item(p: &mut Parser) {
2018-01-20 19:49:58 +01:00
if p.at(IDENT) {
2018-01-20 21:25:34 +01:00
let meta_item = p.start();
2018-01-20 19:49:58 +01:00
p.bump();
2018-01-20 22:31:29 +01:00
match p.current() {
EQ => {
p.bump();
2018-07-31 11:26:19 +02:00
if expressions::literal(p).is_none() {
2018-02-09 20:44:50 +01:00
p.error("expected literal");
2018-01-20 22:31:29 +01:00
}
2018-01-07 19:46:10 +01:00
}
2018-01-20 22:31:29 +01:00
L_PAREN => meta_item_arg_list(p),
_ => (),
2018-01-07 19:46:10 +01:00
}
2018-01-20 21:25:34 +01:00
meta_item.complete(p, META_ITEM);
2018-01-20 19:49:58 +01:00
} else {
2018-02-09 20:44:50 +01:00
p.error("expected attribute value");
2018-01-20 19:49:58 +01:00
}
2018-01-07 19:46:10 +01:00
}
2018-01-20 22:31:29 +01:00
fn meta_item_arg_list(p: &mut Parser) {
assert!(p.at(L_PAREN));
p.bump();
loop {
match p.current() {
EOF | R_PAREN => break,
IDENT => meta_item(p),
2018-07-31 11:26:19 +02:00
c => if expressions::literal(p).is_none() {
2018-01-20 22:31:29 +01:00
let message = "expected attribute";
2018-01-07 19:46:10 +01:00
2018-01-20 22:31:29 +01:00
if items::ITEM_FIRST.contains(c) {
2018-02-09 20:44:50 +01:00
p.error(message);
2018-01-20 22:31:29 +01:00
return;
}
let err = p.start();
2018-02-09 20:44:50 +01:00
p.error(message);
2018-01-20 22:31:29 +01:00
p.bump();
err.complete(p, ERROR);
2018-01-28 00:31:23 +01:00
continue;
},
2018-01-20 22:31:29 +01:00
}
if !p.at(R_PAREN) {
p.expect(COMMA);
}
}
p.expect(R_PAREN);
}