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/expressions/atom.rs | 73 ++++++----------------- crates/libsyntax2/src/grammar/expressions/mod.rs | 67 ++++++++++++++++++--- 2 files changed, 78 insertions(+), 62 deletions(-) (limited to 'crates/libsyntax2/src/grammar/expressions') diff --git a/crates/libsyntax2/src/grammar/expressions/atom.rs b/crates/libsyntax2/src/grammar/expressions/atom.rs index 9d98340af..417366026 100644 --- a/crates/libsyntax2/src/grammar/expressions/atom.rs +++ b/crates/libsyntax2/src/grammar/expressions/atom.rs @@ -148,7 +148,11 @@ fn lambda_expr(p: &mut Parser) -> CompletedMarker { p.eat(MOVE_KW); params::param_list_opt_types(p); if opt_fn_ret_type(p) { - block(p); + if p.at(L_CURLY) { + block(p); + } else { + p.error("expected a block"); + } } else { expr(p); } @@ -254,6 +258,17 @@ fn match_expr(p: &mut Parser) -> CompletedMarker { let m = p.start(); p.bump(); expr_no_struct(p); + if p.at(L_CURLY) { + match_arm_list(p); + } else { + p.error("expected `{`") + } + m.complete(p, MATCH_EXPR) +} + +fn match_arm_list(p: &mut Parser) { + assert!(p.at(L_CURLY)); + let m = p.start(); p.eat(L_CURLY); while !p.at(EOF) && !p.at(R_CURLY) { // test match_arms_commas @@ -271,7 +286,7 @@ fn match_expr(p: &mut Parser) -> CompletedMarker { } } p.expect(R_CURLY); - m.complete(p, MATCH_EXPR) + m.complete(p, MATCH_ARM_LIST); } // test match_arm @@ -307,62 +322,10 @@ pub(super) fn block_expr(p: &mut Parser) -> CompletedMarker { assert!(p.at(L_CURLY) || p.at(UNSAFE_KW) && p.nth(1) == L_CURLY); let m = p.start(); p.eat(UNSAFE_KW); - p.bump(); - while !p.at(EOF) && !p.at(R_CURLY) { - match p.current() { - LET_KW => let_stmt(p), - _ => { - // test block_items - // fn a() { fn b() {} } - let m = p.start(); - match items::maybe_item(p, items::ItemFlavor::Mod) { - items::MaybeItem::Item(kind) => { - m.complete(p, kind); - } - items::MaybeItem::Modifiers => { - m.abandon(p); - p.error("expected an item"); - } - // test pub_expr - // fn foo() { pub 92; } //FIXME - items::MaybeItem::None => { - let is_blocklike = expressions::expr_stmt(p) == BlockLike::Block; - if p.eat(SEMI) || (is_blocklike && !p.at(R_CURLY)) { - m.complete(p, EXPR_STMT); - } else { - m.abandon(p); - } - } - } - } - } - } - p.expect(R_CURLY); + block(p); m.complete(p, BLOCK_EXPR) } -// test let_stmt; -// fn foo() { -// let a; -// let b: i32; -// let c = 92; -// let d: i32 = 92; -// } -fn let_stmt(p: &mut Parser) { - assert!(p.at(LET_KW)); - let m = p.start(); - p.bump(); - patterns::pattern(p); - if p.at(COLON) { - types::ascription(p); - } - if p.eat(EQ) { - expressions::expr(p); - } - p.expect(SEMI); - m.complete(p, LET_STMT); -} - // test return_expr // fn foo() { // return; diff --git a/crates/libsyntax2/src/grammar/expressions/mod.rs b/crates/libsyntax2/src/grammar/expressions/mod.rs index 9ce0c1f8f..e133c1d9b 100644 --- a/crates/libsyntax2/src/grammar/expressions/mod.rs +++ b/crates/libsyntax2/src/grammar/expressions/mod.rs @@ -26,11 +26,62 @@ fn expr_no_struct(p: &mut Parser) { // fn c() { 1; 2; } // fn d() { 1; 2 } pub(super) fn block(p: &mut Parser) { - if !p.at(L_CURLY) { - p.error("expected block"); - return; + assert!(p.at(L_CURLY)); + let m = p.start(); + p.bump(); + while !p.at(EOF) && !p.at(R_CURLY) { + match p.current() { + LET_KW => let_stmt(p), + _ => { + // test block_items + // fn a() { fn b() {} } + let m = p.start(); + match items::maybe_item(p, items::ItemFlavor::Mod) { + items::MaybeItem::Item(kind) => { + m.complete(p, kind); + } + items::MaybeItem::Modifiers => { + m.abandon(p); + p.error("expected an item"); + } + // test pub_expr + // fn foo() { pub 92; } //FIXME + items::MaybeItem::None => { + let is_blocklike = expressions::expr_stmt(p) == BlockLike::Block; + if p.eat(SEMI) || (is_blocklike && !p.at(R_CURLY)) { + m.complete(p, EXPR_STMT); + } else { + m.abandon(p); + } + } + } + } + } + } + p.expect(R_CURLY); + m.complete(p, BLOCK); + + // test let_stmt; + // fn foo() { + // let a; + // let b: i32; + // let c = 92; + // let d: i32 = 92; + // } + fn let_stmt(p: &mut Parser) { + assert!(p.at(LET_KW)); + let m = p.start(); + p.bump(); + patterns::pattern(p); + if p.at(COLON) { + types::ascription(p); + } + if p.eat(EQ) { + expressions::expr(p); + } + p.expect(SEMI); + m.complete(p, LET_STMT); } - atom::block_expr(p); } #[derive(Clone, Copy)] @@ -339,7 +390,7 @@ fn path_expr(p: &mut Parser, r: Restrictions) -> CompletedMarker { paths::expr_path(p); match p.current() { L_CURLY if !r.forbid_structs => { - struct_lit(p); + named_field_list(p); m.complete(p, STRUCT_LIT) } EXCL => { @@ -356,8 +407,9 @@ fn path_expr(p: &mut Parser, r: Restrictions) -> CompletedMarker { // S { x, y: 32, }; // S { x, y: 32, ..Default::default() }; // } -fn struct_lit(p: &mut Parser) { +fn named_field_list(p: &mut Parser) { assert!(p.at(L_CURLY)); + let m = p.start(); p.bump(); while !p.at(EOF) && !p.at(R_CURLY) { match p.current() { @@ -367,7 +419,7 @@ fn struct_lit(p: &mut Parser) { if p.eat(COLON) { expr(p); } - m.complete(p, STRUCT_LIT_FIELD); + m.complete(p, NAMED_FIELD); } DOTDOT => { p.bump(); @@ -380,4 +432,5 @@ fn struct_lit(p: &mut Parser) { } } p.expect(R_CURLY); + m.complete(p, NAMED_FIELD_LIST); } -- cgit v1.2.3