Skip to main content

Overview

Expressions are code constructs that evaluate to values. The Expression enum encompasses all expression types in Lua.

Expression Enum

pub enum Expression {
    /// A binary operation, such as `1 + 3`
    BinaryOperator {
        /// The left hand side of the binary operation, the `1` part of `1 + 3`
        lhs: Box<Expression>,
        /// The binary operation used, the `+` part of `1 + 3`
        binop: BinOp,
        /// The right hand side of the binary operation, the `3` part of `1 + 3`
        rhs: Box<Expression>,
    },
    
    /// A statement in parentheses, such as `(#list)`
    Parentheses {
        /// The parentheses of the expression
        contained: ContainedSpan,
        /// The expression inside the parentheses
        expression: Box<Expression>,
    },
    
    /// A unary operation, such as `#list`
    UnaryOperator {
        /// The unary operation, the `#` part of `#list`
        unop: UnOp,
        /// The expression the operation is being done on, the `list` part of `#list`
        expression: Box<Expression>,
    },
    
    /// An anonymous function, such as `function() end`
    Function(Box<AnonymousFunction>),
    
    /// A call of a function, such as `call()`
    FunctionCall(FunctionCall),
    
    /// An if expression, such as `if foo then true else false` (Luau only)
    #[cfg(feature = "luau")]
    IfExpression(IfExpression),
    
    /// An interpolated string, such as `` `hello {"world"}` `` (Luau only)
    #[cfg(feature = "luau")]
    InterpolatedString(InterpolatedString),
    
    /// A table constructor, such as `{ 1, 2, 3 }`
    TableConstructor(TableConstructor),
    
    /// A number token, such as `3.3`
    Number(TokenReference),
    
    /// A string token, such as `"hello"`
    String(TokenReference),
    
    /// A symbol, such as `true`
    Symbol(TokenReference),
    
    /// A value that has been asserted for a particular type (Luau only)
    #[cfg(feature = "luau")]
    TypeAssertion {
        /// The expression being asserted
        expression: Box<Expression>,
        /// The type assertion
        type_assertion: TypeAssertion,
    },
    
    /// A more complex value, such as `call().x`
    Var(Var),
}

Binary Operators

pub enum BinOp {
    Caret(TokenReference),           // ^
    Percent(TokenReference),         // %
    Slash(TokenReference),           // /
    Star(TokenReference),            // *
    DoubleSlash(TokenReference),     // // (Luau/Lua 5.3)
    Minus(TokenReference),           // -
    Plus(TokenReference),            // +
    TwoDots(TokenReference),         // ..
    DoubleLessThan(TokenReference),  // << (Lua 5.3)
    DoubleGreaterThan(TokenReference), // >> (Lua 5.3)
    Ampersand(TokenReference),       // & (Lua 5.3)
    Tilde(TokenReference),           // ~ (Lua 5.3)
    Pipe(TokenReference),            // | (Lua 5.3)
    GreaterThan(TokenReference),     // >
    GreaterThanEqual(TokenReference), // >=
    LessThan(TokenReference),        // <
    LessThanEqual(TokenReference),   // <=
    TildeEqual(TokenReference),      // ~=
    TwoEqual(TokenReference),        // ==
    And(TokenReference),             // and
    Or(TokenReference),              // or
}

Methods

pub fn token(&self) -> &TokenReference
The token associated with this operator.
pub fn precedence(&self) -> u8
The precedence of the operator. The larger the number, the higher the precedence.
pub fn is_right_associative(&self) -> bool
Whether the operator is right associative. If not, it is left associative.
pub fn precedence_of_token(token: &TokenReference) -> Option<u8>
The precedence of non-unary operator. The larger the number, the higher the precedence.

Unary Operators

pub enum UnOp {
    Minus(TokenReference),  // -
    Not(TokenReference),    // not
    Hash(TokenReference),   // #
    #[cfg(feature = "lua53")]
    Tilde(TokenReference),  // ~ (Lua 5.3)
}

Methods

pub fn token(&self) -> &TokenReference
pub fn precedence() -> u8
The precedence of unary operator. The larger the number, the higher the precedence. Shares the same precedence table as binary operators.

TableConstructor

A table being constructed, such as { 1, 2, 3 } or { a = 1 }.
pub struct TableConstructor {
    braces: ContainedSpan,
    fields: Punctuated<Field>,
}

Methods

pub fn new() -> Self
Creates a new empty TableConstructor. Brace tokens are followed by spaces, such that { fields }.
pub fn braces(&self) -> &ContainedSpan
pub fn fields(&self) -> &Punctuated<Field>
pub fn with_braces(self, braces: ContainedSpan) -> Self
pub fn with_fields(self, fields: Punctuated<Field>) -> Self

Field Enum

Fields of a TableConstructor.
pub enum Field {
    /// A key in the format of `[expression] = value`
    ExpressionKey {
        /// The `[...]` part of `[expression] = value`
        brackets: ContainedSpan,
        /// The `expression` part of `[expression] = value`
        key: Expression,
        /// The `=` part of `[expression] = value`
        equal: TokenReference,
        /// The `value` part of `[expression] = value`
        value: Expression,
    },
    
    /// A key in the format of `name = value`
    NameKey {
        /// The `name` part of `name = value`
        key: TokenReference,
        /// The `=` part of `name = value`
        equal: TokenReference,
        /// The `value` part of `name = value`
        value: Expression,
    },
    
    /// A set constructor field, such as .a inside { .a } which is equivalent to { a = true } (CFX Lua only)
    #[cfg(feature = "cfxlua")]
    SetConstructor {
        /// The `.` part of `.a`
        dot: TokenReference,
        /// The `a` part of `.a`
        name: TokenReference,
    },
    
    /// A field with no key, just a value (such as `"a"` in `{ "a" }`)
    NoKey(Expression),
}

AnonymousFunction

An anonymous function, such as function() end.
pub struct AnonymousFunction {
    #[cfg(feature = "luau")]
    attributes: Vec<LuauAttribute>,
    function_token: TokenReference,
    body: FunctionBody,
}

Methods

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

FunctionCall

A function being called, such as call().
pub struct FunctionCall {
    prefix: Prefix,
    suffixes: Vec<Suffix>,
}

Methods

pub fn new(prefix: Prefix) -> Self
Creates a new FunctionCall from the given prefix. Sets the suffixes such that the return is prefixes().
pub fn prefix(&self) -> &Prefix
pub fn suffixes(&self) -> impl Iterator<Item = &Suffix>
pub fn with_prefix(self, prefix: Prefix) -> Self
pub fn with_suffixes(self, suffixes: Vec<Suffix>) -> Self

FunctionArgs

Arguments used for a function.
pub enum FunctionArgs {
    /// Used when a function is called in the form of `call(1, 2, 3)`
    Parentheses {
        /// The `(...)` part of `(1, 2, 3)`
        parentheses: ContainedSpan,
        /// The `1, 2, 3` part
        arguments: Punctuated<Expression>,
    },
    
    /// Used when a function is called in the form of `call "foobar"`
    String(TokenReference),
    
    /// Used when a function is called in the form of `call { 1, 2, 3 }`
    TableConstructor(TableConstructor),
}

Var Enum

Used in assignments and values.
pub enum Var {
    /// An expression, such as `x.y.z` or `x()`
    Expression(Box<VarExpression>),
    
    /// A literal identifier, such as `x`
    Name(TokenReference),
}

VarExpression

A complex expression used by Var, consisting of both a prefix and suffixes.
pub struct VarExpression {
    prefix: Prefix,
    suffixes: Vec<Suffix>,
}

Methods

pub fn new(prefix: Prefix) -> Self
pub fn prefix(&self) -> &Prefix
pub fn suffixes(&self) -> impl Iterator<Item = &Suffix>

Prefix Enum

A node used before another in cases such as function calling. The ("foo") part of ("foo"):upper().
pub enum Prefix {
    /// A complicated expression, such as `("foo")`
    Expression(Box<Expression>),
    
    /// Just a name, such as `foo`
    Name(TokenReference),
}

Suffix Enum

A suffix in certain cases, such as :y() in x:y(). Can be stacked on top of each other, such as in x()()().
pub enum Suffix {
    /// A call, including method calls and direct calls
    Call(Call),
    
    /// An index, such as `x.y`
    Index(Index),
    
    /// A type instantiation, such as `a<<T>>` (Luau only)
    #[cfg(feature = "luau")]
    TypeInstantiation(TypeInstantiation),
}

Index Enum

The indexing of something, such as x.y or x["y"].
pub enum Index {
    /// Indexing in the form of `x["y"]`
    Brackets {
        /// The `[...]` part of `["y"]`
        brackets: ContainedSpan,
        /// The `"y"` part of `["y"]`
        expression: Expression,
    },
    
    /// Indexing in the form of `x.y`
    Dot {
        /// The `.` part of `.y`
        dot: TokenReference,
        /// The `y` part of `.y`
        name: TokenReference,
    },
}

Call Enum

Something being called.
pub enum Call {
    /// A function being called directly, such as `x(1)`
    AnonymousCall(FunctionArgs),
    
    /// A method call, such as `x:y()`
    MethodCall(MethodCall),
}

MethodCall

A method call, such as x:y().
pub struct MethodCall {
    colon_token: TokenReference,
    name: TokenReference,
    #[cfg(feature = "luau")]
    type_instantiation: Option<TypeInstantiation>,
    args: FunctionArgs,
}

Methods

pub fn new(name: TokenReference, args: FunctionArgs) -> Self
pub fn colon_token(&self) -> &TokenReference
pub fn args(&self) -> &FunctionArgs
pub fn name(&self) -> &TokenReference
#[cfg(feature = "luau")]
pub fn type_instantiation(&self) -> Option<&TypeInstantiation>

Build docs developers (and LLMs) love