From 4a3908a35d8186bf81af778a28a9580c7fbd24f6 Mon Sep 17 00:00:00 2001 From: Jason Williams <936006+jasonwilliams@users.noreply.github.com> Date: Wed, 6 Nov 2019 14:10:27 +0000 Subject: [PATCH] parse function arguments (#203) * parse function arguments * no need for type setting here * adding test for arguments --- src/lib/syntax/parser.rs | 84 ++++++++++++++++++++++++++-------------- 1 file changed, 56 insertions(+), 28 deletions(-) diff --git a/src/lib/syntax/parser.rs b/src/lib/syntax/parser.rs index d7782827ff..756da329fd 100644 --- a/src/lib/syntax/parser.rs +++ b/src/lib/syntax/parser.rs @@ -67,6 +67,32 @@ impl Parser { } } + fn parse_function_parameters(&mut self) -> Result, ParseError> { + self.expect_punc(Punctuator::OpenParen, "function parameters ( expected")?; + let mut args = Vec::new(); + let mut tk = self.get_token(self.pos)?; + while tk.data != TokenData::Punctuator(Punctuator::CloseParen) { + match tk.data { + TokenData::Identifier(ref id) => args.push(id.clone()), + _ => { + return Err(ParseError::Expected( + vec![TokenData::Identifier("identifier".to_string())], + tk.clone(), + "function arguments", + )) + } + } + self.pos += 1; + if self.get_token(self.pos)?.data == TokenData::Punctuator(Punctuator::Comma) { + self.pos += 1; + } + tk = self.get_token(self.pos)?; + } + + self.expect_punc(Punctuator::CloseParen, "function parameters ) expected")?; + Ok(args) + } + fn parse_struct(&mut self, keyword: Keyword) -> ParseResult { match keyword { Keyword::Throw => { @@ -306,27 +332,7 @@ impl Parser { } }; // Now we have the function identifier we should have an open paren for arguments ( ) - self.expect_punc(Punctuator::OpenParen, "function")?; - let mut args: Vec = Vec::new(); - let mut tk = self.get_token(self.pos)?; - while tk.data != TokenData::Punctuator(Punctuator::CloseParen) { - match tk.data { - TokenData::Identifier(ref id) => args.push(id.clone()), - _ => { - return Err(ParseError::Expected( - vec![TokenData::Identifier("identifier".to_string())], - tk.clone(), - "function arguments", - )) - } - } - self.pos += 1; - if self.get_token(self.pos)?.data == TokenData::Punctuator(Punctuator::Comma) { - self.pos += 1; - } - tk = self.get_token(self.pos)?; - } - self.pos += 1; + let args = self.parse_function_parameters()?; let block = self.parse()?; Ok(mk!( self, @@ -536,17 +542,13 @@ impl Parser { self.parse()? } TokenData::Punctuator(Punctuator::OpenParen) => { - self.pos += 1; - self.expect( - TokenData::Punctuator(Punctuator::CloseParen), - "Method Block", - )?; + let args = self.parse_function_parameters()?; self.pos += 1; // { let expr = self.parse()?; self.pos += 1; mk!( self, - ExprDef::FunctionDecl(None, vec!(), Box::new(expr)), + ExprDef::FunctionDecl(None, args, Box::new(expr)), token ) } @@ -923,7 +925,7 @@ mod tests { ); } #[test] - fn check_object() { + fn check_object_short_function() { // Testing short function syntax let mut object_properties: BTreeMap = BTreeMap::new(); object_properties.insert( @@ -949,6 +951,32 @@ mod tests { ); } + #[test] + fn check_object_short_function_arguments() { + // Testing short function syntax + let mut object_properties: BTreeMap = BTreeMap::new(); + object_properties.insert( + String::from("a"), + Expr::new(ExprDef::Const(Const::Bool(true))), + ); + object_properties.insert( + String::from("b"), + Expr::new(ExprDef::FunctionDecl( + None, + vec![String::from("test")], + Box::new(Expr::new(ExprDef::Block(vec![]))), + )), + ); + + check_parser( + "{ + a: true, + b(test) {} + }; + ", + &[Expr::new(ExprDef::ObjectDecl(Box::new(object_properties)))], + ); + } #[test] fn check_array() { use crate::syntax::ast::constant::Const;