Skip to content

Commit

Permalink
kwargs
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmindlin committed Jul 22, 2024
1 parent 4e72a53 commit 18383b3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 17 deletions.
15 changes: 14 additions & 1 deletion scout-parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,25 @@ pub enum ExprKind {
SelectAll(String, Option<Identifier>),

// Rest
Call(Identifier, Vec<ExprKind>),
Call(CallLiteral),
Chain(Vec<ExprKind>),
Infix(Box<ExprKind>, TokenKind, Box<ExprKind>),
Prefix(Box<ExprKind>, TokenKind),
}

#[derive(Debug, PartialEq, Clone)]
pub struct CallLiteral {
pub ident: Identifier,
pub args: Vec<ExprKind>,
pub kwargs: Vec<Kwarg>,
}

#[derive(Debug, PartialEq, Clone)]
pub struct Kwarg {
pub ident: Identifier,
pub expr: ExprKind,
}

#[derive(Debug, PartialEq, Clone)]
pub struct CrawlLiteral {
pub bindings: Option<CrawlBindings>,
Expand Down
48 changes: 32 additions & 16 deletions scout-parser/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::collections::HashMap;

use ast::{
CrawlBindings, CrawlLiteral, ExprKind, FnParam, ForLoop, FuncDef, HashLiteral, Identifier,
IfElseLiteral, IfLiteral, Program, StmtKind,
CallLiteral, CrawlBindings, CrawlLiteral, ExprKind, FnParam, ForLoop, FuncDef, HashLiteral,
Identifier, IfElseLiteral, IfLiteral, Kwarg, Program, StmtKind,
};
use scout_lexer::{Lexer, Token, TokenKind};

Expand Down Expand Up @@ -530,11 +530,12 @@ impl Parser {
Ok(ExprKind::Chain(exprs))
}

fn parse_expr_list(&mut self, end: TokenKind) -> ParseResult<Vec<ExprKind>> {
fn parse_call_args(&mut self, end: TokenKind) -> ParseResult<(Vec<ExprKind>, Vec<Kwarg>)> {
let mut args: Vec<ExprKind> = Vec::new();
let mut kwargs: Vec<Kwarg> = Vec::new();

Check warning on line 535 in scout-parser/src/lib.rs

View workflow job for this annotation

GitHub Actions / test

variable does not need to be mutable
if self.peek.kind == end {
self.next_token();
return Ok(args);
return Ok((args, kwargs));
}

self.next_token();
Expand All @@ -549,14 +550,18 @@ impl Parser {
}

self.expect_peek(end)?;
Ok(args)
Ok((args, kwargs))
}

fn parse_call_expr(&mut self, func: ExprKind) -> ParseResult<ExprKind> {
match func {
ExprKind::Ident(ident) => {
let args = self.parse_expr_list(TokenKind::RParen)?;
Ok(ExprKind::Call(ident, args))
let (args, kwargs) = self.parse_call_args(TokenKind::RParen)?;
Ok(ExprKind::Call(CallLiteral {
ident,
args,
kwargs,
}))
}
_ => Err(ParseError::InvalidFnCall),
}
Expand All @@ -565,6 +570,8 @@ impl Parser {

#[cfg(test)]
mod tests {
use self::ast::CallLiteral;

use super::*;
use test_case::test_case;

Expand Down Expand Up @@ -624,8 +631,11 @@ mod tests {
(
Identifier::new("a".into()),
ExprKind::Call(
Identifier::new("fn".into()),
vec![ExprKind::Str("a".into())]
CallLiteral {
ident: Identifier::new("fn".into()),
args: vec![ExprKind::Str("a".into())],
kwargs: vec![]
}
)
)
]
Expand All @@ -642,8 +652,11 @@ mod tests {
ExprKind::Chain(vec![
ExprKind::Select("b".into(), None),
ExprKind::Call(
Identifier::new("fn".into()),
vec![ExprKind::Str("a".into())]
CallLiteral {
ident: Identifier::new("fn".into()),
args: vec![ExprKind::Str("a".into())],
kwargs: vec![]
}
)
])
)
Expand Down Expand Up @@ -707,11 +720,14 @@ mod tests {
r#"f(a, b)"#,
StmtKind::Expr(
ExprKind::Call(
Identifier::new("f".into()),
vec![
ExprKind::Ident(Identifier::new("a".into())),
ExprKind::Ident(Identifier::new("b".into()))
]
CallLiteral {
ident: Identifier::new("f".into()),
args: vec![
ExprKind::Ident(Identifier::new("a".into())),
ExprKind::Ident(Identifier::new("b".into()))
],
kwargs: vec![]
}
)
); "fn call with multi params"
)]
Expand Down

0 comments on commit 18383b3

Please sign in to comment.