lua_parser/statement/
mod.rs

1use crate::Expression;
2use crate::Span;
3
4/// block of statements.
5/// return statement must be optionally placed at the end of the block.
6#[derive(Clone, Debug)]
7pub struct Block {
8    pub statements: Vec<Statement>,
9    pub return_statement: Option<ReturnStatement>,
10    pub span: Span,
11}
12impl Block {
13    pub fn new(
14        statements: Vec<Statement>,
15        return_statement: Option<ReturnStatement>,
16        span: Span,
17    ) -> Self {
18        Self {
19            statements,
20            return_statement,
21            span,
22        }
23    }
24    /// get the span of the block.
25    /// *NOTE* if the block is empty, the span will hold `usize::MAX` (by `Span::new_none()`)
26    pub fn span(&self) -> Span {
27        self.span
28    }
29}
30
31/// return statement
32#[derive(Clone, Debug)]
33pub struct ReturnStatement {
34    pub values: Vec<Expression>,
35    pub span: Span,
36}
37impl ReturnStatement {
38    pub fn new(values: Vec<Expression>, span: Span) -> Self {
39        Self { values, span }
40    }
41    /// get the span of the return statement
42    pub fn span(&self) -> Span {
43        self.span
44    }
45}
46
47/// lua statement
48#[non_exhaustive]
49#[derive(Clone, Debug)]
50pub enum Statement {
51    /// `;`
52    None(StmtNone),
53    /// `l0, l1, l2 = r0, r1, r2`.
54    /// variadic `...` can be used in both `l` and `r`
55    Assignment(StmtAssignment),
56    Label(StmtLabel),
57    Break(StmtBreak),
58    Goto(StmtGoto),
59    Do(StmtDo),
60    While(StmtWhile),
61    Repeat(StmtRepeat),
62    If(StmtIf),
63    For(StmtFor),
64    ForGeneric(StmtForGeneric),
65    LocalDeclaration(StmtLocalDeclaration),
66    FunctionDefinition(StmtFunctionDefinition),
67    FunctionDefinitionLocal(StmtFunctionDefinitionLocal),
68    FunctionCall(StmtFunctionCall),
69}
70impl Statement {
71    /// get the span of the statement
72    pub fn span(&self) -> Span {
73        match self {
74            Self::None(v) => v.span(),
75            Self::Assignment(v) => v.span(),
76            Self::Label(v) => v.span(),
77            Self::Break(v) => v.span(),
78            Self::Goto(v) => v.span(),
79            Self::Do(v) => v.span(),
80            Self::While(v) => v.span(),
81            Self::Repeat(v) => v.span(),
82            Self::If(v) => v.span(),
83            Self::For(v) => v.span(),
84            Self::ForGeneric(v) => v.span(),
85            Self::LocalDeclaration(v) => v.span(),
86            Self::FunctionDefinition(v) => v.span(),
87            Self::FunctionDefinitionLocal(v) => v.span(),
88            Self::FunctionCall(v) => v.span(),
89        }
90    }
91}
92
93#[derive(Clone, Debug)]
94pub struct StmtNone {
95    pub span: Span,
96}
97impl StmtNone {
98    pub fn new(span: Span) -> Self {
99        Self { span }
100    }
101    /// get the span of the `;`
102    pub fn span(&self) -> Span {
103        self.span
104    }
105}
106
107mod assignment;
108pub use assignment::StmtAssignment;
109
110mod break_;
111pub use break_::StmtBreak;
112
113mod label;
114pub use label::StmtLabel;
115
116mod goto_;
117pub use goto_::StmtGoto;
118
119mod do_;
120pub use do_::StmtDo;
121
122mod while_;
123pub use while_::StmtWhile;
124
125mod repeat;
126pub use repeat::StmtRepeat;
127
128mod if_;
129pub use if_::StmtElseIf;
130pub use if_::StmtIf;
131
132mod for_;
133pub use for_::StmtFor;
134pub use for_::StmtForGeneric;
135
136mod localdecl;
137pub use localdecl::AttName;
138pub use localdecl::Attrib;
139pub use localdecl::StmtLocalDeclaration;
140
141mod functiondef;
142pub use functiondef::FunctionName;
143pub use functiondef::StmtFunctionDefinition;
144pub use functiondef::StmtFunctionDefinitionLocal;
145
146mod functioncall;
147pub use functioncall::StmtFunctionCall;