1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use super::{parse_token, DocComment, Expr, Ident, Lookahead, Parse, ParseResult, Peek};
use crate::lexer::{Lexer, Token};
use serde::Serialize;

/// Represents a let statement in the AST.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct LetStatement<'a> {
    /// The doc comments for the statement.
    pub docs: Vec<DocComment<'a>>,
    /// The newly bound identifier.
    pub id: Ident<'a>,
    /// The expression being bound.
    pub expr: Expr<'a>,
}

impl<'a> Parse<'a> for LetStatement<'a> {
    fn parse(lexer: &mut Lexer<'a>) -> ParseResult<Self> {
        let docs = Parse::parse(lexer)?;
        parse_token(lexer, Token::LetKeyword)?;
        let id = Parse::parse(lexer)?;
        parse_token(lexer, Token::Equals)?;
        let expr = Parse::parse(lexer)?;
        parse_token(lexer, Token::Semicolon)?;
        Ok(Self { docs, id, expr })
    }
}

impl Peek for LetStatement<'_> {
    fn peek(lookahead: &mut Lookahead) -> bool {
        lookahead.peek(Token::LetKeyword)
    }
}

#[cfg(test)]
mod test {
    use crate::ast::test::roundtrip;

    #[test]
    fn let_statement_roundtrip() {
        roundtrip(
            "package foo:bar; let x= y;",
            "package foo:bar;\n\nlet x = y;\n",
        )
        .unwrap();
        roundtrip(
            "package foo:bar; let x =y.x.z;",
            "package foo:bar;\n\nlet x = y.x.z;\n",
        )
        .unwrap();
        roundtrip(
            "package foo:bar; let x=y[\"x\"][\"z\"];",
            "package foo:bar;\n\nlet x = y[\"x\"][\"z\"];\n",
        )
        .unwrap();
        roundtrip(
            "package foo:bar; let x = foo[\"bar\"].baz[\"qux\"];",
            "package foo:bar;\n\nlet x = foo[\"bar\"].baz[\"qux\"];\n",
        )
        .unwrap();

        roundtrip(
            "package foo:bar; let x = (y);",
            "package foo:bar;\n\nlet x = (y);\n",
        )
        .unwrap();

        roundtrip(
            "package foo:bar; let x = new foo:bar {};",
            "package foo:bar;\n\nlet x = new foo:bar {};\n",
        )
        .unwrap();

        roundtrip(
            "package foo:bar; let x = new foo:bar { foo, \"bar\": (new baz:qux {...}), \"baz\": foo[\"baz\"].qux };",
            "package foo:bar;\n\nlet x = new foo:bar {\n    foo,\n    \"bar\": (new baz:qux { ... }),\n    \"baz\": foo[\"baz\"].qux,\n};\n",
        )
        .unwrap();

        roundtrip(
            "package foo:bar; let x = new foo:bar { foo, ...i, bar: baz, ...i2, ...,};",
            "package foo:bar;\n\nlet x = new foo:bar {\n    foo,\n    ...i,\n    bar: baz,\n    ...i2,\n    ...\n};\n",
        )
        .unwrap();
    }
}