From ce3462ce8fed8bb86d887643479c73908fc326a3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 31 Jul 2018 20:38:36 +0300 Subject: struct literals --- src/parser/grammar/expressions.rs | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'src/parser/grammar/expressions.rs') diff --git a/src/parser/grammar/expressions.rs b/src/parser/grammar/expressions.rs index 3956df568..5b18e2294 100644 --- a/src/parser/grammar/expressions.rs +++ b/src/parser/grammar/expressions.rs @@ -154,5 +154,42 @@ fn path_expr(p: &mut Parser) -> CompletedMarker { assert!(paths::is_path_start(p)); let m = p.start(); paths::expr_path(p); - m.complete(p, PATH_EXPR) + if p.at(L_CURLY) { + struct_lit(p); + m.complete(p, STRUCT_LIT) + } else { + m.complete(p, PATH_EXPR) + } +} + +// test struct_lit +// fn foo() { +// S {}; +// S { x, y: 32, }; +// S { x, y: 32, ..Default::default() }; +// } +fn struct_lit(p: &mut Parser) { + assert!(p.at(L_CURLY)); + p.bump(); + while !p.at(EOF) && !p.at(R_CURLY) { + match p.current() { + IDENT => { + let m = p.start(); + name_ref(p); + if p.eat(COLON) { + expr(p); + } + m.complete(p, STRUCT_LIT_FIELD); + }, + DOTDOT => { + p.bump(); + expr(p); + }, + _ => p.err_and_bump("expected identifier"), + } + if !p.at(R_CURLY) { + p.expect(COMMA); + } + } + p.expect(R_CURLY); } -- cgit v1.2.3