Skip to main content

ExpressionSyntax Base Class

All expression nodes inherit from ExpressionSyntax, which itself inherits from LuaSyntaxNode. Expressions can appear in many contexts:
  • Assignment values
  • Function arguments
  • Conditions
  • Return values
  • Table fields
  • And more

Literal Expressions

LiteralExpressionSyntax

Represents literal values. Kinds:
  • NumericalLiteralExpression - Numbers: 123, 0xFF, 3.14
  • StringLiteralExpression - Strings: "hello", 'world', [[long string]]
  • TrueLiteralExpression - Boolean true
  • FalseLiteralExpression - Boolean false
  • NilLiteralExpression - nil
Key properties:
  • Token - The literal token
// Number literal
var numLiteral = SyntaxFactory.LiteralExpression(
    SyntaxKind.NumericalLiteralExpression,
    SyntaxFactory.Literal(42)
);

// String literal
var strLiteral = SyntaxFactory.LiteralExpression(
    SyntaxKind.StringLiteralExpression,
    SyntaxFactory.Literal("hello")
);

// Boolean literal
var trueLiteral = SyntaxFactory.LiteralExpression(
    SyntaxKind.TrueLiteralExpression,
    SyntaxFactory.Token(SyntaxKind.TrueKeyword)
);

// Nil literal
var nilLiteral = SyntaxFactory.LiteralExpression(
    SyntaxKind.NilLiteralExpression,
    SyntaxFactory.Token(SyntaxKind.NilKeyword)
);

Identifier Expressions

IdentifierNameSyntax

Represents a variable or function name. Key properties:
  • Identifier - The identifier token
  • Name - The identifier text as a string
var identifier = SyntaxFactory.IdentifierName("myVariable");
Console.WriteLine($"Name: {identifier.Name}");  // "myVariable"

Binary Expressions

BinaryExpressionSyntax

Represents binary operations with two operands. Key properties:
  • Left - Left operand expression
  • OperatorToken - The operator token
  • Right - Right operand expression
Arithmetic operators:
  • AddExpression - a + b
  • SubtractExpression - a - b
  • MultiplyExpression - a * b
  • DivideExpression - a / b
  • FloorDivideExpression - a // b (Lua 5.3+)
  • ModuloExpression - a % b
  • ExponentiateExpression - a ^ b
Comparison operators:
  • EqualsExpression - a == b
  • NotEqualsExpression - a ~= b or a != b
  • LessThanExpression - a < b
  • LessThanOrEqualExpression - a <= b
  • GreaterThanExpression - a > b
  • GreaterThanOrEqualExpression - a >= b
Logical operators:
  • LogicalAndExpression - a and b
  • LogicalOrExpression - a or b
Bitwise operators (if enabled):
  • BitwiseAndExpression - a & b
  • BitwiseOrExpression - a | b
  • ExclusiveOrExpression - a ~ b or a ^^ b
  • LeftShiftExpression - a << b
  • RightShiftExpression - a >> b
String operator:
  • ConcatExpression - a .. b
// Arithmetic: x + y
var addExpr = SyntaxFactory.BinaryExpression(
    SyntaxKind.AddExpression,
    SyntaxFactory.IdentifierName("x"),
    SyntaxFactory.IdentifierName("y")
);

// Comparison: a == b
var eqExpr = SyntaxFactory.BinaryExpression(
    SyntaxKind.EqualsExpression,
    SyntaxFactory.IdentifierName("a"),
    SyntaxFactory.IdentifierName("b")
);

// Logical: x and y
var andExpr = SyntaxFactory.BinaryExpression(
    SyntaxKind.LogicalAndExpression,
    SyntaxFactory.IdentifierName("x"),
    SyntaxFactory.IdentifierName("y")
);

Unary Expressions

UnaryExpressionSyntax

Represents unary operations with one operand. Key properties:
  • OperatorToken - The operator token
  • Operand - The operand expression
Operators:
  • UnaryMinusExpression - -x (negation)
  • LogicalNotExpression - not x
  • LengthExpression - #x (length operator)
  • BitwiseNotExpression - ~x (bitwise not, if enabled)
// Negation: -x
var negExpr = SyntaxFactory.UnaryExpression(
    SyntaxKind.UnaryMinusExpression,
    SyntaxFactory.IdentifierName("x")
);

// Logical not: not flag
var notExpr = SyntaxFactory.UnaryExpression(
    SyntaxKind.LogicalNotExpression,
    SyntaxFactory.IdentifierName("flag")
);

// Length: #str
var lenExpr = SyntaxFactory.UnaryExpression(
    SyntaxKind.LengthExpression,
    SyntaxFactory.IdentifierName("str")
);

Table Constructor

TableConstructorExpressionSyntax

Represents table literal construction. Syntax: { field1, field2, key = value } Key properties:
  • OpenBraceToken - The { token
  • Fields - List of table fields (SeparatedSyntaxList<TableFieldSyntax>)
  • CloseBraceToken - The } token
Field types:
  • UnkeyedTableFieldSyntax - Array-style: value
  • IdentifierKeyedTableFieldSyntax - Named field: key = value
  • ExpressionKeyedTableFieldSyntax - Computed key: [expr] = value
var tableExpr = SyntaxFactory.TableConstructorExpression(
    SyntaxFactory.SeparatedList<TableFieldSyntax>(new[] {
        // Unkeyed field (array element)
        SyntaxFactory.UnkeyedTableField(
            SyntaxFactory.LiteralExpression(
                SyntaxKind.NumericalLiteralExpression,
                SyntaxFactory.Literal(1)
            )
        ),
        // Identifier keyed field
        SyntaxFactory.IdentifierKeyedTableField(
            SyntaxFactory.Identifier("name"),
            SyntaxFactory.LiteralExpression(
                SyntaxKind.StringLiteralExpression,
                SyntaxFactory.Literal("John")
            )
        ),
        // Expression keyed field
        SyntaxFactory.ExpressionKeyedTableField(
            SyntaxFactory.LiteralExpression(
                SyntaxKind.StringLiteralExpression,
                SyntaxFactory.Literal("age")
            ),
            SyntaxFactory.LiteralExpression(
                SyntaxKind.NumericalLiteralExpression,
                SyntaxFactory.Literal(30)
            )
        )
    })
);
// Produces: { 1, name = "John", ["age"] = 30 }

Function Expressions

AnonymousFunctionExpressionSyntax

Represents anonymous function (lambda) expressions. Syntax: function(param1, param2) body end Key properties:
  • FunctionKeyword - The function keyword
  • TypeParameterList - Optional generic type parameters (Luau)
  • Parameters - Parameter list
  • TypeBinding - Optional return type annotation
  • Body - Function body statements
  • EndKeyword - The end keyword
var anonFunc = SyntaxFactory.AnonymousFunctionExpression(
    SyntaxFactory.ParameterList(
        SyntaxFactory.SeparatedList<ParameterSyntax>(new[] {
            SyntaxFactory.NamedParameter(SyntaxFactory.Identifier("x")),
            SyntaxFactory.NamedParameter(SyntaxFactory.Identifier("y"))
        })
    ),
    SyntaxFactory.StatementList(
        SyntaxFactory.ReturnStatement(
            SyntaxFactory.SeparatedList<ExpressionSyntax>(new[] {
                SyntaxFactory.BinaryExpression(
                    SyntaxKind.AddExpression,
                    SyntaxFactory.IdentifierName("x"),
                    SyntaxFactory.IdentifierName("y")
                )
            })
        )
    )
);
// Produces: function(x, y) return x + y end

Function Call Expression

FunctionCallExpressionSyntax

Represents a function call. Syntax: func(arg1, arg2) or func "string" or func { table } Key properties:
  • Expression - The function being called (a PrefixExpressionSyntax)
  • Argument - The function argument (one of ExpressionListFunctionArgumentSyntax, StringFunctionArgumentSyntax, or TableConstructorFunctionArgumentSyntax)
// Regular call: print("hello")
var funcCall = SyntaxFactory.FunctionCallExpression(
    SyntaxFactory.IdentifierName("print"),
    SyntaxFactory.ExpressionListFunctionArgument(
        SyntaxFactory.SeparatedList<ExpressionSyntax>(new[] {
            SyntaxFactory.LiteralExpression(
                SyntaxKind.StringLiteralExpression,
                SyntaxFactory.Literal("hello")
            )
        })
    )
);

// String call: require "module"
var requireCall = SyntaxFactory.FunctionCallExpression(
    SyntaxFactory.IdentifierName("require"),
    SyntaxFactory.StringFunctionArgument(
        SyntaxFactory.LiteralExpression(
            SyntaxKind.StringLiteralExpression,
            SyntaxFactory.Literal("module")
        )
    )
);

// Table call: setmetatable({}, mt)
var tableCall = SyntaxFactory.FunctionCallExpression(
    SyntaxFactory.IdentifierName("setmetatable"),
    SyntaxFactory.TableConstructorFunctionArgument(
        SyntaxFactory.TableConstructorExpression(
            SyntaxFactory.SeparatedList<TableFieldSyntax>()
        )
    )
);

MethodCallExpressionSyntax

Represents a method call using colon syntax. Syntax: obj:method(arg1, arg2) Key properties:
  • Expression - The object expression
  • ColonToken - The : token
  • Identifier - The method name
  • Argument - The function argument
var methodCall = SyntaxFactory.MethodCallExpression(
    SyntaxFactory.IdentifierName("player"),
    SyntaxFactory.Identifier("getName"),
    SyntaxFactory.ExpressionListFunctionArgument(
        SyntaxFactory.SeparatedList<ExpressionSyntax>()
    )
);
// Produces: player:getName()

Member Access Expressions

MemberAccessExpressionSyntax

Represents member access using dot notation. Syntax: table.field Key properties:
  • Expression - The table expression
  • DotSeparator - The . token
  • MemberName - The member identifier token
var memberAccess = SyntaxFactory.MemberAccessExpression(
    SyntaxFactory.IdentifierName("player"),
    SyntaxFactory.Identifier("health")
);
// Produces: player.health

ElementAccessExpressionSyntax

Represents element access using bracket notation. Syntax: table[key] Key properties:
  • Expression - The table expression
  • OpenBracketToken - The [ token
  • KeyExpression - The key expression
  • CloseBracketToken - The ] token
var elementAccess = SyntaxFactory.ElementAccessExpression(
    SyntaxFactory.IdentifierName("arr"),
    SyntaxFactory.LiteralExpression(
        SyntaxKind.NumericalLiteralExpression,
        SyntaxFactory.Literal(1)
    )
);
// Produces: arr[1]

Other Expressions

ParenthesizedExpressionSyntax

Represents an expression wrapped in parentheses. Syntax: (expression) Key properties:
  • OpenParenthesisToken - The ( token
  • Expression - The inner expression
  • CloseParenthesisToken - The ) token
var parenExpr = SyntaxFactory.ParenthesizedExpression(
    SyntaxFactory.BinaryExpression(
        SyntaxKind.AddExpression,
        SyntaxFactory.IdentifierName("a"),
        SyntaxFactory.IdentifierName("b")
    )
);
// Produces: (a + b)

VarArgExpressionSyntax

Represents the vararg expression .... Key properties:
  • VarArgToken - The ... token
var varargExpr = SyntaxFactory.VarArgExpression();
// Produces: ...

TypeCastExpressionSyntax

Represents a type cast (Luau). Syntax: expression :: Type Key properties:
  • Expression - The expression being cast
  • ColonColonToken - The :: token
  • Type - The target type
var typeCast = SyntaxFactory.TypeCastExpression(
    SyntaxFactory.IdentifierName("value"),
    SyntaxFactory.SimpleTypeName(SyntaxFactory.Identifier("number"))
);
// Produces: value :: number

IfExpressionSyntax

Represents an if-expression (Luau). Syntax: if condition then trueValue elseif condition2 then value2 else falseValue Key properties:
  • IfKeyword - The if keyword
  • Condition - The condition expression
  • ThenKeyword - The then keyword
  • TrueValue - Expression for true case
  • ElseIfClauses - List of elseif clauses
  • ElseKeyword - The else keyword
  • FalseValue - Expression for false case
var ifExpr = SyntaxFactory.IfExpression(
    SyntaxFactory.BinaryExpression(
        SyntaxKind.GreaterThanExpression,
        SyntaxFactory.IdentifierName("x"),
        SyntaxFactory.LiteralExpression(
            SyntaxKind.NumericalLiteralExpression,
            SyntaxFactory.Literal(0)
        )
    ),
    SyntaxFactory.LiteralExpression(
        SyntaxKind.StringLiteralExpression,
        SyntaxFactory.Literal("positive")
    ),
    SyntaxFactory.LiteralExpression(
        SyntaxKind.StringLiteralExpression,
        SyntaxFactory.Literal("negative")
    )
);
// Produces: if x > 0 then "positive" else "negative"

InterpolatedStringExpressionSyntax

Represents an interpolated string (if enabled). Syntax: `Hello {name}!` Key properties:
  • StringStartToken - The opening backtick
  • Contents - List of string text and interpolations
  • StringEndToken - The closing backtick
var interpString = SyntaxFactory.InterpolatedStringExpression(
    SyntaxFactory.List<InterpolatedStringContentSyntax>(new[] {
        SyntaxFactory.InterpolatedStringText(
            SyntaxFactory.Token(SyntaxKind.InterpolatedStringTextToken, "Hello ")
        ),
        SyntaxFactory.Interpolation(
            SyntaxFactory.IdentifierName("name")
        ),
        SyntaxFactory.InterpolatedStringText(
            SyntaxFactory.Token(SyntaxKind.InterpolatedStringTextToken, "!")
        )
    })
);
// Produces: `Hello {name}!`

PrefixExpressionSyntax

PrefixExpressionSyntax is an abstract base class for expressions that can appear as prefixes in certain contexts (like the left side of assignments or before method calls). It includes:
  • IdentifierNameSyntax
  • ParenthesizedExpressionSyntax
  • MemberAccessExpressionSyntax
  • ElementAccessExpressionSyntax
  • FunctionCallExpressionSyntax
  • MethodCallExpressionSyntax

VariableExpressionSyntax

VariableExpressionSyntax is an abstract base class for expressions that can be assigned to:
  • IdentifierNameSyntax
  • MemberAccessExpressionSyntax
  • ElementAccessExpressionSyntax

See Also

Build docs developers (and LLMs) love