Skip to main content

openpql_pql_parser/ast/
fncall.rs

1use super::{Expr, Ident, Loc, LocInfo, Spanned, String};
2
3/// Function call with its name, arguments, and source span.
4#[derive(Clone, PartialEq, derive_more::From, derive_more::Debug)]
5#[debug("{:?}({})", self.name, _to_str(&self.args))]
6pub struct FnCall<'i> {
7    /// Function name identifier.
8    pub name: Ident<'i>,
9    /// Positional argument expressions.
10    pub args: Vec<Expr<'i>>,
11
12    /// Source span covering the whole call.
13    pub loc: (Loc, Loc),
14}
15
16impl Spanned for FnCall<'_> {
17    fn loc(&self) -> LocInfo {
18        self.loc
19    }
20}
21
22fn _to_str(elems: &[Expr<'_>]) -> String {
23    elems
24        .iter()
25        .map(|e| format!("{e:?}"))
26        .collect::<Vec<_>>()
27        .join(",")
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33    use crate::*;
34
35    #[test]
36    fn test_fn_call() {
37        let src = "equity(hero, river)";
38        let fncall = parse_fn_call(src).unwrap();
39
40        let ident = |id| Ident::from((id, loc(src, id)));
41
42        assert_eq!(fncall.name, ident("equity"));
43        assert_eq!(fncall.args.len(), 2);
44        assert_eq!(fncall.args[0], ident("hero").into());
45        assert_eq!(fncall.args[1], ident("river").into());
46        assert_eq!(fncall.loc, (0, src.len()));
47    }
48
49    #[test]
50    fn test_debug() {
51        let fncall = parse_fn_call("equity(hero, 1.23)").unwrap();
52
53        assert_eq!(format!("{fncall:?}"), "equity(hero,1.23)");
54    }
55
56    #[test]
57    fn test_loc() {
58        let src = "equity(hero, river)";
59        let fncall = parse_fn_call(src).unwrap();
60        assert_eq!(fncall.loc(), (0, src.len()));
61    }
62}