Skip to main content

Overview

A Block represents a block of statements in the Lua AST, such as those found in if/do/while blocks and function bodies.

Struct Definition

pub struct Block {
    stmts: Vec<(Stmt, Option<TokenReference>)>,
    last_stmt: Option<(LastStmt, Option<TokenReference>)>,
}

Fields

  • stmts: A vector of statements with optional semicolon tokens
  • last_stmt: An optional last statement (return or break) with optional semicolon

Methods

Constructor

pub fn new() -> Self
Creates an empty block.

Accessors

pub fn stmts(&self) -> impl Iterator<Item = &Stmt>
An iterator over the statements in the block, such as local foo = 1. Note that this does not contain the final statement which can be attained via Block::last_stmt.
pub fn stmts_with_semicolon(&self) -> impl Iterator<Item = &(Stmt, Option<TokenReference>)>
An iterator over the statements in the block, including any optional semicolon token reference present.
pub fn last_stmt(&self) -> Option<&LastStmt>
The last statement of the block if one exists, such as return foo.
pub fn last_stmt_with_semicolon(&self) -> Option<&(LastStmt, Option<TokenReference>)>
The last statement of the block if one exists, including any optional semicolon token reference present.

Builders

pub fn with_stmts(self, stmts: Vec<(Stmt, Option<TokenReference>)>) -> Self
Returns a new block with the given statements. Takes a vector of statements, followed by an optional semicolon token reference.
pub fn with_last_stmt(self, last_stmt: Option<(LastStmt, Option<TokenReference>)>) -> Self
Returns a new block with the given last statement, if one is given. Takes an optional last statement, with an optional semicolon.

LastStmt Enum

The last statement of a Block.
pub enum LastStmt {
    /// A `break` statement
    Break(TokenReference),
    
    /// A continue statement (Luau only)
    #[cfg(feature = "luau")]
    Continue(TokenReference),
    
    /// A `return` statement
    Return(Return),
}

Return Struct

A return statement.
pub struct Return {
    token: TokenReference,
    returns: Punctuated<Expression>,
}

Methods

pub fn new() -> Self
Creates a new empty Return. Default return token is followed by a single space.
pub fn token(&self) -> &TokenReference
The return token.
pub fn returns(&self) -> &Punctuated<Expression>
The values being returned.
pub fn with_token(self, token: TokenReference) -> Self
Returns a new Return with the given return token.
pub fn with_returns(self, returns: Punctuated<Expression>) -> Self
Returns a new Return with the given punctuated sequence.

Example

use full_moon::ast::Block;

let block = Block::new();
assert_eq!(block.stmts().count(), 0);
assert!(block.last_stmt().is_none());

Build docs developers (and LLMs) love