From 100968b68999231368fbacf62e8b8f242fbfdd3a Mon Sep 17 00:00:00 2001 From: darksv Date: Fri, 14 Sep 2018 22:51:12 +0200 Subject: Support for unions --- crates/libsyntax2/src/grammar/items/mod.rs | 21 ++++++++++++++++++++- crates/libsyntax2/src/grammar/items/nominal.rs | 16 ++++++++++------ 2 files changed, 30 insertions(+), 7 deletions(-) (limited to 'crates/libsyntax2/src') diff --git a/crates/libsyntax2/src/grammar/items/mod.rs b/crates/libsyntax2/src/grammar/items/mod.rs index 8c19aa179..2567313ab 100644 --- a/crates/libsyntax2/src/grammar/items/mod.rs +++ b/crates/libsyntax2/src/grammar/items/mod.rs @@ -181,7 +181,16 @@ fn items_without_modifiers(p: &mut Parser) -> Option { MODULE } STRUCT_KW => { - nominal::struct_def(p); + // test struct_items + // struct Foo; + // struct Foo {} + // struct Foo(); + // struct Foo(String, usize); + // struct Foo { + // a: i32, + // b: f32, + // } + nominal::struct_def(p, STRUCT_KW); if p.at(SEMI) { p.err_and_bump( "expected item, found `;`\n\ @@ -190,6 +199,16 @@ fn items_without_modifiers(p: &mut Parser) -> Option { } STRUCT_DEF } + IDENT if p.at_contextual_kw("union") => { + // test union_items + // union Foo {} + // union Foo { + // a: i32, + // b: f32, + // } + nominal::struct_def(p, UNION_KW); + STRUCT_DEF + } ENUM_KW => { nominal::enum_def(p); ENUM_DEF diff --git a/crates/libsyntax2/src/grammar/items/nominal.rs b/crates/libsyntax2/src/grammar/items/nominal.rs index 11c43e371..8d02ad555 100644 --- a/crates/libsyntax2/src/grammar/items/nominal.rs +++ b/crates/libsyntax2/src/grammar/items/nominal.rs @@ -1,8 +1,8 @@ use super::*; -pub(super) fn struct_def(p: &mut Parser) { - assert!(p.at(STRUCT_KW)); - p.bump(); +pub(super) fn struct_def(p: &mut Parser, kind: SyntaxKind) { + assert!(p.at(STRUCT_KW) || p.at_contextual_kw("union")); + p.bump_remap(kind); name_r(p, ITEM_RECOVERY_SET); type_params::opt_type_param_list(p); @@ -22,19 +22,23 @@ pub(super) fn struct_def(p: &mut Parser) { } } } - SEMI => { + SEMI if kind == STRUCT_KW => { p.bump(); return; } L_CURLY => named_field_def_list(p), - L_PAREN => { + L_PAREN if kind == STRUCT_KW => { pos_field_list(p); p.expect(SEMI); } - _ => { + _ if kind == STRUCT_KW => { p.error("expected `;`, `{`, or `(`"); return; } + _ => { + p.error("expected `{`"); + return; + } } } -- cgit v1.2.3