Skip to main content

Module ast

Module ast 

Source
Expand description

AST module.

§Lua Abstract Syntax Tree (AST)

This module defines the high-fidelity, strongly-typed AST for Lua. Built on the Green/Red tree architecture, it provides a lossless representation of the source code.

§🌳 Core Structures

  • LuaRoot: The entry point of the AST, representing a full Lua chunk or script.
  • Statement: Enum covering all Lua statements (Local, Assignment, Call, If, For, etc.).
  • Expression: Enum for all expression types (TableConstructor, FunctionDefinition, Binary, etc.).
  • TableField: Represents entries in a table constructor, handling both list-style and record-style fields.

§✨ Key Features

  1. Lossless: Captures all comments and whitespace, making it ideal for formatters and refactoring tools.
  2. Type-Safe: Provides a convenient API to navigate the tree without manual type casting.
  3. Incremental Ready: Designed to work with the Oak framework’s partial tree updates.

§🔍 Usage

You can traverse the tree using the TypedNode traits:

let root = result.typed_root::<LuaRoot>();
for stmt in root.statements() {
    match stmt {
        Statement::Local(l) => println!("Found local: {:?}", l.names()),
        _ => {}
    }
}

Structs§

LuaAssignmentStatement
赋值语句
LuaBinaryExpression
二元表达式
LuaCallExpression
函数调用表达式
LuaFunctionExpression
匿名函数表达式
LuaFunctionStatement
函数定义语句
LuaIfStatement
If 语句
LuaIndexExpression
索引访问表达式
LuaLocalStatement
本地变量声明
LuaMemberExpression
成员访问表达式
LuaRepeatStatement
Repeat 语句
LuaReturnStatement
返回语句
LuaRoot
Lua 根节点
LuaTableConstructor
表构造器
LuaUnaryExpression
一元表达式
LuaWhileStatement
While 语句

Enums§

LuaExpression
Lua 表达式
LuaForStatement
For 语句
LuaStatement
Lua 语句
LuaTableField