From c6f4a06b4297f498da4bc2cd747aa38effb855b0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 18 Feb 2018 01:06:48 +0300 Subject: G: value_parameters, patterns & let statement --- src/parser/grammar/expressions.rs | 17 +++++++++++-- src/parser/grammar/items/mod.rs | 28 +++++++++++++++++++-- src/parser/grammar/mod.rs | 20 +++++++++++++++ src/parser/grammar/patterns.rs | 52 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 4 deletions(-) create mode 100644 src/parser/grammar/patterns.rs (limited to 'src/parser/grammar') diff --git a/src/parser/grammar/expressions.rs b/src/parser/grammar/expressions.rs index 3704cb16f..2145b8d8b 100644 --- a/src/parser/grammar/expressions.rs +++ b/src/parser/grammar/expressions.rs @@ -14,7 +14,20 @@ pub(super) fn literal(p: &mut Parser) -> bool { } pub(super) fn expr(p: &mut Parser) { - if !literal(p) { - p.error("expected expression"); + if literal(p) { + return; } + + match p.current() { + L_PAREN => tuple_expr(p), + _ => p.error("expected expression"), + } +} + +fn tuple_expr(p: &mut Parser) { + assert!(p.at(L_PAREN)); + let m = p.start(); + p.expect(L_PAREN); + p.expect(R_PAREN); + m.complete(p, TUPLE_EXPR); } diff --git a/src/parser/grammar/items/mod.rs b/src/parser/grammar/items/mod.rs index 3af6d13a1..1fe646652 100644 --- a/src/parser/grammar/items/mod.rs +++ b/src/parser/grammar/items/mod.rs @@ -218,9 +218,33 @@ fn fn_item(p: &mut Parser) { p.error("expected function arguments"); } - if p.at(L_CURLY) { - p.expect(L_CURLY); + block(p); + + fn block(p: &mut Parser) { + if !p.at(L_CURLY) { + p.error("expected block"); + } + let m = p.start(); + p.bump(); + while !p.at(EOF) && !p.at(R_CURLY) { + match p.current() { + LET_KW => let_stmt(p), + _ => p.err_and_bump("expected statement"), + } + } p.expect(R_CURLY); + m.complete(p, BLOCK); + } + + fn let_stmt(p: &mut Parser) { + assert!(p.at(LET_KW)); + let m = p.start(); + p.bump(); + patterns::pattern(p); + p.expect(EQ); + expressions::expr(p); + p.expect(SEMI); + m.complete(p, LET_STMT); } } diff --git a/src/parser/grammar/mod.rs b/src/parser/grammar/mod.rs index ee0263203..54a63a547 100644 --- a/src/parser/grammar/mod.rs +++ b/src/parser/grammar/mod.rs @@ -30,6 +30,7 @@ mod items; mod attributes; mod expressions; mod types; +mod patterns; mod paths; mod type_params; @@ -85,10 +86,29 @@ fn abi(p: &mut Parser) { abi.complete(p, ABI); } +// test fn_value_parameters +// fn a() {} +// fn b(x: i32) {} +// fn c(x: i32, ) {} +// fn d(x: i32, y: ()) {} fn fn_value_parameters(p: &mut Parser) { assert!(p.at(L_PAREN)); p.bump(); + while !p.at(EOF) && !p.at(R_PAREN) { + value_parameter(p); + if !p.at(R_PAREN) { + p.expect(COMMA); + } + } p.expect(R_PAREN); + + fn value_parameter(p: &mut Parser) { + let m = p.start(); + patterns::pattern(p); + p.expect(COLON); + types::type_(p); + m.complete(p, VALUE_PARAMETER); + } } fn fn_ret_type(p: &mut Parser) { diff --git a/src/parser/grammar/patterns.rs b/src/parser/grammar/patterns.rs new file mode 100644 index 000000000..6e4f2236b --- /dev/null +++ b/src/parser/grammar/patterns.rs @@ -0,0 +1,52 @@ +use super::*; + +pub(super) fn pattern(p: &mut Parser) { + match p.current() { + UNDERSCORE => placeholder_pat(p), + AMPERSAND => ref_pat(p), + IDENT | REF_KW => bind_pat(p), + _ => p.err_and_bump("expected pattern"), + } +} + +// test placeholder_pat +// fn main() { let _ = (); } +fn placeholder_pat(p: &mut Parser) { + assert!(p.at(UNDERSCORE)); + let m = p.start(); + p.bump(); + m.complete(p, PLACEHOLDER_PAT); +} + +// test ref_pat +// fn main() { +// let &a = (); +// let &mut b = (); +// } +fn ref_pat(p: &mut Parser) { + assert!(p.at(AMPERSAND)); + let m = p.start(); + p.bump(); + p.eat(MUT_KW); + pattern(p); + m.complete(p, REF_PAT); +} + +// test bind_pat +// fn main() { +// let a = (); +// let ref b = (); +// let ref mut c = (); +// let d @ _ = (); +// } +fn bind_pat(p: &mut Parser) { + let m = p.start(); + if p.eat(REF_KW) { + p.eat(MUT_KW); + } + name(p); + if p.eat(AT) { + pattern(p); + } + m.complete(p, BIND_PAT); +} -- cgit v1.2.3