Files
rust/src/parser/grammar/attributes.rs

80 lines
1.8 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-01-21 00:31:29 +03:00
while p.at([POUND, EXCL]) {
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();
if !expressions::literal(p) {
2018-01-27 18:31:23 -05:00
p.error().message("expected literal").emit();
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-01-27 18:31:23 -05:00
p.error().message("expected attribute value").emit()
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),
c => if !expressions::literal(p) {
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) {
p.error().message(message).emit();
return;
}
let err = p.start();
p.error().message(message).emit();
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);
}