Skip to content

Commit

Permalink
feat: parse function call
Browse files Browse the repository at this point in the history
  • Loading branch information
viddrobnic committed May 30, 2024
1 parent 590f518 commit ca1d132
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
23 changes: 22 additions & 1 deletion parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ impl Parser<'_> {
| TokenKind::Or => self.parse_infix_operation(start_token, left)?,
TokenKind::LSquare => self.parse_index(left)?,
TokenKind::Dot => self.parse_dot_index(left)?,
TokenKind::LBracket => todo!("parse function call"),
TokenKind::LBracket => self.parse_fn_call(left)?,
TokenKind::Assign => self.parse_assign(left)?,

_ => return Ok(left),
Expand Down Expand Up @@ -580,6 +580,27 @@ impl Parser<'_> {
))
}

fn parse_fn_call(&mut self, left: ast::Node) -> Result<(ast::NodeValue, Position)> {
// Read arguments
let (args, end) =
self.parse_multiple(TokenKind::RBracket, TokenKind::Comma, |parser, token| {
parser.parse_node(token, Precedence::Lowest)
})?;

// Check all nodes are expression
for arg in &args {
validate_node_kind(arg, NodeKind::Expression)?;
}

Ok((
ast::NodeValue::FunctionCall {
function: Box::new(left),
arguments: args,
},
end,
))
}

// Helper function that reads block { ... }.
// It returns vector of nodes and end position, which is the end
// position of `}`
Expand Down
72 changes: 72 additions & 0 deletions parser/src/parser/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,74 @@ fn fn_literal_named() -> Result<()> {
Ok(())
}

#[test]
fn fn_call() -> Result<()> {
let tests = [
(
"foo()",
ast::Node {
value: ast::NodeValue::FunctionCall {
function: Box::new(ast::Node {
value: ast::NodeValue::Identifier("foo".to_string()),
range: Range {
start: Position::new(0, 0),
end: Position::new(0, 3),
},
}),
arguments: vec![],
},
range: Range {
start: Position::new(0, 0),
end: Position::new(0, 5),
},
},
),
(
"foo(\n1,\n2,\n)",
ast::Node {
value: ast::NodeValue::FunctionCall {
function: Box::new(ast::Node {
value: ast::NodeValue::Identifier("foo".to_string()),
range: Range {
start: Position::new(0, 0),
end: Position::new(0, 3),
},
}),
arguments: vec![
ast::Node {
value: ast::NodeValue::IntegerLiteral(1),
range: Range {
start: Position::new(1, 0),
end: Position::new(1, 1),
},
},
ast::Node {
value: ast::NodeValue::IntegerLiteral(2),
range: Range {
start: Position::new(2, 0),
end: Position::new(2, 1),
},
},
],
},
range: Range {
start: Position::new(0, 0),
end: Position::new(3, 1),
},
},
),
];

for (input, expected) in tests {
let program = parse(input)?;

assert_eq!(program.statements.len(), 1);
assert_eq!(program.statements[0], expected);
}

Ok(())
}

#[test]
fn return_statement() -> Result<()> {
let program = parse("return 1 + 2")?;
Expand Down Expand Up @@ -1273,6 +1341,10 @@ fn precedence() -> Result<()> {
("//", ""),
("return 1 + 1 * 2", "return (1 + (1 * 2))"),
("return fn(){}", "return fn() {}"),
("fn(){}()", "(fn() {}())"),
("foo()[0]", "((foo())[0])"),
("foo[0]()", "((foo[0])())"),
("foo[0].bar(1, 1 + 2)", "(((foo[0])[\"bar\"])(1, (1 + 2)))"),
];

for (input, expected) in tests {
Expand Down

0 comments on commit ca1d132

Please sign in to comment.