lua

package
v0.1.0-dev.121 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 3, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package lua defines the Lua AST node types used as an intermediate representation between TypeScript AST transformation and Lua source code emission.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EscapeString

func EscapeString(s string) string

EscapeString produces a Lua 5.1/LuaJIT compatible quoted string. Lua 5.1 only supports \DDD decimal escapes (not \xNN hex or \uXXXX unicode).

func IsAssignmentTarget

func IsAssignmentTarget(expr Expression) bool

IsAssignmentTarget reports whether expr is a valid Lua assignment target (identifier or table index expression).

func IsNilLiteral

func IsNilLiteral(e Expression) bool

func IsSimpleExpression

func IsSimpleExpression(expr Expression) bool

IsSimpleExpression returns true if the expression contains no calls or function expressions.

func OperatorString

func OperatorString(op Operator) string

OperatorString returns the Lua text for an operator.

func PrintExpression

func PrintExpression(expr Expression) string

PrintExpression prints a single expression and returns the source code.

func PrintStatements

func PrintStatements(stmts []Statement, allowUnicode bool) string

PrintStatements prints a slice of statements and returns the source code. If allowUnicode is true, unicode identifiers are allowed in dot/colon syntax (for LuaJIT).

Types

type AssignmentStatement

type AssignmentStatement struct {
	Comments
	Left  []Expression // Identifier or TableIndexExpression
	Right []Expression
	// contains filtered or unexported fields
}

func Assign

func Assign(left []Expression, right []Expression) *AssignmentStatement

func (*AssignmentStatement) Kind

func (n *AssignmentStatement) Kind() SyntaxKind

func (*AssignmentStatement) SetSourcePos

func (b *AssignmentStatement) SetSourcePos(line, col int)

func (*AssignmentStatement) SourcePosition

func (b *AssignmentStatement) SourcePosition() SourcePos

type BinaryExpression

type BinaryExpression struct {
	Left     Expression
	Operator Operator
	Right    Expression
	// contains filtered or unexported fields
}

func Binary

func Binary(left Expression, op Operator, right Expression) *BinaryExpression

func (*BinaryExpression) Kind

func (n *BinaryExpression) Kind() SyntaxKind

func (*BinaryExpression) SetSourcePos

func (b *BinaryExpression) SetSourcePos(line, col int)

func (*BinaryExpression) SourcePosition

func (b *BinaryExpression) SourcePosition() SourcePos

type Block

type Block struct {
	Pos        SourcePos
	Statements []Statement
}

func (*Block) Kind

func (n *Block) Kind() SyntaxKind

func (*Block) SourcePosition

func (n *Block) SourcePosition() SourcePos

type BooleanLiteral

type BooleanLiteral struct {
	Value bool
	// contains filtered or unexported fields
}

func Bool

func Bool(value bool) *BooleanLiteral

func (*BooleanLiteral) Kind

func (n *BooleanLiteral) Kind() SyntaxKind

func (*BooleanLiteral) SetSourcePos

func (b *BooleanLiteral) SetSourcePos(line, col int)

func (*BooleanLiteral) SourcePosition

func (b *BooleanLiteral) SourcePosition() SourcePos

type BreakStatement

type BreakStatement struct {
	Comments
	// contains filtered or unexported fields
}

func Break

func Break() *BreakStatement

func (*BreakStatement) Kind

func (n *BreakStatement) Kind() SyntaxKind

func (*BreakStatement) SetSourcePos

func (b *BreakStatement) SetSourcePos(line, col int)

func (*BreakStatement) SourcePosition

func (b *BreakStatement) SourcePosition() SourcePos

type CallExpression

type CallExpression struct {
	Expression Expression
	Params     []Expression
	// contains filtered or unexported fields
}

func Call

func Call(fn Expression, params ...Expression) *CallExpression

func (*CallExpression) Kind

func (n *CallExpression) Kind() SyntaxKind

func (*CallExpression) SetSourcePos

func (b *CallExpression) SetSourcePos(line, col int)

func (*CallExpression) SourcePosition

func (b *CallExpression) SourcePosition() SourcePos

type CommentExpression

type CommentExpression struct {
	Text string
	// contains filtered or unexported fields
}

CommentExpression emits `nil --[[ text ]]` — a nil placeholder with a comment. Used for unimplemented features (TODOs) where an expression is required.

func Comment

func Comment(text string) *CommentExpression

func (*CommentExpression) Kind

func (n *CommentExpression) Kind() SyntaxKind

func (*CommentExpression) SetSourcePos

func (b *CommentExpression) SetSourcePos(line, col int)

func (*CommentExpression) SourcePosition

func (b *CommentExpression) SourcePosition() SourcePos

type Comments

type Comments struct {
	LeadingComments  []string
	TrailingComments []string
}

Comments holds leading/trailing comments for a statement.

func (*Comments) GetComments

func (c *Comments) GetComments() *Comments

GetComments returns a pointer to the embedded Comments struct. Since all statement types embed Comments, this allows generic access.

type ConditionalExpression

type ConditionalExpression struct {
	Condition Expression
	WhenTrue  Expression
	WhenFalse Expression
	// contains filtered or unexported fields
}

ConditionalExpression is Luau's ternary: if cond then expr else expr

func Conditional

func Conditional(cond, whenTrue, whenFalse Expression) *ConditionalExpression

Conditional creates a Luau conditional expression: if cond then whenTrue else whenFalse

func (*ConditionalExpression) Kind

func (*ConditionalExpression) SetSourcePos

func (b *ConditionalExpression) SetSourcePos(line, col int)

func (*ConditionalExpression) SourcePosition

func (b *ConditionalExpression) SourcePosition() SourcePos

type ContinueStatement

type ContinueStatement struct {
	Comments
	// contains filtered or unexported fields
}

ContinueStatement represents the Luau-only `continue` keyword.

func Continue

func Continue() *ContinueStatement

func (*ContinueStatement) Kind

func (n *ContinueStatement) Kind() SyntaxKind

func (*ContinueStatement) SetSourcePos

func (b *ContinueStatement) SetSourcePos(line, col int)

func (*ContinueStatement) SourcePosition

func (b *ContinueStatement) SourcePosition() SourcePos

type DoStatement

type DoStatement struct {
	Comments
	Body *Block
	// contains filtered or unexported fields
}

func Do

func Do(stmts ...Statement) *DoStatement

func (*DoStatement) Kind

func (n *DoStatement) Kind() SyntaxKind

func (*DoStatement) SetSourcePos

func (b *DoStatement) SetSourcePos(line, col int)

func (*DoStatement) SourcePosition

func (b *DoStatement) SourcePosition() SourcePos

type DotsLiteral

type DotsLiteral struct {
	// contains filtered or unexported fields
}

func Dots

func Dots() *DotsLiteral

func (*DotsLiteral) Kind

func (n *DotsLiteral) Kind() SyntaxKind

func (*DotsLiteral) SetSourcePos

func (b *DotsLiteral) SetSourcePos(line, col int)

func (*DotsLiteral) SourcePosition

func (b *DotsLiteral) SourcePosition() SourcePos

type Expression

type Expression interface {
	Node
	// contains filtered or unexported methods
}

Expression is a Lua expression node.

type ExpressionStatement

type ExpressionStatement struct {
	Comments
	Expression Expression
	// contains filtered or unexported fields
}

func ExprStmt

func ExprStmt(expr Expression) *ExpressionStatement

func (*ExpressionStatement) Kind

func (n *ExpressionStatement) Kind() SyntaxKind

func (*ExpressionStatement) SetSourcePos

func (b *ExpressionStatement) SetSourcePos(line, col int)

func (*ExpressionStatement) SourcePosition

func (b *ExpressionStatement) SourcePosition() SourcePos

type ForInStatement

type ForInStatement struct {
	Comments
	Names       []*Identifier
	Expressions []Expression
	Body        *Block
	// contains filtered or unexported fields
}

func ForIn

func ForIn(names []*Identifier, exprs []Expression, body *Block) *ForInStatement

func (*ForInStatement) Kind

func (n *ForInStatement) Kind() SyntaxKind

func (*ForInStatement) SetSourcePos

func (b *ForInStatement) SetSourcePos(line, col int)

func (*ForInStatement) SourcePosition

func (b *ForInStatement) SourcePosition() SourcePos

type ForStatement

type ForStatement struct {
	Comments
	ControlVariable            *Identifier
	ControlVariableInitializer Expression
	LimitExpression            Expression
	StepExpression             Expression // nil means step of 1
	Body                       *Block
	// contains filtered or unexported fields
}

func (*ForStatement) Kind

func (n *ForStatement) Kind() SyntaxKind

func (*ForStatement) SetSourcePos

func (b *ForStatement) SetSourcePos(line, col int)

func (*ForStatement) SourcePosition

func (b *ForStatement) SourcePosition() SourcePos

type FunctionExpression

type FunctionExpression struct {
	Params []*Identifier
	Dots   bool
	Body   *Block
	Flags  NodeFlags
	// contains filtered or unexported fields
}

func (*FunctionExpression) Kind

func (n *FunctionExpression) Kind() SyntaxKind

func (*FunctionExpression) SetSourcePos

func (b *FunctionExpression) SetSourcePos(line, col int)

func (*FunctionExpression) SourcePosition

func (b *FunctionExpression) SourcePosition() SourcePos

type GotoStatement

type GotoStatement struct {
	Comments
	Label string
	// contains filtered or unexported fields
}

func Goto

func Goto(label string) *GotoStatement

func (*GotoStatement) Kind

func (n *GotoStatement) Kind() SyntaxKind

func (*GotoStatement) SetSourcePos

func (b *GotoStatement) SetSourcePos(line, col int)

func (*GotoStatement) SourcePosition

func (b *GotoStatement) SourcePosition() SourcePos

type Identifier

type Identifier struct {
	Text string
	// contains filtered or unexported fields
}

func Ident

func Ident(text string) *Identifier

func (*Identifier) Kind

func (n *Identifier) Kind() SyntaxKind

func (*Identifier) SetSourcePos

func (b *Identifier) SetSourcePos(line, col int)

func (*Identifier) SourcePosition

func (b *Identifier) SourcePosition() SourcePos

type IfStatement

type IfStatement struct {
	Comments
	Condition Expression
	IfBlock   *Block
	ElseBlock interface{} // *Block or *IfStatement, nil if no else
	// contains filtered or unexported fields
}

func If

func If(cond Expression, ifBlock *Block, elseBlock interface{}) *IfStatement

func (*IfStatement) Kind

func (n *IfStatement) Kind() SyntaxKind

func (*IfStatement) SetSourcePos

func (b *IfStatement) SetSourcePos(line, col int)

func (*IfStatement) SourcePosition

func (b *IfStatement) SourcePosition() SourcePos

type LabelStatement

type LabelStatement struct {
	Comments
	Name string
	// contains filtered or unexported fields
}

func GotoLabel

func GotoLabel(name string) *LabelStatement

func (*LabelStatement) Kind

func (n *LabelStatement) Kind() SyntaxKind

func (*LabelStatement) SetSourcePos

func (b *LabelStatement) SetSourcePos(line, col int)

func (*LabelStatement) SourcePosition

func (b *LabelStatement) SourcePosition() SourcePos

type Mapping

type Mapping struct {
	GenLine int    // 0-based generated line
	GenCol  int    // 0-based generated column (bytes)
	SrcLine int    // 0-based source line
	SrcCol  int    // 0-based source column (UTF-16)
	Name    string // original TS name (empty if no rename)
}

Mapping records a single source map mapping entry.

type MethodCallExpression

type MethodCallExpression struct {
	Prefix Expression
	Name   string
	Params []Expression
	// contains filtered or unexported fields
}

func MethodCall

func MethodCall(prefix Expression, name string, params ...Expression) *MethodCallExpression

func (*MethodCallExpression) Kind

func (n *MethodCallExpression) Kind() SyntaxKind

func (*MethodCallExpression) SetSourcePos

func (b *MethodCallExpression) SetSourcePos(line, col int)

func (*MethodCallExpression) SourcePosition

func (b *MethodCallExpression) SourcePosition() SourcePos

type NilLiteral

type NilLiteral struct {
	// contains filtered or unexported fields
}

func Nil

func Nil() *NilLiteral

func (*NilLiteral) Kind

func (n *NilLiteral) Kind() SyntaxKind

func (*NilLiteral) SetSourcePos

func (b *NilLiteral) SetSourcePos(line, col int)

func (*NilLiteral) SourcePosition

func (b *NilLiteral) SourcePosition() SourcePos

type Node

type Node interface {
	Kind() SyntaxKind
	SourcePosition() SourcePos
}

Node is the base interface for all Lua AST nodes.

type NodeFlags

type NodeFlags int

NodeFlags modifies how a node is printed.

const (
	FlagNone        NodeFlags = 0
	FlagInline      NodeFlags = 1 << iota // Keep function body on same line
	FlagDeclaration                       // On FunctionExpression: printer may use `function foo()` syntax for assignments.
	// For local declarations, the printer always uses `local function` regardless of this flag.
	FlagTableUnpackCall // Marks a table.unpack call
)

type NumericLiteral

type NumericLiteral struct {
	Value string // stored as text to preserve formatting (e.g. "0xFF", "1e3")
	// contains filtered or unexported fields
}

func Num

func Num(value string) *NumericLiteral

func (*NumericLiteral) Kind

func (n *NumericLiteral) Kind() SyntaxKind

func (*NumericLiteral) SetSourcePos

func (b *NumericLiteral) SetSourcePos(line, col int)

func (*NumericLiteral) SourcePosition

func (b *NumericLiteral) SourcePosition() SourcePos

type Operator

type Operator int

Operator represents a Lua operator (binary or unary).

const (
	// Arithmetic
	OpAdd Operator = iota
	OpSub
	OpMul
	OpDiv
	OpFloorDiv
	OpMod
	OpPow
	OpNeg // unary -
	// String
	OpConcat
	// Length
	OpLen // unary #
	// Relational
	OpEq
	OpNeq
	OpLt
	OpLe
	OpGt
	OpGe
	// Logical
	OpAnd
	OpOr
	OpNot // unary not
	// Bitwise
	OpBitAnd
	OpBitOr
	OpBitXor
	OpBitShr
	OpBitShl
	OpBitNot // unary ~
)

type ParenthesizedExpression

type ParenthesizedExpression struct {
	Inner Expression
	// contains filtered or unexported fields
}

ParenthesizedExpression forces parentheses around the inner expression. Use ONLY for semantic parens (multi-return truncation, structural disambiguation) — never for operator precedence grouping, which the printer handles automatically.

func Paren

func Paren(inner Expression) *ParenthesizedExpression

Paren forces parentheses around the inner expression in the printed output. Only for semantic parens: (f()) to truncate multi-return, or structural disambiguation. Never use for operator precedence — the printer handles that via printExprInParensIfNeeded.

func (*ParenthesizedExpression) Kind

func (*ParenthesizedExpression) SetSourcePos

func (b *ParenthesizedExpression) SetSourcePos(line, col int)

func (*ParenthesizedExpression) SourcePosition

func (b *ParenthesizedExpression) SourcePosition() SourcePos

type Positioned

type Positioned interface {
	SetSourcePos(line, col int)
}

Positioned is implemented by all expression and statement nodes.

type PrintResult

type PrintResult struct {
	Code     string
	Mappings []Mapping
}

PrintResult contains Lua source and collected source map mappings.

func PrintStatementsWithSourceMap

func PrintStatementsWithSourceMap(stmts []Statement, allowUnicode bool) PrintResult

PrintStatementsWithSourceMap prints statements and collects source map mappings.

type Printer

type Printer struct {
	// contains filtered or unexported fields
}

Printer converts Lua AST nodes to formatted source code.

func NewPrinter

func NewPrinter() *Printer

NewPrinter creates a Printer.

type RawStatement

type RawStatement struct {
	Comments
	Code string
	// contains filtered or unexported fields
}

func RawStmt

func RawStmt(code string) *RawStatement

func (*RawStatement) Kind

func (n *RawStatement) Kind() SyntaxKind

func (*RawStatement) SetSourcePos

func (b *RawStatement) SetSourcePos(line, col int)

func (*RawStatement) SourcePosition

func (b *RawStatement) SourcePosition() SourcePos

type RepeatStatement

type RepeatStatement struct {
	Comments
	Condition Expression
	Body      *Block
	// contains filtered or unexported fields
}

func Repeat

func Repeat(cond Expression, body *Block) *RepeatStatement

func (*RepeatStatement) Kind

func (n *RepeatStatement) Kind() SyntaxKind

func (*RepeatStatement) SetSourcePos

func (b *RepeatStatement) SetSourcePos(line, col int)

func (*RepeatStatement) SourcePosition

func (b *RepeatStatement) SourcePosition() SourcePos

type ReturnStatement

type ReturnStatement struct {
	Comments
	Expressions []Expression
	// contains filtered or unexported fields
}

func Return

func Return(exprs ...Expression) *ReturnStatement

func (*ReturnStatement) Kind

func (n *ReturnStatement) Kind() SyntaxKind

func (*ReturnStatement) SetSourcePos

func (b *ReturnStatement) SetSourcePos(line, col int)

func (*ReturnStatement) SourcePosition

func (b *ReturnStatement) SourcePosition() SourcePos

type SourcePos

type SourcePos struct {
	Line       int    // 0-based line in TS source
	Column     int    // 0-based UTF-16 column offset
	HasPos     bool   // true if this position was set
	SourceName string // original TS name when the identifier was renamed (e.g. "type" → "____type")
}

SourcePos records the original TypeScript source position for source map generation. Zero value (HasPos == false) means "no position".

type Statement

type Statement interface {
	Node
	// contains filtered or unexported methods
}

Statement is a Lua statement node.

type StringLiteral

type StringLiteral struct {
	Value string
	// contains filtered or unexported fields
}

func Str

func Str(value string) *StringLiteral

func (*StringLiteral) Kind

func (n *StringLiteral) Kind() SyntaxKind

func (*StringLiteral) SetSourcePos

func (b *StringLiteral) SetSourcePos(line, col int)

func (*StringLiteral) SourcePosition

func (b *StringLiteral) SourcePosition() SourcePos

type SyntaxKind

type SyntaxKind int

SyntaxKind identifies the type of a Lua AST node.

const (
	KindFile SyntaxKind = iota
	KindBlock
	// Statements
	KindDoStatement
	KindVariableDeclarationStatement
	KindAssignmentStatement
	KindIfStatement
	KindWhileStatement
	KindRepeatStatement
	KindForStatement
	KindForInStatement
	KindGotoStatement
	KindLabelStatement
	KindReturnStatement
	KindBreakStatement
	KindContinueStatement // Luau only
	KindExpressionStatement
	// Expressions
	KindStringLiteral
	KindNumericLiteral
	KindNilKeyword
	KindDotsKeyword
	KindTrueKeyword
	KindFalseKeyword
	KindFunctionExpression
	KindTableFieldExpression
	KindTableExpression
	KindUnaryExpression
	KindBinaryExpression
	KindConditionalExpression // Luau: if cond then expr else expr
	KindCallExpression
	KindMethodCallExpression
	KindIdentifier
	KindTableIndexExpression
	KindParenthesizedExpression
	// Comments as AST nodes
	KindCommentExpression
	// Escape hatch for incremental migration (deprecated)
	KindRawStatement
)

type TableExpression

type TableExpression struct {
	Fields []*TableFieldExpression
	// contains filtered or unexported fields
}

func Table

func Table(fields ...*TableFieldExpression) *TableExpression

func (*TableExpression) Kind

func (n *TableExpression) Kind() SyntaxKind

func (*TableExpression) SetSourcePos

func (b *TableExpression) SetSourcePos(line, col int)

func (*TableExpression) SourcePosition

func (b *TableExpression) SourcePosition() SourcePos

type TableFieldExpression

type TableFieldExpression struct {
	Value    Expression
	Key      Expression // nil for array-style fields (positional)
	Computed bool       // true → always emit [key] = val bracket notation
	// contains filtered or unexported fields
}

func ComputedKeyField

func ComputedKeyField(key, value Expression) *TableFieldExpression

func Field

func Field(value Expression) *TableFieldExpression

func KeyField

func KeyField(key, value Expression) *TableFieldExpression

func (*TableFieldExpression) Kind

func (n *TableFieldExpression) Kind() SyntaxKind

func (*TableFieldExpression) SetSourcePos

func (b *TableFieldExpression) SetSourcePos(line, col int)

func (*TableFieldExpression) SourcePosition

func (b *TableFieldExpression) SourcePosition() SourcePos

type TableIndexExpression

type TableIndexExpression struct {
	Table Expression
	Index Expression
	// contains filtered or unexported fields
}

func Index

func Index(table, index Expression) *TableIndexExpression

func (*TableIndexExpression) Kind

func (n *TableIndexExpression) Kind() SyntaxKind

func (*TableIndexExpression) SetSourcePos

func (b *TableIndexExpression) SetSourcePos(line, col int)

func (*TableIndexExpression) SourcePosition

func (b *TableIndexExpression) SourcePosition() SourcePos

type UnaryExpression

type UnaryExpression struct {
	Operator Operator
	Operand  Expression
	// contains filtered or unexported fields
}

func Unary

func Unary(op Operator, operand Expression) *UnaryExpression

func (*UnaryExpression) Kind

func (n *UnaryExpression) Kind() SyntaxKind

func (*UnaryExpression) SetSourcePos

func (b *UnaryExpression) SetSourcePos(line, col int)

func (*UnaryExpression) SourcePosition

func (b *UnaryExpression) SourcePosition() SourcePos

type VariableDeclarationStatement

type VariableDeclarationStatement struct {
	Comments
	Left  []*Identifier
	Right []Expression // nil means no initializer
	// contains filtered or unexported fields
}

func LocalDecl

func LocalDecl(names []*Identifier, values []Expression) *VariableDeclarationStatement

func (*VariableDeclarationStatement) Kind

func (*VariableDeclarationStatement) SetSourcePos

func (b *VariableDeclarationStatement) SetSourcePos(line, col int)

func (*VariableDeclarationStatement) SourcePosition

func (b *VariableDeclarationStatement) SourcePosition() SourcePos

type WhileStatement

type WhileStatement struct {
	Comments
	Condition Expression
	Body      *Block
	// contains filtered or unexported fields
}

func While

func While(cond Expression, body *Block) *WhileStatement

func (*WhileStatement) Kind

func (n *WhileStatement) Kind() SyntaxKind

func (*WhileStatement) SetSourcePos

func (b *WhileStatement) SetSourcePos(line, col int)

func (*WhileStatement) SourcePosition

func (b *WhileStatement) SourcePosition() SourcePos

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL