Skip to main content

Overview

Statements are standalone code structures that perform actions in Lua. The Stmt enum encompasses all statement types.

Stmt Enum

pub enum Stmt {
    /// An assignment, such as `x = 1`
    Assignment(Assignment),
    
    /// A do block, `do end`
    Do(Do),
    
    /// A function call on its own, such as `call()`
    FunctionCall(FunctionCall),
    
    /// A function declaration, such as `function x() end`
    FunctionDeclaration(FunctionDeclaration),
    
    /// A generic for loop, such as `for index, value in pairs(list) do end`
    GenericFor(GenericFor),
    
    /// An if statement
    If(If),
    
    /// A local assignment, such as `local x = 1`
    LocalAssignment(LocalAssignment),
    
    /// A local function declaration, such as `local function x() end`
    LocalFunction(LocalFunction),
    
    /// A numeric for loop, such as `for index = 1, 10 do end`
    NumericFor(NumericFor),
    
    /// A repeat loop
    Repeat(Repeat),
    
    /// A while loop
    While(While),
    
    /// A compound assignment, such as `+=` (Luau/CFX Lua only)
    #[cfg(any(feature = "luau", feature = "cfxlua"))]
    CompoundAssignment(CompoundAssignment),
    
    /// An exported type declaration, such as `export type Meters = number` (Luau only)
    #[cfg(feature = "luau")]
    ExportedTypeDeclaration(ExportedTypeDeclaration),
    
    /// A type declaration, such as `type Meters = number` (Luau only)
    #[cfg(feature = "luau")]
    TypeDeclaration(TypeDeclaration),
    
    /// An exported type function (Luau only)
    #[cfg(feature = "luau")]
    ExportedTypeFunction(ExportedTypeFunction),
    
    /// A type function (Luau only)
    #[cfg(feature = "luau")]
    TypeFunction(TypeFunction),
    
    /// A goto statement, such as `goto label` (Lua 5.2/LuaJIT only)
    #[cfg(any(feature = "lua52", feature = "luajit"))]
    Goto(Goto),
    
    /// A label, such as `::label::` (Lua 5.2/LuaJIT only)
    #[cfg(any(feature = "lua52", feature = "luajit"))]
    Label(Label),
}

Assignment

An assignment, such as x = y. Not used for local assignments.
pub struct Assignment {
    var_list: Punctuated<Var>,
    equal_token: TokenReference,
    expr_list: Punctuated<Expression>,
}

Methods

pub fn new(var_list: Punctuated<Var>, expr_list: Punctuated<Expression>) -> Self
Returns a new Assignment from the given variable and expression list.
pub fn expressions(&self) -> &Punctuated<Expression>
Returns the punctuated sequence over the expressions being assigned. This is the 1, 2 part of x, y["a"] = 1, 2.
pub fn equal_token(&self) -> &TokenReference
The = token in between x = y.
pub fn variables(&self) -> &Punctuated<Var>
Returns the punctuated sequence over the variables being assigned to. This is the x, y["a"] part of x, y["a"] = 1, 2.
pub fn with_variables(self, var_list: Punctuated<Var>) -> Self
pub fn with_equal_token(self, equal_token: TokenReference) -> Self
pub fn with_expressions(self, expr_list: Punctuated<Expression>) -> Self

LocalAssignment

An assignment to a local variable, such as local x = 1.
pub struct LocalAssignment {
    local_token: TokenReference,
    #[cfg(feature = "luau")]
    type_specifiers: Vec<Option<TypeSpecifier>>,
    name_list: Punctuated<TokenReference>,
    #[cfg(feature = "lua54")]
    attributes: Vec<Option<Attribute>>,
    equal_token: Option<TokenReference>,
    expr_list: Punctuated<Expression>,
}

Methods

pub fn new(name_list: Punctuated<TokenReference>) -> Self
pub fn local_token(&self) -> &TokenReference
pub fn equal_token(&self) -> Option<&TokenReference>
pub fn expressions(&self) -> &Punctuated<Expression>
pub fn names(&self) -> &Punctuated<TokenReference>
#[cfg(feature = "luau")]
pub fn type_specifiers(&self) -> impl Iterator<Item = Option<&TypeSpecifier>>
The type specifiers of the variables, in the order that they were assigned. local foo: number, bar, baz: boolean returns an iterator containing: Some(TypeSpecifier(number)), None, Some(TypeSpecifier(boolean)).
#[cfg(any(feature = "lua54", feature = "cfxlua"))]
pub fn attributes(&self) -> impl Iterator<Item = Option<&Attribute>>

FunctionDeclaration

A normal function declaration, supports simple declarations like function x() end as well as complicated declarations such as function x.y.z:a() end.
pub struct FunctionDeclaration {
    #[cfg(feature = "luau")]
    attributes: Vec<LuauAttribute>,
    function_token: TokenReference,
    name: FunctionName,
    body: FunctionBody,
}

Methods

pub fn new(name: FunctionName) -> Self
#[cfg(feature = "luau")]
pub fn attributes(&self) -> impl Iterator<Item = &LuauAttribute>
pub fn function_token(&self) -> &TokenReference
pub fn body(&self) -> &FunctionBody
pub fn name(&self) -> &FunctionName

LocalFunction

A declaration of a local function, such as local function x() end.
pub struct LocalFunction {
    #[cfg(feature = "luau")]
    attributes: Vec<LuauAttribute>,
    local_token: TokenReference,
    function_token: TokenReference,
    name: TokenReference,
    body: FunctionBody,
}

Methods

pub fn new(name: TokenReference) -> Self
#[cfg(feature = "luau")]
pub fn attributes(&self) -> impl Iterator<Item = &LuauAttribute>
pub fn local_token(&self) -> &TokenReference
pub fn function_token(&self) -> &TokenReference
pub fn body(&self) -> &FunctionBody
pub fn name(&self) -> &TokenReference

Control Flow Statements

If

An if statement.
pub struct If {
    if_token: TokenReference,
    condition: Expression,
    then_token: TokenReference,
    block: Block,
    else_if: Option<Vec<ElseIf>>,
    else_token: Option<TokenReference>,
    r#else: Option<Block>,
    end_token: TokenReference,
}

Methods

pub fn new(condition: Expression) -> Self
pub fn if_token(&self) -> &TokenReference
pub fn condition(&self) -> &Expression
pub fn then_token(&self) -> &TokenReference
pub fn block(&self) -> &Block
pub fn else_token(&self) -> Option<&TokenReference>
pub fn else_if(&self) -> Option<&Vec<ElseIf>>
pub fn else_block(&self) -> Option<&Block>
pub fn end_token(&self) -> &TokenReference

While

A while loop.
pub struct While {
    while_token: TokenReference,
    condition: Expression,
    do_token: TokenReference,
    block: Block,
    end_token: TokenReference,
}

Methods

pub fn new(condition: Expression) -> Self
pub fn while_token(&self) -> &TokenReference
pub fn condition(&self) -> &Expression
pub fn do_token(&self) -> &TokenReference
pub fn block(&self) -> &Block
pub fn end_token(&self) -> &TokenReference

Repeat

A repeat loop.
pub struct Repeat {
    repeat_token: TokenReference,
    block: Block,
    until_token: TokenReference,
    until: Expression,
}

Methods

pub fn new(until: Expression) -> Self
pub fn repeat_token(&self) -> &TokenReference
pub fn block(&self) -> &Block
pub fn until_token(&self) -> &TokenReference
pub fn until(&self) -> &Expression

NumericFor

A numeric for loop, such as for index = 1, 10 do end.
pub struct NumericFor {
    for_token: TokenReference,
    index_variable: TokenReference,
    equal_token: TokenReference,
    start: Expression,
    start_end_comma: TokenReference,
    end: Expression,
    end_step_comma: Option<TokenReference>,
    step: Option<Expression>,
    do_token: TokenReference,
    block: Block,
    end_token: TokenReference,
    #[cfg(feature = "luau")]
    type_specifier: Option<TypeSpecifier>,
}

GenericFor

A generic for loop, such as for index, value in pairs(list) do end.
pub struct GenericFor {
    for_token: TokenReference,
    names: Punctuated<TokenReference>,
    in_token: TokenReference,
    expr_list: Punctuated<Expression>,
    do_token: TokenReference,
    block: Block,
    end_token: TokenReference,
    #[cfg(feature = "luau")]
    type_specifiers: Vec<Option<TypeSpecifier>>,
}

Do

A do block, such as do ... end. This is not used for things like while true do end, only those on their own.
pub struct Do {
    do_token: TokenReference,
    block: Block,
    end_token: TokenReference,
}

Methods

pub fn new() -> Self
pub fn do_token(&self) -> &TokenReference
pub fn block(&self) -> &Block
pub fn end_token(&self) -> &TokenReference

Build docs developers (and LLMs) love