Files
rust/crates/libsyntax2/src/grammar/attributes.rs

80 lines
1.7 KiB
Rust
Raw Normal View History

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