diff --git a/src/parser/event_parser/grammar/items.rs b/src/parser/event_parser/grammar/items/mod.rs similarity index 69% rename from src/parser/event_parser/grammar/items.rs rename to src/parser/event_parser/grammar/items/mod.rs index 56e3208ac4..867776415f 100644 --- a/src/parser/event_parser/grammar/items.rs +++ b/src/parser/event_parser/grammar/items/mod.rs @@ -1,5 +1,7 @@ use super::*; +mod structs; + pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) { attributes::inner_attributes(p); while !p.at(EOF) && !(stop_on_r_curly && p.at(R_CURLY)) { @@ -29,7 +31,7 @@ fn item(p: &mut Parser) { USE_ITEM } STRUCT_KW => { - struct_item(p); + structs::struct_item(p); STRUCT_ITEM } FN_KW => { @@ -57,88 +59,6 @@ fn item(p: &mut Parser) { item.complete(p, item_kind); } -fn struct_item(p: &mut Parser) { - assert!(p.at(STRUCT_KW)); - p.bump(); - - if !p.expect(IDENT) { - return; - } - generic_parameters(p); - match p.current() { - WHERE_KW => { - where_clause(p); - match p.current() { - SEMI => { - p.bump(); - return; - } - L_CURLY => named_fields(p), - _ => { - //TODO: special case `(` error message - p.error().message("expected `;` or `{`").emit(); - return; - } - } - } - SEMI => { - p.bump(); - return; - } - L_CURLY => named_fields(p), - L_PAREN => { - pos_fields(p); - p.expect(SEMI); - } - _ => { - p.error().message("expected `;`, `{`, or `(`").emit(); - return; - } - } -} - -fn named_fields(p: &mut Parser) { - assert!(p.at(L_CURLY)); - p.bump(); - while !p.at(R_CURLY) && !p.at(EOF) { - named_field(p); - if !p.at(R_CURLY) { - p.expect(COMMA); - } - } - p.expect(R_CURLY); - - fn named_field(p: &mut Parser) { - let field = p.start(); - visibility(p); - if p.expect(IDENT) { - p.expect(COLON); - types::type_ref(p); - field.complete(p, NAMED_FIELD); - } else { - field.abandon(p); - p.err_and_bump("expected field declaration"); - } - } -} - -fn pos_fields(p: &mut Parser) { - if !p.expect(L_PAREN) { - return; - } - while !p.at(R_PAREN) && !p.at(EOF) { - let pos_field = p.start(); - visibility(p); - types::type_ref(p); - pos_field.complete(p, POS_FIELD); - - if !p.at(R_PAREN) { - p.expect(COMMA); - } - } - p.expect(R_PAREN); -} - fn generic_parameters(_: &mut Parser) {} fn where_clause(_: &mut Parser) {} diff --git a/src/parser/event_parser/grammar/items/structs.rs b/src/parser/event_parser/grammar/items/structs.rs new file mode 100644 index 0000000000..b31cf18df5 --- /dev/null +++ b/src/parser/event_parser/grammar/items/structs.rs @@ -0,0 +1,83 @@ +use super::*; + +pub(super) fn struct_item(p: &mut Parser) { + assert!(p.at(STRUCT_KW)); + p.bump(); + + if !p.expect(IDENT) { + return; + } + generic_parameters(p); + match p.current() { + WHERE_KW => { + where_clause(p); + match p.current() { + SEMI => { + p.bump(); + return; + } + L_CURLY => named_fields(p), + _ => { + //TODO: special case `(` error message + p.error().message("expected `;` or `{`").emit(); + return; + } + } + } + SEMI => { + p.bump(); + return; + } + L_CURLY => named_fields(p), + L_PAREN => { + pos_fields(p); + p.expect(SEMI); + } + _ => { + p.error().message("expected `;`, `{`, or `(`").emit(); + return; + } + } +} + +fn named_fields(p: &mut Parser) { + assert!(p.at(L_CURLY)); + p.bump(); + while !p.at(R_CURLY) && !p.at(EOF) { + named_field(p); + if !p.at(R_CURLY) { + p.expect(COMMA); + } + } + p.expect(R_CURLY); + + fn named_field(p: &mut Parser) { + let field = p.start(); + visibility(p); + if p.expect(IDENT) { + p.expect(COLON); + types::type_ref(p); + field.complete(p, NAMED_FIELD); + } else { + field.abandon(p); + p.err_and_bump("expected field declaration"); + } + } +} + +fn pos_fields(p: &mut Parser) { + if !p.expect(L_PAREN) { + return; + } + while !p.at(R_PAREN) && !p.at(EOF) { + let pos_field = p.start(); + visibility(p); + types::type_ref(p); + pos_field.complete(p, POS_FIELD); + + if !p.at(R_PAREN) { + p.expect(COMMA); + } + } + p.expect(R_PAREN); +}