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
use serde::Deserialize;
use serde::Serialize;

use crate::lexer::token::Span;
use crate::parser::ast::identifiers::SimpleIdentifier;
use crate::parser::ast::Expression;
use crate::parser::ast::Statement;

#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub struct DeclareEntry {
    pub key: SimpleIdentifier,
    pub span: Span,
    pub value: Expression,
}

#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub struct DeclareEntryGroup {
    pub start: Span,
    pub end: Span,
    pub entries: Vec<DeclareEntry>,
}

#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum DeclareBody {
    // declaration is terminated with `;`
    Noop {
        span: Span,
    },
    // declaration is followed by a `{` and terminated with `}` after multiple statements.
    Braced {
        start: Span,
        statements: Vec<Statement>,
        end: Span,
    },
    // declaration is terminated with `;` after a single expression.
    Expression {
        expression: Expression,
        end: Span,
    },
    // declaration is followed by a `:` and terminated with `enddeclare` and `;` after multiple statements.
    Block {
        start: Span,
        statements: Vec<Statement>,
        end: (Span, Span),
    },
}

#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub struct Declare {
    pub span: Span,
    pub entries: DeclareEntryGroup,
    pub body: DeclareBody,
}