From c244dd10343ff96a73879c593bd9253c4073cb90 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 31 Jul 2018 12:26:19 +0300 Subject: Call expr --- src/parser/grammar/attributes.rs | 4 +-- src/parser/grammar/expressions.rs | 61 ++++++++++++++++++++++++++++++--------- src/parser/grammar/mod.rs | 2 +- 3 files changed, 50 insertions(+), 17 deletions(-) (limited to 'src/parser/grammar') diff --git a/src/parser/grammar/attributes.rs b/src/parser/grammar/attributes.rs index 7f1c0401a..c411d4d7f 100644 --- a/src/parser/grammar/attributes.rs +++ b/src/parser/grammar/attributes.rs @@ -36,7 +36,7 @@ fn meta_item(p: &mut Parser) { match p.current() { EQ => { p.bump(); - if !expressions::literal(p) { + if expressions::literal(p).is_none() { p.error("expected literal"); } } @@ -56,7 +56,7 @@ fn meta_item_arg_list(p: &mut Parser) { match p.current() { EOF | R_PAREN => break, IDENT => meta_item(p), - c => if !expressions::literal(p) { + c => if expressions::literal(p).is_none() { let message = "expected attribute"; if items::ITEM_FIRST.contains(c) { diff --git a/src/parser/grammar/expressions.rs b/src/parser/grammar/expressions.rs index ece698248..92aaafa3d 100644 --- a/src/parser/grammar/expressions.rs +++ b/src/parser/grammar/expressions.rs @@ -13,39 +13,72 @@ use super::*; // let _ = b"e"; // let _ = br"f"; // } -pub(super) fn literal(p: &mut Parser) -> bool { +pub(super) fn literal(p: &mut Parser) -> Option { match p.current() { TRUE_KW | FALSE_KW | INT_NUMBER | FLOAT_NUMBER | BYTE | CHAR | STRING | RAW_STRING | BYTE_STRING | RAW_BYTE_STRING => { - let lit = p.start(); + let m = p.start(); p.bump(); - lit.complete(p, LITERAL); - true + Some(m.complete(p, LITERAL)) } - _ => false, + _ => None, } } pub(super) fn expr(p: &mut Parser) { - if literal(p) { - return; + let mut lhs = atom_expr(p); + + while let Some(m) = lhs { + match p.current() { + L_PAREN => lhs = Some(call_expr(p, m)), + _ => break, + } + } +} + +fn atom_expr(p: &mut Parser) -> Option { + match literal(p) { + Some(m) => return Some(m), + None => (), } if paths::is_path_start(p) { - return path_expr(p); + return Some(path_expr(p)); } match p.current() { - L_PAREN => tuple_expr(p), - _ => p.error("expected expression"), + L_PAREN => Some(tuple_expr(p)), + _ => { + p.error("expected expression"); + None + } } } -fn tuple_expr(p: &mut Parser) { +fn tuple_expr(p: &mut Parser) -> CompletedMarker { assert!(p.at(L_PAREN)); let m = p.start(); p.expect(L_PAREN); p.expect(R_PAREN); - m.complete(p, TUPLE_EXPR); + m.complete(p, TUPLE_EXPR) +} + +// test call_expr +// fn foo() { +// let _ = f(); +// let _ = f()(1)(1, 2,); +// } +fn call_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { + assert!(p.at(L_PAREN)); + let m = lhs.precede(p); + p.bump(); + while !p.at(R_PAREN) && !p.at(EOF) { + expr(p); + if !p.at(R_PAREN) && !p.expect(COMMA) { + break; + } + } + p.eat(R_PAREN); + m.complete(p, CALL_EXPR) } // test path_expr @@ -54,9 +87,9 @@ fn tuple_expr(p: &mut Parser) { // let _ = a::b; // let _ = ::a::; // } -fn path_expr(p: &mut Parser) { +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); + m.complete(p, PATH_EXPR) } diff --git a/src/parser/grammar/mod.rs b/src/parser/grammar/mod.rs index e4823eadb..63ad2782e 100644 --- a/src/parser/grammar/mod.rs +++ b/src/parser/grammar/mod.rs @@ -31,7 +31,7 @@ mod type_args; mod types; use { - parser::{parser::Parser, token_set::TokenSet}, + parser::{parser::{Parser, CompletedMarker}, token_set::TokenSet}, SyntaxKind::{self, *}, }; -- cgit v1.2.3