Files
rust/crates/libsyntax2/src/grammar/items/structs.rs

138 lines
3.3 KiB
Rust
Raw Normal View History

use super::*;
2018-08-13 18:34:02 +03:00
pub(super) fn struct_def(p: &mut Parser) {
assert!(p.at(STRUCT_KW));
p.bump();
2018-02-10 14:10:02 +03:00
name(p);
2018-08-24 02:14:10 +03:00
type_params::opt_type_param_list(p);
match p.current() {
WHERE_KW => {
2018-08-24 02:14:10 +03:00
type_params::opt_where_clause(p);
match p.current() {
SEMI => {
p.bump();
return;
}
2018-08-24 19:27:30 +03:00
L_CURLY => named_field_def_list(p),
_ => {
//TODO: special case `(` error message
2018-02-09 22:44:50 +03:00
p.error("expected `;` or `{`");
return;
}
}
}
SEMI => {
p.bump();
return;
}
2018-08-24 19:27:30 +03:00
L_CURLY => named_field_def_list(p),
L_PAREN => {
2018-08-24 19:27:30 +03:00
pos_field_list(p);
p.expect(SEMI);
}
_ => {
2018-02-09 22:44:50 +03:00
p.error("expected `;`, `{`, or `(`");
return;
}
}
}
2018-08-13 18:34:02 +03:00
pub(super) fn enum_def(p: &mut Parser) {
2018-01-28 22:59:18 +03:00
assert!(p.at(ENUM_KW));
p.bump();
2018-02-10 14:10:50 +03:00
name(p);
2018-08-24 02:14:10 +03:00
type_params::opt_type_param_list(p);
type_params::opt_where_clause(p);
2018-08-24 19:27:30 +03:00
if p.at(L_CURLY) {
enum_variant_list(p);
} else {
p.error("expected `{`")
}
}
fn enum_variant_list(p: &mut Parser) {
assert!(p.at(L_CURLY));
let m = p.start();
p.bump();
while !p.at(EOF) && !p.at(R_CURLY) {
let var = p.start();
attributes::outer_attributes(p);
if p.at(IDENT) {
name(p);
match p.current() {
L_CURLY => named_field_def_list(p),
L_PAREN => pos_field_list(p),
EQ => {
p.bump();
expressions::expr(p);
2018-01-28 22:59:18 +03:00
}
2018-08-24 19:27:30 +03:00
_ => (),
2018-01-28 22:59:18 +03:00
}
2018-08-24 19:27:30 +03:00
var.complete(p, ENUM_VARIANT);
} else {
var.abandon(p);
p.err_and_bump("expected enum variant");
}
if !p.at(R_CURLY) {
p.expect(COMMA);
2018-01-28 22:59:18 +03:00
}
}
2018-08-24 19:27:30 +03:00
p.expect(R_CURLY);
m.complete(p, ENUM_VARIANT_LIST);
2018-01-28 22:59:18 +03:00
}
2018-08-24 19:27:30 +03:00
fn named_field_def_list(p: &mut Parser) {
assert!(p.at(L_CURLY));
2018-08-24 19:27:30 +03:00
let m = p.start();
p.bump();
while !p.at(R_CURLY) && !p.at(EOF) {
2018-08-24 19:27:30 +03:00
named_field_def(p);
if !p.at(R_CURLY) {
p.expect(COMMA);
}
}
p.expect(R_CURLY);
2018-08-24 19:27:30 +03:00
m.complete(p, NAMED_FIELD_DEF_LIST);
2018-08-24 19:27:30 +03:00
fn named_field_def(p: &mut Parser) {
2018-08-16 13:20:59 +03:00
let m = p.start();
// test field_attrs
// struct S {
// #[serde(with = "url_serde")]
// pub uri: Uri,
// }
attributes::outer_attributes(p);
2018-08-24 00:16:29 +03:00
opt_visibility(p);
2018-02-10 14:13:30 +03:00
if p.at(IDENT) {
name(p);
p.expect(COLON);
2018-02-11 11:01:00 +03:00
types::type_(p);
2018-08-24 19:27:30 +03:00
m.complete(p, NAMED_FIELD_DEF);
} else {
2018-08-16 13:20:59 +03:00
m.abandon(p);
p.err_and_bump("expected field declaration");
}
}
}
2018-08-24 19:27:30 +03:00
fn pos_field_list(p: &mut Parser) {
assert!(p.at(L_PAREN));
let m = p.start();
if !p.expect(L_PAREN) {
return;
}
while !p.at(R_PAREN) && !p.at(EOF) {
let pos_field = p.start();
2018-08-24 00:16:29 +03:00
opt_visibility(p);
2018-02-11 11:01:00 +03:00
types::type_(p);
pos_field.complete(p, POS_FIELD);
if !p.at(R_PAREN) {
p.expect(COMMA);
}
}
p.expect(R_PAREN);
2018-08-24 19:27:30 +03:00
m.complete(p, POS_FIELD_LIST);
}