From 7edab6ae6b4c5d0c411e88f10e923b91dca31de3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 24 Aug 2018 19:27:30 +0300 Subject: nodes for blocks --- crates/libsyntax2/src/grammar/items/mod.rs | 26 ++++++--- crates/libsyntax2/src/grammar/items/structs.rs | 75 +++++++++++++++---------- crates/libsyntax2/src/grammar/items/traits.rs | 54 ++++++++++++------ crates/libsyntax2/src/grammar/items/use_item.rs | 8 ++- 4 files changed, 106 insertions(+), 57 deletions(-) (limited to 'crates/libsyntax2/src/grammar/items') diff --git a/crates/libsyntax2/src/grammar/items/mod.rs b/crates/libsyntax2/src/grammar/items/mod.rs index 18b681ee2..d236fb506 100644 --- a/crates/libsyntax2/src/grammar/items/mod.rs +++ b/crates/libsyntax2/src/grammar/items/mod.rs @@ -194,8 +194,8 @@ fn items_without_modifiers(p: &mut Parser) -> Option { // extern {} EXTERN_KW if la == L_CURLY || ((la == STRING || la == RAW_STRING) && p.nth(2) == L_CURLY) => { abi(p); - extern_block(p); - EXTERN_BLOCK_EXPR + extern_item_list(p); + EXTERN_BLOCK } _ => return None, }; @@ -212,10 +212,12 @@ fn extern_crate_item(p: &mut Parser) { p.expect(SEMI); } -fn extern_block(p: &mut Parser) { +fn extern_item_list(p: &mut Parser) { assert!(p.at(L_CURLY)); + let m = p.start(); p.bump(); p.expect(R_CURLY); + m.complete(p, EXTERN_ITEM_LIST); } fn function(p: &mut Parser, flavor: ItemFlavor) { @@ -284,14 +286,22 @@ fn mod_item(p: &mut Parser) { p.bump(); name(p); - if !p.eat(SEMI) { - if p.expect(L_CURLY) { - mod_contents(p, true); - p.expect(R_CURLY); - } + if p.at(L_CURLY) { + mod_item_list(p); + } else if !p.eat(SEMI) { + p.error("expected `;` or `{`"); } } +fn mod_item_list(p: &mut Parser) { + assert!(p.at(L_CURLY)); + let m = p.start(); + p.bump(); + mod_contents(p, true); + p.expect(R_CURLY); + m.complete(p, ITEM_LIST); +} + fn macro_call(p: &mut Parser) -> BlockLike { assert!(paths::is_path_start(p)); paths::use_path(p); diff --git a/crates/libsyntax2/src/grammar/items/structs.rs b/crates/libsyntax2/src/grammar/items/structs.rs index cde9d0ae6..ca027d718 100644 --- a/crates/libsyntax2/src/grammar/items/structs.rs +++ b/crates/libsyntax2/src/grammar/items/structs.rs @@ -14,7 +14,7 @@ pub(super) fn struct_def(p: &mut Parser) { p.bump(); return; } - L_CURLY => named_fields(p), + L_CURLY => named_field_def_list(p), _ => { //TODO: special case `(` error message p.error("expected `;` or `{`"); @@ -26,9 +26,9 @@ pub(super) fn struct_def(p: &mut Parser) { p.bump(); return; } - L_CURLY => named_fields(p), + L_CURLY => named_field_def_list(p), L_PAREN => { - pos_fields(p); + pos_field_list(p); p.expect(SEMI); } _ => { @@ -44,46 +44,58 @@ pub(super) fn enum_def(p: &mut Parser) { name(p); type_params::opt_type_param_list(p); type_params::opt_where_clause(p); - if p.expect(L_CURLY) { - 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_fields(p), - L_PAREN => pos_fields(p), - EQ => { - p.bump(); - expressions::expr(p); - } - _ => (), + 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); } - var.complete(p, ENUM_VARIANT); - } else { - var.abandon(p); - p.err_and_bump("expected enum variant"); - } - if !p.at(R_CURLY) { - p.expect(COMMA); + _ => (), } + var.complete(p, ENUM_VARIANT); + } else { + var.abandon(p); + p.err_and_bump("expected enum variant"); + } + if !p.at(R_CURLY) { + p.expect(COMMA); } - p.expect(R_CURLY); } + p.expect(R_CURLY); + m.complete(p, ENUM_VARIANT_LIST); } -fn named_fields(p: &mut Parser) { +fn named_field_def_list(p: &mut Parser) { assert!(p.at(L_CURLY)); + let m = p.start(); p.bump(); while !p.at(R_CURLY) && !p.at(EOF) { - named_field(p); + named_field_def(p); if !p.at(R_CURLY) { p.expect(COMMA); } } p.expect(R_CURLY); + m.complete(p, NAMED_FIELD_DEF_LIST); - fn named_field(p: &mut Parser) { + fn named_field_def(p: &mut Parser) { let m = p.start(); // test field_attrs // struct S { @@ -96,7 +108,7 @@ fn named_fields(p: &mut Parser) { name(p); p.expect(COLON); types::type_(p); - m.complete(p, NAMED_FIELD); + m.complete(p, NAMED_FIELD_DEF); } else { m.abandon(p); p.err_and_bump("expected field declaration"); @@ -104,7 +116,9 @@ fn named_fields(p: &mut Parser) { } } -fn pos_fields(p: &mut Parser) { +fn pos_field_list(p: &mut Parser) { + assert!(p.at(L_PAREN)); + let m = p.start(); if !p.expect(L_PAREN) { return; } @@ -119,4 +133,5 @@ fn pos_fields(p: &mut Parser) { } } p.expect(R_PAREN); + m.complete(p, POS_FIELD_LIST); } diff --git a/crates/libsyntax2/src/grammar/items/traits.rs b/crates/libsyntax2/src/grammar/items/traits.rs index 73ecd4bef..3853ccaab 100644 --- a/crates/libsyntax2/src/grammar/items/traits.rs +++ b/crates/libsyntax2/src/grammar/items/traits.rs @@ -11,18 +11,29 @@ pub(super) fn trait_def(p: &mut Parser) { type_params::bounds(p); } type_params::opt_where_clause(p); - p.expect(L_CURLY); - // test trait_item_items - // impl F { - // type A: Clone; - // const B: i32; - // fn foo() {} - // fn bar(&self); - // } + if p.at(L_CURLY) { + trait_item_list(p); + } else { + p.error("expected `{`"); + } +} + +// test trait_item_list +// impl F { +// type A: Clone; +// const B: i32; +// fn foo() {} +// fn bar(&self); +// } +fn trait_item_list(p: &mut Parser) { + assert!(p.at(L_CURLY)); + let m = p.start(); + p.bump(); while !p.at(EOF) && !p.at(R_CURLY) { item_or_macro(p, true, ItemFlavor::Trait); } p.expect(R_CURLY); + m.complete(p, ITEM_LIST); } // test impl_item @@ -45,19 +56,30 @@ pub(super) fn impl_item(p: &mut Parser) { types::type_(p); } type_params::opt_where_clause(p); - p.expect(L_CURLY); + if p.at(L_CURLY) { + impl_item_list(p); + } else { + p.error("expected `{`"); + } +} + +// test impl_item_list +// impl F { +// type A = i32; +// const B: i32 = 92; +// fn foo() {} +// fn bar(&self) {} +// } +fn impl_item_list(p: &mut Parser) { + assert!(p.at(L_CURLY)); + let m = p.start(); + p.bump(); - // test impl_item_items - // impl F { - // type A = i32; - // const B: i32 = 92; - // fn foo() {} - // fn bar(&self) {} - // } while !p.at(EOF) && !p.at(R_CURLY) { item_or_macro(p, true, ItemFlavor::Mod); } p.expect(R_CURLY); + m.complete(p, ITEM_LIST); } fn choose_type_params_over_qpath(p: &Parser) -> bool { diff --git a/crates/libsyntax2/src/grammar/items/use_item.rs b/crates/libsyntax2/src/grammar/items/use_item.rs index 3da40a629..2fbf2234a 100644 --- a/crates/libsyntax2/src/grammar/items/use_item.rs +++ b/crates/libsyntax2/src/grammar/items/use_item.rs @@ -20,7 +20,7 @@ fn use_tree(p: &mut Parser) { if p.at(COLONCOLON) { p.bump(); } - nested_trees(p); + use_tree_list(p); } _ if paths::is_path_start(p) => { paths::use_path(p); @@ -34,7 +34,7 @@ fn use_tree(p: &mut Parser) { STAR => { p.bump(); } - L_CURLY => nested_trees(p), + L_CURLY => use_tree_list(p), _ => { // is this unreachable? p.error("expected `{` or `*`"); @@ -53,8 +53,9 @@ fn use_tree(p: &mut Parser) { m.complete(p, USE_TREE); } -fn nested_trees(p: &mut Parser) { +fn use_tree_list(p: &mut Parser) { assert!(p.at(L_CURLY)); + let m = p.start(); p.bump(); while !p.at(EOF) && !p.at(R_CURLY) { use_tree(p); @@ -63,4 +64,5 @@ fn nested_trees(p: &mut Parser) { } } p.expect(R_CURLY); + m.complete(p, USE_TREE_LIST); } -- cgit v1.2.3